Exemple #1
0
        static void Main(string[] args)
        {
            CalcMethods MyCalc = new CalcMethods();

            while (true)
            {
                int    num1   = 0;
                int    num2   = 0;
                double result = 0;

                Console.WriteLine("Welcome to Calculator\r");
                Console.WriteLine("---------------------\n");

                Console.WriteLine("Type a number, then press Enter");
                num1 = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Type another number, then press Enter");
                num2 = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Choose an option from the following list:");
                Console.WriteLine("\ta - Add");
                Console.WriteLine("\ts - Subtract");
                Console.WriteLine("\tm - Multiply");
                Console.WriteLine("\tp - Power");
                Console.WriteLine("\td - Divide");
                Console.WriteLine("\tq - Squareroot");

                Console.Write("Your option? ");

                // Use a switch statement to do the math.
                switch (Console.ReadLine())
                {
                case "a":
                    result = MyCalc.Add(num1, num2);
                    break;

                case "s":
                    result = MyCalc.Subtract(num1, num2);
                    break;

                case "m":
                    result = MyCalc.Multiply(num1, num2);
                    break;

                case "p":
                    result = MyCalc.Power(num1, num2);
                    break;

                case "d":
                    result = MyCalc.Divide(num1, num2);
                    break;

                case "q":
                    result = MyCalc.squareroot(num1);
                    break;
                }

                // Wait for the user to respond before closing.
                Console.WriteLine(result);
                Console.Write("Press any key to start over the Calculator console app... \n");
                Console.ReadKey();
            }
        }
Exemple #2
0
        public double ReadString(string equation, Regex pass, int stringSpot)
        {
            stringSize = equation.Length;
            int    place     = stringSpot;
            string nonConNum = "";
            double stringSum = 0;
            double a         = 0;

            Parsing     paran = new Parsing();
            CalcMethods work  = new CalcMethods();

            List <double> numsL = new List <double>();
            List <char>   signL = new List <char>();

            r = pass;

            for (int x = place; x <= stringSize - 1; x++)
            {
                if (r.IsMatch(equation[x].ToString()))
                {
                    nonConNum = nonConNum + equation[x].ToString();

                    if (x == stringSize - 1)
                    {
                        a         = Convert.ToDouble(nonConNum);
                        nonConNum = "";
                        numsL.Add(a);
                    }
                }

                else if (equation[x].ToString() == "(" && r.IsMatch(equation[x - 1].ToString()))
                {
                    a         = Convert.ToDouble(nonConNum);
                    nonConNum = "";
                    numsL.Add(a);
                    signL.Add('*');
                    place = x + 1;
                    numsL.Add(paran.ReadString(equation, pass, place));
                    x = Program.parsespot;
                }

                else if (equation[x].ToString() == "(")
                {
                    place = x + 1;
                    numsL.Add(paran.ReadString(equation, pass, place));
                    x = Program.parsespot;
                }

                else if (equation[x].ToString() == ")")
                {
                    if (nonConNum != "")
                    {
                        a         = Convert.ToDouble(nonConNum);
                        nonConNum = "";
                        numsL.Add(a);
                    }
                    if (x != stringSize - 1)
                    {
                        Program.parsespot = x;
                    }
                    else
                    {
                        Program.parsespot = x;
                    }
                    break;
                }

                else
                {
                    if (nonConNum != "")
                    {
                        a         = Convert.ToDouble(nonConNum);
                        nonConNum = "";
                        numsL.Add(a);
                    }

                    signL.Add(equation[x]);
                }
            }

            work.FillList(numsL, signL);
            stringSum = work.DoMath();

            return(stringSum);
        }