如何从C++中的文件读取到队列中进行后缀评估

How to read from file in C++ into a queue to make postfix evaluation?

本文关键字:队列 评估 后缀 文件 C++ 读取      更新时间:2023-10-16

我是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
}

我不知道这是否有帮助,但毕竟是很多代码。