DIY时钟类--广州百田笔试之一

时间:2024-01-21 10:21:09

2014.05.30 武汉华科大活

题目:(DIY时钟类--广州百田笔试之一原题不记得,大概回忆)用户输入一个时间,输出下一个时间

这个小题看似不难,实际处理起来对date的处理稍微繁琐,每月有30,31,28,29(闰年的判断)天四种可能

判定闰年:当不能被100整除且能被4整除时, 或能被400整除,为闰年

关键是如何让写出的代码规范且反应思路清晰,目前的代码见后文

当时居然没有意识到要用类,真是太菜了,没关系还不晚

虽然不难但比较繁琐,想要在笔试时用20~30分钟,在一张单面A4纸上,完整清晰的实现,

就需要思路很清晰不能大片修改,也没办法像上机一样调试,还是有难度的

自己写了时钟类,和++操作符重载,代码+测试结果如下:

DIY时钟类--广州百田笔试之一
//DataClock.h
#pragma once //2014.05.30百田笔试过这个题
#include <iostream>
using namespace std; class DataClock{
public:
//Clock(int hour=0, int min=0, int sec=0); //默认值???
DataClock(int cYear, int cMonth, int cDate, int cHour, int cMin, int cSec);
void showTime();
bool ifLeapYear(int y);
int howManyDays(int m, int y);
//bool ifThirtyDays(int m);
//bool ifThirtyOneDays(int m); void operator ++();//前置没有形参(可看做默认的规矩??)
//void operator ++(int t);
private:
int year; int month; int date;
int hour; int min; int sec;
};
//DataClock.cpp实现
#include "DataClock.h" DataClock::DataClock(int cYear, int cMonth, int cDate, int cHour, int cMin, int cSec){
int thisMonDays = howManyDays(cMonth, cYear); //这里带哦用方法对么??
if(<=cYear && <cMonth && cMonth<= && <cDate && cDate<=thisMonDays
&& <=cHour && cHour< && <=cMin && < && <=cSec&&cSec<) {
year=cYear; month=cMonth; date=cDate;
hour = cHour; min = cMin; sec = cSec;
}else
cout << "time error!" <<endl;
} void DataClock::showTime(){
cout <<year<<"/"<<month<<"/"<<date<<" " <<hour <<":" <<min << " " <<sec<<"s" <<endl;
} int DataClock::howManyDays(int m, int y){
int days;
if (m==||m==||m==||m==||m==||m==||m==)
days=;
else if(m!=)
days=;
else if(ifLeapYear(y))
days=;
else
days=; return days;
} bool DataClock::ifLeapYear(int year){
if((year%!= && year%==) || year%==)//如果尾数不是00,且能被4整除
return true;
else
return false;
} void DataClock::operator ++(){
++sec; if (sec>=){
sec = sec-;
min++;
if(min>=){
min = min-;
hour++;
if(hour>=){
hour = hour-; //这里相对 hour-24的优势是???
date++;
int thisMonDays = howManyDays(month, year);
if(date> thisMonDays){
date = date-thisMonDays;
month++;
if(month>){
month=month-;
year++;
}
}
}
}
}
}
//年月日 时分秒 测试
void DataClockTest(){
int y,m,d,h,min,s;
cout << "请输入年月日时分秒共六个数:";
cin >>y>>m>>d>> h >> min >>s;
DataClock myTime(y, m, d, h, min, s);
cout<<"输入时间是:";
myTime.showTime();
++myTime;
cout << " 下一秒是:" ;
myTime.showTime();
}

DIY时钟类--广州百田笔试之一

DIY时钟类--广州百田笔试之一

DIY时钟类--广州百田笔试之一

DIY时钟类--广州百田笔试之一

DIY时钟类--广州百田笔试之一

DIY时钟类--广州百田笔试之一