C言語で電卓を作っています。しかし、問題は、私が分裂で立ち往生していることです。0 を 0 で割った値を入力すると undefined が出力され、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();
}