Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Homework HelpRSS Feeds

Program to convert an Infix Expression into a Postfix / Suffix Expression without parenthesize

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

Write a Program to convert an Infix Expression into a Postfix / Suffix Expression without parenthesize

Code for Program to convert an Infix Expression into a Postfix / Suffix Expression without parenthesize in C++ Programming

#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 (UNParanthesized)*****\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();
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to convert an Infix Expression into a Postfix / Suffix Expression without parenthesize 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!