NGQH123 2022-03-15 19:15 采纳率: 33.3%
浏览 26

表达式转换{中缀式转换为后缀式)

问题:
欠嵌套括号格式错误

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxSize 25
typedef struct {
    char elem[maxSize];
    int top;//top 指向当前元素
}SeqStack;
SeqStack* initStack ( ) {
    SeqStack* S0=NULL;
    S0=(SeqStack*)malloc(sizeof(SeqStack));
    S0->top =0;
    return S0;
}
void Push (SeqStack *S0, char x ) {
    S0->elem[S0->top] = x;
    S0->top++;
}
char Pop (SeqStack *S0 ) {
    char x;
    S0->top--;
    x = S0->elem[S0->top];
    return x;
}
char getTop (SeqStack *S0 ) {
    return S0->elem[S0->top-1];
}
int empty(SeqStack *S0){
    if(S0->top==0)
        return 1;
    else
        return 0;
}
int main(){
    char a[maxSize];
    char m;
    int n=0;
    gets(a);
    int len;
    len=strlen(a);
    SeqStack* S;
    S=initStack ( );
    SeqStack* fuhao;
    fuhao=initStack ( );
    int index=0;
    if(a[0]=='-') {
        printf("-");
        index = 1;
    }
    for(int i=index;i<len;i++){
        if(a[i]>='0'&&a[i]<='9'){
            if(i!=index)
                Push(S,' ');
            Push(S,a[i]);
            while((a[i+1]>='0'&&a[i+1]<='9')||a[i+1]=='.'){
                i++;
                Push(S,a[i]);
            }
            continue;
        }
        else if(a[i]=='('){
            Push(fuhao,a[i]);
            continue;
        }
        else if(a[i]==')'){
            while(getTop(fuhao)!='(') {
                Push(S,' ');
                Push(S,Pop(fuhao));
            }
            Pop(fuhao);
        }
            //+
        else if(a[i]=='+'){
            if(a[i-1]=='('&&(a[i+1]>='0'&&a[i+1]<='9'))
                continue;
            else{
                if(getTop(fuhao)=='(') {
                    Push(fuhao, a[i]);
                    continue;
                }
                else {
                    while (getTop(fuhao) != '('&&!empty(fuhao)){
                        Push(S,' ');
                        Push(S,Pop(fuhao));
                    }
                    Push(fuhao, a[i]);
                    continue;
                }
            }
        }
            //-
        else if(a[i]=='-'){
            if(a[i-1]=='('&&(a[i+1]>='0'&&a[i+1]<='9')) {
                Push(S,' ');
                Push(S, a[i]);
                i++;
                Push(S,a[i]);
                continue;
            }
            else{
                if(getTop(fuhao)=='(') {
                    Push(fuhao, a[i]);
                    continue;
                }
                else {
                    while (getTop(fuhao) != '('&&!empty(fuhao)){
                        Push(S,' ');
                        Push(S,Pop(fuhao));
                    }
                    Push(fuhao, a[i]);
                    continue;
                }
            }
        }
            //*/
        else if(a[i]=='*'||a[i]=='/'){
            if(getTop(fuhao)=='('||getTop(fuhao)=='+'||getTop(fuhao)=='-')
                Push(fuhao,a[i]);
            else{
                while(getTop(fuhao)!='(' && getTop(fuhao)!='+' && getTop(fuhao)!='-'&&!empty(fuhao)) {
                    Push(S,' ');
                    Push(S,Pop(fuhao));
                }
                Push(fuhao, a[i]);
            }
        }
    }
    while(!empty(fuhao)) {
        Push(S,' ');
        Push(S, Pop(fuhao));
    }
    SeqStack* S1;
    S1=initStack ( );
    while(!empty(S))
        Push(S1,Pop(S));
    while(!empty(S1)){
        printf("%c",Pop(S1));
    }
    return 0;
}


  • 写回答

1条回答 默认 最新

  • 慕雪华年 C/C++领域新星创作者 2022-03-15 19:32
    关注

    麻烦用代码块提交代码,你这缩进都没了让别人怎么帮你回答问题……

    img

    评论

报告相同问题?

问题事件

  • 修改了问题 3月15日
  • 创建了问题 3月15日

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计