PDA

View Full Version : میانوندی به پیشوندی



ali_1369
جمعه 04 دی 1388, 00:42 صبح
سللام..من یه برنامه نوشتم که یه عبارت میانوندی میگیره ومیکنتش پسوندی بعدم ارزیابیش میکنه ولی کار نمیکنه!




#include <iostream>
#include <conio.h>
using namespace std;

const int maxsize=30;
class stack
{
private:
int top;
int s[30];
public:
stack();
char pop();
void push(char);
int isempty();
int isfull();
char topp();
};
stack::stack()
{
top=-1;
}
int stack :: isempty()
{
if(top==-1)
return 1;
else return 0;
}
char stack:: topp()
{
if(isempty())
cout<<"the list is empty";
else{
char x=s[top];
return x;}}
int stack:: isfull()
{
if(top==(maxsize-1))
return 1;
else return 0;
}
char stack :: pop()
{
if (top==-1)
cout<<"is empty";
else{
char x;
x=s[top--];
return x;}}
void stack :: push(char x)
{
if(isfull())
cout<<"is full";
else
s[++top]=x;
}
class stackint
{
private:
int top;
int s[30];
public:
stackint();
int pop();
void push(int);
int isempty();
int isfull();
int topp();
};
stackint::stackint()
{
top=-1;
}
int stackint :: isempty()
{
if(top==-1)
return 1;
else return 0;
}
int stackint:: topp()
{ if(isempty())
cout<<"is empty";
else {
int x=s[top];
return x;}}
int stackint:: isfull()
{
if(top==(maxsize-1))
return 1;
else return 0;
}
int stackint :: pop()
{
if (top==-1)
cout<<"is empty";
else {
int x;
x=s[top--];
return x;}}
void stackint :: push(int x)
{
if(isfull())
cout<<"is full";
else
s[++top]=x;
}

int eval(int , int , char);
int compare(char , char);
int isoperator(char);
int main()
{
int value;
stack s;
char infix[maxsize],postfix[maxsize];
cout<<"enter infix to convert and calculat";
cin.get(infix,maxsize);
for(int i=0;infix[i];i++)
{
char symbol=infix[i],topsymbol;
if(symbol=='(')
s.push(symbol);
else if(symbol>='a' && symbol<='z' || symbol>='A' && symbol<='Z')
postfix[i++]=symbol;
else if(symbol==')'){
topsymbol=s.pop();
while(topsymbol!='('){
postfix[i++]=topsymbol;
topsymbol=s.pop();}}
else {
topsymbol=s.topp();

if(s.isempty() || !compare(topsymbol , symbol))
s.push(symbol);
else {
topsymbol=s.pop();
while(!s.isempty() || compare(topsymbol , symbol))
{
postfix[i++]=topsymbol;
topsymbol=s.pop();
}
}
}
while(!s.isempty())
{
postfix[i++]=s.pop();}
postfix[i]='\0';
}
for(int e=0; postfix[e];e++)
cout<<postfix[e];
stackint s2;
for(int j=0;postfix[j];j++)
{
if(!isoperator(postfix[j])){
cout<<"enter amount for"<<postfix[j];
int i;
cin>>i;
s2.push(i);}
else {
int p1=s2.pop();
int p2=s2.pop();
value=eval(p1,p2,postfix[j]);
s2.push(value);}}
value=s2.pop();
cout<<"the amount of postfix is"<<value;
getch();
return 0;
}
int eval(int p1,int p2, char op)
{
switch (op)
{
case '+':return p1+p2;
case '*':return p1*p2;
case '/' :return p1/p2;
case '-': return p1-p2;
}
}
int compare(char op1 , char op2)
{
char op[]={'(','+','-','*','/','%'};
int amount[]={1,2,2,3,3,3};
int h,k;
for(int i=0;op[i];i++){
if(op1==op[i])
h=i;}
for(int j=0;op[j];j++){
if(op2==op[j])
k=j;
}
if(amount[h]>amount[k])
return 1;
else return 0;
}
int isoperator(char x)
{
char opr[]={'+','-','/','%','*'};
int a=0;
for(int k=0 ; opr[k];k++)
{
if(x==opr[k])
a=1;}
if(a==1) return 1;
else return 0;}

parnian_h85
شنبه 05 دی 1388, 00:34 صبح
سلام
من 2تا برنامه با این منطق دارم فکر میکنم کمکت کنه:متعجب: