Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int getLength(char first[]);
  6. int fromCharToInt(char arr[]);
  7. int Pow(int num, int pow);
  8.  
  9. int main()
  10. {
  11. char* expression = new char[100];
  12. char* firstSum = new char[35];
  13.  
  14. int firstNum = 0;
  15.  
  16. while (true)
  17. {
  18. bool isValid = true;
  19. cout << "Enter an expression to calculate or write 'exit' to close the program:" << endl;
  20.  
  21. cin.getline(expression, 100);
  22. char operation;
  23.  
  24. if (expression[0] == 'e' && expression[1] == 'x' && expression[2] == 'i' && expression[3] == 't'&&expression[4]=='\0')
  25. return 0;
  26.  
  27. //to do validation _|_
  28.  
  29. int sizeOfArray = getLength(expression);
  30.  
  31. for (int i = 0; i < sizeOfArray - 1; i++)
  32. {
  33. if (expression[0] == ' ')
  34. {
  35. cout << "This expression is invalid" << endl;
  36. isValid = false;
  37. break;
  38. }
  39.  
  40. while (expression[i] != ' ')
  41. {
  42. int n = 0;
  43. firstSum[n] = expression[i];
  44. n++;
  45.  
  46. if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/' || expression[i] == '!')
  47. {
  48. operation = expression[i];
  49. }
  50. }
  51.  
  52. //to do vsichko ostanalo
  53.  
  54. if (expression[i] == ' ')
  55. {
  56. i++;
  57. continue;
  58. }
  59.  
  60.  
  61. }
  62. if (!isValid)
  63. continue;
  64.  
  65. break;
  66. }
  67.  
  68.  
  69.  
  70.  
  71. return 0;
  72. }
  73.  
  74. int getLength(char first[])
  75. {
  76. int count = 0;
  77. char* p = first;
  78. while (*p != '\0')
  79. {
  80. count++;
  81. p++;
  82. }
  83. return count;
  84. }
  85.  
  86. int fromCharToInt(char arr[])
  87. {
  88. int size = getLength(arr);
  89. int number=0;
  90. int pow = size-1;
  91.  
  92. for (int i = 0; i < size; i++)
  93. {
  94. number = (arr[i] - '0') * Pow(10,pow--);
  95. }
  96. return number;
  97. }
  98.  
  99. int Pow(int num, int pow)
  100. {
  101. int result = 1;
  102.  
  103. for (int i = 0; i < pow; i++){
  104. result *= num;
  105. }
  106.  
  107. return result;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement