diff options
| author | jantuomi <jans.tuomi@gmail.com> | 2016-09-01 13:47:24 +0300 |
|---|---|---|
| committer | jantuomi <jans.tuomi@gmail.com> | 2016-09-01 13:47:24 +0300 |
| commit | 53e30cdd779f12ba5b19a59e9e18abf69c795ad5 (patch) | |
| tree | 4c479058450591e6dc7f79bd1fdeaab1b2d2d99e /src | |
| parent | 41f1e410b7cc48db4ec0b99d9d2b49d2edb2a315 (diff) | |
Add sum function
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 2 | ||||
| -rw-r--r-- | src/runtime.cpp | 29 | ||||
| -rw-r--r-- | src/runtime.h | 7 |
3 files changed, 29 insertions, 9 deletions
diff --git a/src/main.cpp b/src/main.cpp index 53a6540..8a00d55 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,5 +37,7 @@ int main(int argc, char** argv) { runtime.initValues(values); runtime.run(tokens); + runtime.printValues(); + return 0; } diff --git a/src/runtime.cpp b/src/runtime.cpp index 26c5885..3f61060 100644 --- a/src/runtime.cpp +++ b/src/runtime.cpp @@ -1,4 +1,5 @@ #include "runtime.h" +#include <iostream> Runtime::Runtime() { @@ -6,20 +7,34 @@ Runtime::Runtime() { void Runtime::initValues(const std::vector<int>& values) { for (const int i : values) { - m_values.push(i); + m_values.push_back(i); } } -/* Returns false if token is not a command */ -bool Runtime::runCommand(const Token& token) { - // TODO - return false; +void Runtime::runCommand(const Token& token) { + if (token.getLexeme() == "S") { + doSum(); + } } void Runtime::run(std::vector<Token>& tokens) { for (auto& token : tokens) { - if (!runCommand(token)) { + runCommand(token); + } +} - } +void Runtime::printValues() const { + for (const int value : m_values) { + std::cout << value << " "; } + std::cout << std::endl; +} + +void Runtime::doSum() { + int sum = 0; + for (const int i : m_values) { + sum += i; + } + + m_values.push_back(sum); } diff --git a/src/runtime.h b/src/runtime.h index af8351f..16c1c28 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -5,11 +5,14 @@ class Runtime { private: - std::stack<int> m_values; - bool runCommand(const Token& token); + std::vector<int> m_values; + void runCommand(const Token& token); + + void doSum(); public: Runtime(); void initValues(const std::vector<int>& values); void run(std::vector<Token>& tokens); + void printValues() const; }; |
