From abb8f292abece62f76e9573b4a5367609e14e54d Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 21 Mar 2007 16:01:50 +0000 Subject: [PATCH] added: into the parser: SetFactorialMax() git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@24 e52654a7-88a9-db11-a3e9-0013d4bc506e --- ttmath/ttmathparser.h | 29 +++++++++++++++++++++++++++-- ttmath/ttmathtypes.h | 3 ++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/ttmath/ttmathparser.h b/ttmath/ttmathparser.h index 2a60f09..aedd014 100644 --- a/ttmath/ttmathparser.h +++ b/ttmath/ttmathparser.h @@ -81,9 +81,9 @@ namespace ttmath == (equal) != (not equal) (all above logical operators have the same priority) - and (logical and) + && (logical and) - or (logical or) (the lowest priority) + || (logical or) (the lowest priority) and Value can be: @@ -393,6 +393,14 @@ typedef std::map VariablesTable; VariablesTable variables_table; + +/*! + you can't calculate the factorial if the argument is greater than 'factorial_max' + default value is zero which means there are not any limitations +*/ +ValueType factorial_max; + + /*! we're using this method for reporting an error */ @@ -601,6 +609,9 @@ void Factorial(int sindex, int amount_of_args, ValueType & result) ErrorCode err; + if( !factorial_max.IsZero() && stack[sindex].value > factorial_max ) + Error( err_too_big_factorial ); + result = ttmath::Factorial(stack[sindex].value, &err, pstop_calculating); if(err != err_ok) @@ -1833,6 +1844,7 @@ Parser(): default_stack_size(100) pfunction_local_variables = 0; base = 10; error = err_ok; + factorial_max.SetZero(); CreateFunctionsTable(); CreateVariablesTable(); @@ -1851,6 +1863,7 @@ Parser & operator=(const Parser & p) pfunction_local_variables = 0; base = p.base; error = err_ok; + factorial_max = p.factorial_max; /* we don't have to call 'CreateFunctionsTable()' etc. @@ -1920,6 +1933,18 @@ void SetFunctions(const Objects * pf) } +/*! + you will not be allowed to calculate the factorial + if its argument is greater than 'm' + there'll be: ErrorCode::err_too_big_factorial + default 'factorial_max' is zero which means you can calculate what you want to +*/ +void SetFactorialMax(const ValueType & m) +{ + factorial_max = m; +} + + /*! the main method using for parsing string */ diff --git a/ttmath/ttmathtypes.h b/ttmath/ttmathtypes.h index bdfceb4..f5b9549 100644 --- a/ttmath/ttmathtypes.h +++ b/ttmath/ttmathtypes.h @@ -219,7 +219,8 @@ namespace ttmath err_must_be_only_one_value, err_object_exists, err_unknown_object, - err_still_calculating + err_still_calculating, + err_too_big_factorial };