如何从C++中的文件读取到队列中进行后缀评估
How to read from file in C++ into a queue to make postfix evaluation?
我是C++新手。我正在尝试做一个练习,我应该将后缀操作读入队列,然后使用堆栈对其进行评估。在我的文件中,每行只包含一个后缀操作,因此所有操作都以 # 结尾。但我无法完成阅读部分。我的函数只工作 2 次,但它需要做 3 次。
这是我到目前为止设法写的:
队列 cpp 文件
#include<iostream>
#include <cassert>
#include "Queue.h"
#include "stdlib.h"
using namespace std;
Queue::Queue(){
Size = 0;
head = NULL;
rear = NULL;
}
Queue::~Queue(){
Node* curPtr = head;
while( curPtr != 0 ) {
Node* nex = curPtr->next;
delete curPtr;
curPtr = nex;
}
head = 0;
}
bool Queue::empty() const {
if(Size==0){
return true;
}
return false;
}
int Queue::size() const {
return Size;
}
void Queue::enqueue(ElementType x){
Node *newNode = new Node;
newNode->data = x;
if(Size == 0){
rear=head = newNode;
Size++;
}
else{
rear->next=newNode;
rear=rear->next;
Size++;
}
}
void Queue::dequeue(){
Node * newNode;
if(Size==1){
newNode=head;
rear=NULL;
head=NULL;
delete newNode;
}
else{
newNode=head;
head=head->next;
delete newNode;
}
Size--;
}
ElementType Queue::front(){
return head->data;
}
//queue .h file
#ifndef QUEUE
#define QUEUE
using namespace std;
typedef string ElementType; // type of item to be stored
class Queue{
public:
int size() const; //return the number of elements in the queue
bool empty() const; //return true if queue is empty, else return false
void enqueue(ElementType x); //add x to the queue, increasing size()
void dequeue(); //remove the element most recently added to the queue, decreasing size()
ElementType front(); //return the element most recently added to the queue
Queue();
~Queue();
private:
class Node{
public:
ElementType data;
Node * next;
};
Node * head;
Node * rear;
int Size;
};
#endif
// stack cpp file
#include<iostream>
#include <cassert>
#include "Stack.h"
#include "stdlib.h"
using namespace std;
Stack::Stack(){
Size = 0;
head = NULL;
}
Stack::~Stack(){
cout << "destructor called2" <<endl;
Node* deleter;
deleter=head;
Node* temp;
while(deleter!=NULL){
temp=deleter;
deleter=deleter->next;
delete temp;
}
}
bool Stack::empty() const {
if(Size==0){
return true;
}
return false;
}
int Stack::size() const {
return Size;
}
void Stack::push(ItemType x){
Node *newNode = new Node;
newNode->data = x;
if(Size == 0){
head = newNode;
Size++;
}
else{
newNode->next = head;
head = newNode;
Size++;
}
}
void Stack::pop(){
Node *newNode;
if(Size==1){
newNode=head;
head=NULL;
delete newNode;
}
else{
newNode=head;
head=head->next;
delete newNode;
}
Size--;
}
ItemType Stack::top(){
return head->data;
}
// stack .h file
#ifndef STACK
#define STACK
#include<iostream>
using namespace std;
typedef int ItemType; // type of item to be stored
class Stack{
public:
Stack();
~Stack();
int size() const; //return the number of elements in the stack
bool empty() const; //return true if stack is empty, else return false
void push(ItemType x); //add x to the stack, increasing size()
void pop(); //remove the element most recently added to the stack, decreasing size()
ItemType top(); //return the element most recently added to the stack
private:
class Node{
public:
ItemType data;
Node * next;
};
Node * head;
int Size;
};
#endif
//test .cpp file where operations happen and main is
#include "Stack.h"
#include "Queue.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
void operatorAndWriter(Queue& k, Stack &l, ofstream &myfile);
void reader(Queue &loko, Stack &l);
int main(){
Stack l;
Queue at;
reader(at, l);
return 0;
}
void reader(Queue &loko, Stack &l){
cout << "Enter the file name : " << endl;
string filename;
cin >> filename;
ifstream meinfile (filename);
string line;
string sub;
ofstream myfile("example.txt");
while (! meinfile.eof()){
getline (meinfile, line);
istringstream iss(line);
while (iss >> sub){
loko.enqueue(sub);
}
operatorAndWriter(loko, l, myfile);
meinfile.close();
}
myfile.close();
}
void operatorAndWriter(Queue &k, Stack &l, ofstream &myfile){
if(myfile.is_open()){
while (k.size()!=0){
string op = k.front();
if (op == "+"){
int a = l.top();
l.pop();
int b = l.top();
l.pop();
l.push(a+b);
myfile << "+ ";
}
else if (op == "-"){
int a = l.top();
l.pop();
int b = l.top();
l.pop();
l.push(b-a);
myfile << "- ";
}
else if (op == "*"){
int a = l.top();
l.pop();
int b = l.top();
l.pop();
l.push(a*b);
myfile << "* ";
}
else if (op == "#"){
myfile << "# " ;
myfile << l.top() << endl;
l.pop();
}
else{
int y;
y=atoi(op.c_str());
l.push(y);
myfile <<l.top()<<" ";
}
k.dequeue();
}
}
}
// here is the input file
23 4 * 19 2 - + #
6 3 - #
36 #
// here is my example file which i tried create and write operations and their solutions in it. however there is just one solution which belongs to the first sentence of the a.txt file.
23 4 * 19 2 - + # 109
这真的是很多代码。首先是一些风格建议:使用 nullptr
而不是 NULL
。2.您应该检查文件是否确实存在,以免意外损坏内存。3.简化代码,只发布重要部分。4.包括护栏应以_H
结尾
现在谈谈你的问题:我没有阅读您的所有代码,但首先您可以通过编写以下内容来简化读取函数:
ifstream meinfile ("input.txt",ios::in);
while (getline (meinfile,line)){
// Use line here
}
我不知道这是否有帮助,但毕竟是很多代码。
相关文章:
- boost::进程间消息队列引发错误
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- std::condition_variable::wait()如何评估给定的谓词
- Android NDK传感器向事件队列报告奇怪的间隔
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 在一个读写器队列中,我可以用volatile替换原子吗
- 为什么我的多线程作业队列崩溃
- c++11评估顺序(未定义的行为)
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 使用"Task"函数指针队列定义作业管理器
- 在c++队列中使用pop和visit实现线程安全
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 打印优先级队列
- 共享队列的线程安全
- 带自定义比较器的最小优先级队列
- 在 Vulkan Qt 中获取队列系列
- 如何从C++中的文件读取到队列中进行后缀评估