Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

quang_cthoigian

using System;

using System.Collections.Generic;

using System.Text;

using System.Threading;

namespace lopthoigian

{

class CThoiGian

{

private int gio, phut, giay;

//THIET LAP

public int Giay

{

get { return giay; }

set { if(KiemTraGiay(value))

giay = value; }

}

public int Phut

{

get { return phut; }

set { if(KiemTraPhut(value))

phut = value; }

}

public int Gio

{

get { return gio; }

set { if(KiemTraGio(value))

gio = value; }

}

public bool KiemTraGio(int h)

{

return (h >= 0 && h <= 23);

}

public bool KiemTraPhut(int p)

{

return (p >= 0 && p <= 59);

}

public bool KiemTraGiay(int s)

{

return (s >= 0 && s <= 59);

}

public CThoiGian(int h, int p, int s)

{

if (!KiemTraGio(h) || !KiemTraPhut(p) || !KiemTraGiay(s))

{

gio = h;

phut = p;

giay = s;

}

}

public CThoiGian()

{

DateTime t = DateTime.Now;

gio = t.Hour;

phut = t.Minute;

giay = t.Second;

}

public CThoiGian(CThoiGian t)

{

gio = t.gio;

phut = t.phut;

giay = t.giay;

}

//KHOI TAO

public void KhoiTao()

{

gio = 12;

phut = 32;

giay = 52;

}

public bool KhoiTao(int h, int p, int s)

{

if (!KiemTraGio(h) || !KiemTraPhut(p) || !KiemTraGiay(s))

return false;

gio = h;

phut = p;

giay = s;

return true;

}

public void KhoiTao(int sogiay)

{

gio = sogiay / 3600;

phut = (sogiay % 3600) / 60;

giay = (sogiay % 3600) % 60;

}

public void KhoiTao(CThoiGian t)

{

gio = t.gio;

phut = t.phut;

giay = t.giay;

}

public void KhoiTaoHT()

{

DateTime t = DateTime.Now;

gio = t.Hour;

phut = t.Minute;

giay = t.Second;

}

//NHAP

public void PhatSinh()

{

Random rd = new Random();

gio = rd.Next(23)+1;

phut = rd.Next(59)+1;

giay = rd.Next(59)+1;

}

public void Nhap()

{

Console.Write("Nhap gio: ");

gio = int.Parse(Console.ReadLine());

Console.Write("Nhap phut: ");

phut = int.Parse(Console.ReadLine());

Console.WriteLine("nhap giay: ");

giay = int.Parse(Console.ReadLine());

}

public void Xuat()

{

Console.WriteLine("{0}:{1}:{2}", gio, phut, giay);

}

public void Xoa()

{

Console.Write("");

}

public int DoiSangGiay()

{

return (gio * 3600 + phut * 60 + giay);

}

//phuong thuc tang giam

public void TangGiay()

{

giay++;

if (giay > 59)

{

giay = 0;

phut++;

if (phut > 59)

{

phut = 0;

gio++;

if (gio > 23)

gio = 0;

}

}

}

public static CThoiGian operator --(CThoiGian t)

{

t.giay--;

if (t.giay < 0)

{

t.giay = 59;

t.phut--;

if (t.phut < 0)

{

t.phut = 59;

t.gio--;

if (t.gio < 0)

t.gio = 23;

}

}

return t;

}

public void TangKGiay(int k)

{

int sogiay = this.DoiSangGiay()+k;

KhoiTao(sogiay);

}

public static CThoiGian operator ++(CThoiGian t)

{

t.giay++;

if (t.giay > 59)

{

t.giay = 0;

t.phut++;

if (t.phut > 59)

{

t.phut = 0;

t.gio++;

if (t.gio > 23)

t.gio = 0;

}

}

return t;

}

public static CThoiGian operator +(CThoiGian t, int k)

{

int h=t.gio+(k/3600);

int p = t.phut + (k % 3600) / 60;

int s = t.giay + ((k % 3600) % 60) / 60;

return t;

}

public void ChayTang()

{

Console.CursorVisible = false;

while (KiemTraGiay(giay) || KiemTraGio(gio) || KiemTraGiay(giay))

{

TangGiay();

Xuat();

Thread.Sleep(1000);

Console.Clear();

}

}

public void ChayGiam()

{

Console.CursorVisible = false;

while (KiemTraGiay(giay) || KiemTraGio(gio) || KiemTraGiay(giay))

{

GiamGiay();

Xuat();

Thread.Sleep(1000);

Console.Clear();

}

}

public void GiamGiay()

{

giay--;

if (giay < 0)

{

giay = 59;

phut--;

if (phut < 0)

{

phut = 59;

gio--;

if (gio < 0)

gio = 23;

}

}

}

public static bool operator ==(cThoiGian t1, CThoiGian t2)

{

if (t1.DoiSangGiay() == t2.DoiSangGiay())

return true;

return false;

}

}

}

Bạn đang đọc truyện trên: Truyen247.Pro