fdai7782
11 months ago
1 changed files with 59 additions and 59 deletions
@ -1,59 +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++; |
|
||||
} |
|
||||
} |
|
||||
|
#include <stdio.h> |
||||
|
#include <string.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <math.h> |
||||
|
|
||||
|
#include "calculator.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++; |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue