fdai7782
11 months ago
5 changed files with 196 additions and 0 deletions
-
59src/main/c/BasicMode.c
-
16src/main/c/main_taschenrechner.c
-
62src/main/c/taschenrechner.h
-
30src/main/c/testForNumber.c
-
29src/main/c/testForOperator.c
@ -0,0 +1,59 @@ |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <math.h> |
|||
|
|||
#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++; |
|||
} |
|||
} |
@ -0,0 +1,30 @@ |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <math.h> |
|||
|
|||
#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; |
|||
} |
@ -0,0 +1,29 @@ |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <math.h> |
|||
#include <stdbool.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[] = { '+','-','*','/','=' }; |
|||
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"); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue