多文件組織項目

源程序:

頭文件: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;
}

實驗結果:




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