I'm writing code for an assignment for infix to postfix conversion using stack class and queue class. My conversion function I think needs to accept a character array, but I have set up in main to get a line from the file that is the infix to be converted. It is currently getting this line and treating it like a string, and I think if I switch the code to retrieving the infix as a character array instead of a string, things will move along. I'm not quite sure what to do though :-(
If I declare Next as string in main, and set the function to accept infix as string, the program compiles and crashes with a Debug Assertion Error, string subscript out of range.
If I set it up how I have in this post, I have errors anywhere I use infix[i] saying subscript requires array or pointer type.
I tried declaring Next as char Next[size]; and then infix in the function as char infix[size] which gave me the error regarding using getline.
Halp! ::banghead::
infix to postfix :
main:Code:void infix_to_postfix(char infix) { Stack Stack; Queue Queue; for (int i=0;i<size;i++) { if (infix[i] != '+' || infix[i] != '-' || infix[i] != '*' || infix[i] != '/' || infix[i] != '^') { Queue.Enqueue(infix[i]); //if infix[i] is number, place in queue }//if else if (infix[i] == '(') { Stack.Push(infix[i]); }//else if else if (infix[i] == ')') { while (Stack.Top() != '(') { Queue.Enqueue(Stack.Top()); Stack.Pop(); }//while Stack.Pop(); //remove ')' from stack }//else if else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') { while (!Stack.Empty() && order_ops(Stack.Top()) <= order_ops(infix[i])){ Queue.Enqueue(Stack.Top()); Stack.Pop(); }//while Stack.Push(infix[i]); }//else if }//for }//end infix_to_postfix
Code:int main(int argc, char *argv[]) { char Next; ifstream infile("A2.txt"); while (infile.good()) { getline(infile,Next); if (infile.good()) cout << "Infix : \n" << Next << endl; infix_to_postfix(Next); }//while infile.close(); system("PAUSE"); return 0; }



LinkBack URL
About LinkBacks



