标题:为什么输入相应符号,程序不会正确执行,而是异常中止
只看楼主
gwrt
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2022-5-10
结帖率:100%
 问题点数:0 回复次数:2 
为什么输入相应符号,程序不会正确执行,而是异常中止
//基本逻辑(功能)是输入一个简单中缀表达式,生成逆波兰式,但是输入含+ —等符号程序不能正确执行,此外应该还有一些其他逻辑错误
#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
do8do8do8
Rank: 10Rank: 10Rank: 10
来 自:沙滩
等 级:贵宾
威 望:17
帖 子:366
专家分:1845
注 册:2010-7-2
得分:0 
哇 用goto!!!厉害  脑里穿针引线,记忆力超强
不敢仔细调试
---
但是输入含+ —等符号程序不能正确执行(针对此句)
数组没有初始化。
把 b[]数组均初始化为'\0'

学C语言从底层开始,学编程从问题开始,一日学会C!!!
2022-06-01 09:05
op123
Rank: 6Rank: 6
等 级:贵宾
威 望:21
帖 子:170
专家分:461
注 册:2022-6-4
得分:0 
应该是scanf("%c",ch);吧!
2022-06-21 21:25



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




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

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