您的位置:首页 > 其它

课程设计:模拟时钟

2015-07-02 01:35 288 查看

电子时钟

设计并实现如下功能:

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

//类声明
Date::Date(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond)
{
setTime(iYear, iMonth, iDay, iHour, iMinute, iSecond);
}

int Date::setTime(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond)
{
int iTotalDays = (iYear / 4 - iYear / 100 + iYear / 400) + 367 * iMonth / 12 + iDay + iYear * 365 - 719499;
m_lTotalSeconds = ((iTotalDays * 24 + iHour) * 60 + iMinute) * 60 + iSecond;
return m_lTotalSeconds;
}

int Date::isLeap(int iYear)
{
return (((iYear%4==0)&&(iYear%100!=0))||(iYear%400==0));
}

int Date::seconds()
{
return m_lTotalSeconds;
}

void Date::getDate(int &iYear, int &iMonth, int &iDay, int &iHour, int &iMinute, int &iSecond)
{
const int monthLengths[2][13] = {
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
};
const int yearLengths[2] = {365, 366};
iDay = m_lTotalSeconds / 86400;
iSecond = m_lTotalSeconds % 86400;

iYear = iDay / 366;
iDay -= iYear*365 + (iYear+2-1)/4 - (iYear+100-30-1)/100 + (iYear+400-30-1)/400;

while (1)
{
if (iDay > 0)
{
break;
}
iDay += yearLengths[isLeap(iYear - 1)];
--iYear;
}
for (iMonth = 1; iMonth <= 12; ++iMonth)
{
if (iDay <= monthLengths[isLeap(iYear)][iMonth])
{
iDay -= monthLengths[isLeap(iYear)][iMonth - 1];
break;
}
}

iHour = m_lTotalSeconds / 3600;        //3600s one hour
iMinute = (m_lTotalSeconds % 3600) / 60;        //60s one minute
iSecond = m_lTotalSeconds % 60;        //ms
}


主函数:Date.cpp

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include"Date.h"
using namespace std;
int main()
{
cout<<"电子时钟"<<endl;                                  //输出题意
cout<<"设计并实现如下功能:"<<endl;
cout<<"1)设置日期"<<endl;
cout<<"2)用“年/月/日 小时:分钟:秒”格式输出日期"<<endl;
cout<<"3)在当前时钟的基础上可以增加、较少时间,并显示"<<endl;
cout<<"4)设置的时钟能随着时间自动增加"<<endl;
cout<<"(比如设置当前时间为2015/5/25 10:10:10,10秒后再显示为2015/5/25 10:10:20)\n"<<endl;
system("pause");
SYSTEMTIME sys;                                          //定义sys用于调用系统时间
do
{
GetLocalTime(&sys);                                  //获取系统当前时间
cout<<"系统当前时间(获得任意输入以后继续):\n";
cout<<sys.wYear<<":"<<sys.wMonth<<":"<<sys.wDay<<" ";//sys.wYear系统年,sys.wMonth系统月,以此类推
cout<<sys.wHour<<":"<<sys.wMinute<<":"<<sys.wSecond<<endl;
Sleep(1);                                            //系统暂停1毫秒
system("cls");                                       //清屏
while(kbhit()){goto a;}                              //有键盘输入则跳出循环,否则一直循环
}while(1);

a:
cout<<"现在可以更改时间了\n";
cout<<"请输入时间:年 月 日  小时 分钟 秒"<<endl;

int year,month,day,hour,minute,second,judge=0,loop=1;
scanf("%d%d%d%d%d%d",&year,&month,&day,&hour,&minute,&second);

do
{
c:
cout<<"自定义的时间(获得任意输入以后继续):\n";
second++;                                             //时间自增
if(second==59){second=0;minute++;}                    //以下if部分用于判断年月日 小时分钟秒的进率以及判断平年闰年
if(minute==60){minute=0;hour++;}
if(hour==24){hour=0;day++;}
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day>31)
{day=1;month++;}
if((month==4||month==6||month==9||month==11)&&day>30)
{day=1;month++;}
if((year%4==0&&year%100!=0) || (year%400==0))
{judge=1;}
if(month==2&&judge==1&&day>29)
{day=1;month++;}
if(month==2&&judge==0&&day>28)
{day=1;month++;}
if(month>12)
{month=1;year++;}
cout<<year<<":"<<month<<":"<<day<<" ";
cout<<hour<<":"<<minute<<":"<<second<<endl;
Sleep(1000);                                           //系统暂停1000毫秒=1秒钟,用于实现时间的自动变化
system("cls");                                         //清屏
while(kbhit()){goto b;}                                //有键盘输入则跳出循环,否则一直循环
}while(1);

b:
if(loop==1)
{
cout<<"现在可以增加或减少时间了,增加为正、减少为负(请输入合法数据):\n";
cout<<"请输入要增加的时间:年 月 日  小时 分钟 秒(不增加的部分写0)\n";
int year1,month1,day1,hour1,minute1,second1;
scanf("%d%d%d%d%d%d",&year1,&month1,&day1,&hour1,&minute1,&second1);
year=year+year1;month=month+month1;day=day+day1;hour=hour+hour1;
minute=minute+minute1;second=second+second1;           //实现时间增加或减少
loop=0;
goto c;
}
system("pause");
return 0;
}


程序写的有点画蛇添足,在起初加入了调用系统时间并输出,和题意没太大关系,是为了体现思路变化的过程23333,写了一坨勿喷^_^
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: