PHÂN SỐ
namespace PhanSo
{
class PhanSo
{
int ts; //tu so
int ms; // mau so
public PhanSo()
{
this.ts = 0;
this.ms = 1;
public void Nhap1PS()
{
Console.Write("Nhap tu so:");
ts = int.Parse(Console.ReadLine());
do{
Console.Write("Nhap mau so (khac 0):");
ms = int.Parse(Console.ReadLine());
if(ms==0)
Console.WriteLine("Phai nhap mau so khac 0!"); }while(ms==0);
public int USCLN()
{
int a = Math.Abs(ts);
int b = Math.Abs(ms);
if (a == 0) return 1;
while (a != b)
{
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
public void RutGon1PS()
{
int uscln = USCLN();
ts = ts / uscln;
ms = ms /uscln;
if (ms * ts < 0)
ts = -1 * Math.Abs(ts);
else ts = Math.Abs(ts);
ms = Math.Abs(ms);
public void Xuat1PS()
{
RutGon1PS();
Console.WriteLine("[{0}/{1}]", ts, ms);
public PhanSo Cong(PhanSo ps1)
{
Trang 2this.RutGon1PS();
ps1.RutGon1PS();
PhanSo tam = new PhanSo();
tam.ts = this.ts * ps1.ms + this.ms * ps1.ts;
tam.ms = this.ms * ps1.ms;
tam.RutGon1PS();
return tam;
public PhanSo Tru(PhanSo ps1)
{
this.RutGon1PS();
ps1.RutGon1PS();
PhanSo tam = new PhanSo();
tam.ts = this.ts * ps1.ms - this.ms * ps1.ts;
tam.ms = this.ms * ps1.ms;
tam.RutGon1PS();
return tam;
public override string ToString()
{
RutGon1PS();
return "[" + ts + "/" + ms + "]";
}
}
}
NGÀY THÁNG NĂM
namespace NgayThang
{
class Date
{
int ngay;
int thang;
int nam;
public Date()
{
ngay = 0;
thang = 0;
nam = 0;
public Date(int ng, int th, int nnam)
{
ngay = ng;
thang = th;
nam = nnam;
public bool KTNamNhuan(int year)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) return true;
return false;
public bool KTHopLe(int day, int month, int year)
{
Trang 3int[] soNgayCuaThang = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (KTNamNhuan(year) == true)
soNgayCuaThang[2] = 29;
if (day < 1 || day > soNgayCuaThang[month])
return false;
if (month < 1 || month > 12)
return false;
if (year < 1)
return false;
return true;
public void Nhap1Date()
{
int ngtam, thgtam, namtam;
do
{
Console.Write("Nhap ngay:");
ngtam = int.Parse(Console.ReadLine());
Console.Write("Nhap thang:");
thgtam = int.Parse(Console.ReadLine());
Console.Write("Nhap nam:");
namtam = int.Parse(Console.ReadLine());
if (KTHopLe(ngtam, thgtam, namtam) == false)
Console.WriteLine("Ban nhap ngay thang nam khong hop le.\nHay nhap lai!"); } while (KTHopLe(ngtam, thgtam, namtam) == false);
this.ngay = ngtam;
this.thang = thgtam;
this.nam = namtam;
}
public override string ToString()
{
return "[" + ngay + "/" + thang + "/" + nam + "]";
public double TongSoNgay()
{
int[] soNgayCuaThang = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if(KTNamNhuan(nam)==true)
soNgayCuaThang[2]=29;
long S=0;
for (int i = 1; i < nam; i++)
if (KTNamNhuan(i) == true)
S = S + 366;
else S = S + 365;
for (int i = 0; i < thang; i++)
S = S + soNgayCuaThang[i];
S = S + ngay;
return S;
public string ThuMay()
{
int tam = (int)(TongSoNgay() % 7);
switch (tam)
{
Trang 4case 0: return "Chu Nhat";
case 1: return "Thu Hai";
case 2: return "Thu Ba";
case 3: return "Thu Tu";
case 4: return "Thu Nam";
case 5: return "Thu Sau";
case 6: return "Thu Bay";
}
return "";
public Date NgayTruocDo()
{
int day = ngay, month = thang, year = nam;
int[] soNgayCuaThang = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if(KTNamNhuan(year)==true)
soNgayCuaThang[2]=29;
day = day - 1;
if (day == 0)
{
month = month - 1;
if (month == 0)
{
month = 12;
year = year - 1;
}
day = soNgayCuaThang[month];
}
Date t = new Date(day, month, year);
return t;
public Date NgayTiepTheo()
{
int day = ngay, month = thang, year = nam;
int[] soNgayCuaThang = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (KTNamNhuan(year) == true)
soNgayCuaThang[2] = 29;
day = day + 1;
if (day > soNgayCuaThang[month])
{
day = 1;
month = month + 1;
if (month > 12)
{
month = 1;
year = year + 1;
}
}
Date t = new Date(day, month, year);
return t;
}
}
}