private void OpBtn_binop(object sender, RoutedEventArgs e)
        {
            string op = (sender as Button).Content.ToString();

            cs.BinOp(op);
            UpdateNumberField();
        }
Example #2
0
        public void BinOpTest()
        {
            CStack cs = new CStack();

            cs.entry = "5";
            cs.Enter();
            cs.entry = "5";
            cs.Enter();
            cs.BinOp("+");
            Assert.AreEqual(cs.X, 10);

            CStack cs2 = new CStack();

            cs2.entry = "15";
            cs2.Enter();
            cs2.entry = "5";
            cs2.Enter();
            cs2.BinOp("-");
            Assert.AreEqual(cs2.X, 5);

            CStack cs3 = new CStack();

            cs3.entry = "2";
            cs3.Enter();
            cs3.entry = "5";
            cs3.Enter();
            cs3.BinOp("*");
            Assert.AreEqual(cs3.X, 5);

            CStack cs4 = new CStack();

            cs4.entry = "10";
            cs4.Enter();
            cs4.entry = "2";
            cs4.Enter();
            cs4.BinOp("÷");
            Assert.AreEqual(cs4.X, 5);

            CStack cs5 = new CStack();

            cs5.entry = "5";
            cs5.Enter();
            cs5.entry = "2";
            cs5.Enter();
            cs5.BinOp("yˣ");
            Assert.AreEqual(cs5.X, 25);

            CStack cs6 = new CStack();

            cs6.entry = "9";
            cs6.Enter();
            cs6.entry = "2";
            cs6.Enter();
            cs6.BinOp("ˣ√y");
            Assert.AreEqual(cs6.X, 3);
        }
        static void Main(string[] args)
        {
            CStack cs;

            cs = new CStack();
            bool stop = false;

            Console.WriteLine("Welcome to ConsoleCalc");
            string input;

            string[] commands;
            do
            {
                Console.Write("> ");
                input    = Console.ReadLine();
                commands = input.Split(' ');

                if (commands[0] == "quit")
                {
                    Console.WriteLine("Bye! Press any key to continue ...");
                    stop = true;
                }
                else if (commands[0] == "enter" && commands.Length == 2)
                {
                    cs.entry = commands[1];
                    cs.Enter();
                }
                else if (commands[0] == "+")
                {
                    cs.BinOp("+");
                }
                else if (commands[0] == "*")
                {
                    cs.BinOp("×");
                }
                else if (commands[0] == "-")
                {
                    cs.BinOp("−");
                }
                else if (commands[0] == "/")
                {
                    cs.BinOp("÷");
                }
                else if (commands[0] == "/")
                {
                    cs.BinOp("÷");
                }
                else if (commands[0] == "^")
                {
                    cs.BinOp("yˣ");
                }
                else if (commands[0] == @"\")
                {
                    cs.BinOp("ˣ√y");
                }
                else if (commands[0] == "sqr")
                {
                    cs.Unop("x²");
                }
                else if (commands[0] == "sqrt")
                {
                    cs.Unop("√x");
                }
                else if (commands[0] == "log")
                {
                    cs.Unop("log x");
                }
                else if (commands[0] == "ln")
                {
                    cs.Unop("ln x");
                }
                else if (commands[0] == "10^")
                {
                    cs.Unop("10ˣ");
                }
                else if (commands[0] == "e^")
                {
                    cs.Unop("eˣ");
                }
                else if (commands[0] == "sin")
                {
                    cs.Unop("sin");
                }
                else if (commands[0] == "cos")
                {
                    cs.Unop("cos");
                }
                else if (commands[0] == "tan")
                {
                    cs.Unop("tan");
                }
                else if (commands[0] == "asin")
                {
                    cs.Unop("sin⁻¹");
                }
                else if (commands[0] == "acos")
                {
                    cs.Unop("cos⁻¹");
                }
                else if (commands[0] == "atan")
                {
                    cs.Unop("tan⁻¹");
                }
                else if (commands[0] == "pi")
                {
                    cs.Nilop("π");
                }
                else if (commands[0] == "e")
                {
                    cs.Nilop("e");
                }
                else if (commands[0] == "show")
                {
                    Show(cs);
                }
                else
                {
                    Console.WriteLine("Unknown command: {0}", commands[0]);
                }
            } while (!stop);
            Console.ReadKey();
        }