OOP class CThoi Gian go phut giay

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ctime { class CThoiGian { private int gio, phut, giay; public CThoiGian() { gio = 0; phut = 0; giay = 0; } public CThoiGian(int g, int p, int gi) { gio = g; phut = p; giay = gi; } public CThoiGian(CThoiGian a) { gio = a.gio; phut = a.phut; giay = a.giay; } public void xuat() { Console.Write(" "); Console.Write("{0}:{1}:{2}", gio, phut, giay); } //// bây giồ mình sẽ tạo phương thức hay cón gọi là operator nhé các bạn public static bool operator >(CThoiGian a, CThoiGian b) { int c = a.gio * 3600 + a.phut * 60 + a.giay; int d = b.gio * 3600 + b.phut * 60 + b.giay; return c > d; } public static bool operator <(CThoiGian a, CThoiGian b) { int c = a.gio * 3600 + a.phut * 60 + a.giay; int d = b.gio * 3600 + b.phut * 60 + b.giay; return c < d; } public static bool operator >=(CThoiGian a, CThoiGian b) { int c = a.gio * 3600 + a.phut * 60 + a.giay; int d = b.gio * 3600 + b.phut * 60 + b.giay; return c >= d; } public static bool operator<=(CThoiGian a, CThoiGian b) { int c = a.gio * 3600 + a.phut * 60 + a.giay; int d = b.gio * 3600 + b.phut * 60 + b.giay; return c <= d; } public static bool operator ==(CThoiGian a, CThoiGian b) { int c = a.gio * 3600 + a.phut * 60 + a.giay; int d = b.gio * 3600 + b.phut * 60 + b.giay; return c ==d; } public static bool operator !=(CThoiGian a, CThoiGian b) { int c = a.gio * 3600 + a.phut * 60 + a.giay; int d = b.gio * 3600 + b.phut * 60 + b.giay; return c != d; } // bây giờ là +- 2 thời gian lại nhé các bạn //+ public static CThoiGian operator +(CThoiGian a, CThoiGian b) { CThoiGian c = new CThoiGian(); c.gio = a.gio + b.gio; c.phut = a.phut + b.phut; c.giay = a.giay + b.giay; if (c.giay > 59) { c.phut += 1; c.giay%=10 ; } if (c.phut > 59) { c.gio += 1; c.phut%=10; } return c; } // ta qua phần trừ nhé các bạn public static CThoiGian operator -(CThoiGian a, CThoiGian b) { CThoiGian c = new CThoiGian(); c.gio= a.gio - b.gio; c.phut = a.phut - b.phut; c.giay = a.giay - b.giay; if (c.giay < 0) { c.giay *= -1; } if (c.phut < 0) { c.phut *= -1; } if (c.gio < 0) { c.gio *= -1; } return c; } } class Program { static void Main(string[] args) { CThoiGian t = new CThoiGian(1,2,3); CThoiGian b = new CThoiGian(1, 59, 58); t -= b; t.xuat(); Console.ReadKey(); } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.