|
@ -0,0 +1,158 @@ |
|
|
|
|
|
#include <stdio.h> |
|
|
|
|
|
#define M_PI 3.14159265358979323 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Function prototypes |
|
|
|
|
|
void displayMenu(); |
|
|
|
|
|
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 combination(int n, int r); |
|
|
|
|
|
double pow(double num1, double num2); |
|
|
|
|
|
|
|
|
|
|
|
int calculator() { |
|
|
|
|
|
int choice; |
|
|
|
|
|
double num1, num2, result; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
|
displayMenu(); |
|
|
|
|
|
printf("Enter your choice: "); |
|
|
|
|
|
scanf("%d", &choice); |
|
|
|
|
|
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; |
|
|
|
|
|
} |
|
|
|
|
|
if (choice != 4 || (choice == 4 && num2 != 0)) { |
|
|
|
|
|
result = performOperation(choice, num1, num2); |
|
|
|
|
|
printf("Result: %.2lf\n", result); |
|
|
|
|
|
} else { |
|
|
|
|
|
printf("Error: Division by zero\n"); |
|
|
|
|
|
} |
|
|
|
|
|
} else if (choice != 20) { |
|
|
|
|
|
printf("Invalid choice\n"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 performOperation(int choice, double num1, double num2) { |
|
|
|
|
|
switch (choice) { |
|
|
|
|
|
case 1: |
|
|
|
|
|
return num1 + num2; |
|
|
|
|
|
case 2: |
|
|
|
|
|
return num1 - num2; |
|
|
|
|
|
case 3: |
|
|
|
|
|
return num1 * num2; |
|
|
|
|
|
case 4: |
|
|
|
|
|
return num1 / num2; |
|
|
|
|
|
case 5: |
|
|
|
|
|
return pow(num1, num2); |
|
|
|
|
|
case 6: |
|
|
|
|
|
return 0;// fmod(num1, num2); |
|
|
|
|
|
case 7: |
|
|
|
|
|
return 0;// log(num1); |
|
|
|
|
|
case 8: |
|
|
|
|
|
return pow(num1, num2); |
|
|
|
|
|
case 9: |
|
|
|
|
|
return 0;// sqrt(num1); |
|
|
|
|
|
case 10: |
|
|
|
|
|
return num1 * num1; |
|
|
|
|
|
case 11: |
|
|
|
|
|
return num1 * num1 * num1; |
|
|
|
|
|
case 12: |
|
|
|
|
|
return 0;// cbrt(num1); |
|
|
|
|
|
case 13: |
|
|
|
|
|
return factorial(num1); |
|
|
|
|
|
case 14: |
|
|
|
|
|
return pow(num1, num2); |
|
|
|
|
|
case 15: |
|
|
|
|
|
return 0;// sin(num1 * M_PI / 180.0); |
|
|
|
|
|
case 16: |
|
|
|
|
|
return 0;// cos(num1 * M_PI / 180.0); |
|
|
|
|
|
case 17: |
|
|
|
|
|
return 0;// tan(num1 * M_PI / 180.0); |
|
|
|
|
|
|
|
|
|
|
|
case 18: |
|
|
|
|
|
return permutation((int)num1, (int)num2); |
|
|
|
|
|
case 19: |
|
|
|
|
|
return combination((int)num1, (int)num2); |
|
|
|
|
|
default: |
|
|
|
|
|
return 0.0; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
double getDoubleInput(const char *message) { |
|
|
|
|
|
double input; |
|
|
|
|
|
printf("%s", message); |
|
|
|
|
|
scanf("%lf", &input); |
|
|
|
|
|
return input; |
|
|
|
|
|
} |
|
|
|
|
|
double factorial(double num) { |
|
|
|
|
|
if (num == 0) { |
|
|
|
|
|
return 1; |
|
|
|
|
|
} else { |
|
|
|
|
|
return num * factorial(num - 1); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
double permutation(int n, int r) { |
|
|
|
|
|
double result = 1; |
|
|
|
|
|
for (int i = 0; i < r; i++) { |
|
|
|
|
|
result *= (n - i); |
|
|
|
|
|
} |
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
double combination(int n, int r) { |
|
|
|
|
|
return permutation(n, r) / factorial(r); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int getIntInput(const char *message) { |
|
|
|
|
|
int input; |
|
|
|
|
|
printf("%s", message); |
|
|
|
|
|
scanf("%d", &input); |
|
|
|
|
|
return input; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
double pow(double num1, double num2){ |
|
|
|
|
|
double product = 1; |
|
|
|
|
|
for(int i = 0; i < num2; i++){ |
|
|
|
|
|
product *= num1; |
|
|
|
|
|
} |
|
|
|
|
|
return product; |
|
|
|
|
|
} |