Exemplo n.º 1
0
        public MathFunction(string inputExpression)
        {
            // Name
            inputExpression = inputExpression.Replace(" ", string.Empty);
            Name            = Regex.Match(inputExpression, @"^([^()]*)\(").Groups[1].Value;
            // Variables
            Variables = GetVariablesList(inputExpression);
            // Formula
            string        rawFormula   = Regex.Match(inputExpression, @"(?<==).*$").Value;
            List <string> infixFormula = ExpressionManaging.ExpressionToInfix(rawFormula);

            Formula = ExpressionManaging.InfixToPostfix(infixFormula);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // to change language of exceptions' messages ;)
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
            //
            List <MathFunction> Functions = new List <MathFunction>();

            for (;;)
            {
                try
                {
                    Console.WriteLine();
                    Console.WriteLine("Type specific letter to perform action:");
                    Console.WriteLine("f - to define new function");
                    Console.WriteLine("c - to give arguments to calculate existing function");
                    Console.WriteLine("x - to calculate direct given expression");
                    Console.WriteLine("h - to get help");
                    Console.WriteLine("q - to quit");
                    string userChoice = Console.ReadLine();
                    if (userChoice.Equals("q"))
                    {
                        Console.WriteLine("Bye bye!");
                        break;
                    }
                    else if (userChoice.Equals("f"))
                    {
                        Console.WriteLine("Provide function's pattern below:");
                        string inputExpression = Console.ReadLine();
                        if (inputExpression.Equals("") || !inputExpression.Contains("="))
                        {
                            Console.WriteLine("Given funtion is incomplete.");
                            continue;
                        }
                        string       functionName     = Regex.Match(inputExpression, @"^([^()]*)\(").Groups[1].Value;
                        MathFunction functionToRemove = Functions.SingleOrDefault(f => f.Name.Equals(functionName));
                        if (functionToRemove != null)
                        {
                            Console.WriteLine("Function with specified name already existed. Do you want to replace it with your function? y/n");
                            string choice = Console.ReadLine();
                            if (choice.Equals("y"))
                            {
                                Functions.Remove(functionToRemove);
                            }
                            else if (choice.Equals("n"))
                            {
                                continue;
                            }
                            else
                            {
                                Console.WriteLine("Unknown command, function not created.");
                                continue;
                            }
                        }
                        Functions.Add(new MathFunction(inputExpression));
                    }
                    else if (userChoice.Equals("c"))
                    {
                        Console.WriteLine("Provide function's symbol with variables' values below:");
                        string       inputExpression   = Console.ReadLine();
                        string       functionName      = Regex.Match(inputExpression, @"^([^()]*)\(").Groups[1].Value;
                        MathFunction functionToCompute = Functions.SingleOrDefault(f => f.Name.Equals(functionName));
                        if (functionToCompute != null)
                        {
                            inputExpression = inputExpression.Replace(" ", string.Empty);
                            string        variablesSection = Regex.Match(inputExpression, @"\(([^()]*)\)").Groups[1].Value;
                            List <double> variablesValues  = new List <double>();
                            string        tempVariable     = "";
                            foreach (string v in variablesSection.Select(c => c.ToString()).ToList())
                            {
                                if (v.Equals(","))
                                {
                                    if (!tempVariable.Equals(""))
                                    {
                                        variablesValues.Add(Convert.ToDouble(tempVariable));
                                        tempVariable = "";
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                                else
                                {
                                    tempVariable += v;
                                }
                            }
                            if (!tempVariable.Equals(""))
                            {
                                variablesValues.Add(Convert.ToDouble(tempVariable));
                            }
                            double result = functionToCompute.ComputeFunction(variablesValues);
                            Console.WriteLine(result.ToString());
                        }
                        else
                        {
                            Console.WriteLine("This function is not defined.");
                        }
                    }
                    else if (userChoice.Equals("x"))
                    {
                        Console.WriteLine("Provide expression to calculate:");
                        string        inputExpression   = Console.ReadLine();
                        List <string> postfixExpression = ExpressionManaging.InfixToPostfix(ExpressionManaging.ExpressionToInfix(inputExpression));
                        Console.WriteLine(Calculator.PerformCalculation(postfixExpression).ToString());
                    }
                    else if (userChoice.Equals("h"))
                    {
                        Console.WriteLine("*****************************************************************");
                        Console.WriteLine("Functions should be declared as below:");
                        Console.WriteLine("f(x,y,z) = x^3 + 2*x*y - 3*(y/z)^2 + z^2");
                        Console.WriteLine("Direct expressions should be given like this:");
                        Console.WriteLine("4.25 * (5.68/(2.1+3.44) - 89.2)^4.2");
                        Console.WriteLine("Spaces are non-compulsory - you can use them wherever you want.");
                        Console.WriteLine("*****************************************************************");
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("Incorrect choice, try again.");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception has been caught:");
                    Console.WriteLine(e.Message);
                }
            }
        }
Exemplo n.º 3
0
        public static double PerformCalculation(List <string> postfixExpression)
        {
            Heap <double> calcHeap = new Heap <double>();

            try
            {
                foreach (string elem in postfixExpression)
                {
                    if (ExpressionManaging.IsMathOperator(elem))
                    {
                        double secondOperand = calcHeap.Pop();
                        double firstOperand  = calcHeap.Pop();
                        double result;
                        switch (elem)
                        {
                        case "+":
                            result = firstOperand + secondOperand;
                            break;

                        case "-":
                            result = firstOperand - secondOperand;
                            break;

                        case "*":
                            result = firstOperand * secondOperand;
                            break;

                        case "/":
                            result = firstOperand / secondOperand;
                            break;

                        case "^":
                            result = Math.Pow(firstOperand, secondOperand);
                            break;

                        default:
                            throw new ArgumentException("Wrong operator readed");
                        }
                        calcHeap.Push(result);
                    }
                    else
                    {
                        calcHeap.Push(Convert.ToDouble(elem));
                    }
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            try
            {
                if (calcHeap.Amount != 1)
                {
                    throw new ApplicationException("Wrong output data in method!");
                }
                else
                {
                    return(calcHeap.Pop());
                }
            }
            catch
            {
                throw;
            }
        }