一、后缀表达式转中缀表达式
二、我的代码
#include<stdio.h>
#define N 200
//栈结构
typedef struct Stack{
int top;
char stack[N];
}Stack;
//栈初始化
void Init_Stack(Stack *s)
{
int i;
for(i = 0; i < N; i++)
s->stack[i] = '\0';
s->top = 0;
}
//判空
int Is_Empty(Stack *s)
{
if(s->top == 0)
return 1;
else
return 0;
}
//入栈
void Push_Stack(Stack *s, char ele)
{
if (s->top == N)
printf("栈满了!\n");
else
{
s->stack[s->top] = ele;
s->top++;
}
}
//出栈
char Pop_Stack(Stack *s)
{
//本题无需在此判空,函数外再判空
s->top--;
return s->stack[s->top];
}
//查看栈顶元素
char Search_Stack(Stack *s)
{
return s->stack[s->top - 1];
}
//判断运算符优先级
char Priority(char a, char b)
{
if (b == '(' || b == ')')
return '>';
if (a == '+' || a == '-')
return '<';
else
{
if ( (a == '*' || a == '/') && (b == '*' || b == '/'))
return '<';
else
return '>';
}
}
int main()
{
char equ[N];
Stack opr;
int i;
while(scanf("%s", equ) != EOF)
{
Init_Stack(&opr);
for(i=0; i < N; i++)
{
if((equ[i] >= 'A' && equ[i] <= 'Z') || (equ[i] >= 'a' && equ[i] <= 'z'))
printf("%c", equ[i]);
else
{
if(Is_Empty(&opr))
Push_Stack(&opr, equ[i]);
else if(equ[i] == '(')
{
Push_Stack(&opr, equ[i]);
}
else if(equ[i] == ')')
{
while(Search_Stack(&opr) != '(')
{
printf("%c", Pop_Stack(&opr));
}
Pop_Stack(&opr);
}
else if(equ[i] == '\0')
{
while(!Is_Empty(&opr))
printf("%c", Pop_Stack(&opr));
break;
}
else if(Priority(equ[i], Search_Stack(&opr)) == '>')
Push_Stack(&opr, equ[i]);
else
{
printf("%c", Pop_Stack(&opr));
i--;
}
}
}
}
return 0;
}
三、报错信息
报错表示了,其中两个样式错误了,可我跟答案对比一样啊,不知道为什么错了?