Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » ParsingRSS Feeds

Program to implement a Translator that reads an Infix Expression translates it into a Postfix Expression and evaluate the Postfix Expression

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

A C++ Program to implement a Translator that reads an Infix Expression, checks its Syntax (using Bottom-Up Parsing), translates the Infix Expression into a Postfix Expression and evaluate the Postfix Expression.

Code for Program to implement a Translator that reads an Infix Expression translates it into a Postfix Expression and evaluate the Postfix Expression in C++ Programming

 # include <iostream.h>
 # include <graphics.h>
 # include   <string.h>
 # include   <stdlib.h>
 # include    <ctype.h>
 # include    <conio.h>
 # include     <math.h>
 # include      <dos.h>


 staticchar Stack[50][10]={NULL};
 staticint top=-1;
 staticint cit=0;
 staticint input_length=-1;
 staticint output_length=0;

 staticint productions[6]={5,1,7,7,2,10};

 constchar Grammar[5][11][10]={
               {"S","E"},
               {"E","E+T","E-T","E*T","E/T","E%T","E^T","T"},
               {"T","T+F","T-F","T*F","T/F","T%F","T^F","F"},
               {"F","(E)","D"},
               {"D","0","1","2","3","4","5","6","7","8","9"}
             };

 char Input[25][10]={NULL};
 char InfixExpression[30]={NULL};
 char PostfixExpression[25][10]={NULL};


 /*************************************************************************///-------------------------  ShowMainScreen( )  -------------------------///*************************************************************************/void ShowMainScreen( )
 {
    cleardevice( );

    setfillstyle(2,1);
      bar(0,0,getmaxx( ),getmaxy( ));

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

    for(i=0;i<3;i++)
    {
       delay(400);

       setcolor(1);
     settextstyle(4,0,10);
       outtextxy((20-i),(50-i),"+");
       outtextxy((420-i),20,"^");
       outtextxy((100-i),150,"*");
       outtextxy((550-i),(100-i),"-");
       outtextxy((320-i),20,"/");
       outtextxy((250-i),160,"(");
       outtextxy((500-i),250,")");

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

    delay(400);

    char expression[10][5]={"T","r","a","n","s","l","a","t","o","r"};

    int x=80;
    int y=50;

    for(i=0;i<10;i++)
    {
       settextstyle(4,0,9);
       setcolor(0);

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

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

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

       setcolor(13);

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

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

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

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

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

    delay(500);

    settextstyle(4,0,4);
      setcolor(0);
    outtextxy(160,142,"with Bottom-Up Parser");
    outtextxy(161,141,"with Bottom-Up Parser");

      setcolor(5);
    outtextxy(162,140,"with Bottom-Up Parser");
    outtextxy(163,140,"with Bottom-Up Parser");

    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,"Continue");
    outtextxy(509,422,"Continue");

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

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

    i=0;

    int key_code=0;

    do
    {
       delay(500);

       key_code=0;

       if(kbhit( ))
      key_code=(int)getch( );

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

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

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

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

       i++;
    }
    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,"Continue");
       outtextxy(509,422,"Continue");

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

       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,"Continue");
       outtextxy(509,422,"Continue");

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

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

 /*************************************************************************///-------------------------  ShowInputScreen( )  ------------------------///*************************************************************************/void ShowInputScreen( )
 {
    setfillstyle(2,1);
      bar(15,15,(getmaxx( )-15),(getmaxy( )-15));

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

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

    settextstyle(4,0,9);
      setcolor(0);
    outtextxy(107,42,"Translator");
    outtextxy(108,42,"Translator");
    outtextxy(109,42,"Translator");

      setcolor(13);
    outtextxy(110,40,"Translator");
    outtextxy(111,40,"Translator");
    outtextxy(112,40,"Translator");
    outtextxy(113,40,"Translator");

    settextstyle(4,0,4);
      setcolor(0);
    outtextxy(145,135,"with Bottom-Up Parser");
    outtextxy(146,134,"with Bottom-Up Parser");

      setcolor(5);
    outtextxy(147,133,"with Bottom-Up Parser");
    outtextxy(148,133,"with Bottom-Up Parser");
    outtextxy(149,133,"with Bottom-Up Parser");

    settextstyle(2,0,7);
      setcolor(8);
    outtextxy(29,282,"Enter the Infix Expression :");
    outtextxy(30,281,"Enter the Infix Expression :");

      setcolor(11);
    outtextxy(30,280,"Enter the Infix Expression :");
    outtextxy(31,280,"Enter the Infix Expression :");

    setcolor(15);
      rectangle(29,305,608,375);
      rectangle(30,306,607,374);

    setcolor(7);
      rectangle(31,307,606,373);
      rectangle(32,308,605,372);
      rectangle(33,309,604,371);

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

    setfillstyle(1,0);
      bar(35,311,602,369);
 }

 /*************************************************************************///----------------------   ReadInfixExpression( )  ----------------------///*************************************************************************/void ReadInfixExpression( )
 {
    ShowInputScreen( );

    int x_1=36;
    int x_2=601;
    int y_1=312;
    int y_2=368;

    setfillstyle(1,0);
      bar(x_1,y_1,x_2,y_2);

    int count=0;
    int key_code=0;

    char Key=NULL;

    do
    {
       if(kbhit( ))
       {
      Key=NULL;
      key_code=0;

      Key=getch( );
      key_code=int(Key);

      if(key_code==13 || key_code==27)
         break;

      if(count<25 && key_code!=8)
      {
         InfixExpression[count]=Key;

         count++;
      }

      elseif(key_code==8 && count>0)
      {
         setfillstyle(1,0);
           bar(x_1,y_1,x_2,y_2);

         count--;

         InfixExpression[count]=NULL;
      }
       }

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

       moveto(45,320);
     outtext(InfixExpression);

       int xx=(getx( )-2);
       int yy=gety( );

       while(!kbhit( ))
       {
      setfillstyle(1,15);
        bar(xx,yy,(xx+3),(yy+40));

      delay(200);

      setfillstyle(1,0);
        bar(xx,yy,(xx+3),(yy+40));

      delay(200);
       }
    }
    while(1);
 }

 /*************************************************************************///------------------  ConvertInfixExpressionToInput( )  -----------------///*************************************************************************/void ConvertInfixExpressionToInput( )
 {
    int type=0;
    int count=0;
    int last_type=0;
    int length=strlen(InfixExpression);

    char Temp[10]={NULL};

    do
    {
       Temp[0]=InfixExpression[count];

       if(isdigit((int)Temp[0]))
      type=1;

       else
      type++;

       if(type!=last_type)
       {
      input_length++;

      strcpy(Input[input_length],Temp);
       }

       else
      strcat(Input[input_length],Temp);

       count++;
       last_type=type;
    }
    while(count<=length);

    strcpy(Input[input_length],"$");

    input_length++;
 }

 /*************************************************************************///------------------------------  Push( )  ------------------------------///*************************************************************************/void Push(constchar* Token)
 {
    top++;

    strcpy(Stack[top],Token);
 }

 /*************************************************************************///-------------------------------  Pop( )  ------------------------------///*************************************************************************/constchar* Pop( )
 {
    char Item[15]={NULL};

    strcpy(Item,Stack[top]);
    strset(Stack[top],NULL);

    top--;

    return Item;
 }

 /*************************************************************************///-----------------------------  Reduce( )  -----------------------------///*************************************************************************/void Reduce(constint items,constint index)
 {
    for(int i=0;i<items;i++)
       Pop( );

    Push(Grammar[index][0]);
 }

 /*************************************************************************///------------------------------  Shift( )  -----------------------------///*************************************************************************/void Shift( )
 {
    Push(Input[cit]);

    cit++;
 }

 /*************************************************************************///-----------------------  CheckReduceCondition( )  ---------------------///*************************************************************************/constint CheckReduceCondition( )
 {
    int items;
    int index;

    char TopItems[100]={NULL};

    for(int i=0;i<=top;i++)
    {
       strset(TopItems,NULL);

       for(int j=i;j<=top;j++)
      strcat(TopItems,Stack[j]);

       for(j=0;j<productions[0];j++)
       {
      for(int k=1;k<=productions[(j+1)];k++)
      {
         if(strcmp(TopItems,Grammar[j][k])==0)
         {
        items=(top-i+1);
        index=j;

        goto NextCheck;
         }
      }
       }
    }

    return 0;

    NextCheck:

    char CitInput[20]={NULL};

    strcpy(CitInput,Stack[top]);
    strcat(CitInput,Input[cit]);

    for(i=0;i<productions[0];i++)
    {
       for(int j=1;j<=productions[(i+1)];j++)
       {
      if(strstr(Grammar[i][j],CitInput)!=NULL)
         return 0;
       }
    }

    Reduce(items,index);

    return 1;
 }

 /*************************************************************************///---------------------------  CheckSyntax( )  --------------------------///*************************************************************************/void CheckSyntax( )
 {
    delay(1000);

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

    setfillstyle(1,9);
      bar(10,52,295,55);

    settextstyle(2,0,9);
      setcolor(0);
    outtextxy(17,22,"Syntax Checking");
    outtextxy(18,22,"Syntax Checking");
    outtextxy(19,22,"Syntax Checking");

      setcolor(13);
    outtextxy(20,20,"Syntax Checking");
    outtextxy(21,20,"Syntax Checking");
    outtextxy(22,20,"Syntax Checking");

      setcolor(9);
    outtextxy(20,20,"               :");
    outtextxy(21,20,"               :");
    outtextxy(22,20,"               :");

    setfillstyle(1,1);
      bar(30,75,610,435);

    setlinestyle(0,0,3);

    setcolor(9);
      rectangle(30,74,610,435);

      line(30,104,610,104);
      line(270,75,270,435);
      line(510,75,510,435);

    setlinestyle(0,0,0);

    for(int count=0;count<21;count++)
      line(30,(119+(count*15)),610,(119+(count*15)));

    settextstyle(2,0,7);
      setcolor(0);
    outtextxy(48,79,"Stack");
    outtextxy(49,78,"Stack");

    outtextxy(288,79,"Input");
    outtextxy(289,78,"Input");

      setcolor(5);
    outtextxy(49,77,"Stack");
    outtextxy(50,77,"Stack");

    outtextxy(289,77,"Input");
    outtextxy(290,77,"Input");

    settextstyle(2,0,5);
      setcolor(0);
    outtextxy(518,82,"Next Action");
    outtextxy(519,81,"Next Action");

      setcolor(5);
    outtextxy(519,80,"Next Action");
    outtextxy(520,80,"Next Action");

    int flag=0;

    setcolor(11);
    settextstyle(0,0,1);

    moveto(40,109);

    for(int i=0;i<=top;i++)
    {
       moveto(getx( ),gety( ));
     outtext(Stack[i]);
    }

    moveto(280,109);

    for(int j=cit;j<input_length;j++)
    {
       moveto(getx( ),gety( ));
     outtext(Input[j]);
    }

    outtextxy(520,109,"Shift");

    count=1;

    do
    {
       if(!CheckReduceCondition( ))
       {
      Shift( );

      outtextxy(520,(109+(count*15)),"Shift");
       }

       else
      outtextxy(520,(109+(count*15)),"Reduce");

       moveto(40,(109+(count*15)));

       for(int i=0;i<=top;i++)
       {
      moveto(getx( ),gety( ));
        outtext(Stack[i]);
       }

       moveto(280,(109+(count*15)));

       for(int j=cit;j<input_length;j++)
       {
      moveto(getx( ),gety( ));
        outtext(Input[j]);
       }

       if(top==0 && strcmp(Stack[top],Grammar[0][0])==0 &&
                           strcmp(Input[cit],"$")==0)
       {
      flag=1;

      break;
       }

       elseif(strcmp(Stack[top],"$")==0)
       {
      flag=0;

      break;
       }

       count++;

       if(count==22)
       {
      settextstyle(0,0,1);

      do
      {
         setcolor(14);
           outtextxy(480,450,"Press ANY key...");

         delay(300);

         setcolor(1);
           outtextxy(480,450,"Press ANY key...");

         delay(300);
      }
      while(!kbhit( ));

      while(kbhit( ))
         getch( );

      setfillstyle(2,1);
        bar(470,440,610,465);

      setfillstyle(1,1);
        bar(30,100,610,435);

      setlinestyle(0,0,3);

      setcolor(9);
        rectangle(30,74,610,435);

      line(30,104,610,104);
      line(270,75,270,435);
      line(510,75,510,435);

      setlinestyle(0,0,0);

      for(count=0;count<21;count++)
         line(30,(119+(count*15)),610,(119+(count*15)));

      count=0;

      setcolor(11);
      settextstyle(0,0,1);
       }
    }
    while(1);

    setfillstyle(1,9);
      bar(10,460,85,462);

    settextstyle(2,0,6);
      setcolor(0);
    outtextxy(19,443,"Result");
    outtextxy(20,443,"Result");

      setcolor(13);
    outtextxy(20,442,"Result");
    outtextxy(21,442,"Result");

      setcolor(9);
    outtextxy(20,442,"      :");
    outtextxy(21,442,"      :");

    if(!flag)
    {
       settextstyle(2,0,6);
     setcolor(0);
       outtextxy(94,447,"Incorrect");
       outtextxy(95,446,"Incorrect");

     setcolor(15);
       outtextxy(95,445,"Incorrect");
       outtextxy(96,445,"Incorrect");

       settextstyle(0,0,1);

       do
       {
      setcolor(14);
        outtextxy(435,450,"Press ANY key to Exit.");

      delay(300);

      setcolor(1);
        outtextxy(435,450,"Press ANY key to Exit.");

      delay(300);
       }
       while(!kbhit( ));

       while(kbhit( ))
     getch( );

       setfillstyle(2,1);
     bar(430,440,610,465);
    }

    else
    {
       settextstyle(2,0,6);
     setcolor(0);
       outtextxy(94,447,"Correct");
       outtextxy(95,446,"Correct");

     setcolor(15);
       outtextxy(95,445,"Correct");
       outtextxy(96,445,"Correct");

       settextstyle(0,0,1);

       do
       {
      setcolor(14);
        outtextxy(385,450,"Press ANY key to continue...");

      delay(300);

      setcolor(1);
        outtextxy(385,450,"Press ANY key to continue...");

      delay(300);
       }
       while(!kbhit( ));

       while(kbhit( ))
     getch( );

       setfillstyle(2,1);
     bar(370,440,610,465);
    }
 }

 /*************************************************************************///---------------  ShowInfixToPostfixConversionScreen( )  ---------------///*************************************************************************/void ShowInfixToPostfixConversionScreen( )
 {
    delay(1000);

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

    setfillstyle(1,9);
      bar(10,52,440,55);

    settextstyle(2,0,9);
      setcolor(0);
    outtextxy(17,22,"Infix-Postfix Conversion");
    outtextxy(18,22,"Infix-Postfix Conversion");
    outtextxy(19,22,"Infix-Postfix Conversion");

      setcolor(13);
    outtextxy(20,20,"Infix-Postfix Conversion");
    outtextxy(21,20,"Infix-Postfix Conversion");
    outtextxy(22,20,"Infix-Postfix Conversion");

      setcolor(9);
    outtextxy(20,20,"                       :");
    outtextxy(21,20,"                       :");
    outtextxy(22,20,"                       :");

    setfillstyle(1,9);
      bar(10,87,73,89);

    settextstyle(2,0,6);
      setcolor(0);
    outtextxy(19,70,"Input");
    outtextxy(20,70,"Input");

      setcolor(13);
    outtextxy(20,69,"Input");
    outtextxy(21,69,"Input");

      setcolor(9);
    outtextxy(20,69,"     :");
    outtextxy(21,69,"     :");

    setcolor(12);
    settextstyle(2,0,6);

    moveto(90,73);

    int x;
    int y;

    for(int count=0;count<(input_length-1);count++)
    {
       x=getx( );
       y=(gety( )-1);

       moveto(x,y);
     outtext(Input[count]);

       moveto((x+1),(y+1));
     outtext(Input[count]);
    }

    setcolor(15);

    moveto(91,72);

    for(count=0;count<(input_length-1);count++)
    {
       x=getx( );
       y=gety( );

       moveto(x,y);
     outtext(Input[count]);

       moveto((x+1),y);
     outtext(Input[count]);
    }

    setfillstyle(1,1);
      bar(30,105,610,435);

    setlinestyle(0,0,3);

    setcolor(9);
      rectangle(30,104,610,435);

      line(30,134,610,134);
      line(97,105,97,435);
      line(300,105,300,435);

    setlinestyle(0,0,0);

    for(count=0;count<19;count++)
      line(30,(149+(count*15)),610,(149+(count*15)));

    settextstyle(2,0,7);
      setcolor(0);
    outtextxy(108,109,"Stack");
    outtextxy(109,108,"Stack");

    outtextxy(313,109,"Postfix Expression");
    outtextxy(314,108,"Postfix Expression");

      setcolor(5);
    outtextxy(109,107,"Stack");
    outtextxy(110,107,"Stack");

    outtextxy(314,107,"Postfix Expression");
    outtextxy(315,107,"Postfix Expression");

    settextstyle(2,0,5);
      setcolor(0);
    outtextxy(41,107,"Symbol");
    outtextxy(42,106,"Symbol");

    outtextxy(35,119,"Scanned");
    outtextxy(36,118,"Scanned");

      setcolor(5);
    outtextxy(42,105,"Symbol");
    outtextxy(43,105,"Symbol");

    outtextxy(36,117,"Scanned");
    outtextxy(37,117,"Scanned");
 }

 /*************************************************************************///-------------------  InfixToPostfixConversion( )  ---------------------///*************************************************************************/void InfixToPostfixConversion( )
 {
    ShowInfixToPostfixConversionScreen( );

    for(top=0;top<50;top++)
       strset(Stack[top],NULL);

    top=-1;
    input_length--;

    Push("(");
    strcpy(Input[input_length],")");

    int i=0;
    int j=0;
    int k=0;

    int colors[7]={3,6,7,10,11,14,12};

    char Symbol_scanned[10]={NULL};

    do
    {
       strset(Symbol_scanned,NULL);
       strcpy(Symbol_scanned,Input[i]);

       if(strcmp(Symbol_scanned,"(")==0)
      Push(Symbol_scanned);

       elseif(strcmp(Symbol_scanned,")")==0)
       {
      while(strcmp(Stack[top],"(")!=0)
      {
         strcpy(PostfixExpression[j],Pop( ));

         j++;
      }

      Pop( );
       }

       elseif(strcmp(Symbol_scanned,"/")==0 ||
              strcmp(Symbol_scanned,"*")==0 ||
                 strcmp(Symbol_scanned,"-")==0 ||
                     strcmp(Symbol_scanned,"+")==0 ||
                          strcmp(Symbol_scanned,"^")==0)
       {
      if(strcmp(Symbol_scanned,"^")==0)
      {
      }

      elseif(strcmp(Symbol_scanned,"*")==0 ||
                           strcmp(Symbol_scanned,"/")==0)
      {
         while(strcmp(Stack[top],"^")==0 ||
                    strcmp(Stack[top],"*")==0 ||
                           strcmp(Stack[top],"/")==0)
         {
        strcpy(PostfixExpression[j],Pop( ));

        j++;
         }
      }

      elseif(strcmp(Symbol_scanned,"+")==0 ||
                           strcmp(Symbol_scanned,"-")==0)
      {
         while(strcmp(Stack[top],"(")!=0)
         {
        strcpy(PostfixExpression[j],Pop( ));

        j++;
         }
      }

      Push(Symbol_scanned);
       }

       elseif(strcmp(Symbol_scanned,"/")!=0 ||
            strcmp(Symbol_scanned,"*")!=0 ||
              strcmp(Symbol_scanned,"-")!=0 ||
                   strcmp(Symbol_scanned,"+")!=0 ||
                   strcmp(Symbol_scanned,"(")!=0 ||
                       strcmp(Symbol_scanned,")")!=0 ||
                          strcmp(Symbol_scanned,"^")!=0)
       {
      strcpy(PostfixExpression[j],Symbol_scanned);

      j++;
       }

       settextstyle(0,0,1);
     outtextxy(50,(139+(k*15)),Symbol_scanned);

       int x=110;
       int y=(139+(k*15));

       for(int count=0;count<=top;count++)
       {
      setcolor(colors[count%7]);

      moveto(x,y);
        outtext(Stack[count]);

      x=getx( );
       }

       x=310;
       y=(139+(k*15));

       for(count=0;count<j;count++)
       {
      setcolor(colors[count%7]);

      moveto(x,y);
        outtext(PostfixExpression[count]);

      x=getx( );
       }

       i++;
       k++;

       if(k==20)
       {
      settextstyle(0,0,1);

      do
      {
         setcolor(14);
           outtextxy(480,450,"Press ANY key...");

         delay(300);

         setcolor(1);
           outtextxy(480,450,"Press ANY key...");

         delay(300);
      }
      while(!kbhit( ));

      while(kbhit( ))
         getch( );

      setfillstyle(2,1);
        bar(470,440,610,465);

      setfillstyle(1,1);
        bar(30,130,610,435);

      setlinestyle(0,0,3);

      setcolor(9);
        rectangle(30,104,610,435);

      line(30,134,610,134);
      line(97,105,97,435);
      line(300,105,300,435);

      setlinestyle(0,0,0);

      for(k=0;k<19;k++)
         line(30,(149+(k*15)),610,(149+(k*15)));

      k=0;
       }
    }
    while(top!=-1);

    output_length=j;

    do
    {
       setcolor(14);
     outtextxy(385,450,"Press ANY key to continue...");

       delay(300);

       setcolor(1);
     outtextxy(385,450,"Press ANY key to continue...");

       delay(300);
    }
    while(!kbhit( ));

    while(kbhit( ))
      getch( );

    setfillstyle(2,1);
      bar(370,440,610,465);
 }

 /*************************************************************************///------------  ShowEvaluationOfPostfixExpressionScreen( )  -------------///*************************************************************************/void ShowEvaluationOfPostfixExpressionScreen( )
 {
    delay(1000);

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

    setfillstyle(1,9);
      bar(10,52,590,55);

    settextstyle(2,0,9);
      setcolor(0);
    outtextxy(17,22,"Evaluation of Postfix Expression");
    outtextxy(18,22,"Evaluation of Postfix Expression");
    outtextxy(19,22,"Evaluation of Postfix Expression");

      setcolor(13);
    outtextxy(20,20,"Evaluation of Postfix Expression");
    outtextxy(21,20,"Evaluation of Postfix Expression");
    outtextxy(22,20,"Evaluation of Postfix Expression");

      setcolor(9);
    outtextxy(20,20,"                               :");
    outtextxy(21,20,"                               :");
    outtextxy(22,20,"                               :");

    setfillstyle(1,9);
      bar(10,87,73,89);

    settextstyle(2,0,6);
      setcolor(0);
    outtextxy(19,70,"Input");
    outtextxy(20,70,"Input");

      setcolor(13);
    outtextxy(20,69,"Input");
    outtextxy(21,69,"Input");

      setcolor(9);
    outtextxy(20,69,"     :");
    outtextxy(21,69,"     :");

    setcolor(12);
    settextstyle(2,0,6);

    moveto(90,73);

    int x;
    int y;

    for(int count=0;count<output_length;count++)
    {
       x=getx( );
       y=(gety( )-1);

       moveto(x,y);
     outtext(PostfixExpression[count]);

       moveto((x+1),(y+1));
     outtext(PostfixExpression[count]);
    }

    setcolor(15);

    moveto(91,72);

    for(count=0;count<output_length;count++)
    {
       x=getx( );
       y=gety( );

       moveto(x,y);
     outtext(PostfixExpression[count]);

       moveto((x+1),y);
     outtext(PostfixExpression[count]);
    }

    setfillstyle(1,1);
      bar(30,105,610,435);

    setlinestyle(0,0,3);

    setcolor(9);
      rectangle(30,104,610,435);

      line(30,134,610,134);
      line(189,105,189,435);

    setlinestyle(0,0,0);

    for(count=0;count<19;count++)
      line(30,(149+(count*15)),610,(149+(count*15)));

    settextstyle(2,0,7);
      setcolor(0);
    outtextxy(203,109,"Stack");
    outtextxy(204,108,"Stack");

      setcolor(5);
    outtextxy(204,107,"Stack");
    outtextxy(205,107,"Stack");

    settextstyle(2,0,6);
      setcolor(0);
    outtextxy(41,111,"Symbol Scanned");
    outtextxy(42,110,"Symbol Scanned");

      setcolor(5);
    outtextxy(42,109,"Symbol Scanned");
    outtextxy(43,108,"Symbol Scanned");
 }

 /*************************************************************************///-------------------  InfixToPostfixConversion( )  ---------------------///*************************************************************************/void EvaluationOfPostfixExpression( )
 {
    ShowEvaluationOfPostfixExpressionScreen( );

    for(top=0;top<50;top++)
       strset(Stack[top],NULL);

    top=-1;

    int i=0;
    int j=0;

    int colors[7]={3,6,7,10,11,14,12};

    char Symbol_scanned[10]={NULL};

    strcat(PostfixExpression[output_length],"=");

    do
    {
       strset(Symbol_scanned,NULL);
       strcpy(Symbol_scanned,PostfixExpression[i]);

       if(strcmp(Symbol_scanned,"/")==0 ||
              strcmp(Symbol_scanned,"*")==0 ||
                 strcmp(Symbol_scanned,"-")==0 ||
                     strcmp(Symbol_scanned,"+")==0 ||
                          strcmp(Symbol_scanned,"^")==0)
       {
      long value_1=0;
      long value_2=0;
      long result=0;

      value_1=atol(Pop( ));
      value_2=atol(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;
      }

      char Temp[15]={NULL};

      ltoa(result,Temp,10);
      Push(Temp);
       }

       elseif(strcmp(Symbol_scanned,"/")!=0 &&
            strcmp(Symbol_scanned,"*")!=0 &&
               strcmp(Symbol_scanned,"-")!=0 &&
                   strcmp(Symbol_scanned,"+")!=0  &&
                     strcmp(Symbol_scanned,"^")!=0 &&
                          strcmp(Symbol_scanned,"=")!=0)

      Push(Symbol_scanned);

      settextstyle(0,0,1);
    setcolor(colors[j%7]);
      outtextxy(70,(139+(15*j)),Symbol_scanned);

       int x=200;
       int y=(139+(j*15));

       for(int k=0;k<=top;k++)
       {
      setcolor(colors[k%7]);

      moveto(x,y);
        outtext(Stack[k]);

      x=getx( );
       }

       i++;
       j++;

       if(j==20)
       {
      do
      {
         setcolor(14);
           outtextxy(480,450,"Press ANY key...");

         delay(300);

         setcolor(1);
           outtextxy(480,450,"Press ANY key...");

         delay(300);
      }
      while(!kbhit( ));

      while(kbhit( ))
         getch( );

      setfillstyle(2,1);
        bar(470,440,610,465);

      setfillstyle(1,1);
        bar(30,105,610,435);

      setlinestyle(0,0,3);

      setcolor(9);
        rectangle(30,104,610,435);

      line(30,134,610,134);
      line(189,105,189,435);

      setlinestyle(0,0,0);

      for(j=0;j<19;j++)
         line(30,(149+(j*15)),610,(149+(j*15)));

      j=0;
       }
    }
    while(strcmp(Symbol_scanned,"=")!=0);

    do
    {
       setcolor(14);
     outtextxy(385,450,"Press ANY key to continue...");

       delay(300);

       setcolor(1);
     outtextxy(385,450,"Press ANY key to continue...");

       delay(300);
    }
    while(!kbhit( ));

    while(kbhit( ))
      getch( );

    setfillstyle(2,1);
      bar(370,440,610,465);
 }

 /*************************************************************************///---------------------------  ShowSummary( )  --------------------------///*************************************************************************/void ShowSummary( )
 {
    for(int i=0;i<15;i++)
    {
       setcolor(i);
     rectangle(i,i,(getmaxx( )-i),(getmaxy( )-i));
    }

    setfillstyle(2,1);
      bar(15,15,(getmaxx( )-15),(getmaxy( )-15));

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

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

      setcolor(13);
    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(5);
    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(5);
    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(5);
    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=115;
    int y=157;

    for(i=0;i<input_length;i++)
    {
       moveto((x-1),y);
     outtext(Input[i]);

       moveto(x,y);
     outtext(Input[i]);

       x=getx( );
    }

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

    x=115;
    y=270;

    for(i=0;i<output_length;i++)
    {
       settextstyle(2,0,8);
       setcolor(color_array[(i%7)]);

       moveto((x-1),y);
     outtext(PostfixExpression[i]);

       moveto(x,y);
     outtext(PostfixExpression[i]);

       x=getx( );
    }

    setcolor(11);
    settextstyle(1,0,5);
      outtextxy(120,373,Stack[0]);
      outtextxy(121,373,Stack[0]);

    while(!kbhit( ));

    setcolor(0);
    setfillstyle(1,0);

    for(i=0;i<380;i+=8)
       pieslice(320,240,0,360,i);
 }

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

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

    ShowMainScreen( );
    ReadInfixExpression( );
    ConvertInfixExpressionToInput( );
    CheckSyntax( );
    InfixToPostfixConversion( );
    EvaluationOfPostfixExpression( );
    ShowSummary( );

    closegraph( );

    return 0;
 }
  
Share: 



Easy Tutor
Easy Tutor author of Program to implement a Translator that reads an Infix Expression translates it into a Postfix Expression and evaluate the Postfix Expression 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

 
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!