中缀式变后缀式
- 描述
- 人们的日常习惯是把算术表达式写成中缀式,但对于机器来说更“习惯于”后缀式,关于算术表达式的中缀式和后缀式的论述一般的数据结构书都有相关内容可供参看,这里不再赘述,现在你的任务是将中缀式变为后缀式。
-
- 输入
-
第一行输入一个整数n,共有n组测试数据(n<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式的中缀式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数。数据保证除数不会为0
- 输出
- 每组都输出该组中缀式相应的后缀式,要求相邻的操作数操作符用空格隔开。
- 样例输入
-
2 1.000+2/4= ((1+2)*5+1)/4=
- 样例输出
-
1.000 2 4 / + = 1 2 + 5 * 1 + 4 / =
做题时在输出空格花了些时间
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 10002
#include "iostream"
#include "cstdlib"
#include "cstdio"
#include "cstring"
using namespace std;
typedef int Status;
int
typedef struct Stack1
{
}SqStack1;
//运算符栈操作
void
&S)
{
STACK_INIT_SIZE);
}
Status Stack1Empty(SqStack1 S)//判断是否为空
{
TRUE;
}
void Push1(SqStack1 &S,char e )//插入元素e为新的栈顶元素
{
}
void Pop1(SqStack1 &S,char &e)//出栈
{
}
char GetTop1(SqStack1 S)
{
}
void TraverseStack1(SqStack1 S)//输出当前顺序表
{
";
}
Status In(char ch)//判断ch是否为运算符
{
'*' || ch == '/' || ch == ')'
return TRUE;
}
void In_to_Ne(char s[1002], char p[1002],int
&l)
{
InitStack1(S);
){
p[k++] = c;
c = s[++i];
}
';
)
)
')'
GetTop1(S)!= '(')
= ch; p[k++] =' ';
//将(出栈
)&&(GetTop1(S)=='(' ) )
Push1(S,c);
)&&(GetTop1(S) =='+' ||GetTop1(S)=='-'||GetTop1(S)=='*'
||GetTop1(S)=='/' ) ) {
Pop1(S,ch);
';
}
)&&(GetTop1(S) =='+' ||GetTop1(S)=='-'|| GetTop1(S)=='(' )
)
)&&(GetTop1(S) =='*' ||GetTop1(S)=='/')
)
Pop1(S,ch);
i-1;
p[k++] = ch; p[k++] =' ';
}
int main()
{
}