您的位置:首页 > 编程语言 > C语言/C++

C++程序设计实验报告(三十六)---第四周任务二

2012-03-12 17:32 357 查看
 * 程序的版权和版本声明部分

* Copyright (c) 2012, 烟台大学计算机学院学生 

* All rights reserved.

* 文件名称:           多文件组织项目              

* 作    者:                 刘镇             

* 完成日期:   2012         年    3   月    12    日

* 版 本 号:         1.033

* 对任务及求解方法的描述部分

* 输入描述: 时间(时、分、秒)

* 问题描述: 。。。。

* 程序输出: 。。。。

* 程序头部的注释结束

头文件:

 
#include<iostream>

using namespace std;

class Time
{
public:
void set_time();
void show_time();
inline void add_a_sec()                //增加1秒钟
{
++ sec;

if(sec >= 60)
{
sec = 0;

add_a_minute();
}

}
inline void add_a_minute()                 //增加1分钟
{
++ minute;

if(minute >= 60)
{
minute = 0;

add_an_hour();
}

}
inline void add_an_hour()                    //增加1小时
{
++ hour;

if(hour > 24)
{
hour = 1;
}

}
void add_seconds(int);                    //增加n秒钟
void add_minutes(int);                      //增加n分钟
void add_hours(int);                       //增加n小时

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

};


源文件:main.cpp

 
#include<iostream>

#include"Time.h"

using namespace std;

int main()
{
Time t1;

Time &t2 = t1;

t1.set_time();

t2.show_time();

t1.add_a_sec();

cout << "增加1秒钟:" << endl;

t2.show_time();

t1.add_a_minute();

cout << "增加1分钟:" << endl;

t2.show_time();

t1.add_an_hour();

cout << "增加1小时:" << endl;

t2.show_time();

t1.add_seconds(45);

cout << "增加45秒钟:" << endl;

t2.show_time();

t1.add_minutes(50);

cout << "增加50分钟:" << endl;

t2.show_time();

t1.add_hours(2);

cout << "增加2小时:" << endl;

t2.show_time();

system("PAUSE");

return 0;
}


Time.cpp

 
#include<iostream>

#include"Time.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;
}

void Time::add_seconds(int n)
{
sec = sec + n;

if(sec >= 60)
{
add_minutes(sec/60);

sec = sec % 60;
}

}

void Time::add_minutes(int n)
{
minute = minute + n;

if(minute >= 60)
{
add_hours(minute / 60);

minute =  minute % 60;
}
}

void Time::add_hours(int n)
{
hour = hour + n;

if(hour >= 24)
{
hour = hour % 24;
}
}


运行结果:

 



 

经验积累:

 

1、活学活用的将头文件,源文件分离,在用vs2008时需要构建标准的类,或自己在头文件与源文件中定义,切勿少了对于头文件的包含预处理。

2、vs中方便的使用单步调试,在调试栏中运用也可,使用快捷键F11\F10也可以,在vc++中的标准化格式功能ALT + F8也可以用。

 

上级感言:

vs在我的这几天使用中能体会到它更适合做大工程,兼容问题解决也很好。比如讲文件最小化,实现文件的分割包装,明显有实际。可缺点是比起vc++,它的安装和运行较慢,并且没有自动暂停功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 c++ system c class