利用C++语言基础,制作了一个模拟电子时钟的程序。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<windows.h> //延时与清屏头文件 3 using namespace std; 4 class time 5 { 6 public: 7 time(){year=0;month=0;day=0;hour=0;minute=0;sec=0;}//默认构造函数 8 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){}//构造函数重载 9 time operator++();//声明运算符重载成员函数 10 void display() 11 { 12 cout<<"*********"<<year<<"-"<<month<<"-"<<day<<"**********"<<endl; 13 cout<<"********"<<hour<<" : "<<minute<<" : "<<sec<<"********"<<endl; 14 }//输出时间格式 15 private: 16 int year; 17 int month; 18 int day; 19 int hour; 20 int minute; 21 int sec; 22 }; 23 time time::operator++()//定义运算符重载成员函数 24 { 25 if(++sec>=60) 26 { 27 sec-=60; 28 ++minute; 29 if(minute>=60) 30 { 31 minute-=60; 32 ++hour; 33 if(hour>=24) 34 { 35 hour-=24; 36 ++day; 37 if(day>=30) 38 { 39 day-=30; 40 ++month; 41 if(month>=12) 42 { 43 month-=12; 44 ++year; 45 } 46 } 47 } 48 } 49 } 50 return *this;//返回当前对象值 51 } 52 int main() 53 { 54 int a,b,c,e,f,g,h; 55 cout<<"请修改当前时间:(格式如下) "; 56 cout<<"2018 10 3 12 50 45 "; 57 { 58 cout<<"请输入当前年份(2018):"<<endl; 59 cin>>g; 60 cout<<"请输入当前月份(1~12):"<<endl; 61 cin>>e; 62 cout<<"请输入当前几号(1~30):"<<endl; 63 cin>>h; 64 cout<<"请输入现在时间(12 30 32):"<<endl; 65 cin>>a>>b>>c; 66 } 67 68 { 69 time time1(g,e,h,a,b,c);//如何向time1中输入数据 70 for(int i=0;;i++) 71 { cout<<"*****欢迎进入家庭计时系统*****"<<endl; 72 cout<<"*******假设每个月30天*******"<<endl; 73 ++time1; 74 time1.display() ; 75 cout<<"***********designed by yuumoz. "; 76 Sleep(1000); 77 system("CLS"); 78 } 79 } 80 return 0; 81 }