#include <vector> #include <numeric> long add_arr(std::vector<int> arr) { return std::accumulate(arr.begin(),arr.end(),0); }
- #include <vector>
- #include <numeric>
- long
- add_arr(std::vector<int> arr)
- {
long result = 0;while(arr.size()) {result += arr[0];arr.erase(arr.begin());}return result;- return std::accumulate(arr.begin(),arr.end(),0);
- }
// TODO: Replace examples and use TDD by writing your own tests Describe(sampleTests) { It(shouldBe) { Assert::That(doubleValue(5), Equals(10)); Assert::That(doubleValue(6), Equals(12)); Assert::That(doubleValue(7), Equals(14)); Assert::That(doubleValue(8), Equals(16)); } };
- // TODO: Replace examples and use TDD by writing your own tests
- Describe(sampleTests) {
- It(shouldBe) {
- Assert::That(doubleValue(5), Equals(10));
- Assert::That(doubleValue(6), Equals(12));
- Assert::That(doubleValue(7), Equals(14));
- Assert::That(doubleValue(8), Equals(16));
- }
- };
#include <ostream> using namespace std; string calculator(char op, int a, 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();} default: return "Invalid Input!"; } }
#include <iostream>- #include <ostream>
- using namespace std;
const char * calculator(int op, int a, int b) {- string calculator(char op, int a, int b) {
- stringstream s;
- switch (op) {
case 1: return to_string(a + b).c_str();case 2: return to_string(a - b).c_str();case 3: return to_string(a * b).c_str();case 4: return b != 0 ? to_string(a / b).c_str() : "Invalid Input!";case 5: return to_string(a % b).c_str();- 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();}
- default: return "Invalid Input!";
- }
- }
Describe(Sample_Tests) { It(Test) { Assert::That(calculator(0, 420, 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!")); } };
- Describe(Sample_Tests) {
- It(Test) {
- Assert::That(calculator(0, 420, 420), Equals("Invalid Input!"));
Assert::That(calculator(1, 1, 1), Equals("2"));Assert::That(calculator(1, 16, 9), Equals("25"));Assert::That(calculator(2, 1, 1), Equals("0"));Assert::That(calculator(2, 57, 62), Equals("-5"));Assert::That(calculator(3, -10, 10), Equals("-100"));Assert::That(calculator(3, -12, -12), Equals("144"));Assert::That(calculator(4, 2, 4), Equals("0"));Assert::That(calculator(4, 4, 2), Equals("2"));Assert::That(calculator(4, 1, 0), Equals("Invalid Input!"));Assert::That(calculator(5, 3, 5), Equals("3"));Assert::That(calculator(5, 5, 3), Equals("2"));- 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!"));
- }
- };
#include <ostream> using namespace std; string calculator(int op, int a, int b) { ostringstream s; switch (op) { case 1: { s<<a+b; return s.str(); } case 2: { s<<a-b; return s.str(); } case 3: { s<<a*b; return s.str(); } case 4: { if (b!= 0) { s<<a/b; return s.str(); } else return "Invalid Input!"; } case 5: { s<<a%b; return s.str(); } default: return "Invalid Input!"; } }
#include <array>#include <stdexcept>- #include <ostream>
- using namespace std;
- string calculator(int op, int a, int b)
- {
- ostringstream s;
- switch (op)
- {
- case 1:
return to_string(a+b);- {
- s<<a+b;
- return s.str();
- }
- case 2:
return to_string(a-b);- {
- s<<a-b;
- return s.str();
- }
- case 3:
return to_string(a*b);- {
- s<<a*b;
- return s.str();
- }
- case 4:
- {
- if (b!= 0)
return to_string(a/b);- {
- s<<a/b;
- return s.str();
- }
- else
- return "Invalid Input!";
- }
- case 5:
return to_string(a%b);- {
- s<<a%b;
- return s.str();
- }
- default:
- return "Invalid Input!";
- }
- }