Sunday, July 3, 2016

Ứng Dụng đếm thời gian viết bằng C#





thời gian hiện tại được tự động tăng lên 
các chức năng chính của chương trình là reset thời gian khi bấm r

 tạm dừng đếm khi bấm nút spacebar và tiếp tục đếm khi bấm spacebar.

scrouce code

file .cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace exam_Class
{
    class time
    {
        private int gio;
        private int phut;
        private int giay;
        private bool resume = true;
        /// <summary>
        /// getter setter giây
        /// </summary>
        public int Giay
        {
            get
            {
                return giay;
            }
            set
            {
                if (giay > 58)
                {
                    giay = 0;
                    phut++;
                    return;
                }
                giay = value;
            }
        }
     
        public time()
        {
            gio = 0;
            phut = 0;
            giay = 0;
            resume = true;
        }
        public void pause()
        {
            resume = !resume;
        }
       public void count()
       {
           if (resume)
               Giay++;
           if (phut > 59)
           {
               phut = 0;
               gio++;
           }
           if (gio > 23)
           {
               gio = 0;
               phut = 0;
               giay = 0;
           }
        }
        public void reset()
        {
            gio = 0;
            phut = 0;
            giay = 0;
        }
        public void showtime(int x, int y)
        {
            Console.SetCursorPosition(x, y);
            Console.WriteLine(gio.ToString("00") + ":"+phut.ToString("00")+":"+giay.ToString("00"));
        }
    }
}

File Main 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace exam_Class
{
    class Program
    {
        static void Main(string[] args)
        {
            time abc = new time();
            while (true)
            {
                Thread.Sleep(1000);
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo kitu = Console.ReadKey();
                    if (kitu.Key.Equals(ConsoleKey.R))
                        abc.reset();
                    if (kitu.Key.Equals(ConsoleKey.Spacebar))
                        abc.pause();
                }
                abc.count();
                    abc.showtime(0, 0);
             
            }
        }
    }
    }

0 comments:

Post a Comment