Exemple #1
0
        private static void EvaluateFunction(Function function, String expression)
        {
            try {

                Expression expr = new Expression( expression, Factory );
                BigNum num = expr.Evaluate( _symbols );

                BigNum result = EvaluateFunction(function, num);

                Console.WriteLine("Result: " + result.ToString() );

            } catch(Exception ex) {

                PrintException(ex);
            }
        }
Exemple #2
0
        private static Boolean PromptUser()
        {
            Console.Write( (++_count).ToString() );
            Console.Write(">");

            String s = Console.ReadLine();
            if( s == "q" ) return false;

            if( s.StartsWith("add ", StringComparison.OrdinalIgnoreCase) ) {

                String name = s.Substring(4, s.IndexOf('=') - 5 ).Trim();
                String expr = s.Substring( s.IndexOf('=') + 1 ).Trim();

                try {

                    Expression xp = new Expression( expr, Factory );
                    BigNum ret = xp.Evaluate( _symbols );

                    if( _symbols.ContainsKey( name ) ) _symbols[name] = ret;
                    else                               _symbols.Add( name, ret );

                    Console.WriteLine("Added: " + name + " = " + ret.ToString() );

                } catch(Exception ex) {

                    PrintException(ex);
                }

            } else if( s.StartsWith("rem ", StringComparison.OrdinalIgnoreCase) ) {

                String name = s.Substring(4);

                _symbols.Remove( name );

                Console.WriteLine("Removed: " + name);

            } else if( String.Equals(s, "help", StringComparison.OrdinalIgnoreCase) ) {

                PrintHelp();

            } else if( String.Equals(s, "test", StringComparison.OrdinalIgnoreCase) ) {

            //				BigNumTests.Test();

            } else {

                Int32 startIndex;
                Function func = IsFunction( s, out startIndex );
                if( func != Function.None ) {

                    EvaluateFunction( func, s.Substring( startIndex ) );

                } else {

                    EvaluateExpression( s );

                }
            }

            return true;
        }
Exemple #3
0
        public UserFunction(String name, String expression, params String[] parameterNames)
        {
            Name           = name;
            FunctionType   = FunctionType.User;
            ParameterNames = new ReadOnlyCollection<String>(parameterNames);

            _expr = new Expression( expression, null );
        }