基本C,输出不正确

时间:2018-06-19 05:12:53

标签: c arrays

英语不是我的母语,我实际上说的很糟糕,所以我不能对该程序作出解释。

但这是一个示例输出:

Insert the 1st number: 1234                                                                                                                                                                   
Insert the 2nd number: 1111                                                                                                                                                                   
Select an operation (1=addition; 2=subtraction; 3=multiplication; 4=division): 1                                                                                                              
|0||0||0||0||0||0||1||2||3||4|                                                                                                                                                                
+                                                                                                                                                                                             
|0||0||0||0||0||0||1||1||1||1|                                                                                                                                                                
=                                                                                                                                                                                             
|0||0||0||0||0||0||0||0||0||0||0||0||0||0||0||0||2||3||4||0|

最后一个位置错了,应该是5。

对于经验更丰富的程序员来说,我觉得解决它很简单。

以下是代码:

#include <stdio.h> 
int n[10], n2[10], num, nun, i=9, j=9, sv=0, on, res[4][20], opc[4][20];
main(){
    for (i=0; i<20; i++){
        for (j=0; j<4; j++){
            opc[j][i] = 0;
            res[j][i] = 0;
        }
    }
    for (j=0; j<10; j++){
        n[j] = 0;
        n2[j] = 0;
    }
    i=9; j=9;
    printf("Insert the 1st number: ");
    scanf("%d", &num);
    sv = num;
    while (num != 0){
        n[i] = num%10;
        num = num/10; 
        i--;
    }
    num = sv;
    printf("Insert the 2nd number: ");
    scanf("%d", &nun);
    sv = nun;
    while (nun != 0){
        n2[j] = nun%10;
        nun = nun/10; 
        j--;
    }
    nun = sv;
    printf("Select an operation (1=addition; 2=subtraction; 3=multiplication; 4=division): ");
    scanf("%d", &on);
    while((on>4)||(on<0)){
        printf("Try again: ");
        scanf("%d", &on);
    }
    if (on==1){
        for (i=9; i>=0; i--){
            if ((n[i] + n2[i] + opc[1][10+i]) <= 9){
                opc[1][10+i] += (n[i] + n2[i]);
            }
            if ((n[i] + n2[i] + opc[1][10+i]) > 9){
                opc[1][10+i] += (n[i] + n2[i])-10;
                opc[1][9+i] = (n[i] + n2[i])/10;
            }
        }
        for (i=0; i<=9; i++){
            printf("|%d|", n[i]);
        }
        printf("\n+\n");
        for (i=0; i<=9; i++){
            printf("|%d|", n2[i]);
        }
        printf("\n=\n");
        for (i=0; i<=19; i++){
            printf("|%d|", opc[1][i]);
        }           
    }
}

抱歉我的英语。

1 个答案:

答案 0 :(得分:1)

if块的后续if块:

        if ((n[i] + n2[i] + opc[1][10+i]) <= 9){
            opc[1][10+i] += (n[i] + n2[i]);
        }

应该在else块中,因为在第一个if块中,如果condition的计算结果为true,则表示您正在修改opc[1][10+i]并在连续的if块中检查相同的条件> 9的修改后的opc[1][10+i]值 如果条件((n[i] + n2[i] + opc[1][10+i]) <= 9)计算为false,则只有控件应转到此if ((n[i] + n2[i] + opc[1][10+i]) > 9)块,但如果它的计算结果为true,则控件不应转到下一个if块。

事实上,您不需要进行此项检查 - ((n[i] + n2[i] + opc[1][10+i]) > 9)。只需将第二个if块的语句放在第一个else中,就像这样:

        if ((n[i] + n2[i] + opc[1][10+i]) <= 9){
            opc[1][10+i] += (n[i] + n2[i]);
        } else {
            opc[1][10+i] += (n[i] + n2[i])-10;
            opc[1][9+i] = (n[i] + n2[i])/10;
        }

但是这仍然可能无法提供正确的输出,例如对于输入5545,这不起作用。原因是在opc[1][9+i]循环的连续迭代中未能正确处理结转for。感谢user3386109指出这一点。你可以这样做:

        if ((n[i] + n2[i] + opc[1][10+i]) <= 9){
            opc[1][10+i] += (n[i] + n2[i]);
        } else {
            opc[1][9+i] = (n[i] + n2[i] + opc[1][10+i])/10;
            opc[1][10+i] += (n[i] + n2[i])-10;
        }