• 课程设计:模拟时钟


    电子时钟

    设计并实现如下功能:

    1)设置日期

    2)用“年/月/日 小时:分钟:秒”格式输出日期

    3)在当前时钟的基础上可以增加、较少时间,并显示

    4)设置的时钟能随着时间自动增加(比如设置当前时间为2015/5/25 10:10:10,10秒后再显示为2015/5/25 10:10:20)

      帮别人代写的,写的时候思路走了弯路,想的一直是用<Windows>中的time类取系统时间,然而我在臆想能通过一个小程序改变系统时间设置,然后可以获得自己设定的时间距离1970年1月1日0点0分0秒(系统标准原点)用秒表示的时间,如果如此简单就得到权限电脑估计要崩了233333,后来重新整理思路,其实只需要i++,然后每循环一次让系统休息一秒钟再输出,同时用system("cls")清屏就会呈现出时间在走的假象,另外附加的工作就是自己写一下时间的进制,要判断大小月,平闰年,仅此而已。

    写的时候分成了三个文件(也可以合成一个.cpp),需要用到编译的知识,这个正打算要自学一下,就预先这样做算是做个准备吧23333。


    头文件:Date.h

     1 //Date.h
     2 
     3 class Date
     4 {
     5 public:
     6     Date(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond);
     7     
     8     // Convert from second to date
     9     void getDate(int &iYear, int &iMonth, int &iDay, int &iHour, int &iMinute, int &iSecond);
    10  
    11 // Reset the date 12 int setTime(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond); 13 14 // Add time to the date 15 void addYear(int iYear); 16 void addMonth(int iMonth); 17 void addDay(int iDay); 18 void addHour(int iHour); 19 void addMinute(int iMinute); 20 void addSecond(int iSecond); 21 22 // Whether the year is leap year 23 int isLeap(int iYear); 24 int seconds(); 25 private: 26 int m_lTotalSeconds; 27 };

    声明文件:DateClass.cpp

     1 //类声明
     2 Date::Date(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond)
     3 {
     4     setTime(iYear, iMonth, iDay, iHour, iMinute, iSecond);
     5 }
     6 
     7 int Date::setTime(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond)
     8 {
     9     int iTotalDays = (iYear / 4 - iYear / 100 + iYear / 400) + 367 * iMonth / 12 + iDay + iYear * 365 - 719499;
    10     m_lTotalSeconds = ((iTotalDays * 24 + iHour) * 60 + iMinute) * 60 + iSecond;
    11     return m_lTotalSeconds;
    12 }
    13 
    14 
    15 int Date::isLeap(int iYear)
    16 {
    17     return (((iYear%4==0)&&(iYear%100!=0))||(iYear%400==0));
    18 }
    19 
    20 int Date::seconds()
    21 {
    22     return m_lTotalSeconds;
    23 }
    24 
    25 void Date::getDate(int &iYear, int &iMonth, int &iDay, int &iHour, int &iMinute, int &iSecond)
    26 {
    27     const int monthLengths[2][13] = {
    28         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
    29         { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
    30     };
    31     const int yearLengths[2] = {365, 366};
    32     iDay = m_lTotalSeconds / 86400;
    33     iSecond = m_lTotalSeconds % 86400;
    34     
    35     iYear = iDay / 366;
    36     iDay -= iYear*365 + (iYear+2-1)/4 - (iYear+100-30-1)/100 + (iYear+400-30-1)/400;
    37     
    38     while (1)
    39     {
    40         if (iDay > 0)
    41         {
    42             break;
    43         }
    44         iDay += yearLengths[isLeap(iYear - 1)];
    45         --iYear;
    46     }
    47     for (iMonth = 1; iMonth <= 12; ++iMonth)
    48     {
    49         if (iDay <= monthLengths[isLeap(iYear)][iMonth])
    50         {
    51             iDay -= monthLengths[isLeap(iYear)][iMonth - 1];
    52             break;
    53         }
    54     }
    55 
    56     iHour = m_lTotalSeconds / 3600;        //3600s one hour
    57     iMinute = (m_lTotalSeconds % 3600) / 60;        //60s one minute
    58     iSecond = m_lTotalSeconds % 60;        //ms
    59 }

    主函数:Date.cpp

     1 #include<iostream>
     2 #include<windows.h>
     3 #include<conio.h>
     4 #include<time.h>
     5 #include"Date.h"
     6 using namespace std;
     7 int main()
     8 {
     9     cout<<"电子时钟"<<endl;                                  //输出题意
    10     cout<<"设计并实现如下功能:"<<endl;
    11     cout<<"1)设置日期"<<endl;
    12     cout<<"2)用“年/月/日 小时:分钟:秒”格式输出日期"<<endl;
    13     cout<<"3)在当前时钟的基础上可以增加、较少时间,并显示"<<endl;
    14     cout<<"4)设置的时钟能随着时间自动增加"<<endl;
    15     cout<<"(比如设置当前时间为2015/5/25 10:10:10,10秒后再显示为2015/5/25 10:10:20)
    "<<endl;
    16     system("pause");
    17     SYSTEMTIME sys;                                          //定义sys用于调用系统时间
    18     do
    19     {
    20         GetLocalTime(&sys);                                  //获取系统当前时间
    21         cout<<"系统当前时间(获得任意输入以后继续):
    ";
    22         cout<<sys.wYear<<":"<<sys.wMonth<<":"<<sys.wDay<<" ";//sys.wYear系统年,sys.wMonth系统月,以此类推
    23         cout<<sys.wHour<<":"<<sys.wMinute<<":"<<sys.wSecond<<endl;
    24         Sleep(1);                                            //系统暂停1毫秒
    25         system("cls");                                       //清屏
    26         while(kbhit()){goto a;}                              //有键盘输入则跳出循环,否则一直循环
    27     }while(1);
    28     
    29   a:
    30     cout<<"现在可以更改时间了
    ";
    31     cout<<"请输入时间:年 月 日  小时 分钟 秒"<<endl;
    32     
    33     int year,month,day,hour,minute,second,judge=0,loop=1;
    34     scanf("%d%d%d%d%d%d",&year,&month,&day,&hour,&minute,&second);
    35 
    36     do
    37     {
    38   c:
    39         cout<<"自定义的时间(获得任意输入以后继续):
    ";
    40         second++;                                             //时间自增
    41         if(second==59){second=0;minute++;}                    //以下if部分用于判断年月日 小时分钟秒的进率以及判断平年闰年
    42         if(minute==60){minute=0;hour++;}
    43         if(hour==24){hour=0;day++;}
    44         if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day>31)
    45         {day=1;month++;}
    46         if((month==4||month==6||month==9||month==11)&&day>30)
    47         {day=1;month++;}
    48         if((year%4==0&&year%100!=0) || (year%400==0))
    49         {judge=1;}
    50         if(month==2&&judge==1&&day>29)
    51         {day=1;month++;}
    52         if(month==2&&judge==0&&day>28)
    53         {day=1;month++;}
    54         if(month>12)
    55         {month=1;year++;}
    56         cout<<year<<":"<<month<<":"<<day<<" ";
    57         cout<<hour<<":"<<minute<<":"<<second<<endl;
    58         Sleep(1000);                                           //系统暂停1000毫秒=1秒钟,用于实现时间的自动变化
    59         system("cls");                                         //清屏
    60         while(kbhit()){goto b;}                                //有键盘输入则跳出循环,否则一直循环
    61     }while(1);
    62     
    63   b:
    64     if(loop==1)
    65     {
    66         cout<<"现在可以增加或减少时间了,增加为正、减少为负(请输入合法数据):
    ";
    67         cout<<"请输入要增加的时间:年 月 日  小时 分钟 秒(不增加的部分写0)
    ";
    68         int year1,month1,day1,hour1,minute1,second1;
    69         scanf("%d%d%d%d%d%d",&year1,&month1,&day1,&hour1,&minute1,&second1);
    70         year=year+year1;month=month+month1;day=day+day1;hour=hour+hour1;
    71         minute=minute+minute1;second=second+second1;           //实现时间增加或减少
    72         loop=0;
    73         goto c;
    74     }
    75     system("pause");
    76     return 0;
    77 }


    程序写的有点画蛇添足,在起初加入了调用系统时间并输出,和题意没太大关系,是为了体现思路变化的过程23333,写了一坨勿喷^_^

  • 相关阅读:
    jQuery知识总结
    WEB架构师成长之路之2
    利用Travis CI 让你的github项目持续构建(Node.js为例)
    C#实现UDP分包组包
    win7下安装32位GoSublime Package
    爬虫部分技术要点浅析
    如何使用“依赖注入”的
    分布式ACM Online Judge 架构设计
    “容器组件服务”模型
    Maven学习
  • 原文地址:https://www.cnblogs.com/dzzy/p/4615005.html
Copyright © 2020-2023  润新知