tefutb 2021-11-24 00:35 采纳率: 100%
浏览 53
已结题

为什么在codeblock能运行,在pta上会出现段错误

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图

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

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

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

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
typedef struct SNode *List;
struct SNode
{
    char Data;
    List Next;
};
int ss(char a,char b)
{
    if(a=='+'||a=='-')
    {
        if(b=='+'||b=='-'||b=='#'||b==')')
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
    else if(a=='*'||a=='/')
    {
        if(b=='+'||b=='-'||b=='*'||b=='/'||b=='#'||b==')')
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
    else if(a=='(')
    {
        return 1;
    }
    else if(a=='#')
    {
        if(b!='#')
        {
            return 1;
        }else
        {
            return 0;
        }

    }else
    {
        return 10;
    }
}
int main()
{
    char s[10000];
    char c;
    int cnt=1;
    cin.get(c);
    s[0]=c;
    while(c!='\n')
    {
        cin.get(c);
        s[cnt++]=c;
    }
    List L2=(List)malloc(sizeof(struct SNode));
    List p=(List)malloc(sizeof(struct SNode));
    p->Data='#';
    L2->Next=p;
    s[cnt-1]='#';
    int n=0;
    while(n<cnt&&L2->Next)
    {
        if(s[n]>='0'&&s[n]<='9')
        {
            if(n==cnt-1)
            {
                cout<<s[n];
            }else
            {
                 cout<<s[n]<<" ";
            }

        }
        else
        {
            if(L2->Next->Data=='#')
            {
                List w=(List)malloc(sizeof(struct SNode));
                w->Data=s[n];
                w->Next=L2->Next;
                L2->Next=w;
                //cout<<L2->Next->Data<<endl;;
            }
            else
            {
                char m=L2->Next->Data;
                int x=ss(m,s[n]);
                //cout<<m<<" "<<s[n]<<" "<<x<<endl;
                if(x==1)
                {
                    List d=(List)malloc(sizeof(struct SNode));
                    d->Data=s[n];
                    d->Next=L2->Next;
                    L2->Next=d;
                }
                else if(x==0)
                {
                    while(x==0&&L2->Next)
                    {
                        if(L2->Next->Data!='('&&L2->Next->Data!=')'&&L2->Next->Data!='#')
                        {
                            if(n==cnt-1)
                            {
                                cout<<L2->Next->Data;
                            }else
                            {
                                cout<<L2->Next->Data<<" ";
                            }

                        }
                        L2=L2->Next;
                        char l=L2->Next->Data;
                        if(l=='('&&s[n]==')'&&L2->Next)
                        {
                            L2=L2->Next;
                            break;
                        }
                        x=ss(l,s[n]);
                        if(x==1)
                        {
                           List v=(List)malloc(sizeof(struct SNode));
                           v->Data=s[n];
                           v->Next=L2->Next;
                           L2->Next=v;
                        }
                    }
                }
            }
        }
        n++;
    }
}


pta上一直出现段错误,但 在codeblock能运行。
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月2日
    • 修改了问题 11月24日
    • 创建了问题 11月24日

    悬赏问题

    • ¥200 csgo2的viewmatrix值是否还有别的获取方式
    • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
    • ¥15 请把下列每一行代码完整地读懂并注释出来
    • ¥15 pycharm运行main文件,显示没有conda环境
    • ¥15 易优eyoucms关于二级栏目调用的问题
    • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
    • ¥15 为什么eclipse不能再下载了?
    • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
    • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
    • ¥15 特定网页无法访问,已排除网页问题