C++ || 时间类-类的定义,类作为“零件”的载体,有内部属性,有对外接口

文章描述了一个名为Cmytime的C++类,该类用于处理时间,包括设置时间(Set函数),显示时间(Show函数),检查并限制非法时间,增加秒数(AddOneSecond和AddNSeconds函数)以及不同的构造函数。同时,还展示了一个简单的switch语句例子,用于将数字转换为对应的星期名称。
摘要由CSDN通过智能技术生成

时间类-类的定义

        类作为"零件"的载体,有内部属性(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;
} 
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小嘤嘤怪学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值