From cb1e99442f50c9c73852b3363c0f89a71f79405f Mon Sep 17 00:00:00 2001 From: fdai7782 Date: Tue, 6 Feb 2024 18:47:21 +0000 Subject: [PATCH] refactoring: Split scientificMode into two functions: scientificMode (for setup) and executeScientificFunction (handling switch case and result output). Also, isolated user's calculation choice into getNumberFromUser. --- src/main/c/scientificMode.c | 74 ++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/main/c/scientificMode.c b/src/main/c/scientificMode.c index 09fbc19..31cd161 100644 --- a/src/main/c/scientificMode.c +++ b/src/main/c/scientificMode.c @@ -4,6 +4,14 @@ #include #include "taschenrechner.h" +// get the number which has to be calculated +double getNumberFromUser() { + double num; + printf("Enter a number: "); + scanf("%lf", &num); + return num; +} + // Choice for ScientificMode int getScientificModeChoice() { int choice; @@ -103,49 +111,47 @@ void executeTrigonometricFunction(double num) { } -int scientificMode(){ - - double num, result; - - int choice; - - do { - printf("Enter a number: "); - scanf("%lf", &num); // scan the number from the user - - choice = getScientificModeChoice(); +void executeScientificFunction(double num, int choice) { + double result; - int logChoice, trigChoice; // Move the initialization outside the loops + switch (choice) { + case 1: // Square root + result = squareRootFunction(num); + printf("Result: %lf\n", result); + break; - switch(choice) { + case 2: // Exponential + result = exponentialFunction(num); + printf("Result: %lf\n", result); + break; - case 1: // Square root - result = squareRootFunction(num); - printf("Result: %lf\n", result); - break; + case 3: // Logarithm + executeLogarithmFunction(num); + break; - case 2: // Exponential - result = exponentialFunction(num); - printf("Result: %lf\n", result); - break; + case 4: // Trigonometric + executeTrigonometricFunction(num); + break; - case 3: // Logarithm - executeLogarithmFunction(num); - break; + case 0: // Exit the loop + break; - case 4: // Trigonometric - executeTrigonometricFunction(num); - break; - - case 0: // Exit the loop - break; + default: + printf("Invalid choice. Please try again.\n"); + } +} - default: - printf("Invalid choice. Please try again.\n"); - } +// scientificMode +double scientificMode() { + double result; + int choice; + do { + double num = getNumberFromUser(); + choice = getScientificModeChoice(); + executeScientificFunction(num, choice); } while (choice != 0); return result; -} +} \ No newline at end of file