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;
}