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: LanierWexford is an unknown quantity at this point 
Solved Threads: 0
LanierWexford LanierWexford is offline Offline
Light Poster

Converting equation from infix to postfix using stack

 
0
  #1
Nov 21st, 2007
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

  1. char InOp1='#'; //Holds values to be compared in switch case.
  2. char InOp2='#'; //Holds value for comparison to properly push onto stack.
  3. LinkedStack Cal; //stack used to hold info in postfix format
  4.  
  5. void In2Post()
  6. {
  7. cin>>InOp1;
  8.  
  9. while(InOp1!='=')
  10. {
  11.  
  12. //while loop to push digits on to stack
  13. while(InOp1!='+'&&InOp1!='-'&&InOp1!='*'&&InOp1!='/'&&InOp1!='('&&InOp1!=')'&&InOp1!='?')
  14. {
  15. Cal.Push(InOp1);
  16. cin>>InOp1;
  17. }
  18.  
  19. switch(InOp1) //switch case used to properly format stack
  20. {
  21. case '+': cin>>InOp2;
  22. if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')')
  23. {
  24. cout<<"Incorrect input "<<InOp2<<" cannot follow + sign."<<endl<<"Must be digit.";
  25. InOp1='?';
  26. break;
  27. }
  28. else
  29. {
  30. Cal.Push(InOp2);
  31. Cal.Push(InOp1);
  32. cin>>InOp1;
  33. break;
  34. }
  35. case '-': cin>>InOp2;
  36. if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')')
  37. {
  38. cout<<"Incorrect input "<<InOp2<<" cannot follow - sign."<<endl<<"Must be digit.";
  39. InOp1='?';
  40. break;
  41. }
  42. else
  43. {
  44. Cal.Push(InOp2);
  45. Cal.Push(InOp1);
  46. cin>>InOp1;
  47. break;
  48. }
  49. case '/': cin>>InOp2;
  50. if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')')
  51. {
  52. cout<<"Incorrect input "<<InOp2<<" cannot follow / sign."<<endl<<"Must be digit.";
  53. InOp1='?';
  54. break;
  55. }
  56. else
  57. {
  58. Check=Cal.Top();
  59. if(Check=='+'||Check=='-')
  60. {
  61. Cal.Pop();
  62. Cal.Push(InOp2);
  63. Cal.Push(InOp1);
  64. Cal.Push(Check);
  65. Check='#';
  66. cin>>InOp1;
  67. break;
  68. }
  69. else
  70. {
  71. Cal.Push(InOp2);
  72. Cal.Push(InOp1);
  73. Check='#';
  74. cin>>InOp1;
  75. }
  76. }
  77. case '*': cin>>InOp2;
  78. if(InOp2=='+'||InOp2=='-'||InOp2=='*'||InOp2=='/'||InOp2=='('||InOp2==')')
  79. {
  80. cout<<"Incorrect input "<<InOp2<<" cannot follow * sign."<<endl<<"Must be digit.";
  81. InOp1='?';
  82. break;
  83. }
  84. else
  85. {
  86. Check=Cal.Top();
  87. if(Check=='+'||Check=='-')
  88. {
  89. Cal.Pop();
  90. Cal.Push(InOp2);
  91. Cal.Push(InOp1);
  92. Cal.Push(Check);
  93. Check='#';
  94. cin>>InOp1;
  95. break;
  96. }
  97. else
  98. {
  99. Cal.Push(InOp2);
  100. Cal.Push(InOp1);
  101. Check='#';
  102. cin>>InOp1;
  103. }
  104. }
  105. case '(':case ')': cin>>InOp1;
  106. break;
  107.  
  108. default: cout<<endl<<"Input data contains error can not continue."<<endl<<endl;
  109. system("pause");
  110. return 0;
  111.  
  112. }
  113. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Converting equation from infix to postfix using stack

 
0
  #2
Nov 21st, 2007
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
  1. LinkedStack in2post( std::string expr );
  2.  
  3. int main ( ) {
  4. std::string expr = "3+7-6/2*8+7=";
  5. LinkedStack result = in2post( expr );
  6. }

You can then expand the tests with
  1. LinkedStack in2post( std::string expr );
  2.  
  3. int main ( ) {
  4. std::string expr[] = {
  5. "1=",
  6. "1+2=",
  7. "1+2*3",
  8. "3+7-6/2*8+7=",
  9. };
  10. for ( int i = 0 ; i < sizeof expr / sizeof *expr ; i++ ) {
  11. LinkedStack result = in2post( expr[i] );
  12. }
  13. }

When you're finally happy, then you can do
  1. std::string myExpr;
  2. getline( cin, myExpr );
  3. LinkedStack result = in2post( myExpr );

As far as algorithms go, look up the "shunting yard algorithm".
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Converting equation from infix to postfix using stack

 
0
  #3
Nov 21st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 26
Reputation: LanierWexford is an unknown quantity at this point 
Solved Threads: 0
LanierWexford LanierWexford is offline Offline
Light Poster

Re: Converting equation from infix to postfix using stack

 
0
  #4
Nov 21st, 2007
First and formost THANX!! Walt for the link I knew my code was hard to read. Sorry for the bad formatting I will fix that soon.

Also thanx for the idea Sal. I will tweak and report back with the results.

Thanx again

Lanier
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: chalasesha is an unknown quantity at this point 
Solved Threads: 0
chalasesha chalasesha is offline Offline
Newbie Poster

Re: Converting equation from infix to postfix using stack

 
0
  #5
Mar 7th, 2008
fck
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2458 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC