From bf520689fb5e6f55935f84499d0d8ce3d9fdb810 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 18 Oct 2009 12:37:14 +0000 Subject: [PATCH] added: to the parser: operator percentage e.g. 1000-50%=1000-(1000*0,5)=500 git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@217 e52654a7-88a9-db11-a3e9-0013d4bc506e --- CHANGELOG | 4 +++- ttmath/ttmathparser.h | 32 ++++++++++++++++++++++++++++++-- ttmath/ttmathtypes.h | 3 ++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index a34e9d0..190d8d1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,4 @@ -Version 0.9.0 prerelease (2009.10.16): +Version 0.9.0 prerelease (2009.10.18): * fixed: Big::operator>>(std::istream&, Big<> &) didn't recognize values in scientific mode (with 'e' character) * fixed: UInt::SetBitInWord(uint & value, uint bit) set 1 if the bit was @@ -42,6 +42,8 @@ Version 0.9.0 prerelease (2009.10.16): * added: bool Parser::Calculated() this method returns true is something was calculated (at least one mathematical operator was used or a function or variable) + * added: to the parser: operator percentage + e.g. 1000-50%=1000-(1000*0,5)=500 * changed: Factorial() is using the Gamma() function now * changed: Big::Div(ss2) Big::Mod(ss2) diff --git a/ttmath/ttmathparser.h b/ttmath/ttmathparser.h index 0acd575..8acb616 100644 --- a/ttmath/ttmathparser.h +++ b/ttmath/ttmathparser.h @@ -1939,6 +1939,31 @@ typename OperatorsTable::iterator iter_old, iter_new; } +/*! + this method makes a calculation for the percentage operator + e.g. + 1000-50% = 1000-(1000*0,5) = 500 +*/ +void OperatorPercentage() +{ + if( stack_index < 3 || + stack[stack_index-1].type != Item::numerical_value || + stack[stack_index-2].type != Item::mat_operator || + stack[stack_index-3].type != Item::numerical_value ) + Error(err_percent_from); + + ++pstring; + SkipWhiteCharacters(); + + uint c = 0; + c += stack[stack_index-1].value.Div(100); + c += stack[stack_index-1].value.Mul(stack[stack_index-3].value); + + if( c ) + Error(err_overflow); +} + + /*! this method reads a mathematic operators or the final bracket or the semicolon operator @@ -1950,7 +1975,11 @@ typename OperatorsTable::iterator iter_old, iter_new; int ReadOperator(Item & result) { SkipWhiteCharacters(); - + + if( *pstring == '%' ) + OperatorPercentage(); + + if( *pstring == 0 ) return 1; else @@ -2055,7 +2084,6 @@ uint res; break; - default: /* on the stack left an unknown operator but we had to recognize its before diff --git a/ttmath/ttmathtypes.h b/ttmath/ttmathtypes.h index 8d3e533..98399f0 100644 --- a/ttmath/ttmathtypes.h +++ b/ttmath/ttmathtypes.h @@ -331,7 +331,8 @@ namespace ttmath err_object_exists, err_unknown_object, err_still_calculating, - err_in_short_form_used_function + err_in_short_form_used_function, + err_percent_from };