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