* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 利用多文件组织项目
* 作 者: 李霄汉
* 完成日期: 2011 年 3 月 12 日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
mytime.h
#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;
};
main.cpp
#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;
}
mytime.cpp
#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;
使用VS2008: