Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- std::string getNumber();
- std::string getOperation();
- void NumberCheck(std::string number);
- void OperationCheck(std::string operation);
- double Calculate(std::string FirstNumber, std::string Operation, std::string SecondNumber);
- double Round(double result);
- void ExitProgram();
- const std::vector<char> OperationList = {'+', '-', '*', '/', '%', '^'};
- int main()
- {
- // Get Data
- std::string FirstNumber = getNumber();
- std::string Operation = getOperation();
- std::string SecondNumber = getNumber();
- // Error Check
- NumberCheck(FirstNumber);
- OperationCheck(Operation);
- NumberCheck(SecondNumber);
- // Calculate
- double result = Calculate(FirstNumber, Operation, SecondNumber);
- //Round
- result = Round(result);
- std::cout << "Result: " << result;
- return 0;
- }
- std::string getNumber()
- {
- std::string number;
- std::cout << "Enter Number: ";
- std::getline(std::cin, number);
- return number;
- }
- std::string getOperation()
- {
- std::string operation;
- std::cout << "Enter Operation: ";
- std::getline(std::cin, operation);
- return operation;
- }
- void NumberCheck(std::string number)
- {
- if (!number.empty() && std::all_of(number.begin(), number.end(), ::isdigit))
- {
- return;
- }
- else
- {
- ExitProgram();
- }
- }
- void OperationCheck(std::string operation)
- {
- for (int i = 0; i < OperationList.size(); i++)
- {
- if (OperationList[i] == operation[0])
- {
- return;
- }
- else if (i + 1 == OperationList.size())
- {
- exit(0);
- }
- }
- }
- double Calculate(std::string FirstNumber, std::string Operation, std::string SecondNumber)
- {
- double result;
- char operation = Operation[0];
- double x = std::stoi(FirstNumber);
- double y = std::stoi(SecondNumber);
- if (operation == OperationList[0])
- {
- result = x + y;
- }
- else if (operation == OperationList[1])
- {
- result = x - y;
- }
- else if (operation == OperationList[2])
- {
- result = x * y;
- }
- else if (operation == OperationList[3])
- {
- result = x / y;
- }
- else if (operation == OperationList[4])
- {
- result = std::fmod(x, y);
- }
- else if (operation == OperationList[5])
- {
- result = pow(x, y);
- }
- return result;
- }
- double Round(double result)
- {
- std::vector<int> roundTo = {51, 75, 20};
- std::vector<int> minMax;
- std::sort(roundTo.begin(), roundTo.end());
- if (result < roundTo[0])
- {
- return roundTo[0];
- }
- if (result > roundTo[roundTo.size() - 1])
- {
- return roundTo[roundTo.size() - 1];
- }
- for (int i = 0; i < roundTo.size() - 1; i++)
- {
- if (result > roundTo[i] && result < roundTo[i + 1])
- {
- minMax.push_back(roundTo[i]);
- minMax.push_back(roundTo[i + 1]);
- }
- }
- if ((minMax[1] - minMax[0]) / 2 + minMax[0] <= result)
- {
- return minMax[1];
- }
- else
- {
- return minMax[0];
- }
- return result;
- }
- void ExitProgram()
- {
- std::string pause;
- system("CLS");
- std::cout << "Invalid Number";
- std::getline(std::cin, pause);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement