2021-10-047-1 表达式转换

7-1 表达式转换 (25 分)
算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。

输入格式:
输入在一行中给出不含空格的中缀表达式,可包含+、-、*、\以及左右括号(),表达式不超过20个字符。

输出格式:
在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。

输入样例:
2+3*(7-4)+8/4
结尾无空行
输出样例:
2 3 7 4 - * + 8 4 / +
结尾无空行

在这里插入代码片
#include<iostream>
using namespace std;
typedef struct
{
	char* base;
	char* top;
	int stacksize;
}Sqstack;
void InitStack(Sqstack& s)
{
	s.base = new char[22];
	s.top = s.base;
	s.stacksize = 20;
}
int Push(Sqstack& s, char c)
{
	*s.top++ = c;
	return 1;
}
int Pop(Sqstack& s, char& c)
{
	c = *--s.top;
	if (s.top == s.base)
		return 0;
	return 1;
}
int N(char c)
{
	switch (c)
	{
	case '+':
	case '-':
		return 1;
	case '*':
	case '/':
		return 2;
	case '(':
		return 0;
	case ')':
		return 3;
	}
}
int Empty(Sqstack &s)
{
    if( s.top == s.base)
        return 1;
    return 0;
}
char GetTop(Sqstack &s)
{
     if(s.top != s.base)
        return *(s.top - 1);
}
int GetLength(char *s)
{
    int i;
    for(i = 0; s[i] != '\0'; i++)
        ;
    return i;
}
int main()
{
    Sqstack st;
    InitStack(st);
    char s[25];
    scanf("%s",s);
    int length = GetLength(s),i;
    bool firstprint = true;
    for(i = 0; i<length; i++)
    {
        if((s[i]>='0'&&s[i]<='9')||s[i] == '.'||((i == 0||s[i-1] == '(')&&(s[i] == '+'||s[i] == '-')))//数字直接输出
        {
            if(!firstprint)
                cout<<" ";
             if(s[i]!= '+')
                cout<<s[i];

                while((s[i+1]>='0'&&s[i+1]<='9')||s[i+1] == '.')
                {
                      i++;
                    cout<<s[i];
                }

            firstprint = false;

        }
        else
        {
            if(s[i] == '(')//
                Push(st,s[i]);

            else if(Empty(st)== 1||N(s[i])>N(GetTop(st)))//第一次输入操作符or输入的操作符优先级大于栈顶操作符
            {
                if(s[i] == ')')
                {
                    while(GetTop(st) != '('&& Empty(st) == 0 )
                    {
                        char x;
                        Pop(st,x);
                        cout<<' '<<x;
                    }
                    char x;
                    Pop(st,x);
                }
                else
                Push(st,s[i]);
            }
           else
            {
                    while(Empty(st)==0&&  (N(GetTop(st))>=N(s[i])) )
                {
                    char x;
                    Pop(st,x);
                    cout<<" "<<x;
                }
                      Push(st,s[i]);

            }
        }
    }
    	while(st.top!=st.base)
    {
        char x;
        Pop(st, x);
        if(x != '('&& x!= ')')
        cout << " " << x;
    }
    return 0;

}



在这里插入图片描述
这道题的段错误可让我好找啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值