From a3cbfd7de1d0f83a90f0822b5abb162180971f1f Mon Sep 17 00:00:00 2001 From: Enrico Schellenberger Date: Sat, 3 Feb 2024 16:59:42 +0100 Subject: [PATCH] added BasicMode.c and refractoring to testForOperator/taschenrechner.h --- src/main/c/BasicMode.c | 64 ++++++++++++++++--- src/main/c/taschenrechner.h | 115 ++++++++++++++++++----------------- src/main/c/testForOperator.c | 28 +++++++++ 3 files changed, 143 insertions(+), 64 deletions(-) create mode 100644 src/main/c/testForOperator.c diff --git a/src/main/c/BasicMode.c b/src/main/c/BasicMode.c index 9904742..396440f 100644 --- a/src/main/c/BasicMode.c +++ b/src/main/c/BasicMode.c @@ -1,8 +1,56 @@ -#include -#include -#include -#include - -#include "taschenrechner.h" - - +#include +#include +#include +#include + +#include "taschenrechner.h" + + +void BasicMode() { + char endtmp = '0'; //0 false, 1 true + double result = 0; + double numbers[100] = {0.0}; + char operatoren[100]; + + do { + //100 doubles in this array goes through the for loop till 100 numbers or = is entered + for (int i = 0; i < 100) { + printf("Enter a Number: "); + numbers[i] = testForNumber(); //gets number + + printf("Enter a operation: "); + operator[i] = testForOperator(); //gets operator + + if (i > 1) { //if function to prevent numbers[-1] + switch (operator[i]) //checks of the operator and switches to the case with the needed function + { + case '+': + result += add(numbers[i - 1], numbers[i]) + break; + + case '-': + result += minus(numbers[i - 1], numbers[i]) + break; + + case '*': + result += multiply(numbers[i - 1], numbers[i]) + break; + + case '/': + result += divide(numbers[i - 1], numbers[i]) + break; + + case '=': + printf("The result is: %d", result); + endtmp = 1; + break; + + default: + break; + } + } + i++; + } + } while (endtmp != '1') + +} diff --git a/src/main/c/taschenrechner.h b/src/main/c/taschenrechner.h index 23cc26f..3855a6c 100644 --- a/src/main/c/taschenrechner.h +++ b/src/main/c/taschenrechner.h @@ -1,56 +1,59 @@ -#ifndef TASCHENRECHNER_H -#define TASCHENRECHNER_H - -//add function -doulbe add(doulbe a, doulbe b); - -//minus function -doulbe minus(doulbe a, doulbe b); - -//multiply function -doulbe multiply(doulbe a, doulbe b); - -//divide function -doulbe divide(doulbe a, doulbe b); - -//get input and check if its a number -double testForNumber() - -// Square root function -double squareRootFunction(double x); - -//.. -// Trigonometric functions -double sineFunction(double angle); - -double cosineFunction(double angle); - -double tangentFunction(double angle); - -//.. -// Logarithmic functions -double logarithmFunction(double x); - -double naturalLogarithmFunction(double x); - -double logarithmBase2Function(double x); - -//.. -// Exponential function -double exponentialFunction(double x); - -// .. -// Bitwise AND function -int bitwiseAND(int num1, int num2); - -// Bitwise OR function -int bitwiseOR(int num1, int num2); - -// Bitwise XOR function -int bitwiseXOR(int num1, int num2); - -int mode(int userChoice); - -int displayMenu(); - -#endif // TASCHENRECHNER_H +#ifndef TASCHENRECHNER_H +#define TASCHENRECHNER_H + +//add function +doulbe add(doulbe a, doulbe b); + +//minus function +doulbe minus(doulbe a, doulbe b); + +//multiply function +doulbe multiply(doulbe a, doulbe b); + +//divide function +doulbe divide(doulbe a, doulbe b); + +//get input and check if its a number +double testForNumber() + +//get input and check if its a operator +char testForOperator() + +// Square root function +double squareRootFunction(double x); + +//.. +// Trigonometric functions +double sineFunction(double angle); + +double cosineFunction(double angle); + +double tangentFunction(double angle); + +//.. +// Logarithmic functions +double logarithmFunction(double x); + +double naturalLogarithmFunction(double x); + +double logarithmBase2Function(double x); + +//.. +// Exponential function +double exponentialFunction(double x); + +// .. +// Bitwise AND function +int bitwiseAND(int num1, int num2); + +// Bitwise OR function +int bitwiseOR(int num1, int num2); + +// Bitwise XOR function +int bitwiseXOR(int num1, int num2); + +int mode(int userChoice); + +int displayMenu(); + +#endif // TASCHENRECHNER_H diff --git a/src/main/c/testForOperator.c b/src/main/c/testForOperator.c new file mode 100644 index 0000000..51b1931 --- /dev/null +++ b/src/main/c/testForOperator.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "taschenrechner.h" + +char testForOperator() { + //saving the number of operators in a variable so it only needs to be changed in one location + int numberOfOperators = 5; + //array where all valid Operators are saved, can be esily expanded + char oppArray[numberOfOperators] = { '+','-','*','/','=' }; + char input; + bool validInput = false; + + //Loops as long as input isn't valid, enters at least one time + while (!validInput) { + scanf_s("%c\n", &input); + //checks every position of the array if a position is equal to input returns the input, which also stopps the function + for (int i = 0; i <= numberOfOperators; i++) { + if (input == oppArray[i]) { + return input; + } + } + //if the input was deemed invalid it asks for new input and goes to the top of the while loop + printf("Diese Eigabe war nicht zulässig probieren sie es noch einmal!\n"); + } +} \ No newline at end of file