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: 2151

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 <graphics.h>
 #include <string.h>
 #include <stdlib.h>
 #include <conio.h>
 #include <math.h>
 #include <dos.h>

 const max_length=10;


 class Evaluator
    {
       private:
        char infix_expression[25][6];
        char postfix_expression[25][6];

        char char_stack[10];
        long int_stack[max_length];

        int int_top;
        int char_top;

        int postfix_rows;
        int infix_rows;

        int input_characters_count;

       public:
        void set_initial_values( );
        void show_main_screen( );
        void get_infix_expression( );
        void show_infix_screen( );
        void char_push(char);
        char char_pop( );
        void show_char_stack(int);
        void show_explanation(int);
        void show_infix_to_postfix_convertion( );
        void int_push(long);
        long int_pop( );
        void show_int_stack(int);
        void show_postfix_screen ( );
        void show_evaluation_of_postfix_expression( );
        void show_summary( );
    };

 /*************************************************************************//*************************************************************************//***********************  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]='\0';
      }

       for(int count_2=0;count_2<25;count_2++)
      {
         for(int count_3=0;count_3<6;count_3++)
        {
           infix_expression[count_2][count_3]='\0';
           postfix_expression[count_2][count_3]='\0';
        }
      }
    }

 /*************************************************************************///-----------------------  show_main_screen( )  -------------------------///*************************************************************************/void Evaluator::show_main_screen( )
    {
       cleardevice( );

       for(int count_1=0;count_1<340;count_1+=20)
      {
         setfillstyle(6,1);
           bar(getmaxx( )/2-count_1,getmaxy( )/2-count_1,
                   getmaxx( )/2+count_1,getmaxy( )/2+count_1);
      }

       for(int count_2=0;count_2<15;count_2++)
      {
         setcolor(count_2);
           rectangle(5+count_2,count_2+5,getmaxx( )-5-count_2,
                               getmaxy( )-5-count_2);
         delay(10);
      }

       delay(500);

       char expression[10][2]={"E","x","p","r","e","s","s","i","o","n"};

       int x=80;
       int y=50;

       for(int count_3=0;count_3<10;count_3++)
      {
         settextstyle(1,0,8);
           setcolor(0);

           moveto(x-3,y+2);
         outtext(expression[count_3]);

           moveto(x-2,y+2);
         outtext(expression[count_3]);

           moveto(x-1,y+2);
         outtext(expression[count_3]);

           setcolor(12);

           moveto(x,y);
         outtext(expression[count_3]);

           moveto(x+1,y);
         outtext(expression[count_3]);

           moveto(x+2,y);
         outtext(expression[count_3]);

           moveto(x+3,y);
         outtext(expression[count_3]);

         x=getx( );
         delay(150);
      }

       delay(500);

       char evaluator[9][10]={"r","or","tor","ator","uator","luator",
                  "aluator","valuator","Evaluator"};

       for(int count_4=0;count_4<9;count_4++)
      {
         settextstyle(1,0,8);
           setcolor(0);
         outtextxy(217,122,evaluator[count_4]);
         outtextxy(218,122,evaluator[count_4]);
         outtextxy(219,122,evaluator[count_4]);

           setcolor(12);
         outtextxy(220,120,evaluator[count_4]);
         outtextxy(221,120,evaluator[count_4]);
         outtextxy(222,120,evaluator[count_4]);
         outtextxy(223,120,evaluator[count_4]);

         delay(150);

         if(count_4<8)
        {
           setfillstyle(6,1);
             bar(210,140,580,200);
        }
      }

       delay(500);

       settextstyle(1,0,4);
     setcolor(0);
       outtextxy(150,52,"Arithmetic");
       outtextxy(151,52,"Arithmetic");

     setcolor(12);
       outtextxy(152,50,"Arithmetic");
       outtextxy(153,50,"Arithmetic");

       delay(500);

       setcolor(15);
     line(495,415,580,415);
     line(496,416,580,416);

     line(495,449,580,449);
     line(496,448,580,448);

     line(495,415,495,449);
     line(496,417,496,448);

     arc(580,432,270,90,17);
     arc(580,432,270,90,16);

       setcolor(7);
     line(497,417,580,417);
     line(498,418,580,418);

     line(497,447,580,447);
     line(498,446,580,446);

     line(497,417,497,447);
     line(498,418,498,446);

     arc(580,432,270,90,15);
     arc(580,432,270,90,14);

       setcolor(8);
     line(499,419,580,419);
     line(499,445,580,445);
     line(499,419,499,445);

     arc(580,432,270,90,13);

       setcolor(9);
       setfillstyle(1,9);
     bar(500,420,580,444);

     pieslice(580,432,0,360,12);

       settextstyle(2,0,6);
     setcolor(0);
       outtextxy(508,422,"Evaluate");
       outtextxy(509,422,"Evaluate");

     setcolor(14);
       outtextxy(510,420,"Evaluate");
       outtextxy(511,420,"Evaluate");

       setcolor(15);
     line(425,415,490,415);
     line(426,416,489,416);

     line(425,449,490,449);
     line(426,448,489,448);

     line(490,415,490,449);
     line(489,416,489,448);

     arc(430,432,90,270,17);
     arc(430,432,90,270,16);

       setcolor(7);
     line(427,417,488,417);
     line(428,418,487,418);

     line(427,447,488,447);
     line(428,446,487,446);

     line(488,417,488,447);
     line(487,418,487,446);

     arc(430,432,90,270,15);
     arc(430,432,90,270,14);

       setcolor(8);
     line(429,419,486,419);
     line(429,445,486,445);
     line(486,419,486,445);

     arc(430,432,90,270,13);

       setcolor(9);
       setfillstyle(1,9);
     bar(430,420,485,444);

     pieslice(430,432,0,360,12);

       settextstyle(2,0,6);
     setcolor(0);
       outtextxy(433,422,"Exit");
       outtextxy(434,422,"Exit");

     setcolor(14);
       outtextxy(435,420,"Exit");
       outtextxy(436,420,"Exit");

       setcolor(15);
       settextstyle(0,0,1);
     outtextxy(30,425,"* Copyright  (C) 2000-01");
     outtextxy(30,440,"* Muhammad Tahir Shahzad");

       int count_5=0;
       int key_code=0;

       char key='\0';

       do
     {
         delay(500);

         key='\0';
         key_code=0;

         if(kbhit( ))
        key=getch( );

         key_code=int(key);

         if(count_5%2==0)
        {
           settextstyle(0,0,1);
             setcolor(1);
               outtextxy(435,400,"<Esc>");

             setcolor(4);
               outtextxy(515,400,"<Enter>");
        }

         elseif(count_5%2==1)
        {
           settextstyle(0,0,1);
             setcolor(4);
               outtextxy(435,400,"<Esc>");

             setcolor(1);
               outtextxy(515,400,"<Enter>");
        }

         count_5++;
     }
       while(key_code!=13 && key_code!=27);

       if(key_code==13)
      {
         settextstyle(0,0,1);
           setcolor(1);
         outtextxy(435,400,"<Esc>");

           setcolor(4);
         outtextxy(515,400,"<Enter>");

         setcolor(12);
         setfillstyle(1,12);
           bar(500,420,580,444);

           pieslice(580,432,0,360,12);

         settextstyle(2,0,6);
           setcolor(0);
         outtextxy(508,422,"Evaluate");
         outtextxy(509,422,"Evaluate");

           setcolor(14);
         outtextxy(510,420,"Evaluate");
         outtextxy(511,420,"Evaluate");

         delay(500);

         setcolor(9);
         setfillstyle(1,9);
           bar(500,420,580,444);

           pieslice(580,432,0,360,12);

         settextstyle(2,0,6);
           setcolor(0);
         outtextxy(508,422,"Evaluate");
         outtextxy(509,422,"Evaluate");

           setcolor(14);
         outtextxy(510,420,"Evaluate");
         outtextxy(511,420,"Evaluate");
      }

       elseif(key_code==27)
      {
         settextstyle(0,0,1);
           setcolor(4);
         outtextxy(435,400,"<Esc>");

           setcolor(1);
         outtextxy(515,400,"<Enter>");

         setcolor(12);
         setfillstyle(1,12);
           bar(430,420,485,444);

           pieslice(430,432,0,360,12);

         settextstyle(2,0,6);
           setcolor(0);
         outtextxy(433,422,"Exit");
         outtextxy(434,422,"Exit");

           setcolor(14);
         outtextxy(435,420,"Exit");
         outtextxy(436,420,"Exit");

         delay(500);

         setcolor(9);
         setfillstyle(1,9);
           bar(430,420,485,444);

           pieslice(430,432,0,360,12);

         settextstyle(2,0,6);
           setcolor(0);
         outtextxy(433,422,"Exit");
         outtextxy(434,422,"Exit");

           setcolor(14);
         outtextxy(435,420,"Exit");
         outtextxy(436,420,"Exit");

         delay(500);
         exit(0);
      }

       delay(500);
       cleardevice( );
    }

 /*************************************************************************///-----------------------  get_infix_expression( )  ---------------------///*************************************************************************/void Evaluator::get_infix_expression( )
    {
       setfillstyle(1,6);
     bar(0,0,getmaxx( ),getmaxy( ));

       setfillstyle(1,3);
     bar(5,5,getmaxx( )-5,getmaxy( )-5);

       setfillstyle(6,1);
     bar(10,10,getmaxx( )-10,getmaxy( )-10);

       for(int count_1=0;count_1<3;count_1++)
      {
         setcolor(1);
           settextstyle(4,0,10);
         outtextxy(20-count_1,50-count_1,"+");
         outtextxy(420-count_1,20,"^");
         outtextxy(100-count_1,150,"*");
         outtextxy(550-count_1,100-count_1,"-");
         outtextxy(320-count_1,20,"/");
         outtextxy(250-count_1,140,"(");
         outtextxy(500-count_1,250,")");

           settextstyle(1,0,10);
         outtextxy(70-count_1,320,"0");
         outtextxy(550-count_1,-20,"1");
         outtextxy(150-count_1,100,"2");
         outtextxy(450-count_1,100,"3");
         outtextxy(220-count_1,-20,"4");
         outtextxy(570-count_1,220,"5");
         outtextxy(360-count_1,170,"6");
         outtextxy(200-count_1,335,"7");
         outtextxy(20-count_1,-10,"8");
         outtextxy(400-count_1,320,"9");
      }

       settextstyle(1,0,4);
     setcolor(0);
       outtextxy(150,52,"Arithmetic");
       outtextxy(151,52,"Arithmetic");

     setcolor(12);
       outtextxy(152,50,"Arithmetic");
       outtextxy(153,50,"Arithmetic");

       settextstyle(1,0,8);
     setcolor(0);
       outtextxy(77,52,"Expression");
       outtextxy(78,52,"Expression");
       outtextxy(79,52,"Expression");

     setcolor(12);
       outtextxy(80,50,"Expression");
       outtextxy(81,50,"Expression");
       outtextxy(82,50,"Expression");
       outtextxy(83,50,"Expression");

       settextstyle(1,0,8);
     setcolor(0);
       outtextxy(217,122,"Evaluator");
       outtextxy(218,122,"Evaluator");
       outtextxy(219,122,"Evaluator");

     setcolor(12);
       outtextxy(220,120,"Evaluator");
       outtextxy(221,120,"Evaluator");
       outtextxy(222,120,"Evaluator");
       outtextxy(223,120,"Evaluator");

       settextstyle(6,0,4);
     setcolor(8);
       outtextxy(28,262,"Enter the Infix Expression :");
       outtextxy(29,262,"Enter the Infix Expression :");

     setcolor(10);
       outtextxy(30,260,"Enter the Infix Expression :");
       outtextxy(31,260,"Enter the Infix Expression :");
       outtextxy(32,260,"Enter the Infix Expression :");

       setcolor(15);
     rectangle(29,305,595,375);
     rectangle(30,306,594,374);

       setcolor(7);
     rectangle(31,307,593,373);
     rectangle(32,308,592,372);
     rectangle(33,309,591,371);

       setcolor(8);
     rectangle(34,310,590,370);

       setfillstyle(1,0);
     bar(35,311,589,369);

       int flag=0;
       int count_2=-1;
       int number_length=0;
       int operand_count=0;
       int operator_count=0;
       int expression_right_wrong=1;
       int left_parenthesis_count=0;
       int right_parenthesis_count=0;

       do
      {
         set_initial_values( );

         flag=0;
         count_2=-1;
         number_length=0;
         operand_count=0;
         operator_count=0;
         input_characters_count=0;
         expression_right_wrong=1;
         left_parenthesis_count=0;
         right_parenthesis_count=0;

         do
        {
           if(infix_expression[0][0]=='\0')
              {
             while(!kbhit( ))
                {
                   setcolor(14);
                   settextstyle(1,0,4);
                 outtextxy(40,313,"Þ");
                 outtextxy(40,327,"Þ");

                   delay(250);

                   setfillstyle(0,0);
                 bar(35,311,589,369);

                   delay(300);
                }
              }

           char temp_expression[2]={'\0'};

           temp_expression[0]=getch( );

           if( (int(temp_expression[0])>=48 &&
                int(temp_expression[0])<=57) && number_length<5 )
              {
             if(flag==0)
                {
                   count_2++;
                   strcpy(infix_expression[count_2],
                                temp_expression);
                   flag=1;
                   operand_count++;
                }

             elseif(flag==1)
                strcat(infix_expression[count_2],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]==')')
              {
             count_2++;

             strcpy(infix_expression[count_2],temp_expression);

             flag=0;
             number_length=0;

             if(temp_expression[0]=='(')
                left_parenthesis_count++;

             if(temp_expression[0]==')')
                right_parenthesis_count++;

             if(temp_expression[0]!='(' &&
                             temp_expression[0]!=')')
                operator_count++;
              }

           if(int(temp_expression[0])==8 && count_2>=0)
              {
             setfillstyle(1,0);
               bar(35,311,589,369);

             if(int(infix_expression[count_2][0])>=48 &&
                       int(infix_expression[count_2][0])<=57)
                operand_count--;

             if(infix_expression[count_2][0]=='^' ||
                   infix_expression[count_2][0]=='/' ||
                  infix_expression[count_2][0]=='*' ||
                     infix_expression[count_2][0]=='-' ||
                    infix_expression[count_2][0]=='+' )
                operator_count--;

             if(infix_expression[count_2][0]=='(')
                left_parenthesis_count--;

             if(infix_expression[count_2][0]==')')
                right_parenthesis_count--;

             for(int count_3=0;count_3<6;count_3++)

             strset(infix_expression[count_2],NULL);

             count_2--;
             input_characters_count--;
             flag=0;
              }

           if(int(temp_expression[0])==13)
              break;

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

           if(operand_count<operator_count)
              {
             infix_expression[25][6]='$';
             break;
              }

           if(!left_parenthesis_count && right_parenthesis_count)
              {
             count_2++;
             break;
              }

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

           int x=45;
           int y=320;

           for(int count_4=0;count_4<=count_2;count_4++)
              {
             setcolor(14);
             settextstyle(1,0,4);

             moveto(x,y);
               outtext(infix_expression[count_4]);

             x=getx( );
              }

           input_characters_count++;
        }
         while(count_2<=22 && input_characters_count<=24);

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

         if(left_parenthesis_count!=right_parenthesis_count)
        expression_right_wrong=0;

         if(count_2<2)
        expression_right_wrong=0;

         if(infix_expression[25][6]=='$')
        expression_right_wrong=0;

         if(expression_right_wrong==0)
        {
           setfillstyle(1,0);
             bar(35,311,589,369);

           setcolor(10);
           settextstyle(1,0,5);
             outtextxy(60,310,"Wrong Expression");

           sound(2500);
           delay(500);
           nosound( );

           setfillstyle(1,0);
             bar(35,311,589,369);
        }
      }
       while(!expression_right_wrong);

       if(input_characters_count==25)
      while(int(getch( ))!=13);

       infix_rows=count_2+1;

       cleardevice( );
    }

 /*************************************************************************///---------------------------  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]='\0';
       char_top--;

       return item;
    }

 /*************************************************************************///----------------------  show_infix_screen( )  -------------------------///*************************************************************************/void Evaluator::show_infix_screen( )
    {
       setfillstyle(1,1);
     bar(0,0,getmaxx( ),getmaxy( ));

       setfillstyle(1,3);
     bar(5,5,getmaxx( )-5,getmaxy( )-5);

       setfillstyle(1,6);
     bar(10,10,getmaxx( )-10,getmaxy( )-10);

       setcolor(15);
     line(75,45,530,45);

       setcolor(7);
     line(75,46,530,46);
     line(75,47,530,47);
     line(75,48,530,48);

       setcolor(8);
     line(75,49,530,49);

       setcolor(0);
     line(75,50,530,50);

       settextstyle(2,0,8);
     setcolor(0);
       outtextxy(78,21,"Convertion from Infix to Postfix");
       outtextxy(79,21,"Convertion from Infix to Postfix");

     setcolor(3);
       outtextxy(80,20,"Convertion from Infix to Postfix");
       outtextxy(81,20,"Convertion from Infix to Postfix");
       outtextxy(82,20,"Convertion from Infix to Postfix");

       setcolor(10);
       settextstyle(2,0,7);
     outtextxy(20,65,"Infix Expression :");
     outtextxy(21,65,"Infix Expression :");

       setcolor(15);
     rectangle(18,88,287,127);

       setcolor(7);
     rectangle(20,90,285,125);
     rectangle(19,89,286,126);

       setfillstyle(1,0);
     bar(21,91,284,124);

       setcolor(8);
     rectangle(21,91,284,124);

       setcolor(15);
     line(55,217,255,217);
     line(55,253,255,253);

     arc(55,235,90,270,18);
     arc(255,235,270,90,18);

     rectangle(52,255,258,443);

       setcolor(7);
     line(55,218,255,218);
     line(55,219,255,219);

     line(55,251,255,251);
     line(55,252,255,252);

     arc(55,235,90,270,17);
     arc(55,235,90,270,16);

     arc(255,235,270,90,17);
     arc(255,235,270,90,16);

     rectangle(53,256,257,442);
     rectangle(54,257,256,441);

       setcolor(8);
     line(55,220,255,220);
     line(55,250,255,250);

     arc(55,235,90,270,15);
     arc(255,235,270,90,15);

     rectangle(55,258,255,440);

     setcolor(9);
     setfillstyle(1,9);
       bar(55,221,255,249);

       pieslice(55,235,0,360,14);
       pieslice(255,235,0,360,14);

       settextstyle(2,0,8);
     setcolor(0);
       outtextxy(77,221,"Explanation");
       outtextxy(78,221,"Explanation");

     setcolor(14);
       outtextxy(79,220,"Explanation");
       outtextxy(80,220,"Explanation");
       outtextxy(81,220,"Explanation");

       setfillstyle(1,0);
     bar(56,259,254,439);

       setcolor(8);
     line(183,404,220,404);
     line(183,430,220,430);

     line(183,404,183,430);

     arc(220,417,270,90,13);

       setcolor(7);
     line(184,405,220,405);
     line(185,406,220,406);

     line(184,429,220,429);
     line(185,428,220,428);

     line(184,405,184,429);
     line(185,406,185,428);

     arc(220,417,270,90,12);
     arc(220,417,270,90,11);

       setcolor(8);
     line(186,407,220,407);
     line(186,427,220,427);
     line(186,407,186,427);

     arc(220,417,270,90,10);

       setcolor(9);
       setfillstyle(1,9);
     bar(187,408,220,426);

     pieslice(220,417,0,360,9);

       settextstyle(2,0,5);
     setcolor(0);
       outtextxy(189,409,"Next>");
       outtextxy(190,409,"Next>");

     setcolor(14);
       outtextxy(191,408,"Next>");
       outtextxy(192,408,"Next>");

       setfillstyle(1,0);
     bar(295,88,620,460);

       setcolor(15);
     rectangle(295,88,620,460);

       setcolor(7);
     rectangle(296,89,619,459);
     rectangle(297,90,618,458);
     rectangle(298,91,617,457);

       setcolor(8);
     rectangle(299,92,616,456);

       setfillstyle(1,9);
     bar(300,92,615,119);

       setcolor(15);
     line(300,120,615,120);

       setcolor(7);
     line(300,121,615,121);
     line(300,122,615,122);

       setcolor(8);
     line(300,123,615,123);

       setcolor(15);
     line(367,92,367,456);

       setcolor(7);
     line(368,92,368,456);
     line(369,92,369,456);

       setcolor(8);
     line(370,92,370,456);

       setcolor(15);
     line(442,92,442,456);

       setcolor(7);
     line(443,92,443,456);
     line(444,92,444,456);

       setcolor(8);
     line(445,92,445,456);

       settextstyle(2,0,5);
     setcolor(0);
       outtextxy(305,92,"Symbol");
       outtextxy(305,105,"Scanned");

       outtextxy(306,92,"Symbol");
       outtextxy(306,105,"Scanned");

     setcolor(14);
       outtextxy(306,91,"Symbol");
       outtextxy(306,104,"Scanned");

       outtextxy(307,91,"Symbol");
       outtextxy(307,104,"Scanned");

       settextstyle(2,0,7);
     setcolor(0);
       outtextxy(375,96,"STACK");
       outtextxy(376,96,"STACK");

     setcolor(14);
       outtextxy(377,95,"STACK");
       outtextxy(378,95,"STACK");

     setcolor(0);
       outtextxy(459,96,"Expression P");
       outtextxy(459,96,"Expression P");

     setcolor(14);
       outtextxy(460,95,"Expression P");
       outtextxy(461,95,"Expression P");

       settextstyle(2,0,4);
     setcolor(15);
       outtextxy(20,447,"Note : If you don't want to see explanation,");
       outtextxy(60,459,"Press <Esc> , else Press any Key.");

     setcolor(10);
       outtextxy(20,447,"Note :");
       outtextxy(21,447,"Note :");
    }

 /*************************************************************************///-----------------  show_infix_to_postfix_convertion( )  ---------------///*************************************************************************/void Evaluator::show_infix_to_postfix_convertion( )
    {
       show_infix_screen( );

       int x_1=30;
       int y_1=97;
       int color_array[7]={3,6,5,10,11,14,12};

       for(int count_1=0;count_1<=infix_rows;count_1++)
     {
        settextstyle(2,0,6);
        setcolor(color_array[count_1%7]);

          moveto(x_1-1,y_1);
        outtext(infix_expression[count_1]);

          moveto(x_1,y_1);
        outtext(infix_expression[count_1]);

        x_1=getx( );
     }

       char_push('(');
       strcpy(infix_expression[infix_rows],")");

       int count_2=0;
       int count_3=0;
       int explain_yes_not=0;

       do
      {
         char symbol_scanned[6]={'\0'};
         char step_number[5]={'\0'};

         strcpy(symbol_scanned,infix_expression[count_2]);

         itoa(count_2+1,step_number,10);
         strcat(step_number,".");

         if(symbol_scanned[0]=='(')
        {
           char_push(symbol_scanned[0]);

           if(explain_yes_not==0)
              show_explanation(2);
        }

         elseif(symbol_scanned[0]==')')
        {
           char temp[2]={'\0'};

           while(char_stack[char_top]!='(')
              {
             temp[0]='\0';
             temp[0]=char_pop( );

             strcpy(postfix_expression[count_3],temp);

             count_3++;
              }

            temp[0]=char_pop( );

            if(explain_yes_not==0)
               show_explanation(3);
        }

         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[2]={'\0'};

                temp[0]=char_pop( );
                strcpy(postfix_expression[count_3],temp);

                count_3++;
                }
              }

           elseif(symbol_scanned[0]=='+' || symbol_scanned[0]=='-')
              {
             while(char_stack[char_top]!='(')
                {
                char temp[2]={'\0'};

                temp[0]=char_pop( );
                strcpy(postfix_expression[count_3],temp);

                count_3++;
                 }
              }

           char_push(symbol_scanned[0]);

           if(explain_yes_not==0)
              show_explanation(4);
        }

         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_3],symbol_scanned);

           if(explain_yes_not==0)
              show_explanation(1);

           count_3++;
        }

         if(infix_rows>16 || input_characters_count>20)
        {
           settextstyle(2,0,4);
             setcolor(15);
               outtextxy(302,130+(12*count_2),step_number);

             setcolor(14);
               outtextxy(328,130+(12*count_2),symbol_scanned);
        }

         else
        {
           settextstyle(0,0,1);
             setcolor(15);
               outtextxy(302,130+(14*count_2),step_number);

             setcolor(14);
               outtextxy(328,130+(14*count_2),symbol_scanned);
        }

         int x_2=450;
         int y_2;

         if(infix_rows>16 || input_characters_count>20)
        {
           y_2=130+(count_2*12);

           settextstyle(2,0,4);
        }

         else
        {
           y_2=130+(count_2*14);

           settextstyle(0,0,1);
        }

         for(int count_4=0;count_4<count_3;count_4++)
        {
           setcolor(color_array[count_4%7]);

           moveto(x_2,y_2);
             outtext(postfix_expression[count_4]);

           x_2=getx( );
        }

         show_char_stack(count_2);

         count_2++;

         char key='\0';

         int key_code=0;

         if(explain_yes_not==0)
        {
           key=getch( );
           key_code=int(key);

           if(key_code==27)
              explain_yes_not=1;
        }

         else
        delay(200);

      }
       while(char_stack[char_top]!='\0');

       show_explanation(8);

       while(int(getch( ))!=13);

       postfix_rows=count_3;

    }

 /*************************************************************************///--------------------------  show_explanation(int)  --------------------///*************************************************************************/void Evaluator::show_explanation(int flag)
    {
       setcolor(12);
       setfillstyle(1,12);
     bar(187,408,220,426);

     pieslice(220,417,0,360,9);

       settextstyle(2,0,5);
     setcolor(0);
       outtextxy(189,409,"Next>");
       outtextxy(190,409,"Next>");

     setcolor(14);
       outtextxy(191,408,"Next>");
       outtextxy(192,408,"Next>");

       delay(500);

       setfillstyle(1,0);
     bar(56,259,254,395);

       setcolor(15);
       settextstyle(2,0,4);

       if(flag==0)
      {
         outtextxy(60,263,"  The scanned symnol");
         outtextxy(60,278,"is an operand, so it");
         outtextxy(60,293,"will be pushed onto ");
         outtextxy(60,308,"the stack.");
      }

       elseif(flag==1)
      {
         outtextxy(60,263,"    The scanned symnol is an");
         outtextxy(60,278,"operand, so it is added to the");
         outtextxy(60,293,"Postfix Expression 'P'. ");
      }

       elseif(flag==2)
      {
         outtextxy(60,263,"    The scanned symbol is the");
         outtextxy(60,278,"left parenthesis '(', so it is");
         outtextxy(60,293,"pushed onto the Stack straight");
         outtextxy(60,308,"away.");
      }

       elseif(flag==3)
      {
         outtextxy(60,262,"    The scanned symbol is an");
         outtextxy(60,277,"right parenthesis ')', so the");
         outtextxy(60,292,"operators from the top of the");
         outtextxy(60,307,"Stack are Poped & added to the");
         outtextxy(60,322,"Postfix Expression 'P' until the");
         outtextxy(60,337,"left parenthesis '(' occured.");
         outtextxy(60,352,"   The left parenthesis '(' is");
         outtextxy(60,367,"also Poped from the Stack.");
      }

       elseif(flag==4)
      {
         outtextxy(60,262,"      The scanned symbol is an");
         outtextxy(60,275,"operator, so the operator on the");
         outtextxy(60,288,"top of the Stack are Poped and");
         outtextxy(60,301,"added to the Postfix Expression");
         outtextxy(60,314,"'P' until the operator on the top");
         outtextxy(60,327,"of the Stack has more or equal");
         outtextxy(60,340,"precedence then the scanned");
         outtextxy(60,353,"operator (if any).");
         outtextxy(60,366,"    Finally, the scanned operator");
         outtextxy(60,379,"is Pushed onto the Stack.");
      }

       elseif(flag==5)
      {
         outtextxy(60,262,"    The scanned symnol is an");
         outtextxy(60,277,"operand, so it is Pushed onto");
         outtextxy(60,292,"the Stack. ");
      }

       elseif(flag==6)
      {
         outtextxy(60,262,"    The scanned symbol is an");
         outtextxy(60,277,"operator, so two values from the");
         outtextxy(60,292,"top of the Stack are Poped and");
         outtextxy(60,307,"their result, attained by mani-");
         outtextxy(60,322,"pluating the Poped values & the");
         outtextxy(60,337,"scanned operator, is Pushed onto");
         outtextxy(60,352,"the Stack.");
      }

       elseif(flag==7)
      {
         outtextxy(60,262,"    The scanned symnol is the");
         outtextxy(60,277,"equal sign, so that is end of");
         outtextxy(60,292,"the evaluation of the Expression");
         outtextxy(60,307,"'P' and the value on the top of ");
         outtextxy(60,322,"Stack is our Result.");
      }
       elseif(flag==8)
      {
         outtextxy(60,262,"     This is the end of the");
         outtextxy(60,277,"convertion of Infix Expression");
         outtextxy(60,292,"into the Postfix Expression.");
      }


       setcolor(9);
       setfillstyle(1,9);
     bar(187,408,220,426);

     pieslice(220,417,0,360,9);

       settextstyle(2,0,5);
     setcolor(0);
       outtextxy(189,409,"Next>");
       outtextxy(190,409,"Next>");

     setcolor(14);
       outtextxy(191,408,"Next>");
       outtextxy(192,408,"Next>");
    }

 /*************************************************************************///------------------------  show_postfix_screen( )  ---------------------///*************************************************************************/void Evaluator::show_postfix_screen( )
    {
       setfillstyle(1,6);
     bar(75,25,530,45);

       settextstyle(2,0,8);
     setcolor(0);
       outtextxy(76,21,"Evaluation of Postfix Expression");
       outtextxy(77,21,"Evaluation of Postfix Expression");

     setcolor(3);
       outtextxy(78,20,"Evaluation of Postfix Expression");
       outtextxy(79,20,"Evaluation of Postfix Expression");
       outtextxy(80,20,"Evaluation of Postfix Expression");

       setcolor(15);
     rectangle(18,158,287,197);

       setcolor(7);
     rectangle(20,160,285,195);
     rectangle(19,159,286,196);

       setfillstyle(1,0);
     bar(21,161,284,194);

       setcolor(8);
     rectangle(21,161,284,194);

       setcolor(10);
       settextstyle(2,0,7);
     outtextxy(20,135,"Postfix Expression :");
     outtextxy(21,135,"Postfix Expression :");

       int color_array[7]={3,6,5,10,11,14,12};
       int x=30;

       for(int count_1=0;count_1<=postfix_rows;count_1++)
      {
         settextstyle(2,0,6);
         setcolor(color_array[count_1%7]);

           moveto(x-1,167);
         outtext(postfix_expression[count_1]);

           moveto(x,167);
         outtext(postfix_expression[count_1]);

         x=getx( );
     }

       setfillstyle(1,0);
     bar(295,88,620,460);

       setcolor(15);
     rectangle(295,88,620,460);

       setcolor(7);
     rectangle(296,89,619,459);
     rectangle(297,90,618,458);
     rectangle(298,91,617,457);

       setcolor(8);
     rectangle(299,92,616,456);

       setfillstyle(1,9);
     bar(300,92,615,119);

       setcolor(15);
     line(300,120,615,120);

       setcolor(7);
     line(300,121,615,121);
     line(300,122,615,122);

       setcolor(8);
     line(300,123,615,123);

       setcolor(15);
     line(387,92,387,456);

       setcolor(7);
     line(388,92,388,456);
     line(389,92,389,456);

       setcolor(8);
     line(390,92,390,456);

       settextstyle(2,0,5);
     setcolor(0);
       outtextxy(303,92,"Symbol");
       outtextxy(318,105,"Scanned");

       outtextxy(304,92,"Symbol");
       outtextxy(319,105,"Scanned");

     setcolor(14);
       outtextxy(305,91,"Symbol");
       outtextxy(320,104,"Scanned");

       outtextxy(306,91,"Symbol");
       outtextxy(321,104,"Scanned");

       settextstyle(2,0,7);
     setcolor(0);
       outtextxy(473,96,"STACK");
       outtextxy(474,96,"STACK");

     setcolor(14);
       outtextxy(475,95,"STACK");
       outtextxy(476,95,"STACK");
       outtextxy(477,95,"STACK");
    }

 /*************************************************************************///------------  show_evaluation_of_postfix_expression( )  ---------------///*************************************************************************/void Evaluator::show_evaluation_of_postfix_expression( )
    {
       show_postfix_screen( );

       int count_1=0;
       int explain_yes_not=0;

       char symbol_scanned[6]={'\0'};

       strcat(postfix_expression[postfix_rows],"=");

       do
      {
         for(int count_2=0;count_2<6;count_2++)
        symbol_scanned[count_2]='\0';

         strcpy(symbol_scanned,postfix_expression[count_1]);

         count_1++;

         char step_number[5]={'\0'};

         itoa(count_1,step_number,10);
         strcat(step_number,".");

         setcolor(15);
         settextstyle(0,0,1);
        outtextxy(305,120+(15*count_1),step_number);

         setcolor(14);
        outtextxy(335,120+(15*count_1),symbol_scanned);

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

           if(explain_yes_not==0)
              show_explanation(6);
        }

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

           if(explain_yes_not==0)
              show_explanation(5);
        }

         show_int_stack(count_1);

         char key='\0';

         int key_code=0;

         if(explain_yes_not==0)
        {
           key=getch( );
           key_code=int(key);

           if(key_code==27)
              explain_yes_not=1;
        }

         else
        delay(200);

      }
       while(symbol_scanned[0]!='=');

       show_explanation(7);

       while(int(getch( ))!=13);
     }

 /*************************************************************************///-------------------------  show_char_stack(int)  ----------------------///*************************************************************************/void Evaluator::show_char_stack(int element_number)
    {
       char stack[25]={'\0'};

       for(int count=0;count<=char_top;count++)
      {
         char temp_stack[2]={'\0'};

         temp_stack[0]=char_stack[count];

         strcat(stack,temp_stack);
      }

       setcolor(11);

       if(infix_rows>16 || input_characters_count>20)
      {
         settextstyle(2,0,4);
           outtextxy(375,130+(12*element_number),stack);
      }

       else
      {
         settextstyle(0,0,1);
           outtextxy(375,130+(14*element_number),stack);
      }
    }

 /*************************************************************************///--------------------------  show_int_stack(int)  ----------------------///*************************************************************************/void Evaluator::show_int_stack(int element_number)
    {
       char stack[10][10]={'\0'};

       for(int count_1=0;count_1<10;count_1++)
      ltoa(int_stack[count_1],stack[count_1],10);

       int color_array[7]={3,6,5,10,11,14,12};
       int x_2=400;
       int y_2=120+(element_number*15);

       for(int count_2=0;count_2<=int_top;count_2++)
      {
         settextstyle(0,0,1);
         setcolor(color_array[count_2%7]);

           moveto(x_2,y_2);
         outtext(stack[count_2]);

         x_2=getx( );
      }
    }

 /*************************************************************************///--------------------------  show_summary( )  --------------------------///*************************************************************************/void Evaluator::show_summary( )
    {
       cleardevice( );

       setfillstyle(6,1);
       bar(5,5,getmaxx( )-5,getmaxy( )-5);

    for(int count_1=0;count_1<15;count_1++)
       {
          setcolor(count_1);
          rectangle(5+count_1,5+count_1,getmaxx( )-5-count_1,
                               getmaxy( )-5-count_1);
       }

       setfillstyle(1,14);
     bar(180,75,450,78);

       settextstyle(1,0,6);
     setcolor(0);
       outtextxy(138,20,"* Summary *");
       outtextxy(139,20,"* Summary *");

     setcolor(12);
       outtextxy(140,20,"* Summary *");
       outtextxy(141,20,"* Summary *");
       outtextxy(142,20,"* Summary *");

       settextstyle(1,0,4);
     setcolor(0);
       outtextxy(98,106,"Infix Expression :");
       outtextxy(99,106,"Infix Expression :");

     setcolor(10);
       outtextxy(100,105,"Infix Expression :");
       outtextxy(101,105,"Infix Expression :");

       setcolor(15);
     rectangle(99,145,501,200);
     rectangle(100,146,500,199);

       setcolor(7);
     rectangle(101,147,499,198);
     rectangle(102,148,498,197);

       setcolor(8);
     rectangle(103,149,497,196);
     rectangle(104,150,496,195);

       setfillstyle(1,0);
     bar(105,151,495,194);

       settextstyle(1,0,4);
     setcolor(0);
       outtextxy(98,220,"Postfix Expression :");
       outtextxy(99,220,"Postfix Expression :");

     setcolor(10);
       outtextxy(100,219,"Postfix Expression :");
       outtextxy(101,219,"Postfix Expression :");

       setcolor(15);
     rectangle(99,259,501,316);
     rectangle(100,260,500,315);

       setcolor(7);
     rectangle(101,261,499,314);
     rectangle(102,262,498,313);

       setcolor(8);
     rectangle(103,263,497,312);
     rectangle(104,264,496,311);

       setfillstyle(1,0);
     bar(105,265,495,310);

       settextstyle(1,0,5);
     setcolor(0);
       outtextxy(98,323,"Result :");
       outtextxy(99,323,"Result :");

     setcolor(10);
       outtextxy(100,322,"Result :");
       outtextxy(101,322,"Result :");

       setcolor(15);
     rectangle(99,369,501,431);
     rectangle(100,370,500,430);

       setcolor(7);
     rectangle(101,371,499,429);
     rectangle(102,372,498,428);

       setcolor(8);
     rectangle(103,373,497,427);
     rectangle(104,374,496,426);

       setfillstyle(1,0);
     bar(105,375,495,425);

       setcolor(11);
       settextstyle(2,0,8);

       int x_1=120;
       int y_1=157;

       for(int count_2=0;count_2<infix_rows;count_2++)
     {
        moveto(x_1-1,y_1);
          outtext(infix_expression[count_2]);

        moveto(x_1,y_1);
          outtext(infix_expression[count_2]);

        x_1=getx( );
     }

       int color_array[7]={3,6,5,10,11,14,12};
       int x_2=120;
       int y_2=270;

       for(int count_3=0;count_3<postfix_rows;count_3++)
     {
         settextstyle(2,0,8);
         setcolor(color_array[count_3%7]);

           moveto(x_2-1,y_2);
         outtext(postfix_expression[count_3]);

           moveto(x_2,y_2);
         outtext(postfix_expression[count_3]);

         x_2=getx( );
     }

       char result[10]={'\0'};

       ltoa(int_stack[0],result,10);

       setcolor(11);
       settextstyle(1,0,5);
     outtextxy(120,373,result);
     outtextxy(121,373,result);

       while(!getch( ));

       for(int count_4=0;count_4<320;count_4++)
      {
         setcolor(0);
           rectangle(getmaxx( )/2-count_4,getmaxy( )/2-count_4,
                  getmaxx( )/2+count_4,getmaxy( )/2+count_4);

         delay(1);
      }
    }

 int main( )
    {
       int driver=VGA;
       int mode=VGAHI;

       initgraph(&driver,&mode,"..\\Bgi");

       Evaluator obj;

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

       closegraph( );

       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:


 

Other Interesting Articles in C++ Programming:


 
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!