1. Trang chủ
  2. » Thể loại khác

Lý thuyết Lập trình Hướng đối tượng - Võ Tấn Dũng (votandung) Date

4 93 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 4
Dung lượng 18,86 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Trang 1

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

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)

{

int[] 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()

Trang 2

{

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)

{

case 0: return "Chu Nhat";

case 1: return "Thu Hai";

case 2: return "Thu Ba";

case 3: return "Thu Tu";

case 4: return "Thu Nam";

Trang 3

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;

}

}

Trang 4

class Program

{

static void Main(string[] args)

{

Date ngay1 = new Date();

Console.WriteLine("Nhap mot ngay thang nam:");

ngay1.Nhap1Date();

Console.WriteLine("Ngay vua nhap la: {0} {1}", ngay1.ThuMay(), ngay1);

Console.WriteLine("Ngay ke tiep la : {0} {1}", ngay1.NgayTiepTheo().ThuMay(), ngay1.NgayTiepTheo());

Console.WriteLine("Ngay truoc do la: {0} {1}", ngay1.NgayTruocDo().ThuMay(), ngay1.NgayTruocDo());

}

}

}

Ngày đăng: 15/12/2017, 21:48

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w