Browse Source
Integrate additional mathematical functions into the scientific calculator and also a do loop to change between modes.
remotes/origin/kabrel
Integrate additional mathematical functions into the scientific calculator and also a do loop to change between modes.
remotes/origin/kabrel
fdai7782
11 months ago
1 changed files with 113 additions and 66 deletions
@ -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() { |
|
||||
|
int scientificMode(){ |
||||
|
|
||||
double num, result; |
double num, result; |
||||
|
|
||||
int choice; |
int choice; |
||||
|
|
||||
|
do { |
||||
printf("Enter a number: "); |
printf("Enter a number: "); |
||||
scanf("%lf", &num); // scan the number from user |
|
||||
|
scanf("%lf", &num); // scan the number from the user |
||||
|
|
||||
printf("Scientific mode Options:\n"); |
printf("Scientific mode Options:\n"); |
||||
printf("1: Square root\n"); |
printf("1: Square root\n"); |
||||
printf("2: Exponential\n"); |
printf("2: Exponential\n"); |
||||
printf("3: Logarithm\n"); |
printf("3: Logarithm\n"); |
||||
printf("4: Trigonometric\n"); |
printf("4: Trigonometric\n"); |
||||
|
printf("0: Exit\n"); |
||||
scanf("%d", &choice); // user choice |
scanf("%d", &choice); // user choice |
||||
|
|
||||
switch (choice) { |
|
||||
|
int logChoice, trigChoice; // Move the initialization outside the loops |
||||
|
|
||||
|
switch(choice) { |
||||
|
|
||||
case 1: // Square root |
case 1: // Square root |
||||
result = squareRootFunction(num); |
result = squareRootFunction(num); |
||||
|
printf("Result: %lf\n", result); |
||||
break; |
break; |
||||
|
|
||||
case 2: // Exponential |
case 2: // Exponential |
||||
result = exponentialFunction(num); |
result = exponentialFunction(num); |
||||
|
printf("Result: %lf\n", result); |
||||
break; |
break; |
||||
|
|
||||
case 3: // Logarithm |
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); |
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; |
break; |
||||
|
|
||||
case 4: // Trigonometric |
case 4: // Trigonometric |
||||
|
do { |
||||
printf("Trigonometric functions:\n"); |
printf("Trigonometric functions:\n"); |
||||
printf("5: Sine\n"); |
|
||||
printf("6: Cosine\n"); |
|
||||
printf("7: Tangent\n"); |
|
||||
int trigChoice; |
|
||||
|
printf("1: Sine\n"); |
||||
|
printf("2: Cosine\n"); |
||||
|
printf("3: Tangent\n"); |
||||
|
printf("0: Exit Trigonometric Menu\n"); |
||||
scanf("%d", &trigChoice); |
scanf("%d", &trigChoice); |
||||
|
|
||||
switch (trigChoice) { |
|
||||
case 5: // Sine |
|
||||
result = sin(num); |
|
||||
|
switch(trigChoice) { |
||||
|
case 1: // Sine |
||||
|
result = sineFunction(num); |
||||
|
printf("Result: %lf\n", result); |
||||
break; |
break; |
||||
|
|
||||
case 6: // Cosine |
|
||||
result = cos(num); |
|
||||
|
case 2: // Cosine |
||||
|
result = cosineFunction(num); |
||||
|
printf("Result: %lf\n", result); |
||||
break; |
break; |
||||
|
|
||||
case 7: // Tangent |
|
||||
result = tan(num); |
|
||||
|
case 3: // Tangent |
||||
|
result = tangentFunction(num); |
||||
|
printf("Result: %lf\n", result); |
||||
|
break; |
||||
|
|
||||
|
case 0: // Exit the trigonometric menu |
||||
break; |
break; |
||||
|
|
||||
default: |
default: |
||||
printf("Invalid trigonometric function choice.\n"); |
|
||||
return -1; // Return an error code to indicate failure |
|
||||
|
printf("Invalid trigonometric function choice. Please try again.\n"); |
||||
} |
} |
||||
|
} while (trigChoice != 0); |
||||
|
break; |
||||
|
|
||||
|
case 0: // Exit the loop |
||||
break; |
break; |
||||
|
|
||||
default: |
default: |
||||
printf("Invalid choice. Please try again.\n"); |
printf("Invalid choice. Please try again.\n"); |
||||
return -1; // Return an error code to indicate failure |
|
||||
|
|
||||
} |
} |
||||
|
|
||||
printf("Result: %lf\n", result); |
|
||||
|
} while (choice != 0); |
||||
|
|
||||
return result; |
return result; |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue