Код:
//Infix to postfix
//paranthesized
#include <iostream.h>
#include <stdlib.h> //for exit()
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <windows.h>
#define row 7
#define col 5
class revpol{
int top,rank;
char *stack_arr,*polish,*infix;
public:
int f(char); //Input precedence function
int g(char); //Stack precedence function
int r(char); //rank function
void push(char);
char pop();
char *concat(char *,char);
void suffix();//infix to suffix conversion function
void printStack();
void printSuffix();
void printRank();
void printInfix(char);
};
void revpol :: printInfix(char ch){
static int y=row,x=col;
gotoxy(x,y++);
cout<<ch;
}
void revpol :: printStack(){
static int x=col,y=row;
gotoxy(x+10,y++);
cout<<stack_arr;
}
void revpol :: printSuffix(){
static int x=col,y=row;
gotoxy(x+30,y++);
cout<<polish;
}
void revpol :: printRank(){
static int x=col,y=row;
gotoxy(x+50,y++);
cout<<rank;
}
int revpol :: f(char inputchar){
if(inputchar==')')
return(0);
else if(inputchar=='+' || inputchar=='-')
return(1);
else if(inputchar=='*' || inputchar=='/')
return(3);
else if(inputchar=='^')
return(6);
else if(inputchar>='a' || inputchar<='z')
return(7);
else if(inputchar>='A' || inputchar<='Z')
return(7);
else if(inputchar=='(')
return(9);
else
return(-99); //for error
}
int revpol :: g(char inputchar){
if(inputchar=='(')
return(0);
else if(inputchar=='+' || inputchar=='-')
return(2);
else if(inputchar=='*' || inputchar=='/')
return(4);
else if(inputchar=='^')
return(5);
else if(inputchar>='a' || inputchar<='z')
return(8);
else if(inputchar>='A' || inputchar<='Z')
return(8);
else
return(-99); //for error
}
int revpol :: r(char inputchar){
if(inputchar=='+' || inputchar=='-' || inputchar=='*' || inputchar=='/'|| inputchar=='^')
return(-1);
else if(inputchar>='a' || inputchar<='z')
return(1);
else if(inputchar>='A' || inputchar<='Z')
return(1);
else
return(-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;
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 stack
for(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 invalid
if(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(){
revpol obj;
obj.suffix();
getch();
}