Skip to content

Commit bc7241a

Browse files
committedOct 10, 2023
Add exception handling
1 parent cdc184e commit bc7241a

7 files changed

+61
-76
lines changed
 

‎Calculator.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ int main(int argc, char** argv)
1111
std::string input;
1212
std::getline(std::cin, input);
1313
app.setInput(input);
14-
app.calculation();
14+
try
15+
{
16+
app.calculation();
17+
}
18+
catch(const std::runtime_error& error_message )
19+
{
20+
std::cerr << error_message.what() << std::endl;
21+
}
1522
}
1623

1724
return 0;

‎Calculator.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class Calculator
2121
}
2222
void calculation()
2323
{
24-
if (parser->parse(input, output))
25-
parser->evaluate(output);
24+
parser->parse(input, output);
25+
parser->evaluate(output);
2626
}
2727

2828
};

‎Operations.cpp

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
#include "Operations.h"
22

3-
int Operations::getPriority(const std::string& symbol)
3+
int Operations::getPriority(const std::string& symbols)
44
{
5-
if (this->operations.count(symbol) == 1)
6-
return this->operations[symbol]->getPriority();
7-
std::cerr << "Unavaliable operation: " << symbol << std::endl;
8-
return 1;
5+
if (operations.count(symbols) == 0)
6+
throw std::runtime_error("Unavaliable operation: " + symbols);
7+
return operations[symbols]->getPriority();
98
}
109

11-
bool Operations::getAssociativity(const std::string& symbol)
10+
bool Operations::getAssociativity(const std::string& symbols)
1211
{
13-
if (this->operations.count(symbol) == 1)
14-
return this->operations[symbol]->getAssociativity();
15-
std::cerr << "Unavaliable operation: " << symbol << std::endl;
16-
return true;
12+
if (operations.count(symbols) == 0)
13+
throw std::runtime_error("Unavaliable operation: " + symbols);
14+
return operations[symbols]->getAssociativity();
1715
}
1816

19-
int Operations::getBinary(const std::string& symbol)
17+
int Operations::getBinary(const std::string& symbols)
2018
{
21-
if (this->operations.count(symbol) == 1)
22-
return this->operations[symbol]->getBinary();
23-
std::cerr << "Unavaliable operation: " << symbol << std::endl;
24-
return 2;
19+
if (operations.count(symbols) == 0)
20+
throw std::runtime_error("Unavaliable operation: " + symbols);
21+
return operations[symbols]->getBinary();
2522
}
2623

27-
double Operations::calculation(const std::string& symbol, double a, double b)
24+
double Operations::calculation(const std::string& symbols, double a, double b)
2825
{
29-
if (this->operations.count(symbol) == 1)
30-
return this->operations[symbol]->calculation(a,b);
31-
std::cerr << "Unavaliable operation: " << symbol << std::endl;
32-
return 0;
33-
}
26+
if (operations.count(symbols) == 0)
27+
throw std::runtime_error("Unavaliable operation: " + symbols);
28+
return operations[symbols]->calculation(a, b);
29+
}

‎Parser.cpp

+31-49
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
#include "Parser.h"
22

3-
int Parser::opPriority(const std::string& symbol)
3+
int Parser::opPriority(const std::string& symbols)
44
{
5-
return this->operations->getPriority(symbol);
5+
return operations->getPriority(symbols);
66
}
77

8-
bool Parser::opAssociativity(const std::string& symbol)
8+
bool Parser::opAssociativity(const std::string& symbols)
99
{
10-
return this->operations->getAssociativity(symbol);
10+
return operations->getAssociativity(symbols);
1111
}
1212

13-
int Parser::opBinary(const std::string& symbol)
13+
int Parser::opBinary(const std::string& symbols)
1414
{
15-
return this->operations->getBinary(symbol);
15+
return operations->getBinary(symbols);
1616
}
1717

18-
bool Parser::isOperator(const std::string& symbol)
18+
bool Parser::isOperator(const std::string& symbols)
1919
{
20-
if (symbol == "(" || symbol == ")")
20+
if (symbols == "(" || symbols == ")")
2121
return false;
22-
bool acc = this->opAssociativity(symbol);
23-
int bin = this->opBinary(symbol);
24-
if ((acc == true && bin == 2) || (acc == false && bin == 1) )
22+
bool associativity = opAssociativity(symbols);
23+
int binary = opBinary(symbols);
24+
if ((associativity == true && binary == 2) || (associativity == false && binary == 1) )
2525
return true;
2626
return false;
2727
}
2828

29-
bool Parser::isFunction(const std::string& symbol)
29+
bool Parser::isFunction(const std::string& symbols)
3030
{
31-
if (symbol == "(" || symbol == ")")
31+
if (symbols == "(" || symbols == ")")
3232
return false;
33-
if (this->opAssociativity(symbol) == true && this->opBinary(symbol) == 1)
33+
if (opAssociativity(symbols) == true && opBinary(symbols) == 1)
3434
return true;
3535
return false;
3636
}
3737

38-
bool Parser::isIdent(const std::string& symbol)
38+
bool Parser::isIdent(const std::string& symbols)
3939
{
40-
return (symbol >= "0" && symbol <= "9");
40+
return (symbols >= "0" && symbols <= "9");
4141
}
4242

4343
bool Parser::isIdent(char symbol)
@@ -50,17 +50,17 @@ bool Parser::isLetter(char symbol)
5050
return (symbol >= 'a' && symbol <= 'z');
5151
}
5252

53-
bool Parser::isLetter(const std::string& symbol)
53+
bool Parser::isLetter(const std::string& symbols)
5454
{
55-
return (symbol >= "a" && symbol <= "z");
55+
return (symbols >= "a" && symbols <= "z");
5656
}
5757

5858
double Parser::calculation(const std::string& symbol, double a, double b)
5959
{
60-
return this->operations->calculation(symbol, a, b);
60+
return operations->calculation(symbol, a, b);
6161
}
6262

63-
bool Parser::parenthesesBalance(std::stack<std::string>& operations_, std::string& output)
63+
void Parser::parenthesesBalance(std::stack<std::string>& operations_, std::string& output)
6464
{
6565
std::string symbols;
6666
while (!operations_.empty())
@@ -73,10 +73,7 @@ bool Parser::parenthesesBalance(std::stack<std::string>& operations_, std::strin
7373
output += symbols + "|";
7474
}
7575
if (operations_.empty() && symbols != "(")
76-
{
77-
std::cerr << "Error: parentheses mismatched" << std::endl;
78-
return false;
79-
}
76+
throw std::runtime_error("Error: parentheses mismatched");
8077
if (!operations_.empty())
8178
{
8279
symbols = operations_.top();
@@ -86,7 +83,6 @@ bool Parser::parenthesesBalance(std::stack<std::string>& operations_, std::strin
8683
operations_.pop();
8784
}
8885
}
89-
return true;
9086
}
9187

9288
void Parser::operationPriorityArrangment(std::stack<std::string>& operations_, std::string& current_operator, std::string& output)
@@ -106,7 +102,7 @@ void Parser::operationPriorityArrangment(std::stack<std::string>& operations_, s
106102
operations_.push(current_operator);
107103
}
108104

109-
bool Parser::parse(const std::string& input, std::string& output)
105+
void Parser::parse(const std::string& input, std::string& output)
110106
{
111107
std::stack<std::string> operations_;
112108
int length = input.length();
@@ -119,10 +115,7 @@ bool Parser::parse(const std::string& input, std::string& output)
119115
if (current_substring == "(")
120116
operations_.push(current_substring);
121117
else if (current_substring == ")")
122-
{
123-
if (!parenthesesBalance(operations_, output))
124-
return false;
125-
}
118+
parenthesesBalance(operations_, output);
126119
else if (isIdent(current_substring))
127120
{
128121
int j = 0;
@@ -162,27 +155,20 @@ bool Parser::parse(const std::string& input, std::string& output)
162155
else if (isOperator(current_substring))
163156
operationPriorityArrangment(operations_, current_substring, output);
164157
else
165-
{
166-
std::cerr << "Unknown token in" << current_substring << std::endl;
167-
return false;
168-
}
158+
throw std::runtime_error("Unknown operation in " + current_substring);
169159
}
170160
}
171161
while (!operations_.empty())
172162
{
173163
std::string current_substring = operations_.top();
174164
operations_.pop();
175165
if (current_substring == "(" || current_substring == ")")
176-
{
177-
std::cerr << "Error: parentheses mismatched" << std::endl;
178-
return false;
179-
}
166+
throw std::runtime_error("Error: parentheses mismatched");
180167
output += current_substring + "|";
181168
}
182-
return true;
183169
}
184170

185-
bool Parser::evaluate(const std::string& input)
171+
void Parser::evaluate(const std::string& input)
186172
{
187173
int length = input.length();
188174
std::vector<std::string> operations_(length);
@@ -229,10 +215,7 @@ bool Parser::evaluate(const std::string& input)
229215
std::string res = "(" + std::to_string(iteration_of_calculation++) + ")";
230216
std::cout << res << " = ";
231217
if (last_index < arguments)
232-
{
233-
std::cerr << "Error: insufficient arguments in expression" << std::endl;
234-
return false;
235-
}
218+
throw std::runtime_error("Error: insufficient arguments in expression");
236219
std::string prev_substring = operations_[last_index - 1];
237220
double prev_value = values[last_index - 1];
238221
if (arguments == 1)
@@ -248,7 +231,7 @@ bool Parser::evaluate(const std::string& input)
248231
{
249232
std::string prev_substring1 = operations_[last_index - 2];
250233
double prev_value1 = values[last_index - 2];
251-
std::cout << prev_substring1 << " " << input[i] << " ";
234+
std::cout << prev_substring1 << " " << current_substring << " ";
252235
value = calculation(current_substring, prev_value1, prev_value);
253236
std::cout << prev_substring << " = " << value << std::endl;
254237
last_index -= 2;
@@ -264,11 +247,10 @@ bool Parser::evaluate(const std::string& input)
264247
std::string final_operation = operations_[last_index - 1];
265248
double final_value = values[last_index - 1];
266249
last_index--;
267-
std::cerr << "Result : " << final_operation << " = " << final_value << std::endl;
268-
return true;
250+
std::cout << "Result : " << final_operation << " = " << final_value << std::endl;
269251
}
270-
std::cerr << "Error: too many values entered by the user" << std::endl;
271-
return false;
252+
else
253+
throw std::runtime_error("Error: too many values entered by the user");
272254
}
273255

274256

‎Parser.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ class Parser
1818
bool isIdent(char symbol);
1919
bool isLetter(char symbol);
2020
bool isLetter(const std::string& symbol);
21-
bool parenthesesBalance(std::stack<std::string>& operations_, std::string& output);
21+
void parenthesesBalance(std::stack<std::string>& operations_, std::string& output);
2222
void operationPriorityArrangment(std::stack<std::string>& operations_, std::string& current_symbol, std::string& output);
2323

2424
public:
25-
bool parse(const std::string& input, std::string& output);
26-
bool evaluate(const std::string& input);
25+
void parse(const std::string& input, std::string& output);
26+
void evaluate(const std::string& input);
2727
double calculation(const std::string& symbol, double a, double b);
2828

2929
Parser(const std::string& folder, const std::string& extension)

‎plugins/DynamicLIb7.dll

81 KB
Binary file not shown.

‎plugins/DynamicLib4.dll

-82.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.