我正在使用 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();
}