我在 Linux 中为我的 CSC 课程使用后缀评估器的中缀,我们必须将输出通过管道传输到文本文件以与我们讲师的输出进行比较。
问题是当我简单地在命令行上运行它时,我的程序正在打印正确的输出,但是当我运行./a.out > output.txt
. 我们整个季度都是这样做作业的,这是第一次发生这样的事情。
我所有的代码都在下面,因为我不知道问题可能是什么。
请注意,Stack
andQueue
是我以前的作业中自己设计的,并使用 aLinkedList
作为底层结构。这些作业都很完美,所以我没有理由相信它们是问题所在。此外,该程序并不完全完整,还没有考虑括号,但已经有一些逻辑,这是这样做所必需的。这个问题真的很奇怪。
#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;