using namespace std
#include <stdio.h>
#include <stdlib.h>
#define add 43
#define subs 45
#define mult 42
#define div 47
#define MAXSIZE 100
typedef struct
{
int stkdata[MAXSIZE]
int top
} STKzone
typedef STKzone *STK
typedef enum {True=1,False=0} boo
typedef enum {ok,error} status
STKzone expSTKzone
STK expSTK
STK initSTK(STKzone *stack_zone)
{
stack_zone->top=-1
return stack_zone
}
status push(int*term,STK pstk)
{
if(pstk->top==MAXSIZE-1)
return error
pstk->stkdata[++pstk->top]=*term
return ok
}/*push*/
bool emptySTK(STK pstk)
{
return(pstk->top==-1)
}
status pop(int *pdata,STK pstk)
{
if(emptySTK(pstk))
return error
else
{
*pdata=pstk->stkdata[pstk->top]
(pstk->top)--
return ok
}
}
void synerror()
{
printf("\n 表达式语法错误!\n")
exit(0)
}
int eval(int a1,int a2,char tag)
{
switch(tag)
{
case add:
return(a1+a2)
case subs:
return(a1-a2)
case mult:
return(a1*a2)
case div:
return(a1/a2)
}
}
int main()
{
char c
int opd1,opd2,temp,c1
expSTK=initSTK(&expSTKzone)
opd1 = 1
opd2 = 2
push(&opd1,expSTK)
push(&opd2,expSTK)
printf("\n后缀表达式:")
while((c=getchar())!='/n')
{
if(c==' ')continue
if((c>47)&&(c<58))
{
c1=c-48
if(push(&c1,expSTK)==error)
{
printf("\表达式太长\n")
exit(0)
}
}
else if((c==add)||(c==subs)||(c==mult)||(c==div))
{
if(pop(&opd1,expSTK)==error)
synerror()
if(pop(&opd2,expSTK)==error)
synerror()
temp=eval(opd1,opd2,c)
push(&temp,expSTK)
}
else {
// 你输入的c是多少,根本就不对
printf("c = [%c], c=%0X\n",c,c)
synerror()
}
}/*while*/
if(pop(&temp,expSTK)==error) synerror()
if(!(emptySTK(expSTK))) synerror()
printf("=%-3d\n",temp)
return 0
}/*main_end*/
// 执行程序看看,你输入的c值是多少,重新设计一下吧
#include<stdio.h>#include<iostream>
#include<string>
#include<stack>
using namespace std
int main() {
string str
stack<char>mystack
while (getline(cin, str)) {
printf("\n")
for (int i = 0i <str.size()i++) {
if (str[i] == 'a' or str[i]==',') {
cout <<str[i]
}
else if (str[i] == '+' or str[i] == '-' or str[i] == '*' or str[i] == '/' ) {
mystack.push(str[i])
}
else if (str[i] == '(') {
cout <<str[i]
}
else if (str[i] == ')') {
cout <<str[i] <<mystack.top()
mystack.pop()
}
}
}
}