Class CTIME
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class CTIME
{
int gio, phut, giay;
#region khoi tao
public void nhap()
{
lap:
try
{
do
{
Console.Write("Nhap gio: ");
gio = int.Parse(Console.ReadLine());
}
while (!kiemtragio(gio));
do{
Console.Write("Nhap phut: ");
phut = int.Parse(Console.ReadLine());
}
while(!kiemtraphutgiay(phut));
do
{
Console.Write("nhap giay: ");
giay = int.Parse(Console.ReadLine());
}
while (!kiemtraphutgiay(giay));
}
catch
{
Console.Write("Nhap sai: ");
goto lap;
}
}
public CTIME()
{
}
public CTIME(int t)
{
gio = (t / 3600)%24;
phut = (t % 3600) / 60;
giay = (t % 3600) % 60;
}
public CTIME(int a, int b, int c)
{
gio = a;
phut = b;
giay = c;
}
public CTIME(CTIME t)
{
gio = t.gio;
phut = t.phut;
giay=t.giay;
}
public override string ToString()
{
string s = gio + ":" + phut + ":" + giay;
return s;
}
#endregion
#region kiem tra rang buoc
public bool kiemtragio(int g)
{
return (g>=0 && g<24);
}
public bool kiemtraphutgiay(int g)
{
return (g >= 0 && g < 60);
}
#endregion
#region xu li
public static CTIME operator ++(CTIME t) //tang 1 giay
{
t.giay++;
if (t.giay==60)
{
t.giay = 0;
t.phut++;
if (t.phut == 60)
{
t.phut = 0;
t.gio++;
if (t.gio == 24)
t.gio = 0;
}
}
return t;
}
public int doigiay()
{
return gio * 3600 + phut * 60 + giay;
}
public CTIME TangKgiay(int k)//tang k giay
{
CTIME t = new CTIME();
t.gio = gio;
t.phut = phut;
t.giay = giay;
int t2 = t.doigiay()+k;
return new CTIME(t2);
}
public static CTIME operator --(CTIME t) //giam 1 giay
{
t.giay--;
if (t.giay ==-1)
{
t.giay = 59;
t.phut--;
if (t.phut ==-1)
{
t.phut = 59;
t.gio--;
if (t.gio ==-1)
t.gio = 23;
}
}
return t;
}
public int Khoangcach(CTIME t)
{
return Math.Abs( (gio * 3600 + phut * 60 + giay) - t.doigiay());
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class CTIME
{
int gio, phut, giay;
#region khoi tao
public void nhap()
{
lap:
try
{
do
{
Console.Write("Nhap gio: ");
gio = int.Parse(Console.ReadLine());
}
while (!kiemtragio(gio));
do{
Console.Write("Nhap phut: ");
phut = int.Parse(Console.ReadLine());
}
while(!kiemtraphutgiay(phut));
do
{
Console.Write("nhap giay: ");
giay = int.Parse(Console.ReadLine());
}
while (!kiemtraphutgiay(giay));
}
catch
{
Console.Write("Nhap sai: ");
goto lap;
}
}
public CTIME()
{
}
public CTIME(int t)
{
gio = (t / 3600)%24;
phut = (t % 3600) / 60;
giay = (t % 3600) % 60;
}
public CTIME(int a, int b, int c)
{
gio = a;
phut = b;
giay = c;
}
public CTIME(CTIME t)
{
gio = t.gio;
phut = t.phut;
giay=t.giay;
}
public override string ToString()
{
string s = gio + ":" + phut + ":" + giay;
return s;
}
#endregion
#region kiem tra rang buoc
public bool kiemtragio(int g)
{
return (g>=0 && g<24);
}
public bool kiemtraphutgiay(int g)
{
return (g >= 0 && g < 60);
}
#endregion
#region xu li
public static CTIME operator ++(CTIME t) //tang 1 giay
{
t.giay++;
if (t.giay==60)
{
t.giay = 0;
t.phut++;
if (t.phut == 60)
{
t.phut = 0;
t.gio++;
if (t.gio == 24)
t.gio = 0;
}
}
return t;
}
public int doigiay()
{
return gio * 3600 + phut * 60 + giay;
}
public CTIME TangKgiay(int k)//tang k giay
{
CTIME t = new CTIME();
t.gio = gio;
t.phut = phut;
t.giay = giay;
int t2 = t.doigiay()+k;
return new CTIME(t2);
}
public static CTIME operator --(CTIME t) //giam 1 giay
{
t.giay--;
if (t.giay ==-1)
{
t.giay = 59;
t.phut--;
if (t.phut ==-1)
{
t.phut = 59;
t.gio--;
if (t.gio ==-1)
t.gio = 23;
}
}
return t;
}
public int Khoangcach(CTIME t)
{
return Math.Abs( (gio * 3600 + phut * 60 + giay) - t.doigiay());
}
#endregion
}
}
Class Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CTIME a = new CTIME();
a.nhap();
Console.WriteLine("Thoi gian a la: "+a);
Console.WriteLine("Tang a len 1 giay: " + a++);
CTIME b = new CTIME(10,59,50);
Console.WriteLine("Thoi gian b la: "+b);
Console.WriteLine("Tang b len 10 giay: "+b.TangKgiay(10));
CTIME c= new CTIME(9000);
Console.WriteLine("Thoi gian c la: "+c);
Console.WriteLine("giam c xuong 1 giay: "+c--);
Console.WriteLine("Khoang cach thoi gian giua a va b la " + a.Khoangcach(b) + " giay");
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CTIME a = new CTIME();
a.nhap();
Console.WriteLine("Thoi gian a la: "+a);
Console.WriteLine("Tang a len 1 giay: " + a++);
CTIME b = new CTIME(10,59,50);
Console.WriteLine("Thoi gian b la: "+b);
Console.WriteLine("Tang b len 10 giay: "+b.TangKgiay(10));
CTIME c= new CTIME(9000);
Console.WriteLine("Thoi gian c la: "+c);
Console.WriteLine("giam c xuong 1 giay: "+c--);
Console.WriteLine("Khoang cach thoi gian giua a va b la " + a.Khoangcach(b) + " giay");
}
}
}
0 comments:
Post a Comment