Advertisement
totobac

Untitled

Jan 24th, 2022
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. int numDigits(int number)
  5. {
  6.     int digits = 0;
  7.     while (number) {
  8.         number /= 10;
  9.         digits++;
  10.     }
  11.     return digits;
  12. }
  13.  
  14.  
  15. int isValidExpr(char* expr) {  
  16.     if (strcmp(expr, "zero") == 0)
  17.         return 0;
  18.     else if (strcmp(expr, "one") == 0)
  19.         return 1;
  20.     else if (strcmp(expr, "two") == 0)
  21.         return 2;
  22.     else if (strcmp(expr, "three") == 0)
  23.         return 3;
  24.     else if (strcmp(expr, "four") == 0)
  25.         return 4;
  26.     else if (strcmp(expr, "five") == 0)
  27.         return 5;
  28.     else if (strcmp(expr, "six") == 0)
  29.         return 6;
  30.     else if (strcmp(expr, "seven") == 0)
  31.         return 7;
  32.     else if (strcmp(expr, "eight") == 0)
  33.         return 8;
  34.     else if (strcmp(expr, "nine") == 0)
  35.         return 9;
  36.     else if (strcmp(expr, "plus") == 0)
  37.         return 10;
  38.     else if (strcmp(expr, "minus") == 0)
  39.         return 11;
  40.     else
  41.         return -1;
  42. }
  43.  
  44. void calculateExpression(char* expr) {
  45.     int result = 0;
  46.     int i = 0;
  47.     int j = 0;
  48.     char substr[1024];
  49.     bool isAtPlus = true;
  50.     int lastExpr = 0;
  51.  
  52.     while (expr[i] != '\0')
  53.     {
  54.         substr[j] = expr[i];
  55.         substr[j + 1] = '\0';
  56.         if (isValidExpr(substr) != -1)
  57.         {
  58.             int expr = isValidExpr(substr);
  59.             if (expr == 10)
  60.             {
  61.                 if (isAtPlus)
  62.                     result += lastExpr;
  63.                 else
  64.                     result -= lastExpr;
  65.  
  66.                 isAtPlus = true;
  67.                 lastExpr = 0;
  68.             }
  69.             else if (expr == 11)
  70.             {
  71.                 if (isAtPlus)
  72.                     result += lastExpr;
  73.                 else
  74.                     result -= lastExpr;
  75.  
  76.                 isAtPlus = false;
  77.                 lastExpr = 0;
  78.             }
  79.             else
  80.             {
  81.                 lastExpr = lastExpr * 10 + expr;
  82.             }
  83.             j = 0;
  84.             ++i;
  85.             continue;
  86.         }
  87.  
  88.         ++i;
  89.         ++j;
  90.     }
  91.  
  92.     if (isAtPlus)
  93.         result += lastExpr;
  94.     else
  95.         result -= lastExpr;
  96.  
  97.     int number = result;
  98.     char numberArray[124];
  99.  
  100.     bool isNegative = number < 0;
  101.     if (isNegative)
  102.         number *= -1;
  103.  
  104.     for (i = 0; number != 0; ++i, number /= 10)
  105.     {
  106.         numberArray[i] = number % 10 + '0';
  107.     }
  108.     numberArray[i--] = '\0';
  109.     char resultString[124];
  110.  
  111.     if (isNegative)
  112.     {
  113.         resultString[0] = '-';
  114.         j++;
  115.     }
  116.     while (i >= 0)
  117.     {
  118.         resultString[j] = numberArray[i];
  119.         j++;
  120.         i--;
  121.     }
  122.     resultString[j] = '\0';
  123.     strcpy_s(expr, strlen(expr), resultString);
  124. }
  125.  
  126. int main()
  127. {
  128.     int size;
  129.     std::cout << "Enter string's size: ";
  130.     std::cin >> size;
  131.     std::cout << "Enter the string: ";
  132.     char* arr = new char[size + 1];
  133.     std::cin >> arr;
  134.     calculateExpression(arr);
  135.     std::cout << arr;
  136.  
  137.     delete [] arr;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement