#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_LENTH 30
#define MAX 100
typedef struct
{
int data[MAX_LENTH];
int top;
} Stack;
Stack *Createstack()
{
Stack *p;
p = (Stack *)malloc(sizeof(*p));
p->top = -1;
return p;
}
int Push(Stack *p,int x)
{
if (p->top == MAX_LENTH - 1)
{
return -1;
}
p->top++;
p->data[p->top] = x;
return 0;
}
int Pop(Stack *L,int *x)
{
if (L->top == -1)
{
return -1;
}
*x = L->data[L->top];
L->top--;
return 0;
}
int TOP(Stack *L,int *x)
{
if (L->top == -1)
{
return -1;
}
*x = L->data[L->top];
return 0;
}
int Empty(Stack *L)
{
return (L->top == -1);
}
int Priority(int ope)
{
switch(ope)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default :
return -1;
}
}
void Calculation(Stack *snum,int ope)
{
int n,n1,n2;
Pop(snum,&n1);
Pop(snum,&n2);
switch(ope)
{
case '+':
n = n1 + n2;
break;
case '-':
n = n2 - n1;
break;
case '*':
n = n1 * n2;
break;
case '/':
n = n2 / n1;
break;
}
Push(snum,n);
}
//初始sope栈空 遵守规则4-a 运算符直接入栈
//规则2 左括号入栈
//规则4 old_ope是栈顶运算符
//规则4-a Priority(ope) > Priority(old_ope
//规则4-b Priority(ope) <= Priority(old_ope
//规则4-b-1 栈顶运算符弹出并打印, 调用Calculation(snum,old_ope)结果入snum栈
//规则4-b-2准备下一次用于比较的新的栈顶运算符
//规则4-b-3 运算符压栈
void Deal_ope(Stack *snum,Stack *sope,int ope)
{
int old_ope;
//初始sope栈空 遵守规则4-a 运算符直接入栈
if (ope == ')') {
while(!Empty(sope) && old_ope != '(') {
Pop(sope,&old_ope);
Push(snum,old_ope);
TOP(sope,&old_ope);
}
if(old_ope == '(') {
Pop(sope,&old_ope);//剔除左括号
}
return ;
}
while(!Empty(sope) && old_ope != '(' && Priority(ope) <= Priority(old_ope))
{
Pop(sope,&old_ope);
Push(snum,old_ope);
TOP(sope,&old_ope);//规则4-b-2准备下一次用于比较的新的栈顶运算符
}
Push(sope,ope);
}
//此此函数是规则3 遇到右括号
//规则3-a sope栈顶的运算符弹出并打印
//并调用Calculation(snum,old_ope)结果入snum栈 直到遇到左括号
//规则3-b 出栈左括号
void Right(Stack *snum,Stack *sope)
{
int old_ope;
TOP(sope,&old_ope);
while (old_ope != '(') //规则3-a直到遇到左括号
{
Pop(sope,&old_ope);
printf("%c ",old_ope);
Calculation(snum,old_ope);
TOP(sope,&old_ope);
}
Pop(sope,&old_ope); //规则3-b弹出左括号
}
int Display(Stack *L)
{
int i;
if (L->top == -1)
{
return 0 ;
}
for (i=0; i<=L->top; i++)
{
printf("%d ",L->data[i]);
}
printf("\n");
return 0;
}
void Displayope(Stack *L)
{
int i;
if (L->top == -1)
{
return ;
}
for (i=0; i<=L->top; i++)
{
printf("%c ",L->data[i]);
}
printf("\n");
}
//此函数是判断输入字符是否符合四则运算式定义
int func(char a[])
{
int i=0,d1=0,d2=0;
char b[1005];
char c[1005];
while(a[i]!='\0')
{
if ((a[i]>=48&&a[i]<=57)||a[i]=='('||a[i]==')'||a[i]=='+'||a[i]=='-'||a[i] =='*'||a[i]=='/')
{
}
else
{
printf("含有非法字符\n");
return 0;
}
if(a[i]=='(')
{
b[d1]=a[i];
d1++;
}
else if(a[i]==')')
{
c[d2]=a[i];
d2++;
}
if(a[i]=='/'&&a[i+1]=='0')
{
printf("除数不得为0\n");
return 0;
}
else if(a[i]=='('&&a[i+1]==')')
{
printf("括号内为空\n");
return 0;
}
else if (a[i]=='('&&a[i-1]>=48&&a[i-1]<=57) {printf("表达式错误\n");return 0;}
else if (a[i]==')'&&a[i+1]>=48&&a[i+1]<=57) {
printf("表达式错误%d\n",a[i+1]);
return 0;
}
i++;
}
if(d1==d2)
{
return 1;
}
else
{
printf("括号不匹配\n");
return 0;
}
}
int main()
{
int ffs;
char str[MAX],dst[MAX];
printf("(请使用英文输入法,表达式不用加等号)\n");
printf("中缀表达式为:");
scanf("%s",str);
ffs=func(str);
if(ffs==0)
{
printf("error!\n");
return 0;
}
else
{
printf("%s合法\n",str);
}
int i = 0,value = 0,flag = 0;
int old_ope;
Stack *numstack,*opestack;
numstack = Createstack();
opestack = Createstack();
printf("后缀表达式为:");
//处理多位数字字符 flag为1时表明多位数字值为value
while (str[i] != '\0')
{
if (str[i] >= '0' && str[i] <= '9')
{
value *= 10;
value +=str[i]-'0';
flag = 1;
}
else
{
if (flag)
{
printf("%d ",value);
Push (numstack, value);
flag = 0;//flag标示数字已入栈
value = 0;
}
if(str[i] == ')') Right(numstack,opestack);
else Deal_ope(numstack,opestack,str[i]);
}
i++;
}
if (flag)//flag=1 标示数字已经处理妥当 可以打印输出
{
printf("%d ",value);Push(numstack,value);
}
while (!Empty(opestack)) //规则5
{
Pop(opestack,&old_ope);
printf("%c ",old_ope);
Calculation(numstack,old_ope);
}
Pop(numstack,&value);
printf("\n%s = %d\n",str,value);
return 0;
}