From 9b2839b6f09177bda6b3654c56a750f84173123a Mon Sep 17 00:00:00 2001 From: rguezennec Date: Thu, 27 Apr 2017 18:17:03 +0200 Subject: -management of arithmetic operator for scalar result of dice. --- result/diceresult.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 8 deletions(-) (limited to 'result/diceresult.cpp') diff --git a/result/diceresult.cpp b/result/diceresult.cpp index aa39524..2825eec 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -87,16 +87,56 @@ qreal DiceResult::getScalarResult() } else { - qint64 scalarSum = 0; - foreach(Die* die,m_diceValues) - { - scalarSum+=die->getValue(); - } - return scalarSum; + qint64 scalar=0; + int i = 0; + for(auto tmp : m_diceValues) + { + if(i>0) + { + switch(m_operator) + { + case Die::PLUS: + scalar+=tmp->getValue(); + break; + case Die::MULTIPLICATION: + scalar*=tmp->getValue(); + break; + case Die::MINUS: + scalar-=tmp->getValue(); + break; + case Die::DIVIDE: + if(tmp!=0) + { + scalar/=tmp->getValue(); + } + else + { + //error(); + } + break; + } + } + else + { + scalar=tmp; + } + ++i; + } + return scalar; } return 0; } + +Die::ArithmeticOperator DiceResult::getOperator() const +{ + return m_operator; +} + +void DiceResult::setOperator(const Die::ArithmeticOperator& dieOperator) +{ + m_operator = dieOperator; +} QString DiceResult::toString(bool wl) { QStringList scalarSum; @@ -104,8 +144,8 @@ QString DiceResult::toString(bool wl) { scalarSum << QString::number(die->getValue()); } - if(wl) - { + if(wl) + { return QStringLiteral("%3 [label=\"DiceResult Value %1 dice %2\"]").arg(getScalarResult()).arg(scalarSum.join('_')).arg(m_id); } else -- cgit v1.2.3-70-g09d2