Skip to content
Snippets Groups Projects
Commit 6e64d15e authored by Niklas Hannekotte's avatar Niklas Hannekotte
Browse files
parents 131ee5b8 1c945b35
No related branches found
No related tags found
1 merge request!5Fix missing operands
......@@ -2,11 +2,13 @@
#include <stack>
#include <stdexcept>
#include <cmath>
int plus( 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 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 pop_stack(auto &stack) {
......@@ -30,6 +32,7 @@ int evaluate(const std::string &s) {
case '-': change_stack_state(minus, stack); break;
case '*': change_stack_state(multiply, stack); break;
case '/': change_stack_state(divide, stack); break;
case '^': change_stack_state(power, stack); break;
case '%': change_stack_state(modulo, stack); break;
case '0'...'9': stack.push(c - '0'); break;
default:
......
......@@ -7,6 +7,12 @@ TEST_CASE( "some example calculations", "[calc]") {
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]"){
REQUIRE( evaluate("55+") == 10);
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