1. 程式人生 > >C++_數字時鐘設計

C++_數字時鐘設計

利用C++學習內容,通過windows自帶函式實現yige一個簡易的時鐘

#include<iostream>
#include<windows.h> //延時與清屏標頭檔案 
using namespace std;
class time
{
	public:
		time(){year=0;month=0;day=0;hour=0;minute=0;sec=0;}//預設建構函式 
		time(int y,int mo,int d,int h ,int m,int s):year(y),month(mo),day(d),hour(h),minute(m),sec(s){}//建構函式過載 
		time operator++();//宣告運算子過載成員函式 
		void display()
			{
				cout<<"*********"<<year<<"-"<<month<<"-"<<day<<"**********"<<endl;
				cout<<"********"<<hour<<" : "<<minute<<" : "<<sec<<"********"<<endl;
			}//輸出時間格式 
	private:
		int year;
		int month;
		int day;
		int hour;
		int minute;
		int sec;	
};
time time::operator++()//定義運算子過載成員函式 
{
	if(++sec>=60)
	{
		sec-=60;
		++minute;
		if(minute>=60)
		{
			minute-=60;
			++hour;
			if(hour>=24)
        		{
        			hour-=24;
        			++day;
        			if(day>=30)
        			{
        				day-=30;
        				++month;
        				if(month>=12)
        				{
        					month-=12;
        					++year;
						}
					}
				}
		}
	}
	return *this;//返回當前物件值 
}
int main()
{
	int a,b,c,e,f,g,h;
	cout<<"請修改當前時間:(格式如下)\n";
	cout<<"2018 10 3 12 50 45\n";
	{
	cout<<"請輸入當前年份(2018):"<<endl;
	cin>>g;
	cout<<"請輸入當前月份(1~12):"<<endl; 
	cin>>e;
	cout<<"請輸入當前幾號(1~30):"<<endl;
	cin>>h;
	cout<<"請輸入現在時間(12 30 32):"<<endl;
	cin>>a>>b>>c;
	}
	
	{
		time time1(g,e,h,a,b,c);//如何向time1中輸入資料 
		for(int i=0;;i++)
		{	cout<<"*****歡迎進入家庭計時系統*****"<<endl;
			cout<<"*******假設每個月30天*******"<<endl; 
			++time1;
			time1.display() ;
			cout<<"***********designed by yuumoz.\n";
			Sleep(1000);
	     	system("CLS");
		}
	}
	return 0;
 }