Пример #1
0
        /// <summary>
        /// Перегрузка вычитания
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static fullFraction operator -(fullFraction a, fullFraction b)
        {
            fullFraction result = new fullFraction();

            if (a.BOT != b.BOT)//если значенатели не равны, то нужно привести к общему знаменателю
            {
                fraction first  = getFraction(a);
                fraction second = getFraction(b);

                int LCD = first.BOT * second.BOT / (gcd(first.BOT, second.BOT));
                first.TOP  *= LCD / first.BOT;
                second.TOP *= LCD / second.BOT;
                result.TOP  = first.TOP - second.TOP;
                result.BOT  = LCD;
            }
            else
            {
                fraction first  = getFraction(a);
                fraction second = getFraction(b);

                result.TOP = first.TOP - second.TOP;
                result.BOT = first.BOT;
            }
            while (result.TOP >= result.BOT)
            {
                result.FULLValue++;
                result.TOP -= result.BOT;
            }

            return(result);
        }
Пример #2
0
        private static fraction getFraction(fullFraction tmp)
        {
            fraction newFr = new fraction();

            if (tmp.FULLValue == 0)
            {
                newFr.TOP = tmp.TOP;
                newFr.BOT = tmp.BOT;
            }
            else
            {
                newFr.TOP = tmp.fullValue * tmp.BOT + tmp.TOP;
                newFr.BOT = tmp.BOT;
            }

            return(newFr);
        }
Пример #3
0
        /// <summary>
        /// Перегрузка деления
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static fullFraction operator /(fullFraction a, fullFraction b)
        {
            fullFraction result = new fullFraction();

            fraction first  = getFraction(a);
            fraction second = getFraction(b);

            result.TOP = first.TOP * second.BOT;
            result.BOT = first.BOT * second.TOP;

            while (result.TOP >= result.BOT)
            {
                result.FULLValue++;
                result.TOP -= result.BOT;
            }

            return(result);
        }
Пример #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0" || textBox2.Text == "0" || textBox3.Text == "0" || textBox4.Text == "0" || textBox5.Text == "0" || textBox6.Text == "0")
            {
                MessageBox.Show("Введите валидное значение!");
                return;
            }
            else
            {
                try
                {
                    //------------------первая-----------------------------------
                    //Считали целое
                    if (textBox1.Text != "")
                    {
                        a.FULLValue = int.Parse(textBox1.Text);
                    }
                    else
                    {
                        a.FULLValue = 0;
                    }

                    //Считали числитель
                    if (textBox2.Text != "")
                    {
                        a.TOP = int.Parse(textBox2.Text);
                    }
                    else
                    {
                        throw new Exception("У первой дроби нет числителя!");
                    }

                    //Считали знаменатель
                    if (textBox3.Text != "")
                    {
                        a.BOT = int.Parse(textBox3.Text);
                    }
                    else
                    {
                        throw new Exception("У первой дроби нет знаменателя!");
                    }
                    //-----------------------------------------------------------

                    //--------------------вторая---------------------------------
                    //Считали целое
                    if (textBox6.Text != "")
                    {
                        b.FULLValue = int.Parse(textBox6.Text);
                    }
                    else
                    {
                        b.FULLValue = 0;
                    }

                    //Считали числитель
                    if (textBox5.Text != "")
                    {
                        b.TOP = int.Parse(textBox5.Text);
                    }
                    else
                    {
                        throw new Exception("У второй дроби нет числителя!");
                    }

                    //Считали знаменатель
                    if (textBox4.Text != "")
                    {
                        b.BOT = int.Parse(textBox4.Text);
                    }
                    else
                    {
                        throw new Exception("У второй дроби нет знаменателя!");
                    }
                    //-----------------------------------------------------------
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                    return;
                }
            }
            string item = comboBox1.Text;

            try
            {
                switch (item)
                {
                case "сложение":
                {
                    resultFraction = a + b;

                    break;
                }

                case "вычитание":
                {
                    resultFraction = a - b;

                    break;
                }

                case "умножение":
                {
                    resultFraction = a * b;

                    break;
                }

                case "деление":
                {
                    resultFraction = a / b;

                    break;
                }

                default:
                { MessageBox.Show("Выберите действие!"); break; }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                return;
            }
            if (resultFraction.FULLValue != 0)
            {
                textBox9.Visible = true;
                textBox9.Text    = resultFraction.FULLValue.ToString();
            }
            else
            {
                textBox9.Visible = false;
            }
            if (resultFraction.TOP != 0)
            {
                textBox7.Visible    = true;
                pictureBox3.Visible = true;
                textBox8.Visible    = true;
                textBox8.Text       = resultFraction.TOP.ToString();
                textBox7.Text       = resultFraction.BOT.ToString();
            }
        }