| | | 1 | | #include "../header/expressionParser.h" |
| | | 2 | | #include "../header/calculator.h" |
| | | 3 | | |
| | | 4 | | #include <cctype> |
| | | 5 | | #include <sstream> |
| | | 6 | | #include <stack> |
| | | 7 | | #include <stdexcept> |
| | | 8 | | |
| | | 9 | | using namespace Coruh::Calculator; |
| | | 10 | | |
| | 158 | 11 | | bool ExpressionParser::isOperator(char c) { |
| | 158 | 12 | | return (c == '+' || c == '-' || c == '*' || c == '/'); |
| | | 13 | | } |
| | | 14 | | |
| | 14 | 15 | | int ExpressionParser::precedence(char c) { |
| | 14 | 16 | | if(c == '+' || c == '-') return 1; |
| | 7 | 17 | | if(c == '*' || c == '/') return 2; |
| | 0 | 18 | | return 0; |
| | | 19 | | } |
| | | 20 | | |
| | 38 | 21 | | std::string ExpressionParser::toPostfix(const std::string& infix) { |
| | 38 | 22 | | std::stack<char> s; |
| | 38 | 23 | | std::ostringstream postfix; |
| | 38 | 24 | | bool expectOperand = true; // true at the start, after an operator and after '(' |
| | | 25 | | |
| | 182 | 26 | | for(size_t i = 0; i < infix.size(); ++i) { |
| | 156 | 27 | | char c = infix[i]; |
| | | 28 | | |
| | 156 | 29 | | if(std::isdigit(static_cast<unsigned char>(c)) || c == '.') { |
| | 71 | 30 | | if(!expectOperand) { |
| | 1 | 31 | | throw std::invalid_argument("Missing operator between numbers."); |
| | | 32 | | } |
| | | 33 | | // read the whole number (digits and at most one decimal point) |
| | 70 | 34 | | std::string number; |
| | 70 | 35 | | bool seenDot = false; |
| | 161 | 36 | | while(i < infix.size() && (std::isdigit(static_cast<unsigned char>(infix[i])) || infix[i] == '.')) { |
| | 92 | 37 | | if(infix[i] == '.') { |
| | 10 | 38 | | if(seenDot) { |
| | 1 | 39 | | throw std::invalid_argument("A number has more than one decimal point."); |
| | | 40 | | } |
| | 9 | 41 | | seenDot = true; |
| | | 42 | | } |
| | 91 | 43 | | number += infix[i++]; |
| | | 44 | | } |
| | 69 | 45 | | --i; |
| | 69 | 46 | | if(number == ".") { |
| | 2 | 47 | | throw std::invalid_argument("A decimal point without digits is not a number."); |
| | | 48 | | } |
| | 67 | 49 | | postfix << number << ' '; |
| | 67 | 50 | | expectOperand = false; |
| | 155 | 51 | | } else if(isOperator(c)) { |
| | 42 | 52 | | if(expectOperand) { |
| | 2 | 53 | | throw std::invalid_argument("An operator is missing its left operand."); |
| | | 54 | | } |
| | 43 | 55 | | while(!s.empty() && s.top() != '(' && precedence(s.top()) >= precedence(c)) { |
| | 3 | 56 | | postfix << s.top() << ' '; |
| | 3 | 57 | | s.pop(); |
| | | 58 | | } |
| | 40 | 59 | | s.push(c); |
| | 40 | 60 | | expectOperand = true; |
| | 43 | 61 | | } else if(c == '(') { |
| | 13 | 62 | | if(!expectOperand) { |
| | 1 | 63 | | throw std::invalid_argument("Missing operator before '('."); |
| | | 64 | | } |
| | 12 | 65 | | s.push(c); |
| | 30 | 66 | | } else if(c == ')') { |
| | 12 | 67 | | if(expectOperand) { |
| | 2 | 68 | | throw std::invalid_argument("Empty parentheses or an operator before ')'."); |
| | | 69 | | } |
| | 18 | 70 | | while(!s.empty() && s.top() != '(') { |
| | 8 | 71 | | postfix << s.top() << ' '; |
| | 8 | 72 | | s.pop(); |
| | | 73 | | } |
| | 10 | 74 | | if(s.empty()) { |
| | 1 | 75 | | throw std::invalid_argument("Unmatched ')'."); |
| | | 76 | | } |
| | 9 | 77 | | s.pop(); // discard '(' |
| | 18 | 78 | | } else if(!std::isspace(static_cast<unsigned char>(c))) { |
| | 6 | 79 | | throw std::invalid_argument(std::string("Unexpected character '") + c + "'."); |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | 26 | 83 | | if(expectOperand) { |
| | 3 | 84 | | throw std::invalid_argument(infix.find_first_not_of(" \t\r\n") == std::string::npos |
| | | 85 | | ? "The expression is empty." |
| | 3 | 86 | | : "The expression ends with an operator."); |
| | | 87 | | } |
| | | 88 | | |
| | 48 | 89 | | while(!s.empty()) { |
| | 26 | 90 | | if(s.top() == '(') { |
| | 1 | 91 | | throw std::invalid_argument("Unmatched '('."); |
| | | 92 | | } |
| | 25 | 93 | | postfix << s.top() << ' '; |
| | 25 | 94 | | s.pop(); |
| | | 95 | | } |
| | | 96 | | |
| | 44 | 97 | | return postfix.str(); |
| | 54 | 98 | | } |
| | | 99 | | |
| | 23 | 100 | | double ExpressionParser::evaluatePostfix(const std::string& postfix) { |
| | 23 | 101 | | std::stack<double> s; |
| | 23 | 102 | | std::istringstream iss(postfix); |
| | 23 | 103 | | std::string token; |
| | | 104 | | |
| | 105 | 105 | | while(iss >> token) { |
| | 85 | 106 | | if(token.size() == 1 && isOperator(token[0])) { |
| | 31 | 107 | | if(s.size() < 2) { |
| | 1 | 108 | | throw std::invalid_argument("Not enough operands for '" + token + "'."); |
| | | 109 | | } |
| | 30 | 110 | | double b = s.top(); s.pop(); |
| | 30 | 111 | | double a = s.top(); s.pop(); |
| | 30 | 112 | | double result = 0.0; |
| | | 113 | | |
| | 30 | 114 | | switch(token[0]) { |
| | 13 | 115 | | case '+': result = Calculator::add(a, b); break; |
| | 5 | 116 | | case '-': result = Calculator::subtract(a, b); break; |
| | 7 | 117 | | case '*': result = Calculator::multiply(a, b); break; |
| | 5 | 118 | | case '/': |
| | 5 | 119 | | if (b == 0) { |
| | 1 | 120 | | throw std::invalid_argument("Division by zero is not allowed."); |
| | | 121 | | } |
| | 4 | 122 | | result = Calculator::divide(a, b); break; |
| | | 123 | | } |
| | | 124 | | |
| | 29 | 125 | | s.push(result); |
| | | 126 | | } else { |
| | | 127 | | try { |
| | 54 | 128 | | s.push(std::stod(token)); |
| | 1 | 129 | | } catch(const std::exception&) { |
| | 1 | 130 | | throw std::invalid_argument("'" + token + "' is not a valid number."); |
| | 1 | 131 | | } |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | 20 | 135 | | if(s.size() != 1) { |
| | 1 | 136 | | throw std::invalid_argument("The expression is not well formed."); |
| | | 137 | | } |
| | 38 | 138 | | return s.top(); |
| | 31 | 139 | | } |
| | | 140 | | |
| | 36 | 141 | | double ExpressionParser::evaluateInfix(const std::string& infix) { |
| | 36 | 142 | | return evaluatePostfix(toPostfix(infix)); |
| | | 143 | | } |