You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言进制转换程序do...while内switch数值叠加问题求解

问题原因

你用到的累计计算变量仅在程序启动时初始化了一次,每次循环执行转换功能时没有重置旧值,导致多次运行结果叠加:

  • 十进制转八进制时用到的o(存储八进制结果)、place(存储位数权重),第一次运行后值没有归零,第二次转换会在上次结果基础上累加
  • 八进制转十进制时用到的dec(存储十进制结果)、e(存储指数权重),同样存在旧值残留的问题
    另外还有个隐藏bug:case 'b'/'B'分支最后没有加break,执行完转换逻辑后会直接落到default分支,额外输出"Exit."。

修改方案

1. 每个转换分支执行前重置对应变量

在case 'a'/'A'开头加两行变量重置代码:

o = 0;
place = 1;

在case 'b'/'B'开头加两行变量重置代码:

dec = 0;
e = 0;

2. 给case 'b'/'B'分支补break

在八进制转十进制的逻辑结束、default分支之前加break;,避免分支穿透。

可选优化

pow是浮点数运算函数,计算整数次幂容易出现精度误差,建议替换为整数乘法逻辑,比如八进制转十进制的循环可以改成:

dec = 0;
int weight = 1;
while(n!=0)
{
    r = n%10;
    dec = dec + r * weight;
    n = n/10;
    weight *= 8;
}

修正后完整代码

#include<stdio.h>
#include<math.h>

int main(){
    int c, k, r=0, e=0, dec=0, o=0, place=1;
    long n;
    char choice;
    
   do{ 
    printf("Choices:\na. Decimal to binary and octal\nb. Octal to decimal and binary\nd. Exit.\n\n");
    printf("enter your choice:\t\n");
    
    scanf(" %c", &choice);
    
    switch(choice){
            
        case 'A':
        case 'a':
            // 重置十进制转八进制用到的变量
            o = 0;
            place = 1;
            printf("Conversion: Decimal to binary and octal.\n");
            printf("Enter number:\n");
            scanf("%ld", &n);
            printf("%ld is ", n);
           
            for (c = 28; c >= 0; c--){
                k = n >> c;
                if (k & 1)
                    printf("1");
                else
                    printf("0");
            }
            printf(" in Binary Form. \n");
                
            printf("%ld is ", n);
            while (n != 0)
            {
                o = o + (n%8)*place;
                n = n/8;
                place = place*10;
            }
            printf("%ld in Octal Form.\n\n", o);
        break;
            
        case 'b':
        case 'B':
            // 重置八进制转十进制用到的变量
            dec = 0;
            e = 0;
            printf("Conversion: Octal to decimal and binary.\n");
            printf("Enter number:\n");
            scanf("%ld", &n);
            printf("%ld is ", n);
               
            for (c =28; c >= 0; c--)
            {
                k = n >> c;
                if (k & 1)
                    printf("1");
                else
                    printf("0");
            }
            printf(" in Binary Form.\n");
                
            printf("%ld is ", n);
            while(n!=0)
            {
                r = n%10;
                dec = dec + r*(pow(8, e));
                n = n/10;
                e++;
            }
            printf("%ld in Decimal Form.\n", dec);
        // 补break避免分支穿透
        break;
        
        default:
            printf("Exit.\n\n");
        break;
    }
    
   }while(choice == 'a'|| choice =='A'|| choice == 'b'|| choice =='B');
    
    return 0;
}

内容的提问来源于stack exchange,提问作者Dexter1403

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 17:15:01