برنامه تبدیل infix به prefix
سلام خدمت دوستان گرامی
این برنامه که تبدیل infix به prefix است و به زبان c++ نوشته شده و از تمرین هایی هست که تو کتاب آقای مقسمی هست
اگه مشکلی داشتین من در خدمتم
--------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<cstring.h>
#define m 21
class stack
{
public:
stack();
void push(char);
char pop();
int empty();
void popandtest(char & , int &);
void topandtest(char & , int &);
void display();
void pushandtest(char & , int &);
private :
int mytop;
char items[m];
};
/////////////////////
stack::stack()
{
mytop=-1;
}
//////////////////
void stack::push(char x)
{
items[++mytop]=x;
}
char stack::pop()
{
return items[mytop--];
}
int stack::empty()
{
return (mytop==-1);
}
//////////////////////////
void stack::popandtest(char &temp , int &underflow)
{
if(empty())
underflow=1;
else
{
temp=items[mytop--];
underflow=0;
}
}
//////////////////////////////////
void stack::pushandtest(char &x,int &overflow)
{
if(mytop==m-1)
overflow=1;
else
{
overflow=0;
items[++mytop]=x;
}
}
//////////////////
void stack::topandtest(char &x , int &underflow)
{
if(empty())
underflow=1;
else
{
x=items[mytop];
underflow=0;
}
}
void convert(char[],char[]);
int cheak(char);
int main()
{
char infix[m],prefix[m];
clrscr();
cout<<"please enter the infix expertion: ";
cin.get(infix,m);
convert(infix , prefix);
int len=strlen(prefix);
for(int i = 0 ; i<len;i++)
cout<<prefix[i];
return 0;
}
int cheak(char symbol)
{
if (symbol>='a' && symbol <='z')
return 1;
return 0;
}
void convert(char infix[],char prefix[])
{ int len;
char symbol ,temp, topsymbol;
int underflow,overflow , j=0;
stack s1;
stack s2;
len=strlen(infix);
for(int i =len-1;i>=0;i--)
{
symbol=infix[i];
if(cheak(symbol))
s2.pushandtest(symbol,overflow);
/////////////////////////////
if(symbol=='(')
{
s1.popandtest(temp,underflow) ;
if(temp=='+'|| temp=='*'|| temp=='-'|| temp=='/')
{
s2.pushandtest(temp,overflow);
}
s1.popandtest(temp,underflow) ;
if(temp=='+' || temp=='*'|| temp=='-'|| temp=='/')
s2.pushandtest(temp,overflow);
}
//////////////////////////
else
s1.pushandtest(symbol,overflow);
}
while(!s1.empty())
{
s1.popandtest(temp,underflow) ;
if(temp=='+'|| temp=='-'|| temp=='*'|| temp=='/')
{
s2.pushandtest(temp,overflow);
}
i=0;
}
for(i=0;underflow!=1;i++)
{
s2.popandtest(prefix[i],underflow);
}
prefix[i-1]='\0';
}
در صورت مشکل داخل note pad کپی کنید بعد ببرید تو محیط برنامه نویسی
تبلیغات