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);
        }
        // Function to create a fraction object.
        public static bool CreateFraction(string s, ref Fraction fraction)
        {
            // If there are / symbols.
            if (s.IndexOf('/') != -1)
            {
                // Getting the array of symbols.
                string[] frac = s.Split('/');

                int buf;

                // Future numerator.
                int numer = 0;
                // Future denominator.
                int denom = 1;

                // Checking all the symbols.
                for (int i = 0; i < frac.Length; i++)
                {
                    // If it's an integer then add it.
                    if (Int32.TryParse(frac[i], out buf))
                    {
                        // The 1st integer is the numerator.
                        if (i == 0)
                        {
                            numer = Convert.ToInt32(frac[i]);
                        }
                        // The multiplication of the rest is the denominator.
                        else
                        {
                            denom *= Convert.ToInt32(frac[i]);
                        }
                    }
                    // If it's not an integer then can't create a fracton.
                    else
                    {
                        return(false);
                    }
                }

                try
                {
                    fraction = new Fraction(numer, denom);
                }
                catch
                {
                    return(false);
                }

                return(true);
            }
            else
            {
                try
                {
                    fraction = new Fraction(Convert.ToInt32(s), 1);
                }
                catch
                {
                    return(false);
                }
                return(true);
            }
        }
Ejemplo n.º 3
0
 public static double ToDecimal(Fraction curFrac)
 {
     return((double)(curFrac.Numerator) / curFrac.Denominator);
 }
Ejemplo n.º 4
0
 public static Fraction Divide(Fraction a, Fraction b)
 {
     return(a / b);
 }
Ejemplo n.º 5
0
 public static Fraction Multiply(Fraction a, Fraction b)
 {
     return(a * b);
 }
Ejemplo n.º 6
0
 public static Fraction Minus(Fraction a, Fraction b)
 {
     return(a - b);
 }
Ejemplo n.º 7
0
 public static Fraction Plus(Fraction a, Fraction b)
 {
     return(a + b);
 }