public static PhanSo operator +(PhanSo a, PhanSo b) { PhanSo phanSoTong = new PhanSo(); phanSoTong.tuso = b.tuso * a.mauso + b.mauso * a.tuso; phanSoTong.mauso = b.mauso * a.mauso; return(phanSoTong); }
public static PhanSo operator /(PhanSo ps1, PhanSo ps2) { PhanSo tich = new PhanSo(); tich.tuSo = ps1.tuSo * ps2.tuSo; tich.mauSo = ps1.mauSo * ps2.mauSo; return(tich); }
public static PhanSo operator +(PhanSo ps1, PhanSo ps2) { PhanSo tong = new PhanSo(); tong.tuSo = ps1.tuSo * ps2.mauSo + ps2.tuSo * ps1.mauSo; tong.mauSo = ps1.mauSo * ps2.mauSo; return(tong); }
public static PhanSo operator -(PhanSo ps1, PhanSo ps2) { PhanSo hieu = new PhanSo(); hieu.tuSo = ps1.tuSo * ps2.mauSo - ps2.tuSo * ps1.mauSo; hieu.mauSo = ps1.mauSo * ps2.mauSo; return(hieu); }
private void btnCongPhanSo_Click(object sender, EventArgs e) { int tuSoA = int.Parse(this.txtBoxTuSoA.Text); int mauSoA = int.Parse(this.txtBoxMauSoA.Text); int tuSoB = int.Parse(this.txtBoxTuSoB.Text); int mauSoB = int.Parse(this.txtBoxMauSoB.Text); PhanSo KQ1 = new PhanSo(tuSoA, mauSoA) + new PhanSo(tuSoB, mauSoB); KQ1.RutGon(); lblKetQua.Text = "Kết quả cộng 2 phân số là: " + KQ1.toString(); }
private void btnCongPhanSo_Click(object sender, EventArgs e) { int tuSoA = int.Parse(this.txtBoxTuSoA.Text); int mauSoA = int.Parse(this.txtBoxMauSoA.Text); int tuSoB = int.Parse(this.txtBoxTuSoB.Text); int mauSoB = int.Parse(this.txtBoxMauSoB.Text); PhanSo kq = new PhanSo(tuSoA, mauSoA) + new PhanSo(tuSoB, mauSoB); kq = PhanSo.Reduce(kq); this.lblKetQua.Text = "Ket qua cong hai phan so la: " + kq.tuSo + "/" + kq.mauSo; this.lblKetQua.Location = new System.Drawing.Point(this.Width / 2 - this.lblKetQua.Width / 2, this.lblKetQua.Location.Y); }
private void btnCongPhanSo_Click(object sender, EventArgs e) { int tusoa = int.Parse(this.txtBoxtusoa.Text); int mausoa = int.Parse(this.txtBoxmausoa.Text); int tusob = int.Parse(this.txtBoxtusob.Text); int mausob = int.Parse(this.txtBoxmausob.Text); PhanSo kq = new PhanSo(tusoa, mausoa) + new PhanSo(tusob, mausob); kq = PhanSo.Reduce(kq); this.label5.Text = "ket qua cong 2 phan so la: " + kq.tuso + "/" + kq.mauso; this.label5.Location = new System.Drawing.Point(this.Width / 2 - this.label5.Width / 2, this.label5.Location.Y); }
public static PhanSo Reduce(PhanSo phanSo) { int divisor, numerator, denominator, reducedNumerator, reducedDenominator; numerator = phanSo.tuso; denominator = phanSo.mauso; divisor = gcd(numerator, denominator); if (divisor != 1) { reducedNumerator = divvision(numerator, divisor); reducedDenominator = divvision(denominator, divisor); return(new PhanSo(reducedNumerator, reducedDenominator)); } else { return(phanSo); } }
static void Main(string[] args) { int x; Console.WriteLine("Nhap \'1\' neu muon cong"); Console.WriteLine("Nhap \'2\' neu muon tru"); Console.WriteLine("Nhap \'3\' neu muon nhan"); Console.WriteLine("Nhap \'4\' neu muon chia"); Console.WriteLine("Nhap \'5\' neu toi gian"); x = int.Parse(Console.ReadLine()); PhanSo ps1 = new PhanSo(); PhanSo ps2 = new PhanSo(); PhanSo ps3 = new PhanSo(); switch (x) { case 1: { Console.WriteLine("Phan so 1: "); ps1.Nhap(); Console.WriteLine("Phan so 2: "); ps2.Nhap(); ps3 = ps1 + ps2; Console.WriteLine("{0}/{1} + {2}/{3} = {4}/{5}", ps1.TuSo, ps1.MauSo, ps2.TuSo, ps2.MauSo, ps3.TuSo, ps3.MauSo); } break; case 2: { Console.WriteLine("Phan so 1: "); ps1.Nhap(); Console.WriteLine("Phan so 2: "); ps2.Nhap(); ps3 = ps1 - ps2; Console.WriteLine("{0}/{1} - {2}/{3} = {4}/{5}", ps1.TuSo, ps1.MauSo, ps2.TuSo, ps2.MauSo, ps3.TuSo, ps3.MauSo); } break; case 3: { Console.WriteLine("Phan so 1: "); ps1.Nhap(); Console.WriteLine("Phan so 2: "); ps2.Nhap(); ps3 = ps1 * ps2; Console.WriteLine("{0}/{1} * {2}/{3} = {4}/{5}", ps1.TuSo, ps1.MauSo, ps2.TuSo, ps2.MauSo, ps3.TuSo, ps3.MauSo); } break; case 4: { Console.WriteLine("Phan so 1: "); ps1.Nhap(); Console.WriteLine("Phan so 2: "); ps2.Nhap(); ps3 = ps1 / ps2; Console.WriteLine("{0}/{1} / {2}/{3} = {4}/{5}", ps1.TuSo, ps1.MauSo, ps2.TuSo, ps2.MauSo, ps3.TuSo, ps3.MauSo); } break; case 5: { Console.WriteLine("Nhap phan so muon toi gian: "); ps1.Nhap(); ps1.KiemTra(); Console.WriteLine("Dang kiem tra..."); if (ps1.KiemTra()) { Console.WriteLine("Phan so da toi gian."); } else { ps1.ToiGian(); Console.WriteLine("Sau khi toi gian: " + ps1.TuSo + "/" + ps1.TuSo); } } break; default: { Console.WriteLine("Nhap sai !!!"); break; } } }
// So sanh bang a / b va c / d => a / b - c / d = 0 public static bool operator ==(PhanSo a, PhanSo b) { PhanSo kq = a - b; return(kq.tuSo == 0); }
public static bool operator <(PhanSo a, PhanSo b) { PhanSo kq = a - b; return(kq.tuso < 0); }