#include<stdio.h>
#include<math.h>
#define MAXSIZE 30
//出栈函数
void pop(int *num,char *c,int *num_p,int *str_p)
{
int num1,num2,result=0;
num1=num[(*num_p)--];
num2=num[(*num_p)--];
if(c[*str_p]=='+') result=num1+num2;
else if(c[*str_p]=='-') result=num2-num1;
else if(c[*str_p]=='*') result=num1*num2;
else if(c[*str_p]=='/') result=num2/num1;
//printf("中间结果:=%d \n",result);
num[++(*num_p)]=result;
(*str_p)--;
}
//字符数组里的数字转换成int型
void change(int *num,char *all,int *all_p,int *num_p)
{
int item=*all_p;
int number1=0;
while((all[item]-'0')>=0&&(all[item]-'0')<=9)
{
number1++;
item++;
}
number1--;
int result=0;
while(all[*all_p]-'0'>=0&&all[*all_p]-'0'<=9)
{
result=result+pow(10,number1)*(all[*all_p]-'0');
(*all_p)++;
number1--;
}
(*all_p)--;
//printf("=%d,",*all_p);
num[++(*num_p)]=result;
}
int main()
{
int number[MAXSIZE]; //number存储数字
char all_put[60],str[MAXSIZE]; //all_put存储标准输入,str存储运算符号
int num_top=-1,str_top=0; //两个数组的栈顶指针
str[0]='(';
char c;
int i=0;
while((c=getchar())!='\n')
all_put[i++]=c;
int j;
for(j=0;j<i;j++)
{
c=all_put[j];
if((c-'0'>=0)&&(c-'0'<=9)) //该字符是数字时
change(number,all_put,&j,&num_top);
else if(c=='(') //该字符是'('
str[++str_top]=c;
else if(c=='+'||c=='-') //该字符是'+'或'-'
{
if(str[str_top]=='+'||str[str_top]=='-'||str[str_top]=='*'||str[str_top]=='/')
{
pop(number,str,&num_top,&str_top);
str[++str_top]=c;
}
else
str[++str_top]=c;
}
else if(c=='*'||c=='/') //该字符是'*'或者'/'
{
if(str[str_top]=='*'||str[str_top]=='/')
{
pop(number,str,&num_top,&str_top);
str[++str_top]=c;
}
else
str[++str_top]=c;
}
else if(c==')') //该字符是')'
{
pop(number,str,&num_top,&str_top);
str_top--;
}
//printf("字符:%c\n",str[str_top]);
}
while(str_top>0)
pop(number,str,&num_top,&str_top);
printf("运算结果为:%d\n",number[num_top]);
return 0;
}
#include<stdio.h>
#include<ctype.h>
#define N 100
#define error 0
typedef char ElementType;
typedef struct{
ElementType date[N];
int top;
}stack;
typedef struct{
int date[N];
int top;
}zstack;
void chushi(stack &p)
{
p.top=-1;
};
void zchushi(zstack &p)
{
p.top=-1;
};
void push(stack &p,ElementType s)
{
if(p.top==N-1)
printf("堆栈满");
else{
p.top++;
p.date[p.top]=s;
}
};
void zpush(zstack &p,int s)
{
if(p.top==N-1)
printf("堆栈满");
else{
p.top++;
p.date[p.top]=s;
}
};
ElementType pop(stack &p)
{
if(p.top==-1){
printf("堆栈空");
return error;
}
else
return (p.date[(p.top)--]);
};
int zpop(zstack &p)
{
if(p.top==-1){
printf("堆栈空");
return 0;
}
else
return (p.date[(p.top)--]);
};
void show(stack &p)
{
int i=0;
while(i<=p.top)
{
printf("%c ",p.date[i]);
i++;
}
printf("\n");
};
int houzhui(stack &hz,stack &zf,zstack &zs,ElementType &s)
{
int k,i;
char j;
printf("请挨个输入四则运算表达式:(输入#表示结束)\n");
while((s=getchar())!='#')
{
if(isdigit(s)){
i=0;
i=i*10+(s-'0');
push(hz,s);
while(isdigit(s=getchar())){
i=i*10+(s-'0');
push(hz,s);
}
zpush(zs,i);
if(s=='#')
break;
}
if(!isdigit(s)){
if(s=='('){
push(zf,s);
}
else if(s==')'){
while(zf.date[zf.top]!='('){
j=pop(zf);
push(hz,j);
switch (j){
case'+':zpush(zs,zpop(zs)+zpop(zs));break;
case'-':
k=zpop(zs);
zpush(zs,zpop(zs)-k);
break;
case'*':zpush(zs,zpop(zs)*zpop(zs));break;
case'/':
k=zpop(zs);
if(k!=0)
zpush(zs,zpop(zs)/k);
else{
printf("分母为0,出错!\n");
return 0;
}
break;
default:printf("不为运算符,出错!");break;
}
}
pop(zf);
}
else if(s=='+' || s=='-' )
{
if(zf.date[zf.top]=='(' || zf.top==-1)
push(zf,s);
else {
while(zf.date[zf.top]!='(' && zf.top!=-1){
j=pop(zf);
push(hz,j);
switch (j){
case'+':zpush(zs,zpop(zs)+zpop(zs));break;
case'-':
k=zpop(zs);
zpush(zs,zpop(zs)-k);
break;
case'*':zpush(zs,zpop(zs)*zpop(zs));break;
case'/':
k=zpop(zs);
if(k!=0)
zpush(zs,zpop(zs)/k);
else{
printf("分母为0,出错!\n");
return 0;
}
break;
default:printf("不为运算符,出错!");break;
}
}
push(zf,s);
}
}
else if(s=='*' || s=='/' )
{
if(zf.date[zf.top]=='+' || zf.date[zf.top]=='-' || zf.date[zf.top]=='(' || zf.top==-1)
push(zf,s);
else {
j=pop(zf);
push(hz,j);
switch (j){
case'+':zpush(zs,zpop(zs)+zpop(zs));break;
case'-':
k=zpop(zs);
zpush(zs,zpop(zs)-k);
break;
case'*':zpush(zs,zpop(zs)*zpop(zs));break;
case'/':
k=zpop(zs);
if(k!=0)
zpush(zs,zpop(zs)/k);
else{
printf("分母为0,出错!\n");
return 0;
}
break;
default:printf("不为运算符,出错!");break;
}
push(zf,s);
}
}
}
}
while(zf.top!=-1){
j=pop(zf);
push(hz,j);
switch (j){
case'+':zpush(zs,zpop(zs)+zpop(zs));break;
case'-':
k=zpop(zs);
zpush(zs,zpop(zs)-k);
break;
case'*':zpush(zs,zpop(zs)*zpop(zs));break;
case'/':
k=zpop(zs);
if(k!=0)
zpush(zs,zpop(zs)/k);
else{
printf("分母为0,出错!\n");
return 0;
}
break;
default:printf("不为运算符,出错!");break;
}
}
printf("后缀表达式为:");
show(hz);
};
int main()
{
while(1)
{
char i;
int k=1;
stack hz,zf;
zstack zs;
ElementType s;
chushi(hz);
chushi(zf);
zchushi(zs);
k=houzhui(hz,zf,zs,s);
if(k)
printf("运算结果为:%d\n",zs.date[zs.top]);
printf("是否继续计算?(输入y继续)\n");
scanf("%s",&i);
if(i!='y')
break;
}
}