944,069 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1906
  • C++ RSS
Nov 11th, 2006
0

help with a infix to postfix program

Expand Post »
I have this program but need to adjust a little bit. At first it asks to enter an expression but I can only enter it in this form -> 7*(23-2)+10
but i want to enter it like this -> 7 * (23- 2)+ 10;
therefore the program should ignore the spaces and semicolon. also if i enter only the semicolon it should exit the program.
Also i can't get the program to restart and ask the enter the expression thing. please help me!!!!! thanks





  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. //----------------- Declaration of Constant Variable ------------------//
  9.  
  10. const int max_length=10;
  11.  
  12. //------------------------ Class Definition ---------------------------//
  13.  
  14. class Evaluator
  15. {
  16. private:
  17. char infix_expression[25][6];
  18. char postfix_expression[25][6];
  19.  
  20. char char_stack[max_length];
  21. long int_stack[max_length];
  22.  
  23. int int_top;
  24. int char_top;
  25.  
  26. int postfix_rows;
  27. int infix_rows;
  28.  
  29. int input_characters_count;
  30.  
  31. public:
  32. char char_pop( );
  33.  
  34. long int_pop( );
  35.  
  36. void set_initial_values( );
  37. void get_infix_expression( );
  38. void char_push(char);
  39. void show_infix_to_postfix_convertion( );
  40. void int_push(long);
  41. void show_evaluation_of_postfix_expression( );
  42. };
  43.  
  44. //---------------------- Function Definitions -------------------------//
  45.  
  46.  
  47. //---------------------- set_initial_values( ) ------------------------//
  48.  
  49.  
  50. void Evaluator::set_initial_values( )
  51. {
  52. int_top=-1;
  53. char_top=-1;
  54. infix_rows=0;
  55. postfix_rows=0;
  56.  
  57. for(int count_1=0;count_1<max_length;count_1++)
  58. {
  59. int_stack[count_1]=0;
  60. char_stack[count_1]=NULL;
  61. }
  62.  
  63. for(int count_2=0;count_2<25;count_2++)
  64. {
  65. strset(infix_expression[count_2],NULL);
  66. strset(postfix_expression[count_2],NULL);
  67. }
  68. }
  69.  
  70. /*************************************************************************/
  71. //----------------------- get_infix_expression( ) ---------------------//
  72. /*************************************************************************/
  73.  
  74. void Evaluator::get_infix_expression( )
  75. {
  76. Input:
  77. system("cls");
  78.  
  79. set_initial_values( );
  80.  
  81. int flag=0;
  82. int count_1=-1;
  83. int number_length=0;
  84. int operand_count=0;
  85. int operator_count=0;
  86. int expression_right_wrong=1;
  87. int input_characters_count=0;
  88. int left_parenthesis_count=0;
  89. int right_parenthesis_count=0;
  90.  
  91. char temp_expression[5]={NULL};
  92.  
  93. cout<<"\n\n\n\t Enter the Infix Expression : "<<endl;
  94.  
  95. do
  96. {
  97. temp_expression[0]=getch();
  98.  
  99.  
  100. if(int(temp_expression[0])!=8)
  101. cout<<temp_expression;
  102.  
  103.  
  104. if((int(temp_expression[0])>=48 &&
  105. int(temp_expression[0])<=57) && number_length<5)
  106. {
  107. if(flag==0)
  108. {
  109. count_1++;
  110. flag=1;
  111. operand_count++;
  112.  
  113. strcpy(infix_expression[count_1],temp_expression);
  114. }
  115.  
  116. else if(flag==1)
  117. strcat(infix_expression[count_1],temp_expression);
  118.  
  119. number_length++;
  120. }
  121.  
  122. else if( temp_expression[0]=='^' || temp_expression[0]=='/'
  123. || temp_expression[0]=='*' || temp_expression[0]=='-'
  124. || temp_expression[0]=='+' || temp_expression[0]=='('
  125. || temp_expression[0]==')')
  126. {
  127. flag=0;
  128. count_1++;
  129. number_length=0;
  130.  
  131. strcpy(infix_expression[count_1],temp_expression);
  132.  
  133. if(temp_expression[0]=='(')
  134. left_parenthesis_count++;
  135.  
  136. else if(temp_expression[0]==')')
  137. right_parenthesis_count++;
  138.  
  139. else
  140. operator_count++;
  141. }
  142.  
  143. if(int(temp_expression[0])==8 && count_1>=0)
  144. {
  145. int length = strlen(infix_expression[count_1]);
  146.  
  147. for(int count_2=0;count_2<length;count_2++)
  148. cout<<char(8)<<" "<<char(8);
  149.  
  150. if(int(infix_expression[count_1][0])>=48 &&
  151. int(infix_expression[count_1][0])<=57)
  152. operand_count--;
  153.  
  154. else if(infix_expression[count_1][0]=='^' ||
  155. infix_expression[count_1][0]=='/' ||
  156. infix_expression[count_1][0]=='*' ||
  157. infix_expression[count_1][0]=='-' ||
  158. infix_expression[count_1][0]=='+' )
  159. operator_count--;
  160.  
  161. else if(infix_expression[count_1][0]=='(')
  162. left_parenthesis_count--;
  163.  
  164. else if(infix_expression[count_1][0]==')')
  165. right_parenthesis_count--;
  166.  
  167. strset(infix_expression[count_1],NULL);
  168.  
  169. flag=0;
  170. count_1--;
  171. input_characters_count--;
  172. }
  173.  
  174. if(int(temp_expression[0])==13)
  175. break;
  176.  
  177. else if(int(temp_expression[0])==27)
  178. {
  179. infix_expression[25][6]='$';
  180.  
  181. break;
  182. }
  183.  
  184. else if(operand_count<operator_count)
  185. {
  186. infix_expression[25][6]='$';
  187.  
  188. break;
  189. }
  190.  
  191. else if(!left_parenthesis_count && right_parenthesis_count)
  192. {
  193. count_1++;
  194.  
  195. break;
  196. }
  197.  
  198. else if(temp_expression[0]!='^' && temp_expression[0]!='/' &&
  199. temp_expression[0]!='*' && temp_expression[0]!='-' &&
  200. temp_expression[0]!='(' && temp_expression[0]!=')' &&
  201. temp_expression[0]!='+' && temp_expression[0]!='0' &&
  202. temp_expression[0]!='1' && temp_expression[0]!='2' &&
  203. temp_expression[0]!='3' && temp_expression[0]!='4' &&
  204. temp_expression[0]!='5' && temp_expression[0]!='6' &&
  205. temp_expression[0]!='7' && temp_expression[0]!='8' &&
  206. temp_expression[0]!='9' && int(temp_expression[0])!=8
  207. && int(temp_expression[0])!=27 )
  208. {
  209. infix_expression[25][6]='$';
  210.  
  211. break;
  212. }
  213.  
  214. input_characters_count++;
  215. }
  216. while(count_1<=22 && input_characters_count<=24);
  217.  
  218. if(operator_count!=(operand_count-1))
  219. expression_right_wrong=0;
  220.  
  221. else if(left_parenthesis_count!=right_parenthesis_count)
  222. expression_right_wrong=0;
  223.  
  224. else if(count_1<2)
  225. expression_right_wrong=0;
  226.  
  227. else if(infix_expression[25][6]=='$')
  228. expression_right_wrong=0;
  229.  
  230. if(expression_right_wrong==0)
  231. {
  232. cout<<"\n\n\t Input Expression is not correct."<<endl;
  233.  
  234. goto Input;
  235. }
  236.  
  237. infix_rows=(count_1+1);
  238. }
  239.  
  240. /*************************************************************************/
  241. //--------------------------- int_push( ) -----------------------------//
  242. /*************************************************************************/
  243.  
  244. void Evaluator::int_push(long item)
  245. {
  246. int_top++;
  247. int_stack[int_top]=item;
  248. }
  249.  
  250. /*************************************************************************/
  251. //--------------------------- int_pop( ) ------------------------------//
  252. /*************************************************************************/
  253.  
  254. long Evaluator::int_pop( )
  255. {
  256. long item=0;
  257.  
  258. item=int_stack[int_top];
  259. int_stack[int_top]=0;
  260. int_top--;
  261.  
  262. return item;
  263. }
  264.  
  265. /*************************************************************************/
  266. //-------------------------- char_push( ) -----------------------------//
  267. /*************************************************************************/
  268.  
  269. void Evaluator::char_push(char item)
  270. {
  271. char_top++;
  272. char_stack[char_top]=item;
  273. }
  274.  
  275. /*************************************************************************/
  276. //-------------------------- char_pop( ) ------------------------------//
  277. /*************************************************************************/
  278.  
  279. char Evaluator::char_pop( )
  280. {
  281. char item=0;
  282.  
  283. item=char_stack[char_top];
  284. char_stack[char_top]=NULL;
  285. char_top--;
  286.  
  287. return item;
  288. }
  289.  
  290. /*************************************************************************/
  291. //----------------- show_infix_to_postfix_convertion( ) ---------------//
  292. /*************************************************************************/
  293.  
  294. void Evaluator::show_infix_to_postfix_convertion( )
  295. {
  296. char_push('(');
  297. strcpy(infix_expression[infix_rows],")");
  298.  
  299. int count_1=0;
  300. int count_2=0;
  301.  
  302. do
  303. {
  304. char symbol_scanned[10]={NULL};
  305.  
  306. strcpy(symbol_scanned,infix_expression[count_1]);
  307.  
  308. if(symbol_scanned[0]=='(')
  309. char_push(symbol_scanned[0]);
  310.  
  311. else if(symbol_scanned[0]==')')
  312. {
  313. char temp[5]={NULL};
  314.  
  315. while(char_stack[char_top]!='(')
  316. {
  317. temp[0]=char_pop( );
  318.  
  319. strcpy(postfix_expression[count_2],temp);
  320.  
  321. count_2++;
  322. }
  323.  
  324. temp[0]=char_pop( );
  325. }
  326.  
  327. else if(symbol_scanned[0]=='/' || symbol_scanned[0]=='*' ||
  328. symbol_scanned[0]=='-' || symbol_scanned[0]=='+'
  329. || symbol_scanned[0]=='^')
  330. {
  331. if(symbol_scanned[0]=='^')
  332. {
  333. }
  334.  
  335. else if(symbol_scanned[0]=='*' || symbol_scanned[0]=='/')
  336. {
  337. while(char_stack[char_top]=='^' ||
  338. char_stack[char_top]=='*' ||
  339. char_stack[char_top]=='/')
  340. {
  341. char temp[5]={NULL};
  342.  
  343. temp[0]=char_pop( );
  344. strcpy(postfix_expression[count_2],temp);
  345.  
  346. count_2++;
  347. }
  348. }
  349.  
  350. else if(symbol_scanned[0]=='+' || symbol_scanned[0]=='-')
  351. {
  352. while(char_stack[char_top]!='(')
  353. {
  354. char temp[5]={NULL};
  355.  
  356. temp[0]=char_pop( );
  357. strcpy(postfix_expression[count_2],temp);
  358.  
  359. count_2++;
  360. }
  361. }
  362.  
  363. char_push(symbol_scanned[0]);
  364. }
  365.  
  366. else if(symbol_scanned[0]!='/' || symbol_scanned[0]!='*' ||
  367. symbol_scanned[0]!='-' || symbol_scanned[0]!='+' ||
  368. symbol_scanned[0]!='(' || symbol_scanned[0]!=')' ||
  369. symbol_scanned[0]!='^')
  370. {
  371. strcpy(postfix_expression[count_2],symbol_scanned);
  372.  
  373. count_2++;
  374. }
  375.  
  376. count_1++;
  377. }
  378. while(char_stack[char_top]!=NULL);
  379.  
  380. postfix_rows=count_2;
  381.  
  382. cout<<"\n\n\t Postfix Expression is : ";
  383.  
  384. for(int count_3=0;count_3<postfix_rows;count_3++)
  385. cout<<postfix_expression[count_3]<<" ";
  386.  
  387. cout<<endl;
  388. }
  389.  
  390. /*************************************************************************/
  391. //------------ show_evaluation_of_postfix_expression( ) ---------------//
  392. /*************************************************************************/
  393.  
  394. void Evaluator::show_evaluation_of_postfix_expression( )
  395. {
  396. int count=0;
  397.  
  398. char symbol_scanned[10]={NULL};
  399.  
  400. strcat(postfix_expression[postfix_rows],"=");
  401.  
  402. do
  403. {
  404. strset(symbol_scanned,NULL);
  405. strcpy(symbol_scanned,postfix_expression[count]);
  406.  
  407. count++;
  408.  
  409. if(symbol_scanned[0]=='/' || symbol_scanned[0]=='*' ||
  410. symbol_scanned[0]=='-' || symbol_scanned[0]=='+' ||
  411. symbol_scanned[0]=='^')
  412. {
  413. long value_1=0;
  414. long value_2=0;
  415. long result=0;
  416.  
  417. value_1=int_pop( );
  418. value_2=int_pop( );
  419.  
  420. switch(symbol_scanned[0])
  421. {
  422. case '+': result=value_2+value_1;
  423. break;
  424.  
  425. case '/': result=value_2/value_1;
  426. break;
  427.  
  428. case '*': result=value_2*value_1;
  429. break;
  430.  
  431. case '-': result=value_2-value_1;
  432. break;
  433.  
  434. case '^': result=powl(value_2,value_1);
  435. break;
  436. }
  437.  
  438. int_push(result);
  439. }
  440.  
  441. else if((symbol_scanned[0]!='/' || symbol_scanned[0]!='*' ||
  442. symbol_scanned[0]!='-' || symbol_scanned[0]!='+' ||
  443. symbol_scanned[0]!='^' )&& symbol_scanned[0]!='=')
  444.  
  445. {
  446. long number=atol(symbol_scanned);
  447.  
  448. int_push(number);
  449. }
  450. }
  451. while(symbol_scanned[0]!='=');
  452.  
  453. cout<<"\n\n\t Result of Postfix Expression Evaluation : ";
  454. cout<<int_stack[0];
  455. getch( );
  456.  
  457. }
  458.  
  459. //---------------------------- main( ) --------------------------------//
  460.  
  461. int main( )
  462. {
  463. system("cls");
  464.  
  465. Evaluator obj;
  466.  
  467. obj.get_infix_expression( );
  468. obj.show_infix_to_postfix_convertion( );
  469. obj.show_evaluation_of_postfix_expression( );
  470.  
  471. return 0;
  472. }
Last edited by taruj83; Nov 11th, 2006 at 1:25 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
taruj83 is offline Offline
9 posts
since Oct 2006
Nov 11th, 2006
0

Re: help with a infix to postfix program

In your in[ut function, if you get one of the characters you want to ignore, just go back and input another character. It's just an addition to your already complex input loop.
Moderator
Reputation Points: 3281
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Nov 11th, 2006
0

Re: help with a infix to postfix program

>this form -> 7*(23-2)+10
but i want to enter it like this -> 7 * (23- 2)+ 10;

So create a method which strips it of whitespace. Or do you mean it crashes when you use spaces? If so use cin.getline() instead.

>also if i enter only the semicolon it should exit the program.

C++ Syntax (Toggle Plain Text)
  1. if input == ";"
  2. Then exit
  3. end if

>i can't get the program to restart and ask the enter the expression thing.

So are you telling me you know how to write a infix to postfix program but don't understand the basic while loop?
Last edited by iamthwee; Nov 11th, 2006 at 4:53 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Turbo C doubt
Next Thread in C++ Forum Timeline: Recursion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC