Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.29 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int maxLenghtOfArray = 100; const int maxInputLenght = 1000;
  5. bool inputValidation(char input[]) {
  6.     if (!((input[0] >= '0' && input[0] <= '9')||input[0]=='-')) {
  7.         return false;
  8.     }
  9.     for (int i = 0;input[i] != '\0';i++) {
  10.         if ((input[i] >= '0' && input[i] <= '9') || input[i] == ' ' || input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '!') {
  11.             if ((input[i] >= '0' && input[i] <= '9') && ((input[i + 1] >= '0' && input[i + 1] <= '9') || input[i + 1] == ' ' || input[i + 1] == '!' || input[i + 1] == '\0')) {
  12.                 continue; //if the number after the symbol is correct
  13.             }
  14.             else if (input[i] == '!' && (input[i + 1] == ' ' || input[i + 1] == '\0')) {
  15.                 continue; //if the input after the factorial is valid
  16.             }
  17.             else if ((input[i] == '-')&& (input[i+1] >= '0' && input[i+1] <= '9')) {
  18.                 //if the typed number is negative
  19.                 int i_test = i+1;
  20.                 while ((input[i_test] >= '0' && input[i_test] <= '9')|| (input[i_test] == '!')) {
  21.                     //is '!' correctly used
  22.                     if (input[i_test] == '!') {
  23.                         return false;
  24.                     }
  25.                     i_test++;
  26.                 }
  27.                 continue;
  28.             }
  29.             else if ((input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/') && (input[i + 1] == ' ') && ((input[i + 2] >= '0' && input[i + 2] <= '9')||input[i+2]=='-') && (input[i + 1] != '\0')) {
  30.                 continue; //if the symbol after the operation is correct
  31.             }
  32.             else if (input[i] == ' ') {
  33.                 if (!(input[i - 1] == '+' || input[i - 1] == '-' || input[i - 1] == '*' || input[i - 1] == '/') && !(input[i + 1] == '+' || input[i + 1] == '-' || input[i + 1] == '*' || input[i + 1] == '/')) {
  34.                     return false; //if after space the input is correct
  35.                 }
  36.             }
  37.             else {
  38.                 return false;
  39.             }
  40.         }
  41.         else {
  42.             return false;
  43.         }
  44.     }
  45.     return true;
  46. }
  47. bool exit(char input[]) {
  48.     //exit from the console
  49.     if (input[0] == 'e' && input[1] == 'x' && input[2] == 'i' && input[3] == 't') {
  50.         return true;
  51.     }
  52.     else {
  53.         return false;
  54.     }
  55. }
  56. int convertDigitsIntoNumber(int digits[], int lenght, bool negativeNumber) {
  57.     int copyOfLenght = lenght - 1;
  58.     int number = 0;
  59.     for (int i = 0;i < lenght;copyOfLenght--, i++) {
  60.         number += digits[i] * pow(10, copyOfLenght);
  61.     }
  62.     if (negativeNumber == true) {
  63.         number *= -1;
  64.         return number;
  65.     }
  66.     return number;
  67. }
  68. int factorialCalculation(int number) {
  69.     int factorial = 1;
  70.     for (int i = 1;i <= number;i++) {
  71.         factorial *= i;
  72.     }
  73.     return factorial;
  74. }
  75. int* convertInputNumbersFromCharToInt(char input[], int& numbersTyped) {
  76.     numbersTyped = 0;
  77.     bool factorial = false;
  78.     bool negativeNumber = false;
  79.     int intigerOfInputCharArray = 0;               //input[intigerOfInputCharArray]
  80.     int digits = 0;                                //how many digits a number has  
  81.     int digitsOfNumber[maxLenghtOfArray];          //saving the digits of a number when it's broken down form the char array
  82.     int arrayOfNumbers[maxLenghtOfArray];          //save the actual numbers
  83.     bool endOfInput = false;                       //when it reaches the end of the input
  84.     while (input[intigerOfInputCharArray] != '\0') {
  85.         digits = 0;
  86.         factorial = false;
  87.         if ((input[intigerOfInputCharArray] == '+' || input[intigerOfInputCharArray] == '-' || input[intigerOfInputCharArray] == '*' || input[intigerOfInputCharArray] == '/')) {
  88.             if (input[intigerOfInputCharArray] == '-' && (input[intigerOfInputCharArray+1] >= '0' && input[intigerOfInputCharArray+1] <= '9')) {
  89.                 //is the number negative
  90.                 negativeNumber = true;
  91.                 intigerOfInputCharArray++;
  92.             }
  93.             else {
  94.                 //skip the operation symbols
  95.                 intigerOfInputCharArray += 2;
  96.             }
  97.         }
  98.         else {
  99.             while (input[intigerOfInputCharArray] != ' ') {
  100.                 //save the digits of the number until it reaches space
  101.                 if (input[intigerOfInputCharArray] == '\0') {
  102.                     endOfInput = true;
  103.                     break;
  104.                 }
  105.                 else if (input[intigerOfInputCharArray] == '!') {
  106.                     factorial = true;
  107.                     intigerOfInputCharArray++;
  108.                 }
  109.                 else {
  110.                     digitsOfNumber[digits] = input[intigerOfInputCharArray] - '0';
  111.                     intigerOfInputCharArray++; digits++;
  112.                 }
  113.             }
  114.             if (factorial == true) {
  115.                 //save the number after it's calculated as factorial
  116.                 arrayOfNumbers[numbersTyped] = factorialCalculation(convertDigitsIntoNumber(digitsOfNumber, digits,negativeNumber));
  117.                 numbersTyped++; intigerOfInputCharArray++;
  118.             }
  119.             else {
  120.                 //save the actual number in the array of numbers
  121.                 arrayOfNumbers[numbersTyped] = convertDigitsIntoNumber(digitsOfNumber, digits,negativeNumber);
  122.                 numbersTyped++; intigerOfInputCharArray++;
  123.                 negativeNumber = false;
  124.             }
  125.         }
  126.         if (endOfInput == true) {
  127.             break;
  128.         }
  129.     }
  130.     return arrayOfNumbers;
  131. }
  132. char* operatorsFunction(char input[]) {
  133.     char operators[maxLenghtOfArray];
  134.     int intigerOfChar = 0;  //operations [intigerOfChar]
  135.     for (int i = 0;input[i] != '\0';i++) {
  136.         if ((input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/')) {
  137.             if (input[i] == '-' && (input[i + 1] >= '0' && input[i + 1] <= '9')) {
  138.                 //if the '-' is for negative number
  139.                 continue;
  140.             }
  141.             operators[intigerOfChar] = input[i];
  142.             intigerOfChar++;
  143.         }
  144.     }
  145.     operators[intigerOfChar] = '\0';
  146.     return operators;
  147. }
  148. void calculationOfInput(int numbers[], char operators[], int& numbersTyped) {
  149.     int answer = 0;
  150.     bool error = false;
  151.     if (numbersTyped == 1) {
  152.         //if it's typed only one number
  153.         cout<<"Answer: "<<numbers[0]<<endl;
  154.     }
  155.     else {
  156.         for (int i = 0; i < numbersTyped - 1; i++) {
  157.             switch (operators[i]){
  158.             case '+':
  159.                 if (i == 0) {
  160.                     answer = numbers[i] + numbers[i + 1];
  161.                 }
  162.                 else {
  163.                     answer += numbers[i + 1];
  164.                 }
  165.                 break;
  166.             case '-':
  167.                 if (i == 0) {
  168.                     answer = numbers[i] - numbers[i + 1];
  169.                 }
  170.                 else {
  171.                     answer -= numbers[i + 1];
  172.                 }
  173.                 break;
  174.             case '*':
  175.                 if (i == 0) {
  176.                     answer = numbers[i] * numbers[i + 1];
  177.                 }
  178.                 else {
  179.                     answer *= numbers[i + 1];
  180.                 }
  181.                 break;
  182.             case '/':
  183.                 if (i == 0) {
  184.                     if (numbers[i + 1] == 0) {
  185.                         error = true;
  186.                         cout << "Error! Can't divide by zero!" << endl;
  187.                     }
  188.                     else {
  189.                         answer = numbers[i] / numbers[i + 1];
  190.                     }
  191.                 }
  192.                 else {
  193.                     if (numbers[i + 1] == 0) {
  194.                         error = true;
  195.                         cout << "Error! Can't divide by zero!" << endl;
  196.                     }
  197.                     else {
  198.                         answer /= numbers[i + 1];
  199.                     }
  200.                 }
  201.                 break;
  202.             }
  203.         }
  204.         if (error != true) {
  205.             cout << "Answer: " << answer << endl;
  206.         }
  207.     }  
  208. }
  209.  
  210. int main() {
  211.     char input[maxInputLenght];
  212.     int numbers[maxLenghtOfArray];
  213.     char operators[maxLenghtOfArray];
  214.     bool stopTheCalculator = false;
  215.     do {
  216.         int numbersTyped = INT_MAX;
  217.         do {
  218.             cout << "Enter an expression to calculate or type \"exit\": " << endl;
  219.             cin.getline(input, maxInputLenght);
  220.             if (exit(input) == true) {
  221.                 stopTheCalculator = true;
  222.                 break;
  223.             }
  224.             if (inputValidation(input) == false) {
  225.                 cout << "This expression is invalid!" << endl;
  226.             }
  227.         } while (inputValidation(input) == false);
  228.         if (stopTheCalculator != true) {
  229.             for (int i = 0;i < numbersTyped;i++) {
  230.                 numbers[i] = convertInputNumbersFromCharToInt(input, numbersTyped)[i];
  231.             }
  232.             for (int i = 0;operatorsFunction(input)[i] != '\0';i++) {
  233.                 operators[i] = operatorsFunction(input)[i];
  234.             }
  235.             calculationOfInput(numbers, operators, numbersTyped);
  236.         }
  237.     } while (stopTheCalculator == false);
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement