You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.8 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include "taschenrechner.h"
  6. void BasicMode() {
  7. char endtmp = '0'; //0 false, 1 true
  8. double result = 0;
  9. double numbers[100] = {0.0};
  10. char operators[100];
  11. do {
  12. //100 doubles in this array goes through the for loop till 100 numbers or = is entered
  13. for (int i = 0; i < 100; i++) {
  14. printf("Enter a Number: ");
  15. numbers[i] = testForNumber(); //gets number
  16. printf("Enter a operation: ");
  17. operators[i] = testForOperator(); //gets operator
  18. }
  19. } while (endtmp != '1');
  20. for (int i = 0; i < 100; i++) {//checks all operators to check for priority
  21. if ((operators[i] == '/' || operators[i] == '*') && i > 1) { //if operators[i] == / or * and i>1 so you dont get numbers[-1]
  22. if (operators[i] == '/') {
  23. result += divide(numbers[i - 1], numbers[i]); //divides if char is / and adds number to the result
  24. }
  25. // for all calculations we take the number and the number before that
  26. else { // and do the operation
  27. result += multiply(numbers[i - 1], numbers[i]); //multiplys if char is * and adds number to the result
  28. }
  29. }
  30. else if ((operators[i] == '+' || operators[i] == '-') && i > 1) {
  31. if (operators[i] == '+') {
  32. result += add(numbers[i - 1], numbers[i]); //adds if char is + and adds number to the result
  33. }
  34. else {
  35. result += minus(numbers[i - 1], numbers[i]); //subtrakts if char is - and adds number to the result
  36. }
  37. }
  38. else if (i<=1 && operators[i] == '=') { //if there are less then 2 numbers in the array
  39. result = numbers[i]; //set result to the 0 digit
  40. }
  41. else if (operators[i] == '=') { //if char is =
  42. printf("The result is: %f", result); //print out the result
  43. i = 100;
  44. break;
  45. }
  46. i++;
  47. }
  48. }