Advertisement
Dubwyn

Untitled

Jan 21st, 2021
873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. int getLength(char array[100])
  6. {
  7.     int j = 0;
  8.     for (int i = 0; array[i] != '\0'; i++)
  9.     {
  10.         j++;
  11.     }
  12.     return j;
  13. }
  14.  
  15. int toNumber(char a){
  16.     switch(a){
  17.         case '0': return 0;
  18.         case '1': return 1;
  19.         case '2': return 2;
  20.         case '3': return 3;
  21.         case '4': return 4;
  22.         case '5': return 5;
  23.         case '6': return 6;
  24.         case '7': return 7;
  25.         case '8': return 8;
  26.         case '9': return 9;
  27.         default: return -1;
  28.     }
  29. }
  30.  
  31. void printArray(char array[100])
  32. {
  33.     int length = getLength(array);
  34.     for (int i = 0; i < length; i++)
  35.     {
  36.         cout << array[i];
  37.     }
  38. }
  39.  
  40. bool isNumber(char a)
  41. {
  42.     return (a >= '0' && a <= '9');
  43. }
  44.  
  45. bool isSymbol(char a)
  46. {
  47.     switch (a)
  48.     {
  49.     case '+': return true;
  50.     case '-': return true;
  51.     case '/': return true;
  52.     case '*': return true;
  53.     case '=': return true;
  54.     default: return false;
  55.     }
  56.  
  57. }
  58.  
  59. bool valid(char array[100], char newArray[100], int &j)
  60. {
  61.     bool number = false, symbol = false, equation = false;
  62.     j = 0;
  63.     int length = getLength(array);
  64.     for (int i = 0; i < length; i++)
  65.     {
  66.         if (isNumber(array[i]) || isSymbol(array[i]))
  67.         {
  68.             newArray[j] = array[i];
  69.             j++;
  70.         }
  71.     }
  72.  
  73.     if (isSymbol(newArray[0]))
  74.     {
  75.         return false;
  76.     }
  77.  
  78.     for (int i = 0; i < j; i++)
  79.     {
  80.         if (isSymbol(newArray[i]) && symbol)
  81.         {
  82.             return false;
  83.         }
  84.         if (isSymbol(newArray[i]))
  85.         {
  86.             if (equation && newArray[i] == '=')
  87.             {
  88.                 return false;
  89.             }
  90.  
  91.             symbol = true;
  92.             number = false;
  93.  
  94.             if (newArray[i] == '=')
  95.             {
  96.                 equation = true;
  97.             }
  98.  
  99.             continue;
  100.         }
  101.         if (isNumber(newArray[i]))
  102.         {
  103.             symbol = false;
  104.             number = true;
  105.         }
  106.  
  107.     }
  108.     if (equation)
  109.     {
  110.         return false;
  111.  
  112.     }
  113.     else
  114.     {
  115.         return true;
  116.     }
  117.  
  118. }
  119.  
  120. int recursion(char array[100], int length, int position, int finalNumber = 0)
  121. {
  122.    
  123.     if (length-1 == position)
  124.     {
  125.         return finalNumber;
  126.     }
  127.  
  128.     if (position == 0)
  129.     {
  130.         finalNumber = toNumber(array[position]);
  131.         position++;
  132.     }
  133.  
  134.   //  cout << "Position: " << position << endl;
  135.     //cout << "Array[i]: " << array[position] << endl;
  136.    // cout << "finalNumber: " << finalNumber << endl;
  137.  
  138.     if (array[position] == '+')
  139.     {
  140.         finalNumber = finalNumber +  toNumber(array[position+1]);
  141.         //cout << "Numbre+ : " << finalNumber << endl;
  142.         return (recursion(array, length, position + 1, finalNumber));
  143.     }
  144.  
  145.     if (array[position] == '-')
  146.     {
  147.         finalNumber = finalNumber -  toNumber(array[position+1]);
  148.        // cout << "Numbre- : " << finalNumber << endl;
  149.         return (recursion(array, length, position + 1, finalNumber));
  150.     }
  151.  
  152.     if (array[position] == '*')
  153.     {
  154.  
  155.         finalNumber = finalNumber *  toNumber(array[position+1]);
  156.         //cout << "Numbre* : " << finalNumber << endl;
  157.         return (recursion(array, length, position + 1, finalNumber));
  158.     }
  159.  
  160.     if (array[position] == '/')
  161.     {
  162.  
  163.         finalNumber = finalNumber / toNumber(array[position+1]);
  164.        // cout << "Numbre/ : " << finalNumber << endl;
  165.         return (recursion(array, length, position + 1, finalNumber));
  166.     }
  167.  
  168.     return recursion(array, length, position+1, finalNumber);
  169.    
  170. }
  171.  
  172. //3-5+2*2 = 0
  173. //4+5-6*6+3 = 21
  174. int main()
  175. {
  176.     int length = 0;
  177.     char array[100];
  178.     char newArray[100];
  179.     cin.getline(array, 100);
  180.     int number = 0;
  181.  
  182.     if (valid(array, newArray,length))
  183.     {
  184.        
  185.         number = recursion(newArray, length, 0);
  186.         cout << number;
  187.     }
  188. }
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement