سلام.
بیا برنامه تبدیل infix به postfix
کد:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//--------------------
int IsFull();
int IsEmpty();
void Push(int x);
void Pop(int &x);
//--------------------
const int MaxSize=8;
int Top=-1;
int Stack[MaxSize];
//--------------------
void main()
{
char e[20];
int value;
int x1,x2;
clrscr();
cout<<"\nEnter expression:";
cin>>e;
//Starting eval algorithm
for(int i=0;e[i]!='\0';i++)
if (e[i]>='0' && e[i]<='9')
{
//oprand
value=e[i]-'0';
Push(value);
}
else
{
//operator
Pop(x2);
Pop(x1);
switch( e[i] )
{
case '*': Push(x1*x2);
break;
case '/': Push(x1/x2);
break;
case '+': Push(x1+x2);
break;
case '-': Push(x1-x2);
break;
default : cout<<"invalid expression.";
}//End switch
}//End else
Pop(value);
if (!IsEmpty())
cout<<"invalid expression.";
else
cout<<"expression= "<<value;
getch();
}//End main
//--------------------
int IsFull()
{
if(Top==MaxSize-1)
return 1;
else
return 0;
}
//--------------------
int IsEmpty()
{
if(Top==-1)
return 1;
else
return 0;
}
//--------------------
void Push(int x)
{
if(IsFull())
cout<<"\nStak is full.";
else
Stack[++Top]=x;
}
//--------------------
void Pop(int &x)
{
if(IsEmpty())
cout<<"\nStak is Empty.";
else
x=Stack[Top--];
}
//--------------------
//End.