第五次作业

一、问题及代码


  1. #include <iostream>  
  2. using namespace std;  
  3. class Time  
  4. {  
  5. public:  
  6.     void set_time( );  
  7.     void show_time( );  
  8.     void add_a_sec()  
  9.     {  
  10.         sec++;  
  11.     }  
  12.     void add_a_minute()  
  13.     {  
  14.         minute++;  
  15.     }  
  16.     void add_an_hour()  
  17.     {  
  18.         hour++;  
  19.     }  
  20.     void add_seconds(int n);  
  21.     void add_minutes(int n) ;  
  22.     void add_hours(int n) ;  
  23.     void update();  
  24. private:  
  25.     bool is_time(intintint);   //这个成员函数设置为私有的,是合适的,请品味  
  26.     int hour;  
  27.     int minute;  
  28.     int sec;  
  29. };  
  30. void Time::set_time( )  
  31. {  
  32.     char c1,c2;  
  33.     cout<<"请输入时间(格式hh:mm:ss)";  
  34.     while(1)  
  35.     {   cin>>hour>>c1>>minute>>c2>>sec;  
  36.     if(c1!=':'||c2!=':')  
  37.         cout<<"格式不正确,请重新输入"<<endl;  
  38.     else if (!is_time(hour,minute,sec))  
  39.         cout<<"时间非法,请重新输入"<<endl;  
  40.     else  
  41.         break;  
  42.     }  
  43. }  
  44. void Time::show_time( )  
  45. {  
  46.     cout<<hour<<":"<<minute<<":"<<sec<<endl;  
  47. }  
  48. bool Time::is_time(int h,int m, int s)  
  49. {  
  50.     if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)  
  51.         return false;  
  52.     return true;  
  53. }  
  54. void Time::add_seconds(int n)  
  55. {  
  56.     sec=sec+n;  
  57. }  
  58. void Time::add_minutes(int n)  
  59. {  
  60.     minute=minute+n;  
  61. }  
  62. void Time::add_hours(int n)  
  63. {  
  64.     hour=hour+n;  
  65.   
  66. }  
  67. void  Time::update()  
  68. {  
  69.     if(sec==60)  
  70.     {  
  71.         sec=0;  
  72.         minute++;  
  73.     }  
  74.     if(minute==60)  
  75.     {  
  76.         minute=0;  
  77.         hour++;  
  78.     }  
  79.     if(hour==24)  
  80.     {  
  81.         hour=0;  
  82.     }  
  83. }  
  84. int main( )  
  85. {  
  86.     int x;  
  87.     Time t1;//定义对象  
  88.     t1.set_time( );  
  89.     t1.update();  
  90.     t1.show_time( );  
  91.   
  92.     cout<<"时分秒各加1"<<endl;  
  93.     t1.add_a_sec();  
  94.     t1.add_a_minute();  
  95.     t1.add_an_hour();  
  96.     t1.update();  
  97.     t1.show_time();  
  98.   
  99.     t1.set_time( );  
  100.     t1.update();  
  101.     t1.show_time( );  
  102.   
  103.     cout<<"输入时分秒加(n?):";  
  104.     cin>>x;  
  105.     t1.add_seconds(x);  
  106.     t1.add_minutes(x);  
  107.     t1.add_hours(x);  
  108.     t1.update();  
  109.     t1.show_time();  
  110.   
  111.     return 0;  
  112. }  

要求:请在原类基础上,增加下列成员函数,要求前三个在类内定义,后三个在类内声明,类外定义。在main()数中增加适当的调用以展示扩充类定义后的功能。
add_a_sec()  //增加1秒钟
add_a_minute() //增加1分钟
add_an_hour() //增加1小时
add_seconds(int) //增加n秒钟
add_minutes(int) //增加n分钟
add_hours(int) //增加n小时
提示:(1)要考虑增加后超出取值范围的情形;(2)增加n秒后,秒数可能会超过60,调整秒数,并可以调用增加分钟数的成员函数,使时间合法;同理,增加分钟数也有类似问题。

代码

/*
* 文件名称:张引
* 作    者:
* 完成日期:  2017年    5月   1日
* 版 本 号:v1.0
* 对任务及求解方法的描述部分:类和对象
* 输入描述:
* 问题描述:类和对象
* 程序输出:
* 问题分析:
* 算法设计:
*/

#include<iostream>
using namespace std;
class Time
{
public:
	void set_time();
	void show_time();
	void add_a_sec()
	{
		sec++;
	}
	void add_a_minute()
	{
        minute++;
	}
	void add_an_hour()
	{
		hour++;
	}
	void add_seconds(int n);
	void add_minutes(int n);
	void add_hours(int n);
	void update();
private:
	bool is_time(int, int, int);//这个成员函数设置为私有的,是合适的,请品味  
	int hour;
	int minute;
	int sec;
};
void Time::set_time()
{
	char c1, c2;
	cout << "请输入时间(格式hh:mm:ss)";
	while (1)
	{
		cin >> hour >> c1 >> minute >> c2 >> sec;
		if (c1 != ':' || c2 != ':')
			cout << "格式不正确,请重新输入" << endl;
		else if (!is_time(hour, minute, sec))
			cout << "时间非法,请重新输入" << endl;
		else
			break;
	}
}
void Time::show_time()
{
	cout << hour << ":" << minute << ":" << sec << endl;
}
bool Time::is_time(int h, int m, int s)
{
	if (h<0 || h>24 || m<0 || m>60 || s<0 || s>60)
		return false;
	return true;
}
void Time::add_seconds(int n)
{
	sec = sec + n;
}
void Time::add_minutes(int n)
{
	minute = minute + n;
}
void Time::add_hours(int n)
{
	hour = hour + n;

}
void Time::update()
{
	if (sec == 60)
	{
		sec = 0;
		minute++;
	}
	if (minute == 60)
	{
		minute = 0;
		hour++;
	}
	if (hour == 24)
	{
		hour = 0;
	}
}
int main()
{
	int x;
	Time t1;//定义对象  
	t1.set_time();
	t1.update();
	t1.show_time();

	cout << "时分秒各加1" << endl;
	t1.add_a_sec();
	t1.add_a_minute();
	t1.add_an_hour();
	t1.update();
	t1.show_time();

	t1.set_time();
	t1.update();
	t1.show_time();

	cout << "输入时分秒加(n?):";
	cin >> x;
	t1.add_seconds(x);
	t1.add_minutes(x);
	t1.add_hours(x);
	t1.update();
	t1.show_time();

	return 0;
}

二 运行结果

心得体会

总结复习了类和对象的实现方法 加深了对类的理解

四 知识点总结

1 类的声明

2 类内函数的声明和调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值