| | |
Converting equation from infix to postfix using stack
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 26
Reputation:
Solved Threads: 0
I am working on a home work assignment which is:
"Write a program that will allow the user to enter an infix expression terminated by '=' your program will convert to Postfix notation"
enter 3+7-6/2*8+7=
prints 3 7 + 6 2 / 8 * - 7 +
I have written the code below to complete this useing single digits, but I want to allow the user to put in any number and have it do the same
ie 37+47/12 prints 37 47 + 12/
This is not part of the assignment, but I have the next two week to fiddle with it.
Any suggestion? I am lost at the moment.
Thanx in advance!
Lanier
"Write a program that will allow the user to enter an infix expression terminated by '=' your program will convert to Postfix notation"
enter 3+7-6/2*8+7=
prints 3 7 + 6 2 / 8 * - 7 +
I have written the code below to complete this useing single digits, but I want to allow the user to put in any number and have it do the same
ie 37+47/12 prints 37 47 + 12/
This is not part of the assignment, but I have the next two week to fiddle with it.
Any suggestion? I am lost at the moment.
Thanx in advance!
Lanier
C++ Syntax (Toggle Plain Text)
char InOp1='#'; //Holds values to be compared in switch case. char InOp2='#'; //Holds value for comparison to properly push onto stack. LinkedStack Cal; //stack used to hold info in postfix format void In2Post() { cin>>InOp1; while(InOp1!='=') { //while loop to push digits on to stack while(InOp1!='+'&&InOp1!='-'&&InOp1!='*'&&InOp1!='/'&&InOp1!='('&&InOp1!=')'&&InOp1!='?') { Cal.Push(InOp1); cin>>InOp1; } switch(InOp1) //switch case used to properly format stack { case '+': cin>>InOp2; if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')') { cout<<"Incorrect input "<<InOp2<<" cannot follow + sign."<<endl<<"Must be digit."; InOp1='?'; break; } else { Cal.Push(InOp2); Cal.Push(InOp1); cin>>InOp1; break; } case '-': cin>>InOp2; if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')') { cout<<"Incorrect input "<<InOp2<<" cannot follow - sign."<<endl<<"Must be digit."; InOp1='?'; break; } else { Cal.Push(InOp2); Cal.Push(InOp1); cin>>InOp1; break; } case '/': cin>>InOp2; if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')') { cout<<"Incorrect input "<<InOp2<<" cannot follow / sign."<<endl<<"Must be digit."; InOp1='?'; break; } else { Check=Cal.Top(); if(Check=='+'||Check=='-') { Cal.Pop(); Cal.Push(InOp2); Cal.Push(InOp1); Cal.Push(Check); Check='#'; cin>>InOp1; break; } else { Cal.Push(InOp2); Cal.Push(InOp1); Check='#'; cin>>InOp1; } } case '*': cin>>InOp2; if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')') { cout<<"Incorrect input "<<InOp2<<" cannot follow * sign."<<endl<<"Must be digit."; InOp1='?'; break; } else { Check=Cal.Top(); if(Check=='+'||Check=='-') { Cal.Pop(); Cal.Push(InOp2); Cal.Push(InOp1); Cal.Push(Check); Check='#'; cin>>InOp1; break; } else { Cal.Push(InOp2); Cal.Push(InOp1); Check='#'; cin>>InOp1; } } case '(':case ')': cin>>InOp1; break; default: cout<<endl<<"Input data contains error can not continue."<<endl<<endl; system("pause"); return 0; } }
The first thing I would suggest is you parse the expression from a string (and not user input), and return an expression list (not void, and not in a global).
You can then test with
You can then expand the tests with
When you're finally happy, then you can do
As far as algorithms go, look up the "shunting yard algorithm".
You can then test with
C++ Syntax (Toggle Plain Text)
LinkedStack in2post( std::string expr ); int main ( ) { std::string expr = "3+7-6/2*8+7="; LinkedStack result = in2post( expr ); }
You can then expand the tests with
C++ Syntax (Toggle Plain Text)
LinkedStack in2post( std::string expr ); int main ( ) { std::string expr[] = { "1=", "1+2=", "1+2*3", "3+7-6/2*8+7=", }; for ( int i = 0 ; i < sizeof expr / sizeof *expr ; i++ ) { LinkedStack result = in2post( expr[i] ); } }
When you're finally happy, then you can do
C++ Syntax (Toggle Plain Text)
std::string myExpr; getline( cin, myExpr ); LinkedStack result = in2post( myExpr );
As far as algorithms go, look up the "shunting yard algorithm".
I would also suggest better formatting. See this and pay particular attention to the Indentation section. As it is your code is very difficult to follow.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Help pplz. . convert infix to postfix. . (C++)
- Object Orientated C+++.net Calculator! (C++)
- Infix to postfix and evaluate. (Java)
- Please Help Stuck Need Help Asap!!! (Java)
- help with a infix to postfix program (C++)
- Please AnyBody Can Help Me To Build Assembly Code Infix To Postfix It So Hard T_T (Assembly)
- Any Boolean Expression to Sum of Minterms (C++)
- Mathematics In A Stack (C++)
Other Threads in the C++ Forum
- Previous Thread: Unique way of identifying a document in a subnet
- Next Thread: Random rotation of a random vector
Views: 2458 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






