中缀表达式转化为后缀表达式 报错啊
中缀表达式转化为后缀表达式 出错啊!!
加上括号就会出错;求解啊!!不加就ok!!
------解决方案--------------------
加上括号就会出错;求解啊!!不加就ok!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXzhansize 30
char stack[MAXzhansize];//
void add(int *top,char item)
{
if(*top>=MAXzhansize)
{
printf("jj\n");
exit(1);
}
stack[++*top]=item;
}
char deleted(int *top)
{
if(*top==-1)
{
printf("kk\n");
exit(1);
}
return stack[(*top)--];
}
void main()
{
int a[128];
a['+']=1;///预备优先级
a['-']=1;
a['*']=2;
a['/']=2;
a['%']=2;
a['(']=3;
a[')']=0;
a[',']=-1;
int top=-1,j=0,toptemp;//top始终是指向栈顶元素的
char b[30];
char c[30];
gets(b);
add(&top,',');// 最低优先级a[',']=-1;
for(int i=0;b[i]!='\0';++i)
{
if(!isdigit( b[i]) )///不是数字符号时
{
if(b[i] == '(' )//前两个if语句 处理特殊情况 一对括号
{
add( &top, b[i] );continue;//直接加到栈里'('
}
if(b[i] == ')' )//处理一对括号
{
while( stack[top] != '(' )
c[j++]=deleted(&top);
deleted(&top);//除掉一个'('
continue;
}
if(a[ b[i] ] > a[ stack[top] ])
////当前的符号优先级大于栈顶的符号是放入,,否则弹出栈顶..........
{
add( &top, b[i] );
continue;
}
else
{
while(a[ b[i] ] <= a[ stack[top] ])/////直到大于栈顶符号的优先级
c[j++]=deleted(&top);
add( &top, b[i] );continue;
}
}
else////是数字符号时
{
c[j++]=b[i];
}
}
while(top !=0 )/////栈里剩余的符号输出
c[j++]=deleted(&top);
c[j++]='\0';////补上空字符
puts(c);
}
------解决方案--------------------