时间类-类的定义
类作为"零件"的载体,有内部属性(private),有对外接口(public), 内部属性的数据成员或函数成员,仅仅供给class内部函数成员使用,不对外开放,public规定的对外开放的接口。
1.任务:设置Cmytime类。具有两个成员函数:
int Set(int h,int m,int s)
Show()
输入:23 25 38
输出: 23:25:38
#include <iostream>
using namespace std;
class Cmytime
{
private:
int h;
int m;
int s;
public:
void Set(int hh,int mm,int ss){
h=hh;m=mm;s=ss;
}
void Show(){
cout<<h<<":"<<m<<":"<<s;
}
};
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(h,m,s);
t1.Show();
return 0;
}
2.任务:限制非法时间
对于Set函数的要求,
1、对于非法赋值不给予执行,三个参数合法范围是:0<=h<=23, 0<=m,s<=59。 如何参数非法,本次Set函数不改变原有值。
2、赋值成功,返回1,否则返回0。
Show()
输入 23 25 38
输出: 23:25:38
#include <iostream>
using namespace std;
class Cmytime {
private:
int h;
int m;
int s;
public:
int Set(int hh,int mm,int ss){
if(hh>=0 && hh<=23 && mm>=0 && mm<=59 && ss>=0 && ss<=59){
h=hh;
m=mm;
s=ss;
return 1;
}
else{
return 0;
}
}
void Show(){
cout<<h<<":"<<m<<":"<<s;
}
};
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(1,2,3);
t1.Set(h,m,s);
t1.Show();
return 0;
}
3.任务:实现在原时间的基础上加1秒的时间值。
void AddOneSecond();
输入 : 23 25 38
输出 :
23:25:38
0
23:25:38
23:25:39
#include <iostream>
using namespace std;
class Cmytime {
private:
int h;
int m;
int s;
public:
int Set(int hh,int mm,int ss){
if(hh>=0 && hh<=23 && mm>=0 && mm<=59 && ss>=0 && ss<=59){
h=hh;
m=mm;
s=ss;
return 1;
}
else{
return 0;
}
}
void Show(){
cout<<h<<":"<<m<<":"<<s;
}
void AddOneSecond() {
if (s != 59) {
s++;
}
else {
s = 0;
if (m != 59) {
m++;
}
else {
m = 0;
if (h != 23) {
h++;
}
else {
h = 0;
}
}
}
}
};
int main(void)
{
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(h,m,s);
t1.Show();
cout<<endl<<t1.Set(24,0,0)<<"\n";
t1.Show();
t1.AddOneSecond();
cout<<endl;
t1.Show();
return 0;
}
4.任务:实现在原时间的基础上加n秒的时间值。返回值要求,如果加的n秒数,返回时间跨越了0:0:0的次数,换句话说反映了日期上进几天。
int AddNSeconds(int n);
输入 23 25 38
输出:
23:25:38
0
23:25:38
23:25:39
2
#include <iostream>
using namespace std;
class Cmytime {
private:
int h;
int m;
int s;
public:
int Set(int hh,int mm,int ss){
if(hh>=0 && hh<=23 && mm>=0 && mm<=59 && ss>=0 && ss<=59){
h=hh;
m=mm;
s=ss;
return 1;
}
else{
return 0;
}
}
void Show(){
cout<<h<<":"<<m<<":"<<s;
}
void AddOneSecond() {
if (s != 59) {
s++;
}
else {
s = 0;
if (m != 59) {
m++;
}
else {
m = 0;
if (h != 23) {
h++;
}
else {
h = 0;
}
}
}
}
int AddNSeconds(int n) {
int day;
int adh, adm, ads, temp;
adh = n / 3600;
n %= 3600;
adm = n / 60;
ads = n % 60;
s = s + ads;
m = m + adm + (s / 60);
s %= 60;
h = h + adh + (m / 60);
m %= 60;
day = h / 24;
h % 24;
return day;
}
};
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(h,m,s);
t1.Show();
cout<<endl<<t1.Set(24,0,0)<<"\n";
t1.Show();
t1.AddNSeconds(1);
cout<<endl;
t1.Show();
cout<<endl<<t1.AddNSeconds(3600*25);
return 0;
}
5.任务:构造函数
Set(int h,int m,int s)
Show()
输入 23 25 38
输出:
3:2:1
23:25:38
#include <iostream>
using namespace std;
class Cmytime {
private:
int h;
int m;
int s;
public:
Cmytime (int hh, int mm, int ss) {
h = hh;
m = mm;
s = ss;
}
int Set(int hh, int mm, int ss) {
h = hh;
m = mm;
s = ss;
return 0;
}
void Show() {
cout << h << ":" << m << ":" << s;
}
};
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1(3,2,1);
t1.Show();
cout<<"\n";
t1.Set(h,m,s);
t1.Show();
return 0;
}
6.任务:
设置Cmytime类。
具有三个成员函数
构造函数
Set(int h,int m,int s)
Show()
输入 23 25 38
输出:
3:2:1
23:25:38
0:0:0
5:0:0
#include <iostream>
using namespace std;
class Cmytime {
private:
int h;
int m;
int s;
public:
Cmytime (int hh, int mm, int ss) {
h = hh;
m = mm;
s = ss;
}
Cmytime () {
h = 0;
m = 0;
s = 0;
}
Cmytime (int hhh) {
h = hhh;
m = 0;
s = 0;
}
int Set(int hh, int mm, int ss) {
h = hh;
m = mm;
s = ss;
return 0;
}
void Show() {
cout << h << ":" << m << ":" << s;
}
};
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1(3,2,1),t2,t3(5);
t1.Show();
cout<<"\n";
t1.Set(h,m,s);
t1.Show();
cout<<"\n";
t2.Show();
cout<<"\n";
t3.Show();
return 0;
}
7.任务:
设置Cmytime类。
具有三个成员函数
构造函数
Set(int h,int m,int s)
Show()
输入 23 25 38
输出:
3:2:1
23:25:38
0:0:0
5:0:0
#include <iostream>
using namespace std;
class Cmytime {
private:
int h;
int m;
int s;
public:
Cmytime (int hh = 0, int mm = 0, int ss = 0) {
h = hh;
m = mm;
s = ss;
}
int Set(int hh, int mm, int ss) {
h = hh;
m = mm;
s = ss;
return 0;
}
void Show() {
cout << h << ":" << m << ":" << s;
}
};
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1(3,2,1),t2,t3(5);
t1.Show();
cout<<"\n";
t1.Set(h,m,s);
t1.Show();
cout<<"\n";
t2.Show();
cout<<"\n";
t3.Show();
return 0;
}
8.输入1~7任意一个整数,将其转换为星期输出
要求:使用switch语句,使用cout
输入输出示例:
输入1:1
输出1:Monday
输入2:7
输出2:Sunday
#include <iostream>
using namespace std;
int main()
{
int d;
cin >> d;
switch (d)
{
case 1: cout << "Monday";break;
case 2: cout << "Tuesday";break;
case 3: cout << "Wednesday";break;
case 4: cout << "Thursday";break;
case 5: cout << "Friday";break;
case 6: cout << "Saturday";break;
case 7: cout << "Sunday";
}
return 0;
}