标签:style io ar 使用 sp for strong on 数据
我们熟悉的表达式如a+b、a+b*(c+d)等都属于中缀表达式。中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2。同理,后缀表达式就是操作符在两个操作数之后:num1 num2 operand。ACM队的“C小加”正在郁闷怎样把一个中缀表达式转换为后缀表达式,现在请你设计一个程序,帮助C小加把中缀表达式转换成后缀表达式。为简化问题,操作数均为个位数,操作符只有+-*/ 和小括号。
21+2(1+2)*3+4*5
12+12+3*45*+
代码
#include<stdio.h>
#include<string.h>
#define max 1000
int main(void)
{
int n,i,t,s;
char a[max],ch[max];
scanf("%d",&n);
while(n--)
{
s=-1;
t=0;
char str[max];
scanf("%s",str);
for(i=0;i<strlen(str);i++)
{
if(str[i]>=‘0‘&&str[i]<=‘9‘)
{
ch[t++]=str[i];
}
else if(str[i]==‘(‘)
{
a[++s]=str[i];
}
else if(str[i]==‘)‘)
{
while(s>=0&&a[s]!=‘(‘)
{
ch[t++]=a[s--];
}
s--;
}
else if(str[i]==‘+‘||str[i]==‘-‘)
{
while(s>=0&&a[s]!=‘(‘)
{
ch[t++]=a[s--];
}
a[++s]=str[i];
}
else
{
while(a[s]==‘*‘||a[s]==‘/‘)
{
ch[t++]=a[s--];
}
a[++s]=str[i];
}
}
while(s>=0)
{
ch[t++]=a[s--];
}
ch[t]=‘\0‘;
printf("%s\n",ch);
}
return 0;
}
标签:style io ar 使用 sp for strong on 数据
原文地址:http://blog.csdn.net/qq_16997551/article/details/41871035