From 117d43a4af8e4bd8095ae3da90bdced98e445b0a Mon Sep 17 00:00:00 2001 From: cxl Date: Tue, 23 Jan 2018 08:57:03 +0000 Subject: [PATCH] .examples git-svn-id: svn://ultimatepp.org/upp/trunk@11719 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- examples/FnGraph/main.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/examples/FnGraph/main.cpp b/examples/FnGraph/main.cpp index 16171329f..2988c2465 100644 --- a/examples/FnGraph/main.cpp +++ b/examples/FnGraph/main.cpp @@ -57,6 +57,8 @@ double ExpressionEvaluator::Factor() return fabs(Factor()); if(p.Id("sqrt")) return sqrt(Factor()); + if(p.Id("exp")) + return exp(Factor()); if(p.Id("ln")) return log(Factor()); if(p.Id("log")) @@ -93,12 +95,26 @@ double ExpressionEvaluator::Factor() return M_E; if(p.Id("pi")) // pi constant return M_PI; + double x; if(p.Char('(')) { // resolve parenthesis - recurse back to Sum (+ - operators) - double y = Expression(); + x = Expression(); p.PassChar(')'); // make sure there is closing parenthesis - return y; } - return p.ReadDouble(); // last possibility is that we are at number... + else + x = p.ReadDouble(); // last possibility is that we are at number... + if(p.Char('!')) { // compute factorial + if(x >= 0 && x < 120) { + int n = (int)x; + if(n == x) { + x = 1; + for(int q = 1; q <= n; q++) + x *= q; + return x; + } + } + p.ThrowError("invalid argument"); + } + return x; } double Evaluate(const char *s, double x)