Skip to content
Snippets Groups Projects
Commit faf1d371 authored by Jacek Galowicz's avatar Jacek Galowicz
Browse files

Merge branch 'master' into 'master'

code style changes

See merge request jg943598/rpn_calculator!15
parents f8f63d21 43c7442b
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,7 @@
/*
* Adds two integers
*/
int add(int a, int b) {
int add(const int a, const int b) {
int res;
bool overflow = __builtin_sadd_overflow(a, b, &res);
if (overflow) {
......@@ -21,7 +21,7 @@ int add(int a, int b) {
/*
* Multiplies two integers
*/
int multiply(int a, int b) {
int multiply(const int a, const int b) {
int res;
bool overflow = __builtin_smul_overflow(a, b, &res);
if (overflow) {
......@@ -33,7 +33,7 @@ int multiply(int a, int b) {
/*
* Subtracts integer b from integer a
*/
int subtract(int a, int b) {
int subtract(const int a, const int b) {
int res;
bool overflow = __builtin_ssub_overflow(b, a, &res);
if (overflow) {
......@@ -46,7 +46,7 @@ int subtract(int a, int b) {
* Divides integer b from integer a, throwing an exception when b is zero.
* Throws an std::invalid_argument exceptions if invalid parameters are provided.
*/
int divide(int a, int b) {
int divide(const int a, const int b) {
if (b == 0) {
throw std::invalid_argument("Malformed input. Division by zero.");
}
......@@ -55,15 +55,13 @@ int divide(int a, int b) {
using binary_operator = decltype(&add);
std::unordered_map<char, binary_operator> map {
{'+', add},
{'-', subtract},
{'*', multiply},
{'/', divide}
};
int evaluate(const std::string &s) {
int a, b;
static const std::unordered_map<char, binary_operator> map {
{'+', add},
{'-', subtract},
{'*', multiply},
{'/', divide}
};
decltype(map)::iterator iter;
std::stack<int> stack;
......@@ -73,9 +71,9 @@ int evaluate(const std::string &s) {
} else if (stack.size() >= 2 ) {
iter = map.find(c);
if (iter != map.end()) {
a = stack.top();
const auto a = stack.top();
stack.pop();
b = stack.top();
const auto b = stack.top();
stack.pop();
stack.push(iter->second(b, a));
} else {
......@@ -112,5 +110,4 @@ int main() {
if (result != INT_MAX) {
std::cout << "Result is: " << result << '\n';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment