From f30020384f816b498fe1f6013758a8de37811821 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 29 Dec 2013 02:04:19 +0100 Subject: Firt commit of the new dice system for rolisteam. --- node/scalaroperatornode.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 node/scalaroperatornode.cpp (limited to 'node/scalaroperatornode.cpp') diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp new file mode 100644 index 0000000..e8a2ec0 --- /dev/null +++ b/node/scalaroperatornode.cpp @@ -0,0 +1,91 @@ +#include "scalaroperatornode.h" + +#include + +ScalarOperatorNode::ScalarOperatorNode() + : m_internalNode(NULL) +{ + m_scalarOperationList.insert('+',PLUS); + m_scalarOperationList.insert('-',MINUS); + m_scalarOperationList.insert('x',MULTIPLICATION); + m_scalarOperationList.insert('*',MULTIPLICATION); + m_scalarOperationList.insert('/',DIVIDE); + m_scalarOperationList.insert('รท',DIVIDE); +} + +void ScalarOperatorNode::run(ExecutionNode* previous) +{ + if(NULL!=m_internalNode) + { + m_internalNode->run(this); + } + if(NULL!=previous) + { + DiceResult* previousResult = previous->getResult(); + ExecutionNode* internal = m_internalNode; + while(NULL != internal->getNextNode() ) + { + internal = internal->getNextNode(); + } + DiceResult* internalResult = internal->getResult(); + + switch(m_myOperator) + { + case PLUS: + m_result.insertResult(add(previousResult->getSum(),internalResult->getSum())); + break; + case MINUS: + m_result.insertResult(substract(previousResult->getSum(),internalResult->getSum())); + break; + case MULTIPLICATION: + m_result.insertResult(multiple(previousResult->getSum(),internalResult->getSum())); + break; + case DIVIDE: + m_result.insertResult(divide(previousResult->getSum(),internalResult->getSum())); + break; + default: + break; + + } + + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } + } + +} +bool ScalarOperatorNode::setOperatorChar(QChar c) +{ + if(m_scalarOperationList.contains(c)) + { + m_myOperator = m_scalarOperationList.value(c); + return true; + } + return false; +} + + +void ScalarOperatorNode::setInternalNode(ExecutionNode* node) +{ + m_internalNode = node; +} +qint64 ScalarOperatorNode::add(qint64 a,qint64 b) +{ + return a+b; +} + +qint64 ScalarOperatorNode::substract(qint64 a,qint64 b) +{ + return a-b; +} + +qint64 ScalarOperatorNode::divide(qint64 a,qint64 b) +{ + return a/b; +} + +qint64 ScalarOperatorNode::multiple(qint64 a,qint64 b) +{ + return a*b; +} -- cgit v1.2.3-70-g09d2