Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to illustrate the implementation of Stack as an Arithmetic Expression Evaluater

Posted By: Easy Tutor     Category: C++ Programming     Views: 4936

A C++ Program to illustrate the implementation of Stack as an Arithmetic Expression Evaluater.

Code for Program to illustrate the implementation of Stack as an Arithmetic Expression Evaluater in C++ Programming

 #include <iostream.h>
 #include   <string.h>
 #include   <stdlib.h>
 #include    <conio.h>
 #include     <math.h>

 const max_length=10;

 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( );
    };

 /*************************************************************************///----------------------  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:

       clrscr( );
       cout<<"\n\n\t **********   Arithmetic Expression Evaluator   ********* "<<endl;

       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 : ";

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

           elseif(flag==1)
              strcat(infix_expression[count_1],temp_expression);

           number_length++;
        }

         elseif( 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++;

           elseif(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--;

           elseif(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--;

           elseif(infix_expression[count_1][0]=='(')
              left_parenthesis_count--;

           elseif(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;

         elseif(int(temp_expression[0])==27)
        {
           infix_expression[25][6]='$';

           break;
        }

         elseif(operand_count<operator_count)
        {
           infix_expression[25][6]='$';

           break;
        }

         elseif(!left_parenthesis_count && right_parenthesis_count)
        {
           count_1++;

           break;
        }

         elseif(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;

       elseif(left_parenthesis_count!=right_parenthesis_count)
      expression_right_wrong=0;

       elseif(count_1<2)
      expression_right_wrong=0;

       elseif(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]);

         elseif(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( );
        }

         elseif(symbol_scanned[0]=='/' || symbol_scanned[0]=='*' ||
               symbol_scanned[0]=='-' || symbol_scanned[0]=='+'
                           || symbol_scanned[0]=='^')
        {
           if(symbol_scanned[0]=='^')
              {
              }

           elseif(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++;
                }
              }

           elseif(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]);
        }

         elseif(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);
        }

         elseif((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( );
    }


 int main( )
    {
       clrscr( );

       Evaluator obj;

       obj.get_infix_expression( );
       obj.show_infix_to_postfix_convertion( );
       obj.show_evaluation_of_postfix_expression( );

       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to illustrate the implementation of Stack as an Arithmetic Expression Evaluater is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!