使用C的除法运算中的计算器

时间:2013-03-17 04:25:46

标签: c calculator

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

这是我的代码:

#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();
}

2 个答案:

答案 0 :(得分:3)

你有:

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

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

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

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

if (num2 == 0.0) {
    if (num1 == 0.0) {
        // zero / zero
    }
    else {
        // non-zero / zero
    }
}
else {
    // ok
}

答案 1 :(得分:1)

您的if else语句未正确排序。 总是尝试在更一般的情况之前提出最具体的案例。

因此,在您的情况下,具体情况是num1 == 0.0 && num2 == 0.0

一般情况为num2 == 0.0

此外,当num1num2都为零时,您的程序会崩溃,因为它会在这种情况下将它们分开。