static void Main(string[] args) { Fraction fraction1 = Fraction.Parse("1050 / 1205"); Console.WriteLine(fraction1.TextRepresentation + " " + fraction1.Value); Console.WriteLine(fraction1.MyToString()); fraction1.Reduce(); Console.WriteLine(fraction1.MyToString()); }
static void Main(string[] args) { Fraction fraction = new Fraction(10, 15); Console.WriteLine($"Property result: {fraction.Result}"); decimal expressionResult = Fraction.Parse("1/2"); Console.WriteLine($"Result of parsing: {expressionResult}"); fraction.ReduceFraction(); }
static void Main(string[] args) { Fraction g1 = new Fraction(8, 5); Fraction g2 = new Fraction(30, 42); Fraction g3 = new Fraction(19, 33); Console.WriteLine(g1); Fraction g4 = g1 * g2; Fraction g5 = g2 * g1; Fraction g6 = g1 * 7; Fraction g7 = g1 * g2 * g4 * g6 * 9; Console.WriteLine(g7); Console.WriteLine(g7.d); Fraction g8 = Fraction.Parse("2/5"); List <Fraction> katse = new List <Fraction>() { g1, g2, g3, g4, g5, g6, g7, g8, new Fraction(2, 10) }; Console.WriteLine("Before we sort \n"); foreach (Fraction c in katse) { Console.WriteLine(c); } katse.Sort(); Console.WriteLine("After the sort \n"); foreach (Fraction c in katse) { Console.WriteLine(c); } Fraction.Parsemulti("4/20,5/2,3/5"); g7.cancel(); Console.WriteLine(g7); Console.ReadKey(); }
public FractionTest() { string test1 = "-5/23"; string test2 = "16/4.5"; string test3 = "21/-3.1"; Fraction fraction = Fraction.Parse(test1); Console.WriteLine("Fraction is {0}", fraction.fraction); Console.WriteLine("Decimal Value is {0}" + Environment.NewLine, fraction.decimalValue); fraction = Fraction.Parse(test2); Console.WriteLine("Fraction is {0}", fraction.fraction); Console.WriteLine("Decimal Value is {0}" + Environment.NewLine, fraction.decimalValue); fraction = Fraction.Parse(test3); Console.WriteLine("Fraction is {0}", fraction.fraction); Console.WriteLine("Decimal Value is {0}" + Environment.NewLine, fraction.decimalValue); }
static void Main(string[] args) { Console.OutputEncoding = Encoding.Unicode; Console.InputEncoding = Encoding.Unicode; int a, b, c, d; Console.WriteLine("Enter a"); a = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter b"); b = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter c"); c = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter d"); d = Int32.Parse(Console.ReadLine()); Fraction f1 = new Fraction(a, b); Fraction f2 = new Fraction(c, d); if (b == 0 || d == 0) { throw new Exception("Знаменник не може бути рівним нулю!"); } Console.WriteLine("Дроби,які ви ввели, мають наступний вигляд(у вигляді правильного дробу): "); Console.WriteLine(f1.ToString() + " " + f2.ToString()); if (f1) { Console.WriteLine("Перший дріб правильний"); } else { Console.WriteLine("Перший дріб неправильний "); } if (f2) { Console.WriteLine("Другий дріб правильний"); } else { Console.WriteLine("Другий дріб неправильний "); } Console.WriteLine("Рівність між першим і другим дробом "); Console.WriteLine("----------------------------------"); Console.WriteLine($"f1 < f2:{ f1 < f2}"); Console.WriteLine($"f1 > f2:{ f1 > f2}"); Console.WriteLine($"f1 == f2:{ f1 == f2}"); Console.WriteLine($"f1 != f2:{ f1 != f2}"); Console.WriteLine("Математичні дії: "); Console.WriteLine("----------------------------------"); Console.WriteLine($"f1 + f2:{ f1 + f2}"); Console.WriteLine($"f1 - f2:{ f1 - f2}"); Console.WriteLine($"f1 * f2:{ f1 * f2}"); Console.WriteLine($"f1 / f2:{ f1 / f2}"); Console.WriteLine("---------------------------------"); int m = 10; Console.WriteLine("f1+m = {0}", f1 + m); Console.WriteLine("f1*m = {0}", f1 * m); Console.WriteLine("m*f1 = {0}", m * f1); Console.WriteLine("-----------------------------------"); Console.WriteLine("Введіть десятковий дріб(через крапку),який ви хочете додати до першого дробу: "); string s = Console.ReadLine(); Fraction f3 = Fraction.Parse(s); Console.WriteLine("Введений вами десятковий дріб має такий вигляд(у вигляды правильного дробу): "); f3.Show(); Fraction f4 = f1 + f3; Console.WriteLine("Ваш результат(у вигляді правильного дробу): "); f4.Show(); }
static void Main(string[] args) { List <Fraction> fractions = new List <Fraction>() { new Fraction(0.25), new Fraction(2), new Fraction(2, 4) //Creating fractions with different constructor types }; fractions = fractions.Concat(new List <Fraction>() { Fraction.Parse("1/3"), Fraction.Parse("3"), Fraction.Parse("-2(1/4)"), Fraction.Parse("-0.75") //Parsing from different string formats }).ToList(); Fraction testing; Console.WriteLine(Fraction.TryParse("Not a fraction", out testing)); Console.WriteLine(Fraction.TryParse("27/54", out testing)); Console.WriteLine(testing); //TryParse function fractions = fractions.Concat(new List <Fraction>() { new Fraction(3, 4) + new Fraction(2, 3), new Fraction(5, 6) - new Fraction(1, 6), new Fraction(1, 9) * new Fraction(9, 4), new Fraction(2, 3) / new Fraction(3, 2), //Math operations }).ToList(); fractions.Add(new Fraction(7, 8).Clone() as Fraction); //Cloning fraction Console.WriteLine("____________________"); Console.WriteLine(new Fraction(3, 4) > new Fraction(2, 3)); Console.WriteLine(new Fraction(5, 6) <= new Fraction(1, 6)); Console.WriteLine(new Fraction(1, 2) == new Fraction(2, 4)); Console.WriteLine(new Fraction(3, 4) != new Fraction(6, 8)); //Comparison operators Console.WriteLine("____________________"); testing = new Fraction(-5, 2); Console.WriteLine("{0:F}", testing); Console.WriteLine("{0:IF}", testing); Console.WriteLine("{0:I}", testing); Console.WriteLine("{0:D}", testing); Console.WriteLine("{0:D5}", testing); //Converting to string in different formats and IFormattible implementation Console.WriteLine("____________________"); Console.WriteLine((int)testing); Console.WriteLine((double)testing); Console.WriteLine(Convert.ToBoolean(testing)); Console.WriteLine(Convert.ToString(testing)); //Conversion operators and implementation of IConvertible fractions.Sort(); fractions.Sort(new Fraction.DecreaseComparer()); fractions.Sort((x, y) => y.CompareTo(x)); //Sorting by using IComparable interface, default Decrease comparer and CompareTo //function Console.WriteLine("____________________"); fractions.ForEach(x => Console.WriteLine("{0:IF}", x)); Console.WriteLine("____________________"); try { new Fraction(2, 0); //Division by zero exception } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } try { Fraction.Parse("Not a fraction"); //Trying to parse incorrect string exception } catch (FormatException ex) { Console.WriteLine(ex.Message); } try { testing.ToString("Unsupported format"); //Unsupported ToString() format exception } catch (FormatException ex) { Console.WriteLine(ex.Message); } }