导航:首页 > 数据分析 > 时钟类到底哪个是数据成员

时钟类到底哪个是数据成员

发布时间:2022-10-20 22:20:26

A. C++ 编写Clock类, Time类和Date类作为它的数据成员类型

所有功能均实现 供参考

#include<iostream>
usingnamespacestd;
staticconstintds[12]={31,28,31,30,31,30,31,31,30,31,30,31};
classDate
{
public:
Date(intyear=2014,intmonth=3,intday=28):y(year),m(month),d(day)
{
check_year();
}
voidoperator+(intdays)
{
while(days--)
{
d++;
if(d>ds[m-1]+(m==2&&r?1:0))
{
d=1;
m++;
if(m>12)
{
m=1;
y++;
check_year();
}
}
}
}
voidshow(){cout<<y<<'-'<<m<<'-'<<d;}
private:
boolr;
inty,m,d;
voidcheck_year()
{
r=(y%400==0)||(y%4==0&&y%100!=0);
}
};

classTime
{
public:
Time(inthour=0,intminute=0,intsecond=0):h(hour),m(minute),s(second){}
intoperator+(intseconds)
{
inta;
s+=seconds;
a=s/60;
s%=60;
m+=a;
a=m/60;
m%=60;
h+=a;
a=h/24;
h%=24;
returna;
}
voidshow()
{
cout<<h<<':'<<m<<':'<<s;
}
private:
inth,m,s;
};
classClock
{
public:
Clock(intyear=2014,intmonth=3,intday=28,inthour=11,intminute=47,intsecond=50):d(year,month,day),t(hour,minute,second){}
voidadd_one_second()
{
inta=t+1;
d+a;
}
voidshow()
{
d.show();
cout<<'';
t.show();
cout<<endl;
}
private:
Dated;
Timet;
};

intmain()
{
Clockc1,c2(2100,2,28,23,59,59),c3(2004,2,28,23,59,59);
c1.show();
c1.add_one_second();
c1.show();
c2.show();
c2.add_one_second();
c2.show();
c3.show();
c3.add_one_second();
c3.show();
return0;
}

B. 怎样让自己用C++写的时钟类后台运行时钟

时钟类,可以设置三个成员变量hour、minute、second,然后是他们的get、set、toString方法,然后再申明一个操纵时钟的类,将时钟类的对象作为操纵类的成员,然后写get、set、计时方法,在主函数中创建时钟操纵类,然后初始化时钟,调用时钟的计时方法 ,如果要后台运行可以写成多线程的

C. 编程中的类是什么

类是对问题的抽象,如“人”就是一个抽象的概念,就是类,而你的,就是一个具体的实物,称为类的一个实例。每个人都有共同的东西,比如都有年龄、性别、身高,这些称为类的属性,人可以走路,可以工作,称为类的接口,也就是函数。但每个人的年龄,身高都不尽相同。也就是说个体的属性是独立的。当然这只是皮毛而已,有助你理解。最好的书是《Thinking In C++》(中文版叫《C++编程思想》)开始就给你建立面向对象的概念!

D. C++程序题。设计一个时钟类。

Clock类
classClock
{
inthour;
intminute;
intseconde;
Clock()
{
hour=0;
minute=0;
seconde=0;
}
~Clock()
{
cout<<"调用析构函数成功"<<endl;
}
publicvoidsetH(inth)
{
hour=h;
}
publicvoidsetM(intm)
{
minute=m;
}
publicvoidsetS(ints)
{
second=s;
}
publicvoiddisplay()
{
Stringhh="";
Stringmm="";
Stringss="";
if(hour<10)hh="0"+hour;
elsehh=""+hour;
if(minute<10)mm="0"+minute;
elsemm=""+minute;
if(seconde<10)ss="0"+seconde;
elsess=""+second;
cout<<hh<<":"<<mm<<":"<<ss<<endl;
}
}

Main类
#include<iostream>
usingnamespacestd;
intmain()
{
Clockclock=newClock();
clock.setH(5);
clock.display();
}

E. 编写Java应用程序。首先,定义一个时钟类——Clock,

class clock { public: clock(); ~clock(); SetAlarm(int hour,int minute,int second); increamenthour(); increamentminute(); increamentsecond(); run(int hour,int minute,int second); private: int hr; int min; int sec; } void clock::SetAlarm(int hour,int minute,int second) { if(0<=hours&&hours<24) hr=hour; else hr=0; if(0<=hours&&hours<24) min=minute; else min=0; if(0<=hours&&hours<24) sec=second; else sec=0; } void clock::increamenthour() { hr++; if(hr>23) hr=0; } void clock::increamentminute() { min++; if(min>59) { min=0; increamenthour(); } } void clock::increamentsecond() { sec++; if(sec>59) { sec=0; increamtentminute(); } } void clock::run(int hour,int minute,int second) { increamenthour(); increamentminute(); increamentsecond(); if(hr==hour&&min==minute&&sec=second) cout<<"铃响"<<endl; }

F. 用C++设计一个时钟类TIME

用下面的循环就可以了。

for(int hour=0;huor<=23;hour++)
for(int second=0;second<=59;second++)
for(int minute=0;minute<=59;minute++)

cout<<hour<<":"<<second<<":"<<minute<<endl;

用这些你该会自己创建类了吧?

G. c++设计时钟类,数据成员有时,分,秒,函数成员可实现修改时钟时间,显示时钟时间,

#pragma once

#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <iostream>
#include <cctype>
#include <string>

using namespace std;
//enum int{JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

class Date
{
public:
//Constractors
Date( int month = 0, int day = 0, int year = 0);
Date( const Date&);
~Date(){}

class BadDate{};
void GetSystemDate( Date&);
//公有存取器函数
int GetYear() const{ return itsYear;}
int GetMonth() const{ return itsMonth;}
int GetDay() const{ return itsDay;}
int GetDayOfWeek() const{ return itsDayOfWeek;}
void SetYear( int year){ itsYear = year;}
void SetMonth( int month){ itsMonth = month;}
void SetDay( int day){ itsDay = day;}//*/
//void SetDayOfWeek( int dayOfWeek){ itsDayOfWeek = dayOfWeek;}

//检查Date的函数
bool IsRightDate() const;
bool IsLeapYear() const;
bool IsRightYear() const;
bool IsRightMonth() const;
bool IsRightDay() const;

//操作的Date的函数
Date& AddYear( int);
Date& AddMonth( int);
Date& AddDay( int);
Date& MinusYear( int);
Date& MinusMonth( int);
Date& MinusDay( int);//*/
long ToDays() const;
int SetWeek();
void SetDayOfWeek( int dayOfWeek){ itsDayOfWeek = dayOfWeek;}

//重载运算符
bool operator ==(const Date&);
bool operator !=(const Date&);
Date& operator +=(const Date&);
Date& operator -=(const Date&);
Date& operator ++();
Date& operator --();

private:
int itsDay;
int itsMonth;
int itsYear;
int itsDayOfWeek;
};

//成员函数的实现
Date::Date( int month, int day, int year)
: itsMonth( month), itsDay( day), itsYear( year)
{
IsRightDate();
//cout << ToDays() << endl;
//SetWeek();
}

Date::Date(const Date& date)
{
//if( date.IsRightDate() == false) throw BadDate();//*****似乎已经没有必要暸?
itsDay = date.itsDay;
itsMonth = date.itsMonth;
itsYear = date.itsYear;
itsDayOfWeek = date.itsDayOfWeek;
}
void Date::GetSystemDate( Date& systemDate)
{
SYSTEMTIME time;
GetSystemTime(&time);
systemDate.SetMonth( int( time.wMonth));
systemDate.SetDay( time.wDay);
systemDate.SetYear( time.wYear);
systemDate.SetDayOfWeek( time.wDayOfWeek);
}
int Date::SetWeek()
{
long days = ToDays();
int week = days % 7;
SetDayOfWeek(week);
return week;
}
bool Date::IsRightDate() const
{
if( ( IsRightMonth() == true) && ( IsRightDay() == true) && ( IsRightYear() == true) )
return true;
else
{
throw BadDate();
return false;
}
}

bool Date::IsLeapYear() const
{
if ( ( itsYear % 100 != 0 && itsYear % 4 == 0) || ( itsYear % 400 == 0))
return true;
else
return false;
}

inline bool Date::IsRightYear() const
{
if( itsYear != -1)
return true;
else
return false;
}

inline bool Date::IsRightMonth() const
{
if( ( itsMonth >= 0) && ( itsMonth <= 12))
return true;
else
return false;
}

bool Date::IsRightDay() const
{
if( itsMonth == 2 && IsLeapYear() == false)
{
return ( itsDay >= 0 && itsDay <= 28);
}
else if( itsMonth == 2 && IsLeapYear() == true)
{
return ( itsDay >= 0 && itsDay <= 29);
}
else
{
if( itsMonth == 1 || itsMonth == 3 || itsMonth == 5 || itsMonth == 7 ||
itsMonth == 8 || itsMonth == 10 || itsMonth == 12)
{
return( itsDay >= 0 && itsDay <= 31);
}
else
{
return(itsDay >= 0 && itsDay <= 30);
}
}
}

inline Date& Date::AddYear(int year)
{
if( itsYear > 0)
itsYear += year;
else
itsYear = itsYear + year + 1;
return *this;
}

inline Date& Date::AddMonth(int month)
{
for ( int mon = month; mon > 0; mon--)
{
if( itsMonth == 12)
{
AddYear( 1);
itsMonth = 1;
}
else
itsMonth++;
}
return *this;
}

inline Date& Date::AddDay(int day)
{
for(int d = day; d > 0; d--)
{
if( itsMonth == 2 && IsLeapYear() == false)
{
if( itsDay == 28)
{
AddMonth( 1);
itsDay = 1;
}
else
itsDay++;
}
else if( itsMonth == 2 && IsLeapYear() == true)
{
if( itsDay == 29)
{
AddMonth( 1);
itsDay = 1;
}
else
itsDay++;
}
else
{
if( itsMonth == 1 || itsMonth == 3 || itsMonth == 5 || itsMonth == 7 ||
itsMonth == 8 || itsMonth == 10 || itsMonth == 12)
{
if( itsDay == 31)
{
AddMonth( 1);
itsDay = 1;
}
else
itsDay++;
}
else
{
if( itsDay == 30)
{
AddMonth( 1);
itsDay = 1;
}
else
itsDay++;
}
}
}
SetWeek();
return *this;
}

inline Date& Date::MinusYear(int year)
{
if( itsYear > 0)
itsYear -= year;
else
itsYear = itsYear - year - 1;
return *this;
}

inline Date& Date::MinusMonth(int month)
{
for ( int mon = month; mon > 0; mon--)
{
if( itsMonth == 1)
{
MinusYear( 1);
itsMonth = 12;
}
else
itsMonth--;
}
return *this;
}

inline Date& Date::MinusDay(int day)
{
for(int d = day; d > 0; d--)
{
if( itsMonth == 3 && IsLeapYear() == false)
{
if( itsDay == 1)
{
MinusMonth( 1);
itsDay = 28;
}
else
itsDay--;
}
else if( itsMonth == 3 && IsLeapYear() == true)
{
if( itsDay == 1)
{
MinusMonth( 1);
itsDay = 29;
}
else
itsDay--;
}
else
{
if( itsMonth == 2 || itsMonth == 4 || itsMonth == 6 ||
itsMonth == 9 || itsMonth == 11 || itsMonth == 1)
{
if( itsDay == 1)
{
MinusMonth( 1);
itsDay = 31;
}
else
itsDay--;
}
else
{
if( itsDay == 1)
{
MinusMonth( 1);
itsDay = 30;
}
else
itsDay--;
}
}
}
SetWeek();
return *this;
}

inline long Date::ToDays() const
{
long days = 0;
Date tempDate( GetMonth() - 1, GetDay(), GetYear());
for( ; tempDate.GetYear() > 1; tempDate.MinusYear( 1))
{
if( tempDate.IsLeapYear() == true)
{
days += 366;
}
else days += 265;
}
for( ; tempDate.GetMonth() >= 1 && tempDate.GetYear() >= 1; tempDate.MinusMonth( 1))
{
if( tempDate.GetMonth() == 2 && tempDate.IsLeapYear() == true)
days += 29;
else if( tempDate.GetMonth() == 2 && tempDate.IsLeapYear() == false)
days += 28;
else
{
if( tempDate.GetMonth() == 1 || tempDate.GetMonth() == 3 || tempDate.GetMonth() == 5 ||
tempDate.GetMonth() == 7 || tempDate.GetMonth() == 8 || tempDate.GetMonth() == 10 ||
tempDate.GetMonth() == 12)
{
days += 31;
}
else
{
days += 30;
}
}
}
days += tempDate.GetDay();
return days;
}

inline bool Date::operator ==( const Date& date)
{
return( itsYear == date.itsYear && itsMonth == date.itsMonth && itsDay == date.itsDay);
}

inline bool Date::operator !=( const Date& date)
{
return( !( itsYear == date.itsYear && itsMonth == date.itsMonth && itsDay == date.itsDay));
}

inline Date& Date::operator +=( const Date& date)
{
itsYear += date.itsYear;
itsMonth += date.itsMonth;
itsDay += date.itsDay;
SetWeek();
return *this;
}

inline Date& Date::operator -=( const Date& date)
{
itsYear -= date.itsYear;
itsMonth -= date.itsMonth;
itsDay -= date.itsDay;
//SetWeek();
return *this;
}

inline Date& Date::operator ++()
{
return (AddDay( 1));
}

inline Date& Date::operator --()
{
return (MinusDay( 1));
}
//以下不是成员函数
Date operator +( Date& dateOne, int day)
{
dateOne.AddDay(day);
//SetWeek();
return dateOne;
}
Date operator +( Date& dateOne, Date& dateTwo)
{
Date date;
date.SetYear( dateOne.GetYear() + dateTwo.GetYear());
date.SetMonth( dateOne.GetMonth() + dateTwo.GetMonth());
date.SetDay( dateOne.GetDay() + dateTwo.GetDay());
//date.SetWeek();
return date;
}

Date operator -( Date& dateOne, Date& dateTwo)
{
Date date;
date.SetYear( dateOne.GetYear() - dateTwo.GetYear());
date.SetMonth( dateOne.GetMonth() - dateTwo.GetMonth());
date.SetDay( dateOne.GetDay() - dateTwo.GetDay());
//date.SetWeek();
return date;
}

ostream& operator <<( ostream& ostreamOne, Date& date)
{
date.SetWeek();
string monthName;
string weekName;
switch(date.GetMonth())
{
case 1:monthName = "January";break;
case 2:monthName = "Febrary";break;
case 3:monthName = "March";break;
case 4:monthName = "April";break;
case 5:monthName = "May";break;
case 6:monthName = "June";break;
case 7:monthName = "July";break;
case 8:monthName = "August";break;
case 9:monthName = "September";break;
case 10:monthName = "October";break;
case 11:monthName = "Novenber";break;
case 12:monthName = "December";break;
default:cerr<<"Erroe month!";exit(-1);
}
switch(date.GetDayOfWeek())
{
case 1:weekName = "Monday";break;
case 2:weekName = "Tuesday";break;
case 3:weekName = "Wendesday";break;
case 4:weekName = "Thursday";break;
case 5:weekName = "Frisday";break;
case 6:weekName = "Saterday";break;
//case 7:weekName = "Sunday";break;
case 0:weekName = "Sunday";break;
//default:cerr<<"Erroe week!";exit(-1);
}
cout << weekName << " "<< monthName << " "<< int( date.GetDay()) << " " << date.GetYear();
return ostreamOne;
}
istream& operator >>( istream& ostreamOne, Date& date)
{
int month = 0;
int day = 0;
int year = 0;
cin >> month >> day >> year;
date.SetMonth( month);
date.SetDay( day);
date.SetYear( year);
date.IsRightDate();//判断是否坏值,保证有意义的值材会被计算
return ostreamOne;
}

int main()
{
try
{
Date dateOne;
dateOne.GetSystemDate( dateOne);
cout << dateOne << endl;
Date dateTwo;
cout << "请输入: (格式: \"月/日/年\" 的数字形式.)";
cin >> dateTwo;
cout << dateTwo << endl;
//cout << "Input Error!";
if( dateOne == dateTwo)
cout << "dateOne is equal to dateTwo!" << endl;
else
cout << "dateOne isn\'t equal to dateTwo!" << endl;
dateTwo++;
cout << dateTwo << endl;
cout << "你可以为这个日期加上一个天数,请输入你想加上的天数 d = ";
int d;
cin >> d;
dateTwo = dateTwo + d;
//dateTwo.AddDay(365);
cout << "After add d: " << dateTwo << endl;
dateTwo--;
dateTwo--;
cout << dateTwo << endl;
dateOne += dateTwo;
cout << dateOne << endl;
Date dateThree( 10, 1, 1);
cout << "There are " << dateThree.ToDays() << "days in dateThree." << endl;
cout << "There are " << dateTwo.ToDays() << "days in dateTwo." << endl;
}
catch(Date::BadDate)
{
cout << "Bad Date!";
}
catch(...)
{
cout << "Unknow Exceptions!";
}
return 0;
}

H. c++设计一个时钟类Clock。数据成员包括hour(小时)、minute(分钟)、 second(秒)。要求用成员函数实现

留意下错误信息就行了

#include<iostream>
using namespace std;
class Clock
{
public:
Clock(int h=0,int m=0,int s=0)
{
hour=h;minute=m;second=s;
}
void setHour(){cin>>hour;}
void setMinute(){cin>>minute;}
void setSecond() {cin>>second;}
void show()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int getHour() {return hour;} // 返回类型是int
int getMinute() {return minute;}
int getSecond() {return second;}
private:
int hour,minute,second;
};
void main() // 应该是main
{
Clock a;
cout<<a.getHour()<<":"<<a.getMinute()<<":"<<a.getSecond()<<endl; // 应该用getHour、getMinute、getSecond
a.show();
}

I. 定义一个时钟类Clock,包括:私有数据成员,其中包括:

#include <iostream>
using namespace std;
class Clock
{
public:
Clock(int x,int y,int z):hour(x),minute(y),second(z){};//成员初始化列表
int get_seconds(){return 3600 * hour + 60 * minute + second;}
void show_time()
{
cout << "Hour: " << hour << "Minute: " << minute << "Second: " << second << endl;
}
private:
int hour;
int minute;
int second;
}

阅读全文

与时钟类到底哪个是数据成员相关的资料

热点内容
word在试图打开文件时错误 浏览:108
主板无vga插槽怎么连接编程器 浏览:521
录视频文件在哪里删除 浏览:881
word2013如何插入文件 浏览:233
proe教程百度网盘 浏览:197
如何控制远程linux服务器 浏览:740
it教学app有哪些 浏览:34
怎么在ps抠的图变成矢量文件 浏览:405
口袋妖怪银魂安卓v11 浏览:1
网站上芒果tv的账号都是什么 浏览:104
带公式的表格如何刷新数据 浏览:81
数据标注语音和2d哪个好 浏览:145
保存excel文件的方法 浏览:655
手机上看不到电脑上的文件 浏览:626
关于ps的微信公众号 浏览:612
矩阵论教程 浏览:971
字体文件分系统吗 浏览:921
编程一级考试要带什么证件 浏览:923
extjs表格修改前数据 浏览:612
什么是数据库的函数 浏览:722

友情链接