Tuesday, 9 June 2015

Program to illustrate the implementation of Arithmetic Expression Evaluater accepting {} & [] also

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

 const max_length=25;


 class Evaluator
    {
       private:
      char infix_expression[50][8];
      char postfix_expression[50][8];

      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( );
      int validate_infix_expression(char*);

      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;

       cout<<"\n\n\n\t Enter the Infix Expression : ";

       char sTemp[100] = {NULL};

       gets(sTemp);

       set_initial_values( );

       if (validate_infix_expression(sTemp) == 0)
      {
         cout<<"\n\n\t Input Expression is not correct.";
         getch( );

         goto Input;
      }
    }

 /*************************************************************************///--------------------  validate_infix_expression( )  -------------------///*************************************************************************/int Evaluator::validate_infix_expression(char* sTemp)
    {
       Input:

       int i=0;
       int length=strlen(sTemp);

       int flag=0;
       int count_1=-1;
       int number_length=0;
       int operand_count=0;
       int operator_count=0;
       int left_parenthesis_count=0;
       int right_parenthesis_count=0;
       int left_braces_count=0;
       int right_braces_count=0;
       int left_brackets_count=0;
       int right_brackets_count=0;

       char temp_expression[10]={NULL};

       do
      {
         temp_expression[0]=sTemp[i];

         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]==')' || 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++;

           elseif(temp_expression[0]=='{')
              left_braces_count++;

           elseif(temp_expression[0]=='}')
              right_braces_count++;

           elseif(temp_expression[0]=='[')
              left_brackets_count++;

           elseif(temp_expression[0]==']')
              right_brackets_count++;

           else
              operator_count++;
        }

         elseif(operand_count<operator_count)
        return 0;

         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]!='}' &&
              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')
           return 0;

         i++;
      }
       while(i<length);

       if(operator_count!=(operand_count-1))
      return 0;

       elseif(left_parenthesis_count!=right_parenthesis_count)
      return 0;

       elseif(left_braces_count!=right_braces_count)
      return 0;

       elseif(left_brackets_count!=right_brackets_count)
      return 0;

       elseif(count_1<2)
      return 0;

       infix_rows=(count_1+1);

       return 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]=='(' || symbol_scanned[0]=='{' || 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]=='}')
        {
           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]==']')
        {
           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_stack[char_top]!='{' && 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]!='{' || 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;
    }
[/Code]

No comments:

Post a Comment