算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。
输入格式:
输入在一行中给出不含空格的中缀表达式,可包含+、-、*、\以及左右括号(),表达式不超过20个字符。
输出格式:
在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。
输入样例:
2+3*(7-4)+8/4
输出样例:
2 3 7 4 - * + 8 4 / +
// 表达式转换
#include <iostream>
#include <map>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
using namespace std;
typedef char ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int top;
}SqStack;
// 初始化堆栈
SqStack *InitStack()
{
SqStack *st;
st = (SqStack *)malloc(sizeof(SqStack)); // 申请空间
st->top = -1;
return st;
}
// 判定堆栈是否为空(C语言没有bool类型,用int类型代替bool类型)
int EmptyStack(SqStack *st)
{
return (st->top == -1);
}
// 判定堆栈是否满了
int FullStack(SqStack *st)
{
return (st->top == MAXSIZE);
}
// 入栈
int PushStack(SqStack *st, ElemType x)
{
int flag = 0;
flag = FullStack