Browse Source

refactoring: if-statement1

remotes/origin/Ulrich
Ulriche Nguefack 11 months ago
parent
commit
4fa18bf349
  1. 137
      src/main/c/Calculator/calculator.c

137
src/main/c/Calculator/calculator.c

@ -19,132 +19,19 @@ double combination(int n, int r);
displayMenu(); displayMenu();
printf("Enter your choice: "); printf("Enter your choice: ");
scanf("%d", &choice); scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = add(num1, num2);
printf("Result: %lf\n", result);
break;
case 2:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = subtract(num1, num2);
printf("Result: %lf\n", result);
break;
case 3:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = multiply(num1, num2);
printf("Result: %lf\n", result);
break;
case 4:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
if (num2 == 0) {
printf("Error: Division by zero\n");
} else {
result = divide(num1, num2);
printf("Result: %lf\n", result);
}
break;
case 5:
printf("Enter base and exponent: ");
scanf("%lf %lf", &num1, &num2);
result = exponent(num1, num2);
printf("Result: %lf\n", result);
break;
case 6:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = modulus(num1, num2);
printf("Result: %lf\n", result);
break;
case 7:
printf("Enter a number: ");
scanf("%lf", &num1);
result = logarithm(num1);
printf("Result: %lf\n", result);
break;
case 8:
printf("Enter base and exponent: ");
scanf("%lf %lf", &num1, &num2);
result = potential(num1, num2);
printf("Result: %lf\n", result);
break;
case 9:
printf("Enter a number: ");
scanf("%lf", &num1);
result = squareRoot(num1);
printf("Result: %lf\n", result);
break;
case 10:
printf("Enter a number: ");
scanf("%lf", &num1);
result = square(num1);
printf("Result: %lf\n", result);
break;
case 11:
printf("Enter angle in degrees: ");
scanf("%lf", &angle);
result = sine(angle);
printf("Sine of %.2lf degrees: %lf\n", angle, result);
break;
case 12:
printf("Enter angle in degrees: ");
scanf("%lf", &angle);
result = cosine(angle);
printf("Cosine of %.2lf degrees: %lf\n", angle, result);
break;
case 13:
printf("Enter angle in degrees: ");
scanf("%lf", &angle);
result = tangent(angle);
printf("Tangent of %.2lf degrees: %lf\n", angle, result);
break;
case 14:
printf("Enter n and r: ");
scanf("%d %d", &n, &r);
result = permutation(n, r);
printf("Permutation of %d and %d: %lf\n", n, r, result);
break;
case 15:
printf("Enter n and r: ");
scanf("%d %d", &n, &r);
result = combination(n, r);
printf("Combination of %d and %d: %lf\n", n, r, result);
break;
case 16:
printf("Enter a number: ");
scanf("%lf", &num1);
result = cube(num1);
printf("Result: %lf\n", result);
break;
case 17:
printf("Enter a number: ");
scanf("%lf", &num1);
result = cubeRoot(num1);
printf("Result: %lf\n", result);
break;
case 18:
printf("Enter a number: ");
scanf("%lf", &num1);
result = factorial(num1);
printf("Result: %lf\n", result);
break;
case 19:
printf("Enter base and exponent: ");
scanf("%lf %lf", &num1, &num2);
result = power(num1, num2);
printf("Result: %lf\n", result);
break;
case 20:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
if (choice >= 1 && choice <= 19) {
if (choice >= 9 && choice <= 17) {
num1 = getDoubleInput("Enter a number: ");
} else if (choice >= 1 && choice <= 8) {
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
} else if (choice == 18 || choice == 19) {
int n = getIntInput("Enter n: ");
int r = getIntInput("Enter r: ");
num1 = n;
num2 = r;
} }
}
while (choice != 20); while (choice != 20);
return 0; return 0;

Loading…
Cancel
Save