标题:为什么输入相应符号,程序不会正确执行,而是异常中止
取消只看楼主
gwrt
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2022-5-10
结帖率:100%
 问题点数:0 回复次数:0 
为什么输入相应符号,程序不会正确执行,而是异常中止
//基本逻辑(功能)是输入一个简单中缀表达式,生成逆波兰式,但是输入含+ —等符号程序不能正确执行,此外应该还有一些其他逻辑错误
#include<stdio.h>
#include<string.h>
#define M 100
int main()
{
    char a[M],b[M],c[M],ch;
    int ai,bi,ci,x,m,n;
    ai=bi=ci=x=m=n=0;
    printf("输入中缀表达式:");
    scanf("%s",a);
  y:  ch=a[ai];ai++;
  if(ch!='\0'){
      if(ch=='+' || ch=='-'||ch=='*' || ch=='/'||ch=='(' || ch==')'){
          w:if(b[0]=='\0'){
              b[bi]=ch;
              bi++;
              goto y;
              
          }
          else{
              if(ch=='('){
                  m++;
                  n=ai;
                  b[bi]=ch;
                  bi++;
                  
              }
              else{
                  if(ch==')'){
                      if(m>0){
                          while(b[bi-1]!='('){
                          c[ci]=b[bi-1];
                          b[bi-1]='\0';
                          ci++;
                          bi--;
                              
                              
                          }
                          b[bi-1]='\0';
                          bi--;
                          goto y;
                          
                      }
                      else{
                          x++;
                          goto z;
                      }
                  }
                  else{
                      if((ch=='*'||ch=='/')&&(b[bi-1]=='+'||b[bi-1]=='-')){
                          b[bi]=ch;
                          bi++;
                          
                      }
                      else{
                          c[ci]=b[bi-1];
                          b[bi-1]='\0';
                          ci++;
                          bi--;
                          goto w;
                          
                          
                          
                      }
                     
                  }
              }
          }
         
      }
      else{
          c[ci]=ch;
          ci++;
          goto y;
      }
      
  }
    else{
        if(b[0]!='\0'){
            if(m>0){
                x++;
                n++;
                printf("输入错误,错误位是第%d位",n);
               
            }
            else{
                while(bi!=0){
                    c[ci]=b[bi-1];
                    ci++;
                    bi--;
                    
                }
               
            }
        }
        
    }
   
   
   
z:    if(x>0){
    printf("输入错误,错误位是第%d位",ai);
   
}
else{
    printf("输入表达式的逆波兰式为:%s",c);
     
}
   
   
   
   
   
   
   
    return 0;
}
                                
搜索更多相关主题的帖子: i++ goto || 输入 错误 
2022-05-18 16:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-509114-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.351250 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved