using System;
using System.Collections.Generic;
using System.Text;
namespace bai5
{
class Program
{
static void Main(string[] args)
{
int tu1,tu2,mau1,mau2;
Console.WriteLine("Tinh Phan So V.01[Code by DKT]");
Console.WriteLine("Nhap phan so thu nhat : ");
Console.Write("Tu = ");
tu1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Mau = ");
mau1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Nhap phan so thu hai : ");
Console.Write("Tu = ");
tu2 = Convert.ToInt16(Console.ReadLine());
Console.Write("Mau = ");
mau2 = Convert.ToInt16(Console.ReadLine());
PHANSO ps1 = new PHANSO(tu1, mau1);
PHANSO ps2 = new PHANSO(tu2, mau2);
PHANSO ps3 = new PHANSO();
PHANSO ps11 = new PHANSO(ps1);
PHANSO ps22 = new PHANSO(ps2);
Console.WriteLine("Phan so thu nhat : "+ps1.ToString());
Console.WriteLine("Phan so thu hai : "+ps2.ToString());
ps3 = ps1.Cong(ps2);
Console.WriteLine(ps1.ToString()+ " + " +ps2.ToString()+" = "+ ps3.ToString());
ps3 = ps1.Tru(ps2);
Console.WriteLine(ps1.ToString() + " - " + ps2.ToString() + " = " + ps3.ToString());
ps3 = ps1.Nhan(ps2);
Console.WriteLine(ps1.ToString() + " * " + ps2.ToString() + " = " + ps3.ToString());
ps3 = ps1.Chia(ps2);
Console.WriteLine(ps1.ToString() + " / " + ps2.ToString() + " = " + ps3.ToString());
Console.ReadLine();
}
}
class PHANSO
{
#region bien
int tuso;
int mauso;
#endregion
#region thuoc tinh
public int TUSO
{
get { return tuso; }
set { tuso = value; }
}
public int MAUSO
{
get { return mauso; }
set { if (value != 0) mauso = value; }
}
#endregion
#region contructors
public PHANSO() { }
public PHANSO(int tuso, int mauso)
{
TUSO = tuso;
MAUSO = mauso;
}
public PHANSO(PHANSO ps)
{
this.TUSO = ps.TUSO;
this.MAUSO = ps.MAUSO;
}
#endregion
#region ham
private int USCLN(int a, int b)
{
a = Math.Abs(a);
b = Math.Abs(b);
while (a != b)
{
if (a > b)
a -= b;
else
b -= a;
}
return a;
}
public void Rutgon()
{
if (mauso < 0)
{
mauso = Math.Abs(mauso);
tuso *= -1;
}
else
{
int uocso = USCLN(tuso, mauso);
tuso /= uocso;
mauso /= uocso;
}
}
public PHANSO Cong(PHANSO ps)
{
PHANSO kq = new PHANSO();
kq.TUSO = this.TUSO * ps.MAUSO + ps.TUSO * this.MAUSO;
kq.MAUSO = this.MAUSO * ps.MAUSO;
kq.Rutgon();
return kq;
}
public PHANSO Nhan(PHANSO ps)
{
PHANSO kq = new PHANSO();
kq.TUSO = this.TUSO * ps.TUSO;
kq.MAUSO = this.MAUSO * ps.MAUSO;
kq.Rutgon();
return kq;
}
public PHANSO Chia(PHANSO ps)
{
PHANSO kq = new PHANSO();
kq.TUSO = this.TUSO * ps.MAUSO;
kq.MAUSO = this.MAUSO * ps.TUSO;
kq.Rutgon();
return kq;
}
public PHANSO Tru(PHANSO ps)
{
PHANSO kq = new PHANSO();
kq.TUSO = this.TUSO * ps.MAUSO - ps.TUSO * this.MAUSO;
kq.MAUSO = this.MAUSO * ps.MAUSO;
kq.Rutgon();
return kq;
}
public override string ToString()
{
return TUSO + "/" + MAUSO;
}
#endregion
//nap chong toan tu
#region operators
public static PHANSO operator +(PHANSO ps1, PHANSO ps2)
{
return ps1.Cong(ps2);
}
//so sanh 2 phan so
public static bool operator ==(PHANSO ps1, PHANSO ps2)
{
ps1.Rutgon();
ps2.Rutgon();
return (ps1.TUSO == ps2.TUSO && ps1.MAUSO == ps2.MAUSO);
}
public static bool operator !=(PHANSO ps1, PHANSO ps2)
{
bool kq = (ps1 == ps2);
return !kq;
}
//phu quyet ham equal() cua lop cha
public override bool Equals(object obj)
{
if (!(obj is PHANSO)) return false;
return this == (PHANSO)obj;
}
#endregion
}
}
Không có nhận xét nào:
Đăng nhận xét