예제 #1
0
        private void ToSimpleBut_Click(object sender, EventArgs e)
        {
            Fraction fr = Calculations.ToSimpleFraction(Convert.ToDouble(MainTextBox.Text));

            SimpFCheckBox.Checked = true;
            MainTextBox.Text      = fr.ToString();
        }
        // Function to calculate an expression with simple fractions.
        public static string SimpFracCalcAll(string s)
        {
            // Find and calculate brackets.
            s = CalculateBrackets(s);

            // Multiplicate.
            s = SimpMult(s);

            // Addition and subtraction
            s = SimpAddAndSub(s);

            if (s.IndexOf('*') == -1 && s.IndexOf('+') == -1 && s.IndexOf('-') == -1 &&
                s.IndexOf('(') == -1 && s.IndexOf(')') == -1)
            {
                // Creating a fraction object in oerder to get rid of several /.
                Fraction fr = new Fraction();
                if (CreateFraction(s, ref fr))
                {
                    if (fr.Denominator != 1 && fr.Denominator != -1)
                    {
                        s = fr.ToString();
                    }
                    else
                    {
                        if (fr.Denominator == -1)
                        {
                            s = "-" + fr.Numerator.ToString();
                        }
                        else
                        {
                            s = fr.Numerator.ToString();
                        }
                    }
                }
                else
                {
                    s = "Cannot be calculated!";
                }
            }

            return(s);
        }
        public static string SimpSub(string s)
        {
            // If there is subtraction.
            if (s.IndexOf('-') != -1)
            {
                // Index of the multiplication sign.
                int multIndex = s.IndexOf('-');

                // The minuend.
                string X = "";

                // The subtrahend.
                string Y = "";

                // Index that finds the minuend.
                int startIndex = multIndex - 1;

                // Getting the minuend.
                while (startIndex > -1 && (Char.IsDigit(s[startIndex]) ||
                                           s[startIndex] == '/' || (startIndex == 0 && s[startIndex] == '-')))
                {
                    X = s[startIndex] + X;
                    startIndex--;
                }

                // Index that finds the subtrahend.
                int endIndex = multIndex + 1;

                // Getting the subtrahend.
                while (endIndex < s.Length && (Char.IsDigit(s[endIndex]) ||
                                               s[endIndex] == '/' || (endIndex == multIndex + 1 && s[endIndex] == '-')))
                {
                    Y += s[endIndex];
                    endIndex++;
                }

                // Creating buffers for future fractions.
                Fraction xFrac = new Fraction();
                Fraction yFrac = new Fraction();

                if (X != "" && Y != "")
                {
                    // If both can be fractions then calcualte and add to the original string.
                    if (CreateFraction(X, ref xFrac) && CreateFraction(Y, ref yFrac))
                    {
                        string result = Calculations.Minus(xFrac, yFrac).ToString();

                        s = s.Substring(0, startIndex + 1) + result + s.Substring(endIndex, s.Length - endIndex);
                    }
                    else
                    {
                        MessageBox.Show("Input error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        s = "Cannot be calculated!";
                        return(s);
                    }
                }
                else
                {
                    if (X == "")
                    {
                        if (CreateFraction(Y, ref yFrac))
                        {
                            s = "-" + yFrac.ToString();
                        }
                        else
                        {
                            MessageBox.Show("Input error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            s = "Cannot be calculated!";
                            return(s);
                        }
                    }
                    else
                    {
                        s = X;
                    }
                }
            }

            return(s);
        }