Liên Thông CNTT K4

Cộng Đồng Liên Thông Công Nghệ Thông Tin Bình Thuận

Trang ChínhTrang Chính  Latest imagesLatest images  Tìm kiếmTìm kiếm  Đăng kýĐăng ký  Đăng NhậpĐăng Nhập  

Share
 
 Giải BTVN - Bài tập 1
Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down 
Tác giảThông điệp
Exterminator


Exterminator
Giới tính : Nam
Posts Posts : 33
Thanked Thanked : 82
Giải BTVN - Bài tập 1 Empty

Code:
    public class CTime
    {
        private int hour, minute, second;
        //Property get, set
        public int Hour
        {
            get { return hour; }
            set
            {
                if (value >= 0 && value <= 23) hour = value;
                else hour = 0;
            }
        }
        public int Minute
        {
            get { return minute; }
            set
            {
                if (value >= 0 && value <= 59) minute = value;
                else minute = 0;
            }
        }
        public int Second
        {
            get { return second; }
            set
            {
                if (value >= 0 && value <= 59) second = value;
                else second = 0;
            }
        }
        public void Export()
        {
            Console.WriteLine("{0} gio {1} phut {2} giay", hour, minute, second);
        }
        //Phương thức thiết lập (constructors)
        public CTime()
        {
            hour = 0;
            minute = 0;
            second = 0;
        }
        public CTime(int ihour, int iminute, int isecond)
        {
            Hour = ihour;
            Minute = iminute;
            Second = isecond;
        }
        public CTime(CTime ct)
        {
            ct.hour = hour;
            ct.minute = minute;
            ct.second = second;
        }
        //Các phương thức xử lý tính toán
        public void TimeSuccess()
        {
            if (second >= 60)
            {
                minute = minute + second / 60;
                second = second % 60;
            }
            if (minute >= 60)
            {
                hour = hour + minute / 60;
                minute = minute % 60;
            }
        }
        public int ConvertTimeToMinute()
        {
            return hour * 60 + minute + second / 60;
        }
        public int ConvertTimeToSecond()
        {
            return hour * 3600 + minute * 60 + second;
        }
        public CTime ConvertSecondToTime(int isecond)
        {
            CTime kq = new CTime();
            kq.hour = isecond / 3600;
            kq.minute = (isecond - kq.hour * 3600) / 60;
            kq.second = isecond - kq.hour * 3600 - kq.minute * 60;
            return kq;
        }
        //Các phép toán
        public CTime CongTime(CTime b)
        {
            CTime kq = new CTime();
            int congsecond = ConvertTimeToSecond() + b.ConvertTimeToSecond();
            kq = ConvertSecondToTime(congsecond);
            return kq;
        }
        public CTime TruTime(CTime b)
        {
            CTime kq = new CTime();
            int trusecond = ConvertTimeToSecond() - b.ConvertTimeToSecond();
            kq = ConvertSecondToTime(trusecond);
            return kq;
        }
        public bool SoSanhTime(CTime b)
        {
            if (ConvertTimeToSecond() > b.ConvertTimeToSecond())
                return true;
            else
                return false;
        }
    }

Mọi người chú ý, nếu copy bài tập này thì nhớ chỉnh sửa đổi tên các hàm lại nha, đây là bài của mình, để nguyên là chết cả đám lun ak

※ Bài viết cùng chuyên mục


Tác giảThông điệp
hieupt


avatar
Posts Posts : 5
Thanked Thanked : 5
Giải BTVN - Bài tập 1 Empty

Thank bác nhá

※ Bài viết cùng chuyên mục


Tác giảThông điệp
phuctn


avatar
Posts Posts : 10
Thanked Thanked : 5
Giải BTVN - Bài tập 1 Empty

sao không có hàm nhập, xuất
code trong hàm main thế nào để mình chạy thử

※ Bài viết cùng chuyên mục


Tác giảThông điệp
Exterminator


Exterminator
Giới tính : Nam
Posts Posts : 33
Thanked Thanked : 82
Giải BTVN - Bài tập 1 Empty

phuctn đã viết:
sao không có hàm nhập, xuất
code trong hàm main thế nào để mình chạy thử
chỉ có hàm xuất thôi nhé Export() ak.
CÒn nhập thì trong main sẽ gọi, code lớp main nek
Code:
    class Program
    {
        static void Main(string[] args)
        {
            CTime ct1 = new CTime();
            Console.WriteLine("<== Nhap CTime 01 ==>");
            Console.Write("Nhap gio: ");
            ct1.Hour = int.Parse(Console.ReadLine());
            Console.Write("Nhap phut: ");
            ct1.Minute = int.Parse(Console.ReadLine());
            Console.Write("Nhap giay: ");
            ct1.Second = int.Parse(Console.ReadLine());
            Console.WriteLine("<== Xuat CTime 01 ==>");
            ct1.Export();

            CTime ct2 = new CTime();
            Console.WriteLine("<== Nhap CTime 02 ==>");
            Console.Write("Nhap gio: ");
            ct2.Hour = int.Parse(Console.ReadLine());
            Console.Write("Nhap phut: ");
            ct2.Minute = int.Parse(Console.ReadLine());
            Console.Write("Nhap giay: ");
            ct2.Second = int.Parse(Console.ReadLine());
            Console.WriteLine("<== Xuat CTime 02 ==>");
            ct2.Export();

            CTime kq = new CTime();
            kq= ct1.TruTime(ct2);
            Console.WriteLine("<== Xuat CTime Cong ==>");
            kq.Export();

            Console.WriteLine(ct1.SoSanhTime(ct2));
            Console.ReadLine();
        }
    }

※ Bài viết cùng chuyên mục


Tác giảThông điệp
Administrator
Administrator

avatar
Tuối : 33
Giới tính : Nam
Posts Posts : 40
Thanked Thanked : 23
Giải BTVN - Bài tập 1 Empty

Chú ý: Mọi người tìm đoạn này
Code:
        public CTime(CTime ct)[size=12][/size]
        {
            ct.hour = hour;
            ct.minute = minute;
            ct.second = second;
        }
Thay bằng đoạn sau
Code:
       public CTime(CTime ct)
        {
            hour = ct.Hour;
            minute = ct.minute;
            second = ct.second;
        }

※ Bài viết cùng chuyên mục


https://lienthongcntt.forum-viet.com
Tác giảThông điệp
Sponsored content


Giải BTVN - Bài tập 1 Empty

※ Bài viết cùng chuyên mục


 
Giải BTVN - Bài tập 1
Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang 
Trang 1 trong tổng số 1 trang

Permissions in this forum:Bạn không có quyền trả lời bài viết
Diễn đàn Liên thông CNTT Bình Thuận :: Góc Học Tập :: Các môn học liên thông :: Lập trình Hướng đối tượng-
[Liên thông CNTT K4] Deverloped by Phạm Anh Dân - Rip by Nguyễn Gia Phú
Powered by © Forumotion.com - phpBB™ version ©phpBB2
Go to top Go to bottom