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