Program to convert infix expression to postfix / suffix expression using paranthesize

on Sunday, 19 January 2014
#include <iostream.h>
#include <stdlib.h>  //for exit()
#include <conio.h>
#include <dos.h>
#define row 7
#define col 5


class revpol{
      int top,rank;
      char *stack_arr,*polish,*infix;
      public:
      int f(char);  //Input precedence functionint g(char);  //Stack precedence functionint r(char);  //rank functionvoid push(char);
      char pop();
      char *concat(char *,char);
      void suffix();//infix to suffix conversion functionvoid printStack();
      void printSuffix();
      void printRank();
      void printInfix(char);
};

void revpol :: printInfix(char ch){
    staticint y=row,x=col;
    gotoxy(x,y++);
    cout<<ch;
}

void revpol :: printStack(){
     staticint x=col,y=row;
     gotoxy(x+10,y++);
     cout<<stack_arr;
}

void revpol :: printSuffix(){
     staticint x=col,y=row;
     gotoxy(x+30,y++);
     cout<<polish;
}

void revpol :: printRank(){
     staticint x=col,y=row;
     gotoxy(x+50,y++);
     cout<<rank;
}

int revpol :: f(char inputchar){
     if(inputchar==')')
      return(0);
     elseif(inputchar=='+' || inputchar=='-')
      return(1);
     elseif(inputchar=='*' || inputchar=='/')
      return(3);
     elseif(inputchar=='^')
      return(6);
     elseif(inputchar>='a' || inputchar<='z')
      return(7);
     elseif(inputchar>='A' || inputchar<='Z')
      return(7);
     elseif(inputchar=='(')
      return(9);
     elsereturn(-99);  //for error
}

int revpol :: g(char inputchar){
     if(inputchar=='(')
      return(0);
     elseif(inputchar=='+' || inputchar=='-')
      return(2);
     elseif(inputchar=='*' || inputchar=='/')
      return(4);
     elseif(inputchar=='^')
      return(5);
     elseif(inputchar>='a' || inputchar<='z')
      return(8);
     elseif(inputchar>='A' || inputchar<='Z')
      return(8);
     elsereturn(-99);  //for error
}


int revpol :: r(char inputchar){
     if(inputchar=='+' || inputchar=='-' || inputchar=='*' || inputchar=='/'|| inputchar=='^')
      return(-1);
     elseif(inputchar>='a' || inputchar<='z')
      return(1);
     elseif(inputchar>='A' || inputchar<='Z')
      return(1);
     elsereturn(-99);  //for error
}


void revpol :: push(char charin){
     top++;
     stack_arr[top]=charin;
     stack_arr[top+1]='\0';
}

char revpol :: pop(){
     char popdata;
     popdata=stack_arr[top];
     stack_arr[top]='\0';
     top--;
     return(popdata);
}

char * revpol :: concat(char *polish,char temp){
     int i;
     for(i=0;polish[i]!='\0';i++);
     polish[i]=temp;
     polish[i+1]='\0';
     return(polish);
}

void revpol :: suffix(){
     int i;
     char next,temp;

     //initialize the stack
     top=0;
     stack_arr[top]='(';

     //Intialize output string and rank count
     polish="";
     rank=0;

     textcolor(LIGHTGREEN);
     gotoxy(2,24);
     cprintf("E-Mail  :  find@myblog.com");
     gotoxy(2,25);
     cprintf("Website :  http://dotnetguts.blogspot.com ");

     gotoxy(1,1);
     textcolor(LIGHTMAGENTA);
     cprintf("*****INFIX TO POSTFIX (Paranthesized)*****\r\n");
     textcolor(WHITE);
     cprintf("Enter Infix string : ");
     cin>>infix;
     infix=concat(infix,')');

     textcolor(LIGHTBLUE);
     gotoxy(2,5);
     cprintf("INFIX");
     gotoxy(15,5);
     cprintf("STACK");
     gotoxy(35,5);
     cprintf("SUFFIX");
     gotoxy(53,5);
     cprintf("RANK");

     gotoxy(15,6);
     cout<<"(";
     cout<<endl;


     //Remove symbols with greater precedence from stackfor(i=0;infix[i]!='\0';i++){
     //obtain next input symbol
     next=infix[i];
     cout<<endl;
     printInfix(next);

     if(top<0){
        cout<<"\n\n\tInvali Infix";
        getch();
        exit(1);
     }
     while(f(next) < g(stack_arr[top])){
           temp=pop();
           polish=concat(polish,temp);
           rank = rank + r(temp);
           if(rank<1){
          cout<<"\n\n\tInvalid Infix";
          getch();
          exit(1);
           }
     }
     //Are there matching parentheses?if(f(next) != g(stack_arr[top]))
         push(next);
     else
         temp=pop();  //here value in temp is of no use
     delay(200);
     printStack();
     printSuffix();
     printRank();
     }


      //Check whether expression is invalidif(rank!=1 || top!=-1){
         textcolor(RED + BLINK);
         cprintf("\r\n\r\nInvalid Infix");
      }
      else{
        textcolor(LIGHTGREEN + BLINK);
        cprintf("\r\n\r\nvalid Infix");
        textcolor(YELLOW);
        cprintf("\r\nSuffix : %s",polish);
      }
      _setcursortype(_NOCURSOR);
}

void main(){
     clrscr();
     revpol obj;
     obj.suffix();
     getch();
}
[/Code]

0 comments:

Post a Comment