#include <ostream> #include <cmath> using namespace std; string calculator(int a, char op, int b) { stringstream s; switch (op) { case '+': {s<<a+b; return s.str();} case '-': {s<<a-b; return s.str();} case '*': {s<<a*b; return s.str();} case '/': {b != 0 ? s<<a/b : s<<"Invalid Input!"; return s.str();} case '%': {s<<a%b; return s.str();} case '^': {s<<pow((float)a, b); return s.str();} default: return "Invalid Input!"; } }
- #include <ostream>
- #include <cmath>
- using namespace std;
- string calculator(int a, char op, int b) {
- stringstream s;
- switch (op) {
- case '+': {s<<a+b; return s.str();}
- case '-': {s<<a-b; return s.str();}
- case '*': {s<<a*b; return s.str();}
- case '/': {b != 0 ? s<<a/b : s<<"Invalid Input!"; return s.str();}
- case '%': {s<<a%b; return s.str();}
- case '^': {s<<pow((float)a, b); return s.str();}
- default: return "Invalid Input!";
- }
- }
Describe(Sample_Tests) { It(Test) { Assert::That(calculator(420, 0, 420), Equals("Invalid Input!")); Assert::That(calculator( 1, '+', 1), Equals("2")); Assert::That(calculator( 16, '+',9), Equals("25")); Assert::That(calculator( 1, '-',1), Equals("0")); Assert::That(calculator( 57,'-', 62), Equals("-5")); Assert::That(calculator(-10,'*', 10), Equals("-100")); Assert::That(calculator(-12,'*', -12), Equals("144")); Assert::That(calculator( 2, '/',4), Equals("0")); Assert::That(calculator( 4, '/',2), Equals("2")); Assert::That(calculator( 1, '/',0), Equals("Invalid Input!")); Assert::That(calculator( 3, '%',5), Equals("3")); Assert::That(calculator( 5,'%', 3), Equals("2")); Assert::That(calculator(6, 69, 69), Equals("Invalid Input!")); Assert::That(calculator(2, '^', 1), Equals("2")); Assert::That(calculator(1, '^', 0), Equals("1")); Assert::That(calculator(2, '^', 3), Equals("8")); } };
- Describe(Sample_Tests) {
- It(Test) {
- Assert::That(calculator(420, 0, 420), Equals("Invalid Input!"));
- Assert::That(calculator( 1, '+', 1), Equals("2"));
- Assert::That(calculator( 16, '+',9), Equals("25"));
- Assert::That(calculator( 1, '-',1), Equals("0"));
- Assert::That(calculator( 57,'-', 62), Equals("-5"));
- Assert::That(calculator(-10,'*', 10), Equals("-100"));
- Assert::That(calculator(-12,'*', -12), Equals("144"));
- Assert::That(calculator( 2, '/',4), Equals("0"));
- Assert::That(calculator( 4, '/',2), Equals("2"));
- Assert::That(calculator( 1, '/',0), Equals("Invalid Input!"));
- Assert::That(calculator( 3, '%',5), Equals("3"));
- Assert::That(calculator( 5,'%', 3), Equals("2"));
- Assert::That(calculator(6, 69, 69), Equals("Invalid Input!"));
- Assert::That(calculator(2, '^', 1), Equals("2"));
- Assert::That(calculator(1, '^', 0), Equals("1"));
- Assert::That(calculator(2, '^', 3), Equals("8"));
- }
- };
Inlined