Browse Source

Integrate additional mathematical functions into the scientific calculator and also a do loop to change between modes.

remotes/origin/kabrel
fdai7782 11 months ago
parent
commit
fea3474ea4
  1. 179
      src/main/c/scientificMode.c

179
src/main/c/scientificMode.c

@ -1,71 +1,118 @@
// scientificMode // scientificMode
#include "taschenrechner.h"
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <math.h>
#include "taschenrechner.h"
int scientificMode() {
double num, result;
int choice;
printf("Enter a number: ");
scanf("%lf", &num); // scan the number from user
printf("Scientific mode Options:\n");
printf("1: Square root\n");
printf("2: Exponential\n");
printf("3: Logarithm\n");
printf("4: Trigonometric\n");
scanf("%d", &choice); // user choice
switch (choice) {
case 1: // Square root
result = squareRootFunction(num);
break;
case 2: // Exponential
result = exponentialFunction(num);
break;
case 3: // Logarithm
result = logarithmFunction(num);
break;
case 4: // Trigonometric
printf("Trigonometric functions:\n");
printf("5: Sine\n");
printf("6: Cosine\n");
printf("7: Tangent\n");
int trigChoice;
scanf("%d", &trigChoice);
switch (trigChoice) {
case 5: // Sine
result = sin(num);
break;
case 6: // Cosine
result = cos(num);
break;
case 7: // Tangent
result = tan(num);
break;
default:
printf("Invalid trigonometric function choice.\n");
return -1; // Return an error code to indicate failure
}
break;
default:
printf("Invalid choice. Please try again.\n");
return -1; // Return an error code to indicate failure
}
printf("Result: %lf\n", result);
return result;
int scientificMode(){
double num, result;
int choice;
do {
printf("Enter a number: ");
scanf("%lf", &num); // scan the number from the user
printf("Scientific mode Options:\n");
printf("1: Square root\n");
printf("2: Exponential\n");
printf("3: Logarithm\n");
printf("4: Trigonometric\n");
printf("0: Exit\n");
scanf("%d", &choice); // user choice
int logChoice, trigChoice; // Move the initialization outside the loops
switch(choice) {
case 1: // Square root
result = squareRootFunction(num);
printf("Result: %lf\n", result);
break;
case 2: // Exponential
result = exponentialFunction(num);
printf("Result: %lf\n", result);
break;
case 3: // Logarithm
do {
printf("Logarithm Options:\n");
printf("1: Logarithm (base 10)\n");
printf("2: Natural Logarithm (ln)\n");
printf("3: Logarithm (base 2)\n");
printf("0: Exit Logarithm Menu\n");
scanf("%d", &logChoice);
switch(logChoice) {
case 1: // Logarithm (base 10)
result = logarithmFunction(num);
printf("Result: %lf\n", result);
break;
case 2: // Natural Logarithm (ln)
result = naturalLogarithmFunction(num);
printf("Result: %lf\n", result);
break;
case 3: // Logarithm (base 2)
result = logarithmBase2Function(num);
printf("Result: %lf\n", result);
break;
case 0: // Exit the logarithm menu
break;
default:
printf("Invalid logarithm function choice. Please try again.\n");
}
} while (logChoice != 0);
break;
case 4: // Trigonometric
do {
printf("Trigonometric functions:\n");
printf("1: Sine\n");
printf("2: Cosine\n");
printf("3: Tangent\n");
printf("0: Exit Trigonometric Menu\n");
scanf("%d", &trigChoice);
switch(trigChoice) {
case 1: // Sine
result = sineFunction(num);
printf("Result: %lf\n", result);
break;
case 2: // Cosine
result = cosineFunction(num);
printf("Result: %lf\n", result);
break;
case 3: // Tangent
result = tangentFunction(num);
printf("Result: %lf\n", result);
break;
case 0: // Exit the trigonometric menu
break;
default:
printf("Invalid trigonometric function choice. Please try again.\n");
}
} while (trigChoice != 0);
break;
case 0: // Exit the loop
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 0);
return result;
} }
Loading…
Cancel
Save