Ejemplo n.º 1
0
        private void equals_Click(object sender, EventArgs e)
        {
            switch (operationPerformed)
            {
            case "+":
                txtResult.Text = Operations.Add(value, decimal.Parse(txtResult.Text, CultureInfo.InvariantCulture)).ToString();
                break;

            case "-":
                txtResult.Text = Operations.Subtract(value, decimal.Parse(txtResult.Text, CultureInfo.InvariantCulture)).ToString();
                break;

            case "*":
                txtResult.Text = Operations.Multiply(value, decimal.Parse(txtResult.Text, CultureInfo.InvariantCulture)).ToString();
                break;

            case "/":
                txtResult.Text = Operations.Divide(value, decimal.Parse(txtResult.Text, CultureInfo.InvariantCulture)).ToString();
                break;

            default:
                break;
            }

            lblMemory.Text     = "";
            value              = decimal.Parse(txtResult.Text, CultureInfo.InvariantCulture);
            operationPerformed = "";
        }
Ejemplo n.º 2
0
        private void Equals_Click(object sender, RoutedEventArgs e)
        {
            var    Input = new Operations();
            string Test  = Text1.Text + " " + Text2.Text;

            Text1.Text = "";
            double[] totalA = new double[15]; // array for Addition
            double[] totalS = new double[15]; // array for Addition
            double[] totalM = new double[15]; // array for Multiplication


            List <int> whereOpisM = new List <int>();
            List <int> whereOpisD = new List <int>();
            List <int> whereOpisS = new List <int>();
            List <int> whereOpisA = new List <int>();



            for (int i = 0; i < operations.Length; i++)
            {
                if (operations[i] == "X")
                {
                    whereOpisM.Add(i);
                }
                if (operations[i] == "/")
                {
                    whereOpisD.Add(i);
                }
                if (operations[i] == "+")
                {
                    whereOpisA.Add(i);
                }
                if (operations[i] == "-")
                {
                    whereOpisS.Add(i);
                }
            }

            if (whereOpisM.Count != 0) // multiply
            {
                for (int j = 0; j < whereOpisM.Count; j++)
                {
                    totalM[j] = Input.Multiply(Array1[j], Array1[j + 1]);
                }
            }

            if (whereOpisS.Count != 0)                              // Subtract
            {
                if (whereOpisS.Count != 0 && whereOpisM.Count != 0) // Subtracting and Multiply Sum
                {
                    for (int k = 0; k < whereOpisS.Count; k++)
                    {
                        if (whereOpisM.Count != 0)
                        {
                            if (whereOpisS.Count != 0)
                            {
                                totalS[k] = totalM[k] - Array1[whereOpisS[k] + 1];
                            }
                            else
                            {
                                totalS[k] = Input.Subtract(totalM[k], totalM[k + 1]);
                            }
                        }
                    }
                }
                else
                {
                    // Subraction only
                    totalS[0] = Array1[0] - Array1[1];

                    if (whereOpisS.Count > 1)
                    {
                        for (int k = 1; k < whereOpisS.Count; k++)
                        {
                            totalS[k] = totalS[k - 1] - Array1[k + 1];
                        }
                    }
                }
            }



            if (whereOpisA.Count != 0)                              // Adding
            {
                if (whereOpisA.Count != 0 && whereOpisM.Count != 0) // Adding and Multiply Sum
                {
                    for (int k = 0; k < whereOpisA.Count; k++)
                    {
                        if (whereOpisM.Count != 0)
                        {
                            if (whereOpisA.Count != 0)
                            {
                                totalA[k] = Array1[whereOpisA[k] + 1] + totalM[k];
                            }
                            else
                            {
                                totalA[k] = Input.Add(totalM[k], totalM[k + 1]);
                            }
                        }
                    }
                }
                else
                {
                    for (int k = 0; k <= whereOpisA.Count; k++) // Adding Only
                    {
                        totalA[k] = Input.Add(totalA[k], Array1[k]);
                    }
                }
            }



            Console.WriteLine("Multiply = " + whereOpisM.Sum());
            Console.WriteLine("Add = " + whereOpisA.Sum());
            if (whereOpisM.Count > 0 && whereOpisA.Count > 0) // Display output
            {
                Text2.Text = Convert.ToString(totalA.Sum());
            }
            if (whereOpisM.Count > 0 && whereOpisA.Count == 0 && whereOpisS.Count == 0)
            {
                Text2.Text = Convert.ToString(totalM.Sum());
            }
            if (whereOpisA.Count > 0 && whereOpisM.Count == 0)
            {
                Text2.Text = Convert.ToString(totalA.Sum());
            }
            if (whereOpisS.Count > 0 && whereOpisM.Count == 0)
            {
                Text2.Text = Convert.ToString(totalS[whereOpisS.Count - 1]);
            }
            if (whereOpisS.Count > 0 && whereOpisM.Count > 0)
            {
                Text2.Text = Convert.ToString(totalS[whereOpisS.Count - 1]);
                Array1[0]  = totalS[whereOpisS.Count - 1];
            }

            whereOpisM.Clear(); //reset operation location
            Array.Clear(operations, 0, operations.Length);
            Array.Clear(Array1, 1, Array1.Length - 1);
            j = 0;
            //Text2.Text = Convert.ToString(totalA.Sum());
            //reset globals
            globalequal  = 1;
            AddToPastSum = 0;
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Clear(); //Tøm skjerm
                int selectedFunction = 0;

                do
                {
                    //Skriv ut liste med funksjoner
                    Console.WriteLine("Choose from the following functions:");
                    Console.WriteLine("1. " + calculatorFunction.Add);
                    Console.WriteLine("2. " + calculatorFunction.Subtract);
                    Console.WriteLine("3. " + calculatorFunction.Multiply);
                    Console.WriteLine("4. " + calculatorFunction.Divide);
                    Console.WriteLine("5. " + calculatorFunction.Percent + "\n");
                    Console.Write("Please enter the desired function (1-5):");
                    //Sjekke om input er mellom 1 og 5
                    try
                    {
                        selectedFunction = Convert.ToUInt16(Console.ReadLine());
                    }
                    catch
                    {
                        Console.Clear(); //Tøm skjerm
                        Console.WriteLine("You have to enter a valid number!\n");
                        continue;
                    }
                }while ((selectedFunction >= 6) || (selectedFunction <= 0));

                bool   confirmValidation = false;
                string confirmFunction;

                do
                {
                    //Bruker må bekrefte for å fortsette
                    Console.WriteLine("\nYou have selected the following function: {0}. Do you want to continue? (Y / N)", Enum.GetName(typeof(calculatorFunction), selectedFunction));

                    try
                    {
                        confirmFunction = Convert.ToString(Console.ReadLine()).Substring(0, 1).ToUpper();
                        try
                        {
                            //Sjekk om Y / N
                            if ((confirmFunction.ToUpper().Contains("Y")) | (confirmFunction.ToUpper().Contains("N")))
                            {
                                confirmValidation = true;
                                //Hvis Y
                                if ((confirmFunction.ToUpper().Contains("Y")))
                                {
                                    Console.Clear(); //Tøm skjerm
                                    Console.WriteLine("Function: " + Enum.GetName(typeof(calculatorFunction), selectedFunction) + "\n");
                                    //Skrive inn 2 tall og utføre valgt kommando
                                    Operations o = new Operations();
                                    o.Numbers();

                                    if (selectedFunction == 1)
                                    {
                                        o.Add();
                                    }
                                    if (selectedFunction == 2)
                                    {
                                        o.Subtract();
                                    }
                                    if (selectedFunction == 3)
                                    {
                                        o.Multiply();
                                    }
                                    if (selectedFunction == 4)
                                    {
                                        o.Divide();
                                    }
                                    if (selectedFunction == 5)
                                    {
                                        o.Percent();
                                    }
                                }
                                //Hvis N
                                else
                                {
                                    Console.Clear(); //Tøm skjerm
                                    continue;
                                }
                            }
                            else
                            {
                                Console.Clear(); //Tøm skjerm
                                Console.WriteLine("\nInvalid entry. \n");
                            }
                        }
                        catch
                        {
                            throw;
                        }
                    }
                    catch
                    {
                        continue;
                    }
                } while (confirmValidation == false);


                //Starte på ny eller avslutte
                Console.WriteLine("\nPress any key to restart, or press Esc to finish.");
                var userInput = Console.ReadKey();
                if (userInput.Key == ConsoleKey.Escape)
                {
                    return;
                }
            }
        }