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

Merge branch 'master' into 'master'

Added '^' (power) operator

See merge request jg943598/rpn_collab!6
parents 8c57682e 4bf70d28
No related branches found
No related tags found
No related merge requests found
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
#include <stack> #include <stack>
#include <stdexcept> #include <stdexcept>
#include <cmath>
int plus( int a, int b) { return a + b; } int plus( int a, int b) { return a + b; }
int minus( int a, int b) { return a - b; } int minus( int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; } int multiply(int a, int b) { return a * b; }
int divide( int a, int b) { return a / b; } int divide( int a, int b) { return a / b; }
int power( int a, int b) { return std::pow(a, b); }
int modulo( int a, int b) { return a % b; } int modulo( int a, int b) { return a % b; }
int pop_stack(auto &stack) { int pop_stack(auto &stack) {
...@@ -30,6 +32,7 @@ int evaluate(const std::string &s) { ...@@ -30,6 +32,7 @@ int evaluate(const std::string &s) {
case '-': change_stack_state(minus, stack); break; case '-': change_stack_state(minus, stack); break;
case '*': change_stack_state(multiply, stack); break; case '*': change_stack_state(multiply, stack); break;
case '/': change_stack_state(divide, stack); break; case '/': change_stack_state(divide, stack); break;
case '^': change_stack_state(power, stack); break;
case '%': change_stack_state(modulo, stack); break; case '%': change_stack_state(modulo, stack); break;
case '0'...'9': stack.push(c - '0'); break; case '0'...'9': stack.push(c - '0'); break;
default: default:
......
...@@ -7,6 +7,12 @@ TEST_CASE( "some example calculations", "[calc]") { ...@@ -7,6 +7,12 @@ TEST_CASE( "some example calculations", "[calc]") {
REQUIRE( evaluate("12+2-3*") == 3 ); REQUIRE( evaluate("12+2-3*") == 3 );
} }
TEST_CASE( "pow function works", "[calc]") {
REQUIRE( evaluate("22^") == 4 );
REQUIRE( evaluate("43^") == 64 );
REQUIRE( evaluate("11^") == 1);
}
TEST_CASE( "basic addition", "[calc]"){ TEST_CASE( "basic addition", "[calc]"){
REQUIRE( evaluate("55+") == 10); REQUIRE( evaluate("55+") == 10);
REQUIRE( evaluate("34+") == 7); REQUIRE( evaluate("34+") == 7);
......
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