不就是postfix么?我有c++的代码,自己看着修改吧
# include <iostream>
# define MAX 20
using namespace std; void operation(char c1, int x, int y, int &ans);
void push(int val, int &top);
int pop(int &top );
bool isEmpty(int top);
bool isFull(int top); int stack[MAX]; int main(void)
{
int top = -1;
char c1;
int x, y;
int val, sum, ans; cout << "Please type in postfix equation" <<endl;
while(cin>>c1)
{
/* primitive way to handle a 2-digit token */
int d[2];
if((c1>='0') && (c1<='9'))
{
static int i = 0;
if(i==0)
{
d[i]= c1 - '0';
i++;
}
else if(i==1)
{
d[i]= c1 - '0';
val = d[i-1]*10 + d[i];
--i;
push (val, top);
}
} /* Check to see if char is Operation, +, - or * */
if((c1=='+') || (c1=='-') || (c1=='*'))
{
/* Pop top 2 items */
y = pop(top);
x = pop(top); operation(c1, x, y, ans); /* Push the answer on the stack */
push(ans, top);
} /* Print the result if char is equal sign */
if( c1 == '=' )
{
sum = pop(top);
cout<<"Answer is "<<sum<<endl;
}
} return 0;
} void operation(char c1, int x, int y, int &ans)
{
switch(c1)
{
case '+': ans = x + y;
break; case '-': ans = x - y;
break; case '*': ans = x * y;
break;
}
}
void push(int newitem, int &top)
{
if(!isFull(top))
stack[++top] = newitem;
else
cout << "Error! Stack is full!" <<endl;
} int pop(int &top )
{
if(!isEmpty(top))
return stack[top--];
else
cout<< "Error! Stack is empty!" <<endl;
exit (1);
} bool isEmpty(int top)
{
return top==-1;
} bool isFull(int top)
{
return top==(MAX);
}
# include <iostream>
# define MAX 20
using namespace std; void operation(char c1, int x, int y, int &ans);
void push(int val, int &top);
int pop(int &top );
bool isEmpty(int top);
bool isFull(int top); int stack[MAX]; int main(void)
{
int top = -1;
char c1;
int x, y;
int val, sum, ans; cout << "Please type in postfix equation" <<endl;
while(cin>>c1)
{
/* primitive way to handle a 2-digit token */
int d[2];
if((c1>='0') && (c1<='9'))
{
static int i = 0;
if(i==0)
{
d[i]= c1 - '0';
i++;
}
else if(i==1)
{
d[i]= c1 - '0';
val = d[i-1]*10 + d[i];
--i;
push (val, top);
}
} /* Check to see if char is Operation, +, - or * */
if((c1=='+') || (c1=='-') || (c1=='*'))
{
/* Pop top 2 items */
y = pop(top);
x = pop(top); operation(c1, x, y, ans); /* Push the answer on the stack */
push(ans, top);
} /* Print the result if char is equal sign */
if( c1 == '=' )
{
sum = pop(top);
cout<<"Answer is "<<sum<<endl;
}
} return 0;
} void operation(char c1, int x, int y, int &ans)
{
switch(c1)
{
case '+': ans = x + y;
break; case '-': ans = x - y;
break; case '*': ans = x * y;
break;
}
}
void push(int newitem, int &top)
{
if(!isFull(top))
stack[++top] = newitem;
else
cout << "Error! Stack is full!" <<endl;
} int pop(int &top )
{
if(!isEmpty(top))
return stack[top--];
else
cout<< "Error! Stack is empty!" <<endl;
exit (1);
} bool isEmpty(int top)
{
return top==-1;
} bool isFull(int top)
{
return top==(MAX);
}













