中缀到后缀转换器 + 评估程序正确打印到控制台,但不正确地打印到文本文件

Infix to Postfix converter + evaluator program printing properly to console but improperly to text file

本文关键字:打印 控制台 正确地 文件 文本 不正确 转换器 后缀 评估 程序 中缀      更新时间:2023-10-16

我正在 Linux 中为我的 CSC 课程开发一个后缀评估器,我们必须将输出通过管道传输到文本文件以与教师的输出进行比较。

问题是当我简单地在命令行上运行它时,我的程序正在打印正确的输出,但是当我运行 ./a.out > output.txt 时,它会将看似随机的 ASCII 字符打印到文本文件中。 我们整个季度都在以这种方式做作业,这是第一次发生这样的事情。

我的所有代码都在下面,因为我不知道问题可能是什么。

请注意,StackQueue是我自己在以前的作业中设计出来的,并使用LinkedList作为底层结构。这些作业都非常有效,所以我没有理由相信它们是问题所在。 此外,该程序尚未完全完成,并且尚未考虑括号,但已经有一些逻辑,这是这样做所必需的。 这个问题真的非常非常奇怪。

#include <iostream>
#include <cmath>
#include <cstring>
#include "GenericList.cc"
#include "Stack.cc"
#include "Queue.cc"
using namespace std;
void clear(char[MAX_SIZE]);
Queue<char> infix_builder(char[MAX_SIZE]);
void post_builder(Queue<char>&);//builds the postfix queue
void next_char(Queue<char>&, char);//find the next character's place in the queue
double solution(Queue<char>&);//Solves the equation
bool notop(char);//returns true if the char is not an operator
bool priority(char, char);//returns true if char a has a higher priority than char b
int main()
{
char input[MAX_SIZE];
//erase all values contained by input
clear(input);
//main loop, read each line and process them, one at a time
while(cin.getline(input, MAX_SIZE))
{
    Queue<char> mainq = infix_builder(input);//create a queue from the input
    //cout << "Printing input: " << endl;
    cout << mainq << endl;
    post_builder(mainq);//create the postfix queue
    //cout << "Printing infixed: " << endl;
    cout << mainq << endl;
    cout << solution(mainq) << endl << endl;//solve the equation and print 
    //cleanup
    clear(input);
}
return 0;
}
//clear the char array
void clear(char input[MAX_SIZE])
{
//set all values of a char array of MAX_SIZE to spaces
for(int i = 0; i < MAX_SIZE; i++)
    input[i] = ' ';
}
//Create a queue from the input data
Queue<char> infix_builder(char input[MAX_SIZE])
{
Queue<char> q;
for(int i = 0; ((input[i] != ' ') && (i < MAX_SIZE)); i++)
    q.Enqueue(input[i]);
return q;
}
void post_builder(Queue<char> &q)
{
Queue<char> p;//The postfix queue
Queue<char> pq;//A prioritized queue of operators
//build a post-fixed queue
while(!q.IsEmpty())
{
    char next = q.Dequeue();
    if(notop(next))//if the character is not an operator
    {
        if(((next - '0') >= 0) && ((next - '0') <= 9))//filter out bad char
            p.Enqueue(next);
    }
    else if(next != '(')
    {
        if(pq.IsEmpty())//if it is empty
            pq.Enqueue(next);
        else if(priority(pq.Peek(), next))//if it is time to empty some
        {
            while(!pq.IsEmpty() && priority(pq.Peek(), next))
                p.Enqueue(pq.Dequeue());
            next_char(pq, next);
        }
        else
        {
            next_char(pq, next);
        };          
    }
}
//Empty the rest of the operators into the postfix queue
while(!pq.IsEmpty())
    p.Enqueue(pq.Dequeue());
q = p;
}
double solution(Queue<char>& q)//solves the equation and returns a floating point answr
{
double a;
double b;
Stack<double> stack;
while(!q.IsEmpty())
{
    char next = q.Dequeue();
    if(((next - '0') >= 0) && ((next - '0') <= 9))
        stack.Push((next - '0'));
    else if(next == '+')//perform operator +
    {
        a = stack.Pop();
        b = stack.Pop();
        b += a;
        stack.Push(b);
    }
    else if(next == '-')//perform operator -
    {
        a = stack.Pop();
        b = stack.Pop();
        b -= a;
        stack.Push(b);
    }
    else if(next == '*')//perform * operator
    {
        a = stack.Pop();
        b = stack.Pop();
        b *= a;
        stack.Push(b);
    }
    else if(next == '/')//perform / operator
    {
        a = stack.Pop();
        b = stack.Pop();
        b /= a;
        stack.Push(b);
    }
    else//perform ^ operation
    {
        a = stack.Pop();
        b = stack.Pop();
        stack.Push(pow(b, a));
    }   
}//end while, q is empty, stack.num_items == 1
return stack.Pop();//return the final value pushed onto the stack
}
void next_char(Queue<char>& pq, char next)
{
Queue<char> temp;
while(!pq.IsEmpty() && priority(pq.Peek(), next))/
    temp.Enqueue(pq.Dequeue());
temp.Enqueue(next);//insert next
while(!pq.IsEmpty())//finish copying the Queue
    temp.Enqueue(pq.Dequeue());
pq = temp;//set pq equal to the new queue
}
bool notop(char c)
{
return ((c != '+') && (c != '-') && (c != '*') && (c != '/') && (c != '^') && (c   != '(') && (c != ')'));
}
bool priority(char a, char b)
{
if((b == '+') || (b == '-'))
        return true;//a will always have priority if b is + or -
else if((b == '*') || (b == '/'))
    return ((a == '^') || (a == '*') || (a == '/'));// * and / are equal 
else
    return false;

当您填充中缀队列时,您查找空格而不是使用 cstring 函数 "strlen(char[])"。 这包括 char 数组用于表示队列中结束的 \0 转义字符。 然后将此运算符打印到.txt文件中,并为您提供所引用的奇怪字符。 使用 strlen()。