Exemple #1
0
        private static void inputTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CalculatorLogic Calc = ((CalculatorWindow)d).CalcLogic;

            Calc.inputText = (string)e.NewValue;
            Calc.Calculate();
        }
        private static string parse_Equation(char[] expr,   int index)
        {
            CalculatorLogic cl = new CalculatorLogic();

            string num = "";
            if((int)expr[index]==83){
                index += 4;
                if((int)expr[index]==32){
                    for(int i = index; i<= expr.Length-2;i++)
                     num += expr [i].ToString();
                }
                double srt_num = double.Parse (num);
                IUnaryOperator sqrt = new Sqrt ();
                return Convert.ToString(sqrt.perform (srt_num));
            }
            if((int)expr[index]==82){
                index += 10;
                if((int)expr[index]==32){
                    for(int i = index; i<= expr.Length-2;i++)
                        num += expr [i].ToString();
                }
                double srt_num = double.Parse (num);
                IUnaryOperator reci = new Reciprocal ();
                return Convert.ToString(reci.perform (srt_num));
            }
            if((int)expr[index]==78){
                index += 6;
                if((int)expr[index]==32){
                    for(int i = index; i<= expr.Length-2;i++)
                        num += expr [i].ToString();
                }
                double srt_num = double.Parse (num);
                IUnaryOperator neg = new Negate ();
                return Convert.ToString(neg.perform (srt_num));
            }
            while (index != expr.Length - 1) {
                double x = getDouble (expr, ref index);
                char op = expr [index];

                index++;

                if (op == '\r') {
                    cl.AcceptNumber (x, null);
                    string result = cl.Equals ();
                    return result;
                }
                perform_op (x, op, cl);

            }
            return "";
            //return value;
        }
        private static void perform_op(double x, char op, CalculatorLogic cl)
        {
            IBinaryOperator subOperator = new SubtractionOperator();
            IBinaryOperator mulOperator = new MultiplicationOperator();
            IBinaryOperator divOperator = new DivisionOperator();
            IBinaryOperator addOperator = new AdditionOperator();

            if (op == '+')
                cl.AcceptNumber (x, addOperator);
            else
                if (op == '-')
                    cl.AcceptNumber (x, subOperator);
                else
                    if (op == '*')
                    cl.AcceptNumber (x,mulOperator);
                    else
                        if (op == '/')
                        cl.AcceptNumber (x, divOperator);
        }
Exemple #4
0
        static void Main(string[] args)
        {
            bool endApp = false;

            Console.WriteLine(ProgramText.Title);
            Console.WriteLine("------------------------\n");
            while (!endApp)
            {
                string numInput1 = "";
                string numInput2 = "";
                double result    = 0;

                Console.WriteLine(ProgramText.SelectNum1);
                numInput1 = Console.ReadLine();

                double cleanNum1 = 0;
                while (!double.TryParse(numInput1, out cleanNum1))
                {
                    Console.Write(ProgramText.IncorrectValue);
                    numInput1 = Console.ReadLine();
                }

                Console.WriteLine(ProgramText.SelectNum2);
                numInput1 = Console.ReadLine();

                double cleanNum2 = 0;
                while (!double.TryParse(numInput1, out cleanNum2))
                {
                    Console.Write(ProgramText.IncorrectValue);
                    numInput2 = Console.ReadLine();
                }

                Console.WriteLine(ProgramText.SelectMathAction);
                Console.WriteLine(ProgramText.Add);
                Console.WriteLine(ProgramText.Subtract);
                Console.WriteLine(ProgramText.Muliply);
                Console.WriteLine(ProgramText.Devide);
                Console.Write(ProgramText.SelectAction);

                string op = Console.ReadLine();

                try
                {
                    result = CalculatorLogic.DoOperation(cleanNum1, cleanNum2, op);
                    if (double.IsNaN(result))
                    {
                        Console.WriteLine(ProgramText.ErrorWarning);
                    }
                    else
                    {
                        Console.WriteLine("Your result: {0:0.##}\n", result);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(ProgramText.ErrorMessage + e.Message);
                }
                Console.WriteLine("------------------------\n");
                Console.WriteLine(ProgramText.ExitMessage);
                if (Console.ReadLine() == "n")
                {
                    endApp = true;
                }
                Console.WriteLine("\n");
            }
            return;
        }