-1

我正在使用 C 语言制作计算器。但问题是我陷入了分裂。我希望当我输入 0 除以 0 时,它将打印 undefined 并且如果一个数字除以零将打印不能除以零(这似乎我明白了)。

这是我的代码:

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

main(){

    float num1, num2, total;
    int a;
    char choice;
    clrscr();
    printf("What operation you want to perform?\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n");
    scanf("%d", &a);
    switch(a)  {
        case 1:  //Addition
            printf("Addition\n Enter two numbers to be added\n");
            scanf("%f%f",&num1, &num2);
            total=num1 + num2;
            printf("\nThe total is: %f + %f = %f\n", num1, num2, total);
            printf("\nDo want to continue? Y or N:  ");
            scanf("%s", &choice);
            if(choice=='y'||choice=='Y')
                main();
            else
                exit(1);
            break;
        case 2: //Subtraction
            printf("Subtraction\n Enter two numbers to be subtract\n");
            scanf("%f%f", &num1, &num2);
            total=num1 - num2;
            printf("\nThe Difference is: %f + %f = %f\n", num1, num2, total);
            printf("\nDo you want to continue? Y or N:  ");
            scanf("%s", &choice);
            if(choice=='y'||choice=='Y')
                main();
            else
                exit(1);
            break;
        case 3: //Multiplication
            printf("Multiplication\n Enter two numbers to be multiplied\n");
            scanf("%f%f",num1, num2);
            total=num1*num2;
            printf("\nThe product is: %f * %f = %f\n", num1, num2, total);
            printf("\nDo you wish to continue? Y or N:  ");
            scanf("%s", &choice);
            if(choice=='y'||choice=='Y')
                main();
            else
                exit(1);
            break;

        case 4: //Division
            abcd:
            printf("Division\n Enter two numbers to be divide\n");
            scanf("%f%f",&num1, &num2);
                if(num2==0.0)
                      {    printf("\n %f cannot be divided by zero\n\n", num1);    // ;(
                     goto abcd;}
                else if(num1==0.0 && num2==0.0)                    //This is my problem here.
                    {printf("\nIts Undefined. 0 / 0\n\n ");
                    }
            total=num1/num2;
            printf("\nThe qoutient is %f / %f = %f\n", num1, num2, total);
            printf("\nDo you want more? Y or N:  ");
            scanf("%s", &choice);
            if(choice=='y'||choice=='Y')
                main();
            else
                exit(1);
            break;
        default:
            printf("Can you add letters? LOL only numbers");
            printf("\nDo you want to continue? Y/N ");
            scanf("%s", &choice);
            if(choice=='y'||choice=='Y')
                main();
            else
                exit(1);
            break;
    }

    getch();
}
4

2 回答 2

3

你有:

if(num2==0.0)
    // division by zero
else if(num1==0.0 && num2==0.0)
    // zero/zero is undefined

问题是如果两个数字都是 0.0,它会被第一个测试捕获,而第二个永远不会执行。

您需要先测试两个数字是否为零,然后检查第二个数字是否为零。

或者你可以结合这两个测试:

if (num2 == 0.0) {
    if (num1 == 0.0) {
        // zero / zero
    }
    else {
        // non-zero / zero
    }
}
else {
    // ok
}
于 2013-03-17T04:33:02.293 回答
1

您的if else陈述没有正确排序。始终尝试将最具体的案例放在更一般的案例之前。

所以在你的情况下,具体情况是什么时候num1 == 0.0 && num2 == 0.0

而一般情况是num2 == 0.0

num1此外,当两者都为零时,您的程序会崩溃num2,因为在这种情况下无论如何都会将它们分开。

于 2013-03-17T04:34:22.690 回答