Пример #1
0
 private void InitializeCalculator()
 {
     currentNumber    = 0;
     previousNumber   = 0;
     decimalPlaces    = 0;
     currentOperation = operation.None;
     DisplayArea.Text = currentNumber.ToString();
 }
Пример #2
0
 private void FactorialButton(object sender, RoutedEventArgs e)
 {
     Equals(operation.Factorial);
     if (previousNumber % 1 > 0 || previousNumber < 0)
     {
         InitializeCalculator();
         currentOperation = operation.Error;
         UpdateDisplay();
     }
     else
     {
         Equals(operation.Equals);
     }
 }
Пример #3
0
        private void Equals(operation nextOp)
        {
            switch (currentOperation)
            {
            case operation.None:
                previousNumber = currentNumber;
                break;

            case operation.Equals:
                break;

            case operation.Plus:
                previousNumber = previousNumber + currentNumber;
                break;

            case operation.Minus:
                previousNumber = previousNumber - currentNumber;
                break;

            case operation.Multiply:
                previousNumber = previousNumber * currentNumber;
                break;

            case operation.Divide:
                previousNumber = previousNumber / currentNumber;
                break;

            case operation.Factorial:
                previousNumber = Factorial(previousNumber);
                break;

            case operation.Root:
                previousNumber = Math.Sqrt(previousNumber);
                break;
            }
            if (currentOperation != operation.Equals)
            {
                currentNumber = 0;
            }
            decimalPlaces    = 0;
            currentOperation = nextOp;
            UpdateDisplay();
        }
Пример #4
0
        private void PerformCalculation(operation previousOperation)
        {
            List <double> lstNums = new List <double>();

            switch (previousOperation)
            {
            case operation.Add:
                lstNums         = txtDisplay.Text.Split('+').Select(double.Parse).ToList();
                txtDisplay.Text = (lstNums[0] + lstNums[1]).ToString();
                break;

            case operation.Sub:
                lstNums         = txtDisplay.Text.Split('-').Select(double.Parse).ToList();
                txtDisplay.Text = (lstNums[0] - lstNums[1]).ToString();
                break;

            case operation.Mul:
                lstNums         = txtDisplay.Text.Split('*').Select(double.Parse).ToList();
                txtDisplay.Text = (lstNums[0] * lstNums[1]).ToString();
                break;

            case operation.Div:
                try
                {
                    lstNums         = txtDisplay.Text.Split('/').Select(double.Parse).ToList();
                    txtDisplay.Text = (lstNums[0] / lstNums[1]).ToString();
                } catch (DivideByZeroException)
                {
                    txtDisplay.Text = "EEEEEE";
                }
                break;

            case operation.None:
                break;

            default:
                break;
            }
        }
Пример #5
0
 public void OpFunction(double fNumber, operation op)
 {
     firstNumber     = fNumber;
     curentOperation = op;
 }
Пример #6
0
 public void OpFunction(double fNumber, operation op)
 {
     firstNumber = fNumber;
     curentOperation = op;
 }
Пример #7
0
        static void Main(string[] args)
        {
            //welcome string declaration
            string welcome = "Welcome to Simple Calculator - created by Kamil Jankowski";

            //display character '-' to separate welcome and program content
            for (int i = 0; i < welcome.Length; i++)
            {
                Console.Write("-");
            }

            Console.WriteLine("");
            Console.WriteLine(welcome);


            //display character '-' to separate welcome and program content
            for (int i = 0; i < welcome.Length; i++)
            {
                Console.Write("-");
            }
            Console.WriteLine("");
            Console.WriteLine("");

            //set value
            double    num, b, c, r, val;
            operation o = new operation();

            first_val fval = new first_val();

            fval.get_val();

            //test and read value

            while (!(double.TryParse(fval.str, out num)))
            {
                Console.WriteLine("This is not a number. Please try again.");
                fval.get_val();
            }
            if (double.TryParse(fval.str, out num))
            {
                Console.WriteLine("This is a number.");
                val = Convert.ToDouble(fval.str);
                b   = fval.read_val(val);
                Console.WriteLine("Return: {0}", b);

                //display operation options
                Console.WriteLine("Select operation from the following options: ");
                Console.WriteLine("1) ADD ");
                Console.WriteLine("2) SUBSTRACT ");
                Console.WriteLine("3) MULTIPLE ");
                Console.WriteLine("4) DEVIDE ");
                char oper = Convert.ToChar(Console.ReadLine());

                //check if operation 1-4 has been chosen
                if ((oper == '1') || (oper == '2') || (oper == '3') || (oper == '4'))
                {
                    Console.WriteLine("Good choice");
                }

                //if wrong/none operation , display options again
                while ((oper != '1') && (oper != '2') && (oper != '3') && (oper != '4'))
                {
                    Console.WriteLine("No operation. Please select operation from 1 - 4.");
                    Console.WriteLine();
                    Console.WriteLine("Select operation from the following options: ");
                    Console.WriteLine("1) ADD ");
                    Console.WriteLine("2) SUBSTRACT ");
                    Console.WriteLine("3) MULTIPLE ");
                    Console.WriteLine("4) DEVIDE ");

                    //read input and convert to char; check if input is between 1-4
                    oper = Convert.ToChar(Console.ReadLine());
                    if ((oper == '1') || (oper == '2') || (oper == '3') || (oper == '4'))
                    {
                        Console.WriteLine("Good choice");
                    }
                }



                //set another value and test
                fval.get_val();

                //false ask again about value
                while (!(double.TryParse(fval.str, out num)))
                {
                    Console.WriteLine("This is not a number. Please try again.");
                    fval.get_val();
                }

                //true
                if (double.TryParse(fval.str, out num))
                {
                    Console.WriteLine("This is a number.");
                    val = Convert.ToDouble(fval.str);
                    c   = fval.read_val(val);
                    Console.WriteLine("Return: {0}", c);

                    switch (oper)
                    {
                    case '1':
                        r = o.add(b, c);
                        Console.WriteLine("Operation ADD Return: {0}", r);
                        break;

                    case '2':
                        r = o.substract(b, c);
                        Console.WriteLine("Operation SUBSTRACT: {0}", r);
                        break;

                    case '3':
                        r = o.multiple(b, c);
                        Console.WriteLine("Operation MULTIPLE: {0}", r);
                        break;

                    case '4':
                        r = o.divide(b, c);
                        Console.WriteLine("Operation DIVIDE: {0}", r);
                        break;
                    }



                    Console.WriteLine("Do you want to contine? (y/n): ");
                    char oper_again = Convert.ToChar(Console.ReadLine());



                    //if true then continue to ask about operation and value
                    while (oper_again == 'y')
                    {
                        //display operation options
                        Console.WriteLine("Select operation from the following options: ");
                        Console.WriteLine("1) ADD ");
                        Console.WriteLine("2) SUBSTRACT ");
                        Console.WriteLine("3) MULTIPLE ");
                        Console.WriteLine("4) DIVIDE ");
                        oper = Convert.ToChar(Console.ReadLine());

                        //check if operation 1-4 has been chosen
                        if ((oper == '1') || (oper == '2') || (oper == '3') || (oper == '4'))
                        {
                            Console.WriteLine("Good choice");
                        }
                        //if wrong/none operation , display options again
                        while ((oper != '1') && (oper != '2') && (oper != '3') && (oper != '4'))
                        {
                            Console.WriteLine("No operation. Please select operation from 1 - 4.");
                            Console.WriteLine();
                            Console.WriteLine("Select operation from the following options: ");
                            Console.WriteLine("1) ADD ");
                            Console.WriteLine("2) SUBSTRACT ");
                            Console.WriteLine("3) MULTIPLE ");
                            Console.WriteLine("4) DEVIDE ");

                            oper = Convert.ToChar(Console.ReadLine());
                            if ((oper == '1') || (oper == '2') || (oper == '3') || (oper == '4'))
                            {
                                Console.WriteLine("Good choice.");
                            }
                        }
                        fval.get_val();
                        while (!(double.TryParse(fval.str, out num)))
                        {
                            Console.WriteLine("This is not a number. Please try again.");
                            fval.get_val();
                        }
                        if (double.TryParse(fval.str, out num))
                        {
                            Console.WriteLine("This is a number.");
                            val = Convert.ToDouble(fval.str);
                            c   = fval.read_val(val);
                            //select and run operation
                            switch (oper)
                            {
                            case '1':



                                r = o.new_add(c);
                                Console.WriteLine("Operation ADD Return: {0}", r);
                                break;

                            case '2':


                                r = o.new_substract(c);
                                Console.WriteLine("Operation SUBSTRACT: {0}", r);
                                break;

                            case '3':

                                r = o.new_multiple(c);
                                Console.WriteLine("Operation MULTIPLE: {0}", r);
                                break;

                            case '4':

                                r = o.new_divide(c);
                                Console.WriteLine("Operation DIVIDE: {0}", r);
                                break;
                            }
                            Console.WriteLine("Do you want to contine? (y/n): ");
                            oper_again = Convert.ToChar(Console.ReadLine());
                        }
                    }
                    // if no and yes
                    while ((oper_again != 'n') && (oper_again != 'y'))
                    {
                        Console.WriteLine("Wrong data. Please choose yes or no.");
                        Console.WriteLine("Do you want to contine? (y/n): ");
                        oper_again = Convert.ToChar(Console.ReadLine());
                    }
                    //if true then continue to ask about operation and value
                    while (oper_again == 'y')
                    {
                        //display operation options
                        Console.WriteLine("Select operation from the following options: ");
                        Console.WriteLine("1) ADD ");
                        Console.WriteLine("2) SUBSTRACT ");
                        Console.WriteLine("3) MULTIPLE ");
                        Console.WriteLine("4) DIVIDE ");
                        oper = Convert.ToChar(Console.ReadLine());

                        if ((oper == '1') || (oper == '2') || (oper == '3') || (oper == '4'))
                        {
                            Console.WriteLine("Good choice");
                        }

                        while ((oper != '1') && (oper != '2') && (oper != '3') && (oper != '4'))
                        {
                            Console.WriteLine("No operation. Please select operation from 1 - 4.");
                            Console.WriteLine();
                            Console.WriteLine("Select operation from the following options: ");
                            Console.WriteLine("1) ADD ");
                            Console.WriteLine("2) SUBSTRACT ");
                            Console.WriteLine("3) MULTIPLE ");
                            Console.WriteLine("4) DEVIDE ");

                            oper = Convert.ToChar(Console.ReadLine());
                            if ((oper == '1') || (oper == '2') || (oper == '3') || (oper == '4'))
                            {
                                Console.WriteLine("Good choice.");
                            }
                        }
                        //another value
                        fval.get_val();
                        while (!(double.TryParse(fval.str, out num)))
                        {
                            Console.WriteLine("This is not a number. Please try again.");
                            fval.get_val();
                        }
                        if (double.TryParse(fval.str, out num))
                        {
                            Console.WriteLine("This is a number.");
                            val = Convert.ToDouble(fval.str);
                            c   = fval.read_val(val);
                            //select and run operation
                            switch (oper)
                            {
                            case '1':

                                r = o.new_add(c);
                                Console.WriteLine("Operation ADD Return: {0}", r);
                                break;

                            case '2':

                                r = o.new_substract(c);
                                Console.WriteLine("Operation SUBSTRACT: {0}", r);
                                break;

                            case '3':

                                r = o.new_multiple(c);
                                Console.WriteLine("Operation MULTIPLE: {0}", r);
                                break;

                            case '4':

                                r = o.new_divide(c);
                                Console.WriteLine("Operation DIVIDE: {0}", r);
                                break;
                            }
                            Console.WriteLine("Do you want to contine? (y/n): ");
                            oper_again = Convert.ToChar(Console.ReadLine());
                        }
                    }

                    //terminate program
                    if (oper_again == 'n')
                    {
                        Console.WriteLine("Thank you. God Bye.");
                    }
                }
            }

            Console.ReadKey();
        }
Пример #8
0
        private void Answer(object sender, RoutedEventArgs e)
        {
            SByte a = 1;

            dotted = false;
            double    kof = 10, number = 0, buf = 0, answer = 0;
            int       itk = 1;
            operation c   = operation.plus;

            for (int i = 0; i < lol.Text.Length; i++)
            {
                if (lol.Text[i] == '-')
                {
                    a = -1;
                }
                bool isNumeric = Char.IsDigit(lol.Text[i]);
                if (isNumeric)
                {
                    if (Math.Abs(kof) == 10)
                    {
                        number *= (double)kof;
                    }
                    if (kof < 1)
                    {
                        number += (double)kof * (lol.Text[i] - '0');
                    }
                    else
                    {
                        number += (double)lol.Text[i] - (double)'0';
                    }
                }
                else if (lol.Text[i] == '.')
                {
                    kof = 1;
                }
                if (Math.Abs(kof) < 10)
                {
                    kof /= 10;
                }
                isNumeric = false;
                if (IsOperand(i) || i == lol.Text.Length - 1)
                {
                    number *= a;
                    a       = 1;
                    switch (c)
                    {
                    case operation.plus:
                        answer = (double)buf + (double)number;
                        break;

                    case operation.minus:
                        answer = (double)buf - (double)number;
                        break;

                    case operation.multiply:
                        answer = (double)buf * (double)number;
                        break;

                    case operation.div:
                        answer = (double)buf / (double)number;
                        break;

                    case operation.power:
                        answer = Math.Pow((double)buf, (double)number);
                        break;
                    }

                    buf = answer;

                    char b = lol.Text[i];

                    switch (b)
                    {
                    case '+':
                        c = operation.plus;
                        break;

                    case '-':
                        c = operation.minus;
                        break;

                    case 'X':
                        c = operation.multiply;
                        break;

                    case '/':
                        c = operation.div;
                        break;

                    case '^':
                        c = operation.power;
                        break;
                    }

                    kof    = 10;
                    number = 0;
                    itk++;
                }
            }
            signed = false;
            if (memoryMode)
            {
                MemoryBox.Text = answer.ToString(System.Globalization.CultureInfo.InvariantCulture);
            }
            else
            {
                lol.Text = answer.ToString(System.Globalization.CultureInfo.InvariantCulture);
            }
            if (Math.Round(answer) != answer)
            {
                dotted = true;
            }
            if (Double.IsNaN(answer) || Double.IsInfinity(answer) ||
                Double.IsNegativeInfinity(answer) || Double.IsPositiveInfinity(answer))
            {
                undefinedValue = true;
            }
            else
            {
                undefinedValue = false;
            }
        }