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
#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;
}