diff --git a/src/main/c/BasicMode.c b/src/main/c/BasicMode.c new file mode 100644 index 0000000..f6101c3 --- /dev/null +++ b/src/main/c/BasicMode.c @@ -0,0 +1,59 @@ +#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 operators[100]; + + do { + //100 doubles in this array goes through the for loop till 100 numbers or = is entered + for (int i = 0; i < 100; i++) { + printf("Enter a Number: "); + numbers[i] = testForNumber(); //gets number + + printf("Enter a operation: "); + operators[i] = testForOperator(); //gets operator + } + } while (endtmp != '1'); + + + for (int i = 0; i < 100; i++) {//checks all operators to check for priority + if ((operators[i] == '/' || operators[i] == '*') && i > 1) { //if operators[i] == / or * and i>1 so you dont get numbers[-1] + if (operators[i] == '/') { + result += divide(numbers[i - 1], numbers[i]); //divides if char is / and adds number to the result + } + // for all calculations we take the number and the number before that + else { // and do the operation + result += multiply(numbers[i - 1], numbers[i]); //multiplys if char is * and adds number to the result + } + } + + else if ((operators[i] == '+' || operators[i] == '-') && i > 1) { + if (operators[i] == '+') { + result += add(numbers[i - 1], numbers[i]); //adds if char is + and adds number to the result + } + + else { + result += minus(numbers[i - 1], numbers[i]); //subtrakts if char is - and adds number to the result + } + } + + else if (i<=1 && operators[i] == '=') { //if there are less then 2 numbers in the array + result = numbers[i]; //set result to the 0 digit + } + + else if (operators[i] == '=') { //if char is = + printf("The result is: %f", result); //print out the result + i = 100; + break; + } + i++; + } +} diff --git a/src/main/c/main_taschenrechner.c b/src/main/c/main_taschenrechner.c index f1fccac..4668192 100644 --- a/src/main/c/main_taschenrechner.c +++ b/src/main/c/main_taschenrechner.c @@ -6,6 +6,7 @@ #include "taschenrechner.h" +<<<<<<< HEAD doulbe add(doulbe a, doulbe b) { return a + b; } @@ -19,6 +20,21 @@ doulbe multiply(doulbe a, doulbe b) { } doulbe divide(doulbe a, doulbe b) { +======= +double add(double a, double b) { + return a + b; +} + +double minus (double a, double b){ + return a - b; +} + +double multiply(double a, double b) { + return a * b; +} + +double divide(double a, double b) { +>>>>>>> feature if (b == 0) { return 0; } diff --git a/src/main/c/taschenrechner.h b/src/main/c/taschenrechner.h index 7b548f9..b08a381 100644 --- a/src/main/c/taschenrechner.h +++ b/src/main/c/taschenrechner.h @@ -1,3 +1,4 @@ +<<<<<<< HEAD #ifndef TASCHENRECHNER_H #define TASCHENRECHNER_H @@ -47,3 +48,64 @@ int mode(int userChoice); int displayMenu(); #endif // TASCHENRECHNER_H +======= +#ifndef TASCHENRECHNER_H +#define TASCHENRECHNER_H + +//add function +double add(double a, double b); + +//minus function +double minus(double a, double b); + +//multiply function +double multiply(double a, double b); + +//divide function +double divide(double a, double 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 +>>>>>>> feature diff --git a/src/main/c/testForNumber.c b/src/main/c/testForNumber.c new file mode 100644 index 0000000..06438d1 --- /dev/null +++ b/src/main/c/testForNumber.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +#include "taschenrechner.h" + +double testForNumber() { + double num; + char storage[25]; + char* notANumber; + + do { + //Emptys the standart output buffer to ensure that this function works in loops + //and returns the value of num after every loop + fflush(stdout); + + //fgets() reads the user input into the buffer storage (storage) + //the if condition is true as long as the input isn't empty + if (fgets(storage, sizeof storage, stdin) != NULL) { + //strtod() tries to convert the input into a double + //if there is anything but a number at any point of the array the pointer notANumber will point to this position + num = strtod(storage, ¬ANumber); + } + //isspace makes sure that notANumber doesn't point to an empty space + //the secount part makes sure that notANumber isn't pointing at 0 + } while (!isspace(*notANumber) && *notANumber != 0); + + return num; +} \ No newline at end of file diff --git a/src/main/c/testForOperator.c b/src/main/c/testForOperator.c new file mode 100644 index 0000000..0ad34c6 --- /dev/null +++ b/src/main/c/testForOperator.c @@ -0,0 +1,29 @@ +#include +#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[] = { '+','-','*','/','=' }; + 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("The input was not allowed. Please try again!\n"); + } +} \ No newline at end of file