| | |
help with a infix to postfix program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 9
Reputation:
Solved Threads: 0
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
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
c Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <stdlib.h> #include <conio.h> #include <math.h> using namespace std; //----------------- Declaration of Constant Variable ------------------// const int max_length=10; //------------------------ Class Definition ---------------------------// class Evaluator { private: char infix_expression[25][6]; char postfix_expression[25][6]; char char_stack[max_length]; long int_stack[max_length]; int int_top; int char_top; int postfix_rows; int infix_rows; int input_characters_count; public: char char_pop( ); long int_pop( ); void set_initial_values( ); void get_infix_expression( ); void char_push(char); void show_infix_to_postfix_convertion( ); void int_push(long); void show_evaluation_of_postfix_expression( ); }; //---------------------- Function Definitions -------------------------// //---------------------- set_initial_values( ) ------------------------// void Evaluator::set_initial_values( ) { int_top=-1; char_top=-1; infix_rows=0; postfix_rows=0; for(int count_1=0;count_1<max_length;count_1++) { int_stack[count_1]=0; char_stack[count_1]=NULL; } for(int count_2=0;count_2<25;count_2++) { strset(infix_expression[count_2],NULL); strset(postfix_expression[count_2],NULL); } } /*************************************************************************/ //----------------------- get_infix_expression( ) ---------------------// /*************************************************************************/ void Evaluator::get_infix_expression( ) { Input: system("cls"); set_initial_values( ); int flag=0; int count_1=-1; int number_length=0; int operand_count=0; int operator_count=0; int expression_right_wrong=1; int input_characters_count=0; int left_parenthesis_count=0; int right_parenthesis_count=0; char temp_expression[5]={NULL}; cout<<"\n\n\n\t Enter the Infix Expression : "<<endl; do { temp_expression[0]=getch(); if(int(temp_expression[0])!=8) cout<<temp_expression; if((int(temp_expression[0])>=48 && int(temp_expression[0])<=57) && number_length<5) { if(flag==0) { count_1++; flag=1; operand_count++; strcpy(infix_expression[count_1],temp_expression); } else if(flag==1) strcat(infix_expression[count_1],temp_expression); number_length++; } else if( temp_expression[0]=='^' || temp_expression[0]=='/' || temp_expression[0]=='*' || temp_expression[0]=='-' || temp_expression[0]=='+' || temp_expression[0]=='(' || temp_expression[0]==')') { flag=0; count_1++; number_length=0; strcpy(infix_expression[count_1],temp_expression); if(temp_expression[0]=='(') left_parenthesis_count++; else if(temp_expression[0]==')') right_parenthesis_count++; else operator_count++; } if(int(temp_expression[0])==8 && count_1>=0) { int length = strlen(infix_expression[count_1]); for(int count_2=0;count_2<length;count_2++) cout<<char(8)<<" "<<char(8); if(int(infix_expression[count_1][0])>=48 && int(infix_expression[count_1][0])<=57) operand_count--; else if(infix_expression[count_1][0]=='^' || infix_expression[count_1][0]=='/' || infix_expression[count_1][0]=='*' || infix_expression[count_1][0]=='-' || infix_expression[count_1][0]=='+' ) operator_count--; else if(infix_expression[count_1][0]=='(') left_parenthesis_count--; else if(infix_expression[count_1][0]==')') right_parenthesis_count--; strset(infix_expression[count_1],NULL); flag=0; count_1--; input_characters_count--; } if(int(temp_expression[0])==13) break; else if(int(temp_expression[0])==27) { infix_expression[25][6]='$'; break; } else if(operand_count<operator_count) { infix_expression[25][6]='$'; break; } else if(!left_parenthesis_count && right_parenthesis_count) { count_1++; break; } else if(temp_expression[0]!='^' && temp_expression[0]!='/' && temp_expression[0]!='*' && temp_expression[0]!='-' && temp_expression[0]!='(' && temp_expression[0]!=')' && temp_expression[0]!='+' && temp_expression[0]!='0' && temp_expression[0]!='1' && temp_expression[0]!='2' && temp_expression[0]!='3' && temp_expression[0]!='4' && temp_expression[0]!='5' && temp_expression[0]!='6' && temp_expression[0]!='7' && temp_expression[0]!='8' && temp_expression[0]!='9' && int(temp_expression[0])!=8 && int(temp_expression[0])!=27 ) { infix_expression[25][6]='$'; break; } input_characters_count++; } while(count_1<=22 && input_characters_count<=24); if(operator_count!=(operand_count-1)) expression_right_wrong=0; else if(left_parenthesis_count!=right_parenthesis_count) expression_right_wrong=0; else if(count_1<2) expression_right_wrong=0; else if(infix_expression[25][6]=='$') expression_right_wrong=0; if(expression_right_wrong==0) { cout<<"\n\n\t Input Expression is not correct."<<endl; goto Input; } infix_rows=(count_1+1); } /*************************************************************************/ //--------------------------- int_push( ) -----------------------------// /*************************************************************************/ void Evaluator::int_push(long item) { int_top++; int_stack[int_top]=item; } /*************************************************************************/ //--------------------------- int_pop( ) ------------------------------// /*************************************************************************/ long Evaluator::int_pop( ) { long item=0; item=int_stack[int_top]; int_stack[int_top]=0; int_top--; return item; } /*************************************************************************/ //-------------------------- char_push( ) -----------------------------// /*************************************************************************/ void Evaluator::char_push(char item) { char_top++; char_stack[char_top]=item; } /*************************************************************************/ //-------------------------- char_pop( ) ------------------------------// /*************************************************************************/ char Evaluator::char_pop( ) { char item=0; item=char_stack[char_top]; char_stack[char_top]=NULL; char_top--; return item; } /*************************************************************************/ //----------------- show_infix_to_postfix_convertion( ) ---------------// /*************************************************************************/ void Evaluator::show_infix_to_postfix_convertion( ) { char_push('('); strcpy(infix_expression[infix_rows],")"); int count_1=0; int count_2=0; do { char symbol_scanned[10]={NULL}; strcpy(symbol_scanned,infix_expression[count_1]); if(symbol_scanned[0]=='(') char_push(symbol_scanned[0]); else if(symbol_scanned[0]==')') { char temp[5]={NULL}; while(char_stack[char_top]!='(') { temp[0]=char_pop( ); strcpy(postfix_expression[count_2],temp); count_2++; } temp[0]=char_pop( ); } else if(symbol_scanned[0]=='/' || symbol_scanned[0]=='*' || symbol_scanned[0]=='-' || symbol_scanned[0]=='+' || symbol_scanned[0]=='^') { if(symbol_scanned[0]=='^') { } else if(symbol_scanned[0]=='*' || symbol_scanned[0]=='/') { while(char_stack[char_top]=='^' || char_stack[char_top]=='*' || char_stack[char_top]=='/') { char temp[5]={NULL}; temp[0]=char_pop( ); strcpy(postfix_expression[count_2],temp); count_2++; } } else if(symbol_scanned[0]=='+' || symbol_scanned[0]=='-') { while(char_stack[char_top]!='(') { char temp[5]={NULL}; temp[0]=char_pop( ); strcpy(postfix_expression[count_2],temp); count_2++; } } char_push(symbol_scanned[0]); } else if(symbol_scanned[0]!='/' || symbol_scanned[0]!='*' || symbol_scanned[0]!='-' || symbol_scanned[0]!='+' || symbol_scanned[0]!='(' || symbol_scanned[0]!=')' || symbol_scanned[0]!='^') { strcpy(postfix_expression[count_2],symbol_scanned); count_2++; } count_1++; } while(char_stack[char_top]!=NULL); postfix_rows=count_2; cout<<"\n\n\t Postfix Expression is : "; for(int count_3=0;count_3<postfix_rows;count_3++) cout<<postfix_expression[count_3]<<" "; cout<<endl; } /*************************************************************************/ //------------ show_evaluation_of_postfix_expression( ) ---------------// /*************************************************************************/ void Evaluator::show_evaluation_of_postfix_expression( ) { int count=0; char symbol_scanned[10]={NULL}; strcat(postfix_expression[postfix_rows],"="); do { strset(symbol_scanned,NULL); strcpy(symbol_scanned,postfix_expression[count]); count++; if(symbol_scanned[0]=='/' || symbol_scanned[0]=='*' || symbol_scanned[0]=='-' || symbol_scanned[0]=='+' || symbol_scanned[0]=='^') { long value_1=0; long value_2=0; long result=0; value_1=int_pop( ); value_2=int_pop( ); switch(symbol_scanned[0]) { case '+': result=value_2+value_1; break; case '/': result=value_2/value_1; break; case '*': result=value_2*value_1; break; case '-': result=value_2-value_1; break; case '^': result=powl(value_2,value_1); break; } int_push(result); } else if((symbol_scanned[0]!='/' || symbol_scanned[0]!='*' || symbol_scanned[0]!='-' || symbol_scanned[0]!='+' || symbol_scanned[0]!='^' )&& symbol_scanned[0]!='=') { long number=atol(symbol_scanned); int_push(number); } } while(symbol_scanned[0]!='='); cout<<"\n\n\t Result of Postfix Expression Evaluation : "; cout<<int_stack[0]; getch( ); } //---------------------------- main( ) --------------------------------// int main( ) { system("cls"); Evaluator obj; obj.get_infix_expression( ); obj.show_infix_to_postfix_convertion( ); obj.show_evaluation_of_postfix_expression( ); return 0; }
Last edited by taruj83; Nov 11th, 2006 at 1:25 am.
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.
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
>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.
>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?
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)
if input == ";" Then exit 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.
*Voted best profile in the world*
![]() |
Similar Threads
- Help pplz. . convert infix to postfix. . (C++)
- Prefix to Infix help please (C)
- need Recursion help (C)
- static variable (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++)
Other Threads in the C++ Forum
- Previous Thread: Turbo C doubt
- Next Thread: Recursion
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






