Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace FractionOp {
- class Fraction {
- public double c = 0;
- public double z = 0;
- public Fraction(int c, int z) {
- this.c = c;
- this.z = z;
- }
- public override string ToString() {
- return "(" + c.ToString() + "/" + z.ToString() + ")";
- }
- public static Fraction operator +(Fraction a, Fraction b) {
- Fraction t = new Fraction(1, 1);
- t.c = (a.c * b.z + a.z * b.c);
- t.z = a.z * b.z;
- Fraction.SetFormat(t);
- return t;
- }
- public static Fraction operator -(Fraction a, Fraction b) {
- Fraction t = new Fraction(1, 1);
- t.c = (a.c * b.z - a.z * b.c);
- t.z = a.z * b.z;
- Fraction.SetFormat(t);
- return t;
- }
- public static Fraction operator *(Fraction a, Fraction b) {
- Fraction t = new Fraction(1, 1);
- t.c = (a.c * b.c);
- t.z = a.z * b.z;
- Fraction.SetFormat(t);
- return t;
- }
- public static Fraction operator /(Fraction a, Fraction b) {
- Fraction t = new Fraction(1, 1);
- t.c = (a.c / b.c);
- t.z = a.z / b.z;
- Fraction.SetFormat(t);
- return t;
- }
- public static Fraction SetFormat(Fraction a) {
- double max = 0;
- if (a.c > a.z) {
- max = Math.Abs(a.z);
- } else {
- max = Math.Abs(a.c);
- }
- for (double i = max; i >= 2; i--) {
- if ((a.c % i == 0) & (a.z % i == 0)) {
- a.c = a.c / i;
- a.z = a.z / i;
- }
- }
- if ((a.z < 0)) {
- a.c = -1 * (a.c);
- a.z = Math.Abs(a.z);
- }
- return (a);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement