多文件组织项目

源程序:

头文件:mytime.h

class Time
{
public:
	void set_time( );   
	void show_time( ); 
	inline void add_seconds(int n)
	{
		int s;
    	s = sec + n;
    	if(s > 60)
		{
			sec = s % 60;
     		add_minutes(s/60);
		}
		else
		{
			sec = s;
		}
		return;
	}
	inline void add_minutes(int n)
	{
		int min;
		min = minute + n;
		if(min > 60)
		{
			minute = min % 60;
			add_hours(min / 60);
		}
		else
		{
			minute = min;
		}
		return;
	}
	inline void add_hours(int n)
	{
		int h;
		h = hour + n;
		if(h > 23)
		{
			hour = h % 24;
		}
		else
		{
			hour = h;
		}
		return;
	}

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


主文件:main.cpp

/* (程序头部注释开始) 
* 程序的版权和版本声明部分 
* Copyright (c) 2011, 烟台大学计算机学院学生  
* All rights reserved. 
* 文件名称:多文件项目组织                             
* 作    者:刘杨                               
* 完成日期:2012 年 03 月 17 日 
* 版 本 号:           
 
* 对任务及求解方法的描述部分 
* 输入描述:  
* 问题描述:  
* 程序输出:  
* 程序头部的注释结束 
*/  
#include <iostream>
#include"mytime.h"
using namespace std;
int main( )
{
	Time t1;  
	Time &t2=t1;
	t1.set_time( );   
	t2.show_time( );

	cout << "增加100秒后,时间是:";
	t1.add_seconds(100);
	t2.show_time();

	cout << "增加5分钟后,时间是:";
	t1.add_minutes(5);
	t2.show_time();

	cout << "增加7小时后,时间是:";
	t1.add_hours(7);
	t2.show_time();

	return 0;
}




类定义文件:mytime.cpp

#include<iostream>
#include"mytime.h"
using namespace std;
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;
}

实验结果:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章