关闭

第四周 任务二

175人阅读 评论(0) 收藏 举报

/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:    多文件运行程序                          * 作    者:   杨森                          * 完成日期:    2012     年   3    月    20    日 * 版 本 号:    V1.0     

* 对任务及求解方法的描述部分 * 输入描述: * 问题描述: * 程序输出: * 程序头部的注释结束

头文件:
#include <iostream>
using namespace std;
class Time 
{
public:
	
	void set_time();
	
	void show_time();
	
	void add_a_sec();
	
	void add_a_minute();
	
	void add_an_hour();

	void add_seconds(int);
	
	void add_minutes(int);

	void add_hours(int);
private:
	
	bool is_time( int, int, int);
	
	int hour;
	
	int minute;
	
	int sec;
};

主函数:
#include <iostream>

#include "mytime.h"

int main()
{
	Time t1;
	
	Time&t2 = t1;
	
	t1.set_time();
	
	t2.show_time();
	
	t1.add_a_sec();
	
	t1.add_a_minute();
	
	t1.add_an_hour();

	t1.add_seconds(30);

	t1.add_minutes(30);

	t1.add_hours(30);

	system("PAUSE");

	return 0;
}

类函数:
#include <iostream>

#include "mytime.h"

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_a_sec()
{
	if ( sec < 59 )
		
		sec++;
	
	else
	{
		minute++;
		
		if( minute == 60)
		{
			hour++;
			
			minute = 0;
			
			if (hour == 24)
			{
				hour = 0;
				
				minute = 0;
				
				sec = 0;
			}
		}
		
		sec = 0;
	}
	
	cout << hour << ":" << minute << ":" << sec << endl;
}

void Time::add_a_minute()
{
	
	if (sec == 0)
	{
		sec = 59;
	}
	
	else if ( minute == 59 )
	{
		
		hour++;
		
		if(hour == 24)
		{
			hour = 0;
		}
		
		minute = 0;
	}
	else 
	{
		minute ++;
		
		sec--;
	}
	
	cout << hour << ":" << minute << ":" << sec <<endl;
}
void Time::add_an_hour()
{
	
	if ( minute == 0 )
	{
		minute = 59;
	}
	else if (hour == 24 )
	{
		hour = 0;
	}
	else
	{
		minute--;
		
		hour++;
	}	
	
	cout << hour << ":" << minute << ":" << sec << endl;
}
void Time::add_seconds(int n)
{

	sec = sec + n;

	if(sec > 60)
	{
		minute = minute + sec / 60;

		sec = sec % 60 ;

		if (minute > 60 )
		{
			hour = hour + minute / 60;

			minute = minute % 60;

			if ( hour > 24)

				hour = hour % 24;
		}
	}

	cout << hour << ":" << minute << ":" << sec << endl;
}
void Time::add_minutes(int n)
{
	minute = minute + n;

	if (minute > 60)
	{
		hour = hour + minute / 60;

		minute = minute % 60;

		if ( hour > 24 )

			hour = hour % 24;
	}
	cout << hour << ":" << minute << ":" << sec << endl;
}
void Time::add_hours (int n)
{
	hour = hour + n;

	if (hour > 24)
	{
		hour = hour % 24;
	}
	cout << hour << ":" << minute << ":" << sec << endl;
}




 


   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23160次
    • 积分:679
    • 等级:
    • 排名:千里之外
    • 原创:44篇
    • 转载:0篇
    • 译文:0篇
    • 评论:8条
    文章分类
    最新评论