Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6.  
  7. std::string getNumber();
  8. std::string getOperation();
  9. void NumberCheck(std::string number);
  10. void OperationCheck(std::string operation);
  11. double Calculate(std::string FirstNumber, std::string Operation, std::string SecondNumber);
  12. double Round(double result);
  13. void ExitProgram();
  14.  
  15. const std::vector<char> OperationList = {'+', '-', '*', '/', '%', '^'};
  16.  
  17. int main()
  18. {
  19. // Get Data
  20. std::string FirstNumber = getNumber();
  21. std::string Operation = getOperation();
  22. std::string SecondNumber = getNumber();
  23.  
  24. // Error Check
  25. NumberCheck(FirstNumber);
  26. OperationCheck(Operation);
  27. NumberCheck(SecondNumber);
  28.  
  29. // Calculate
  30. double result = Calculate(FirstNumber, Operation, SecondNumber);
  31.  
  32. //Round
  33. result = Round(result);
  34.  
  35. std::cout << "Result: " << result;
  36. return 0;
  37. }
  38.  
  39. std::string getNumber()
  40. {
  41. std::string number;
  42.  
  43. std::cout << "Enter Number: ";
  44. std::getline(std::cin, number);
  45.  
  46. return number;
  47. }
  48.  
  49. std::string getOperation()
  50. {
  51. std::string operation;
  52.  
  53. std::cout << "Enter Operation: ";
  54. std::getline(std::cin, operation);
  55.  
  56. return operation;
  57. }
  58.  
  59. void NumberCheck(std::string number)
  60. {
  61. if (!number.empty() && std::all_of(number.begin(), number.end(), ::isdigit))
  62. {
  63. return;
  64. }
  65. else
  66. {
  67. ExitProgram();
  68. }
  69. }
  70.  
  71. void OperationCheck(std::string operation)
  72. {
  73. for (int i = 0; i < OperationList.size(); i++)
  74. {
  75. if (OperationList[i] == operation[0])
  76. {
  77. return;
  78. }
  79. else if (i + 1 == OperationList.size())
  80. {
  81. exit(0);
  82. }
  83. }
  84. }
  85.  
  86. double Calculate(std::string FirstNumber, std::string Operation, std::string SecondNumber)
  87. {
  88. double result;
  89.  
  90. char operation = Operation[0];
  91. double x = std::stoi(FirstNumber);
  92. double y = std::stoi(SecondNumber);
  93.  
  94. if (operation == OperationList[0])
  95. {
  96. result = x + y;
  97. }
  98. else if (operation == OperationList[1])
  99. {
  100. result = x - y;
  101. }
  102. else if (operation == OperationList[2])
  103. {
  104. result = x * y;
  105. }
  106. else if (operation == OperationList[3])
  107. {
  108. result = x / y;
  109. }
  110. else if (operation == OperationList[4])
  111. {
  112. result = std::fmod(x, y);
  113. }
  114. else if (operation == OperationList[5])
  115. {
  116. result = pow(x, y);
  117. }
  118.  
  119. return result;
  120. }
  121.  
  122. double Round(double result)
  123. {
  124. std::vector<int> roundTo = {51, 75, 20};
  125. std::vector<int> minMax;
  126.  
  127. std::sort(roundTo.begin(), roundTo.end());
  128.  
  129. if (result < roundTo[0])
  130. {
  131. return roundTo[0];
  132. }
  133. if (result > roundTo[roundTo.size() - 1])
  134. {
  135. return roundTo[roundTo.size() - 1];
  136. }
  137.  
  138. for (int i = 0; i < roundTo.size() - 1; i++)
  139. {
  140. if (result > roundTo[i] && result < roundTo[i + 1])
  141. {
  142. minMax.push_back(roundTo[i]);
  143. minMax.push_back(roundTo[i + 1]);
  144. }
  145. }
  146.  
  147. if ((minMax[1] - minMax[0]) / 2 + minMax[0] <= result)
  148. {
  149. return minMax[1];
  150. }
  151. else
  152. {
  153. return minMax[0];
  154. }
  155.  
  156. return result;
  157. }
  158.  
  159. void ExitProgram()
  160. {
  161. std::string pause;
  162. system("CLS");
  163. std::cout << "Invalid Number";
  164. std::getline(std::cin, pause);
  165. exit(0);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement