Browse Source

added BasicMode.c and refractoring to testForOperator/taschenrechner.h

remotes/origin/feature
Enrico Schellenberger 11 months ago
parent
commit
a3cbfd7de1
  1. 48
      src/main/c/BasicMode.c
  2. 3
      src/main/c/taschenrechner.h
  3. 28
      src/main/c/testForOperator.c

48
src/main/c/BasicMode.c

@ -6,3 +6,51 @@
#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')
}

3
src/main/c/taschenrechner.h

@ -16,6 +16,9 @@ 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);

28
src/main/c/testForOperator.c

@ -0,0 +1,28 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#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");
}
}
Loading…
Cancel
Save