用C++程序编写一套程序时钟设计
设计编写一个电子时钟类ElectronicClock,包括年月日时分秒等属性,电子时钟类由时间类和日期类组合而成,实现并测试这个类。其中要完成的操作有:1,构造函数2,析...
设计编写一个电子时钟类ElectronicClock,包括年 月 日 时 分 秒等属性,电子时钟类由时间类和日期类组合而成,实现并测试这个类。其中要完成的操作有:
1,构造函数
2,析构函数
3,对时间与日期进行校验的函数,当不正确时进行纠正与改错
4,对年,月,日,时,分,秒等属性性进行增加n个单位,注意不超出范围,进行进位
想要一套能在Microsoft Visual C++中能运行的完整程序,各位大神拜托了 展开
1,构造函数
2,析构函数
3,对时间与日期进行校验的函数,当不正确时进行纠正与改错
4,对年,月,日,时,分,秒等属性性进行增加n个单位,注意不超出范围,进行进位
想要一套能在Microsoft Visual C++中能运行的完整程序,各位大神拜托了 展开
2个回答
展开全部
#include <iostream>
#include <time.h>
using namespace std;
void delay(double seconds)
{
clock_t t = clock();
while((double)(clock()-t)/(double)CLOCKS_PER_SEC<seconds);
}
class Clock
{
private:
int year;
int month;
int day;
int hour;
int minute;
int second;
public:
Clock();
Clock(int y,int m,int d,int h,int mi,int s);
~Clock();
void secondadd();
void changetime();
void showtime();
void test();
};
Clock::Clock()
{
cout<<"please input current year:"<<endl;
cin>>year;
if((year/4==0&&year/100!=0)||(year/400==0))
cout<<"闰年"<<endl;
else
cout<<"不是闰年"<<endl;
month:
cout<<"please input current month:"<<endl;
cin>>month;
if(month<1||month>12)
{
month = 0;
cout<<"month is wrong!input again!"<<endl;
goto month;
}
day:
cout<<"please input current day:"<<endl;
cin>>day;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day<1||day>31)
{
day = 0;
cout<<"day is wrong!input again!"<<endl;
goto day;
}
}
if(month==4||month==6||month==9||month==11)
{
if(day<1||day>30)
{
day = 0;
cout<<"day is wrong!input again!"<<endl;
goto day;
}
}
if(month == 2)
{
if((year/4==0&&year/100!=0)||(year/400==0))
{
if(day<1||day>29)
{
cout<<"闰年"<<endl;
day = 0;
cout<<"day is wrong!input again!"<<endl;
goto day;
}
}
else
{
if(day<1||day>28)
{
cout<<"不是闰年"<<endl;
day = 0;
cout<<"day is wrong!input again!"<<endl;
goto day;
}
}
}
hour:
cout<<"please input current hour:"<<endl;
cin>>hour;
if(hour<0||hour>24)
{
hour = 0;
cout<<"hour is wrong!input again!"<<endl;
goto hour;
}
minute:
cout<<"please input current minute:"<<endl;
cin>>minute;
if(minute<0||minute>60)
{
minute = 0;
cout<<"minute is wrong!input again!"<<endl;
goto minute;
}
second:
cout<<"please input current second:"<<endl;
cin>>second;
if(second<0||second>60)
{
minute = 0;
cout<<"second is wrong!input again!"<<endl;
goto second;
}
}
Clock::Clock(int y,int m,int d,int h,int mi,int s)
{
year = y;
month = m;
day = d;
hour = h;
minute = mi;
second = s;
}
Clock::~Clock()
{
}
void Clock::changetime()
{
int y;int m;int d;int h;int mi;int s;
cout<<"input year:"<<endl;
cin>>y;
cout<<"input month:"<<endl;
cin>>m;
cout<<"input day:"<<endl;
cin>>d;
cout<<"input hour:"<<endl;
cin>>h;
cout<<"input minute:"<<endl;
cin>>mi;
cout<<"input second:"<<endl;
cin>>s;
year = y;
month = m;
day = d;
hour = h;
minute = mi;
second = s;
}
void Clock::secondadd()
{
second = second + 1;
test();
}
void Clock::test()
{
if(second>=60)
{
second = second-60;
minute++;
}
if(minute>=60)
{
minute = minute-60;
hour++;
}
if(hour>=24)
{
hour = hour-24;
day++;
}
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day>=32)
{
day=day-31;
month++;
}
}
if(month==4||month==6||month==9||month==11)
{
if(day>=31)
{
day=day-30;
month++;
}
}
if((year/4==0&&year/100!=0)||(year/400==0))
{
if(month == 2)
{
if(day>=30)
{
day = day-29;
month++;
}
}
}
else
{
if(month == 2)
{
if(day>=29)
{
day = day-28;
month++;
}
}
}
if(month>=13)
{
month = month -12;
year++;
}
}
void Clock::showtime()
{
cout<<"date is "<<year<<"."<<month<<"."<<day<<endl;
cout<<"time is "<<hour<<":"<<minute<<":"<<second<<endl;
}
int main()
{
Clock c;
while(1)
{
delay(1);
c.secondadd();
c.showtime();
}
}
在判断年份上还有一些小BUG,(*^__^*),请自行解决
#include <time.h>
using namespace std;
void delay(double seconds)
{
clock_t t = clock();
while((double)(clock()-t)/(double)CLOCKS_PER_SEC<seconds);
}
class Clock
{
private:
int year;
int month;
int day;
int hour;
int minute;
int second;
public:
Clock();
Clock(int y,int m,int d,int h,int mi,int s);
~Clock();
void secondadd();
void changetime();
void showtime();
void test();
};
Clock::Clock()
{
cout<<"please input current year:"<<endl;
cin>>year;
if((year/4==0&&year/100!=0)||(year/400==0))
cout<<"闰年"<<endl;
else
cout<<"不是闰年"<<endl;
month:
cout<<"please input current month:"<<endl;
cin>>month;
if(month<1||month>12)
{
month = 0;
cout<<"month is wrong!input again!"<<endl;
goto month;
}
day:
cout<<"please input current day:"<<endl;
cin>>day;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day<1||day>31)
{
day = 0;
cout<<"day is wrong!input again!"<<endl;
goto day;
}
}
if(month==4||month==6||month==9||month==11)
{
if(day<1||day>30)
{
day = 0;
cout<<"day is wrong!input again!"<<endl;
goto day;
}
}
if(month == 2)
{
if((year/4==0&&year/100!=0)||(year/400==0))
{
if(day<1||day>29)
{
cout<<"闰年"<<endl;
day = 0;
cout<<"day is wrong!input again!"<<endl;
goto day;
}
}
else
{
if(day<1||day>28)
{
cout<<"不是闰年"<<endl;
day = 0;
cout<<"day is wrong!input again!"<<endl;
goto day;
}
}
}
hour:
cout<<"please input current hour:"<<endl;
cin>>hour;
if(hour<0||hour>24)
{
hour = 0;
cout<<"hour is wrong!input again!"<<endl;
goto hour;
}
minute:
cout<<"please input current minute:"<<endl;
cin>>minute;
if(minute<0||minute>60)
{
minute = 0;
cout<<"minute is wrong!input again!"<<endl;
goto minute;
}
second:
cout<<"please input current second:"<<endl;
cin>>second;
if(second<0||second>60)
{
minute = 0;
cout<<"second is wrong!input again!"<<endl;
goto second;
}
}
Clock::Clock(int y,int m,int d,int h,int mi,int s)
{
year = y;
month = m;
day = d;
hour = h;
minute = mi;
second = s;
}
Clock::~Clock()
{
}
void Clock::changetime()
{
int y;int m;int d;int h;int mi;int s;
cout<<"input year:"<<endl;
cin>>y;
cout<<"input month:"<<endl;
cin>>m;
cout<<"input day:"<<endl;
cin>>d;
cout<<"input hour:"<<endl;
cin>>h;
cout<<"input minute:"<<endl;
cin>>mi;
cout<<"input second:"<<endl;
cin>>s;
year = y;
month = m;
day = d;
hour = h;
minute = mi;
second = s;
}
void Clock::secondadd()
{
second = second + 1;
test();
}
void Clock::test()
{
if(second>=60)
{
second = second-60;
minute++;
}
if(minute>=60)
{
minute = minute-60;
hour++;
}
if(hour>=24)
{
hour = hour-24;
day++;
}
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day>=32)
{
day=day-31;
month++;
}
}
if(month==4||month==6||month==9||month==11)
{
if(day>=31)
{
day=day-30;
month++;
}
}
if((year/4==0&&year/100!=0)||(year/400==0))
{
if(month == 2)
{
if(day>=30)
{
day = day-29;
month++;
}
}
}
else
{
if(month == 2)
{
if(day>=29)
{
day = day-28;
month++;
}
}
}
if(month>=13)
{
month = month -12;
year++;
}
}
void Clock::showtime()
{
cout<<"date is "<<year<<"."<<month<<"."<<day<<endl;
cout<<"time is "<<hour<<":"<<minute<<":"<<second<<endl;
}
int main()
{
Clock c;
while(1)
{
delay(1);
c.secondadd();
c.showtime();
}
}
在判断年份上还有一些小BUG,(*^__^*),请自行解决
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询