Browse Source

refactoring: function-prototypes

remotes/origin/Ulrich
Ulriche Nguefack 11 months ago
parent
commit
9c55ecbe8d
  1. 465
      src/main/c/Calculator/calculator.c
  2. BIN
      src/main/c/Calculator/calculator.exe

465
src/main/c/Calculator/calculator.c

@ -1,264 +1,251 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
// Function prototypes
// Function prototypes
void displayMenu(); void displayMenu();
double add(double num1, double num2);
double subtract(double num1, double num2);
double multiply(double num1, double num2);
double divide(double num1, double num2);
double exponent(double base, double exponent);
double modulus(double num1, double num2);
double logarithm(double num);
double potential(double base, double exponent);
double squareRoot(double num);
double square(double num);
double sine(double angle);
double cosine(double angle);
double tangent(double angle);
double performOperation(int choice, double num1, double num2);
double getDoubleInput(const char *message);
int getIntInput(const char *message);
double factorial(double num);
double permutation(int n, int r); double permutation(int n, int r);
double combination(int n, int r); double combination(int n, int r);
double factorial(double num);
double power(double base, double exponent);
double cube(double num);
double cubeRoot(double num);
int main() {
double num1, num2, result, angle;
int choice,r,n;
do {
displayMenu();
printf("Enter your 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);
int main() {
double num1, num2, result, angle;
int choice,r,n;
do {
displayMenu();
printf("Enter your 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); 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");
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");
}
} }
}
while (choice != 20);
while (choice != 20);
return 0;
}
// Function definitions
void displayMenu() {
printf("\nAdvaced Calculator\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("5. Exponentiation\n");
printf("6. Modulus\n");
printf("7. Logarithm\n");
printf("8. Potential\n");
printf("9. Square Root\n");
printf("10. Square\n");
printf("11. Cube\n");
printf("12. Cube Root\n");
printf("13. Factorial\n");
printf("14. Power\n");
printf("15. Sine\n");
printf("16. Cosine\n");
printf("17. Tangent\n");
printf("18. Permutation\n");
printf("19. Combination\n");
printf("20. Exit\n");
}
double add(double num1, double num2) {
return num1 + num2;
}
return 0;
}
// Function definitions
void displayMenu() {
printf("\nAdvaced Calculator\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("5. Exponentiation\n");
printf("6. Modulus\n");
printf("7. Logarithm\n");
printf("8. Potential\n");
printf("9. Square Root\n");
printf("10. Square\n");
printf("11. Cube\n");
printf("12. Cube Root\n");
printf("13. Factorial\n");
printf("14. Power\n");
printf("15. Sine\n");
printf("16. Cosine\n");
printf("17. Tangent\n");
printf("18. Permutation\n");
printf("19. Combination\n");
printf("20. Exit\n");
}
double add(double num1, double num2) {
return num1 + num2;
}
double subtract(double num1, double num2) {
return num1 - num2;
}
double multiply(double num1, double num2) {
return num1 * num2;
}
double divide(double num1, double num2) {
return num1 / num2;
}
double subtract(double num1, double num2) {
return num1 - num2;
}
double multiply(double num1, double num2) {
return num1 * num2;
}
double divide(double num1, double num2) {
return num1 / num2;
}
double exponent(double base, double exponent) {
return pow(base, exponent);
}
double exponent(double base, double exponent) {
return pow(base, exponent);
}
double modulus(double num1, double num2) {
return fmod(num1, num2);
}
double modulus(double num1, double num2) {
return fmod(num1, num2);
}
double logarithm(double num) {
return log(num);
}
double logarithm(double num) {
return log(num);
}
double potential(double base, double exponent) {
return pow(base, exponent);
}
double squareRoot(double num) {
return sqrt(num);
}
double potential(double base, double exponent) {
return pow(base, exponent);
}
double squareRoot(double num) {
return sqrt(num);
}
double square(double num) {
return num * num;
}
double square(double num) {
return num * num;
}
double cube(double num) {
return num * num * num;
}
double cube(double num) {
return num * num * num;
}
double cubeRoot(double num) {
return cbrt(num);
}double factorial(double num) {
if (num == 0) {
return 1;
} else {
return num * factorial(num - 1);
double cubeRoot(double num) {
return cbrt(num);
}double factorial(double num) {
if (num == 0) {
return 1;
} else {
return num * factorial(num - 1);
}
} }
}
double power(double base, double exponent) {
return pow(base, exponent);
}
double power(double base, double exponent) {
return pow(base, exponent);
}
double sine(double angle) {
return sin(angle * M_PI / 180.0);
}
double sine(double angle) {
return sin(angle * M_PI / 180.0);
}
double cosine(double angle) {
return cos(angle * M_PI / 180.0);
}
double cosine(double angle) {
return cos(angle * M_PI / 180.0);
}
double tangent(double angle) {
return tan(angle * M_PI / 180.0);
}
double permutation(int n, int r) {
int result = 1;
for (int i = 0; i < r; i++) {
result *= (n - i);
double tangent(double angle) {
return tan(angle * M_PI / 180.0);
}
double permutation(int n, int r) {
int result = 1;
for (int i = 0; i < r; i++) {
result *= (n - i);
}
return result;
} }
return result;
}
double combination(int n, int r) {
return permutation(n, r) / factorial(r);
}
double combination(int n, int r) {
return permutation(n, r) / factorial(r);
}

BIN
src/main/c/Calculator/calculator.exe

Loading…
Cancel
Save