1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
const int size =50;
char infix[size],postfix[size],stack[size];
int top=-1;
int precedence(char ch); // function to get the precedence of the operator
char pop(); //function to pop an element from the stack
char topelement(); // returns the top element of the stack
void push(char ch); // pushes an element into the stack
char go,choice;
int main()
{
clrscr();
cout<<endl<<endl;
cout<<" Program for SMART people! "<<endl<<endl<<endl<<endl<<endl;
gotoxy(15,20);
cprintf("Do you want to continue? ..(y/n) ");
cin>>go;
if(go=='y')
{
clrscr();
cout<<endl<<endl<<endl;
cout<<" M E N U "<<endl<<endl;
cout<<" A. Infix "<<endl;
cout<<" B. Postfix "<<endl;
cout<<" C. Prefix "<<endl;
cout<<" D. Exit "<<endl<<endl;
cout<<" Option: ";
cin>>choice;
if(choice=='a')
{
clrscr();
cout<<endl;
char ele,elem,st[2];
int prep,pre,popped,j=0,chk=0;
strcpy(postfix," ");
cout<<" ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ "<<endl;
cout<<" º "<<endl;
cout<<" º INFIX to POSTFIX ... "<<endl;
cout<<" º "<<endl;
cout<<" º "<<endl;
cout<<" º infix = 1+1 -> Normal Arithmetic Notation "<<endl;
cout<<" º prefix = +11 -> Operators comes first before the operands "<<endl;
cout<<" º postfix = 11+ -> Operands comes first before the operator "<<endl;
cout<<" º "<<endl;
cout<<" º "<<endl;
cout<<" º "<<endl;
cout<<" º "<<endl;
cout<<" º "<<endl;
cout<<" º "<<endl;
cout<<" º "<<endl;
cout<<" º "<<endl;
cout<<" º Enter Infix : "<<endl;
cout<<" º "<<endl;
cout<<" º "<<endl;
cout<<" È "<<endl;
gotoxy(32,18);
gets(infix);
for(int i=0;infix[i]!=0;i++)
{
if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infix[i]!='-')
postfix[j++]=infix[i];
else if(infix[i]=='(')
{
elem=infix[i];
push(elem);
}
else if(infix[i]==')')
{
while(popped=pop() != '(')
postfix[j++]=popped;
}
else
{
elem=infix[i];
pre=precedence(elem);//stores the precedence of operator coming frm infix
ele=topelement();
prep=precedence(ele);//stores the precedence of operator at the top of the stack
if(pre > prep)
push(elem);
else
{
while(prep >= pre)
{
if(ele=='#')
break;
popped=pop();
ele=topelement();
postfix[j++]=popped;
prep=precedence(ele);
}
push(elem);
}
}
}
while((popped=pop())!='#')
postfix[j++]=popped;
postfix[j]='\0';
gotoxy(10,20);
cout<<" Postfix : "<<postfix;
system("pause");
}
}
getch();
return 0;
}
int precedence(char ch)
{
switch(ch)
{
case '^' : return 5;
case '/' : return 4;
case '*' : return 4;
case '+' : return 3;
case '-' : return 3;
default : return 0;
}
}
char pop() //function to pop the element from the stack
{
char ret;
if(top!=-1)
{ ret =stack[top];
top--;
return ret;
}
else
return '#';
}
char topelement() // function to return top element from the stack without popping
{
char ch;
if(top!=-1)
ch=stack[top];
else
ch='#';
return ch;
}
void push(char ch) // function to push an element in the stack
{
if(top!=size-1)
{
top++;
stack[top]= ch;
}
}
|