chapter11test4
2015-06-07 12:05
375 查看
time.h
#ifndef TIME_H_
#define TIME_H_
#include<iostream>
class Time
{
private:
int hour;
int minutes;
public:
Time(int h = 0, int m = 0);
void addh(int h);
void addm(int m);
void reset(int h = 0, int m = 0);
friend Time operator+(const Time &t1, const Time &t2);
friend Time operator-(const Time &t1, const Time &t2);
friend Time operator*(const Time &t1, double n);
friend std::ostream &operator<<(std::ostream &os, const Time &t);
};
#endif
time.cpp
#include<iostream>
#include"time.h"
Time::Time(int h, int m)
{
hour = h; minutes = m;
}
void Time::addh(int h)
{
hour += h;
}
void Time::addm(int m)
{
minutes = (minutes + m) % 60;
hour = hour+(minutes + m) / 60;
}
void Time::reset(int h, int m)
{
hour = h; minutes = m;
}
Time operator+(const Time &t1, const Time &t2)
{
Time t;
t.minutes = (t1.minutes + t2.minutes) % 60;
t.hour = t1.hour + t2.hour + (t1.minutes + t2.minutes) / 60;
return t;
}
Time operator-(const Time &t1, const Time &t2)
{
Time t;
long int tot1 = 60 * t1.hour + t1.minutes;
long int tot2 = 60 * t2.hour + t2.minutes;
if (tot1 < tot2)
std::cout << "First time is too little.\n";
else
{
tot1 = tot1 - tot2;
t.hour = tot1 / 60;
t.minutes = tot1 % 60;
}
return t; //if tot1<tot2,return t(0,0);
}
Time operator*(const Time &t1, double n)
{
Time t; long tot = t1.hour * 60 + t1.minutes;
tot = tot*n;
t.minutes = int(tot) % 60;
t.hour = int(tot) / 60;
return t;
}
std::ostream &operator<<(std::ostream &os, const Time &t)
{
os << "Hour= " << t.hour << " ;Minutes= " << t.minutes << std::endl;
return os;
}
user.cpp
#include<iostream>
#include"time.h"
int main()
{
using namespace std;
Time one(3, 35);
Time two(1, 21);
cout << "Show temp#1=plus :" << one+two<< endl;
cout << "Show temp#2=minus :" << one-two << endl;
cout << "Show temp#3=multi :" << one*0.5 << endl;
two.reset(4, 3);
cout << "Show one and two :" << one << " ; "<<two << endl;
cout << "Show temp#4=minus :" << one - two << endl;
one.addh(3);
cout << "Show temp#5=minus :" << one - two << endl;
return 0;
}
#ifndef TIME_H_
#define TIME_H_
#include<iostream>
class Time
{
private:
int hour;
int minutes;
public:
Time(int h = 0, int m = 0);
void addh(int h);
void addm(int m);
void reset(int h = 0, int m = 0);
friend Time operator+(const Time &t1, const Time &t2);
friend Time operator-(const Time &t1, const Time &t2);
friend Time operator*(const Time &t1, double n);
friend std::ostream &operator<<(std::ostream &os, const Time &t);
};
#endif
time.cpp
#include<iostream>
#include"time.h"
Time::Time(int h, int m)
{
hour = h; minutes = m;
}
void Time::addh(int h)
{
hour += h;
}
void Time::addm(int m)
{
minutes = (minutes + m) % 60;
hour = hour+(minutes + m) / 60;
}
void Time::reset(int h, int m)
{
hour = h; minutes = m;
}
Time operator+(const Time &t1, const Time &t2)
{
Time t;
t.minutes = (t1.minutes + t2.minutes) % 60;
t.hour = t1.hour + t2.hour + (t1.minutes + t2.minutes) / 60;
return t;
}
Time operator-(const Time &t1, const Time &t2)
{
Time t;
long int tot1 = 60 * t1.hour + t1.minutes;
long int tot2 = 60 * t2.hour + t2.minutes;
if (tot1 < tot2)
std::cout << "First time is too little.\n";
else
{
tot1 = tot1 - tot2;
t.hour = tot1 / 60;
t.minutes = tot1 % 60;
}
return t; //if tot1<tot2,return t(0,0);
}
Time operator*(const Time &t1, double n)
{
Time t; long tot = t1.hour * 60 + t1.minutes;
tot = tot*n;
t.minutes = int(tot) % 60;
t.hour = int(tot) / 60;
return t;
}
std::ostream &operator<<(std::ostream &os, const Time &t)
{
os << "Hour= " << t.hour << " ;Minutes= " << t.minutes << std::endl;
return os;
}
user.cpp
#include<iostream>
#include"time.h"
int main()
{
using namespace std;
Time one(3, 35);
Time two(1, 21);
cout << "Show temp#1=plus :" << one+two<< endl;
cout << "Show temp#2=minus :" << one-two << endl;
cout << "Show temp#3=multi :" << one*0.5 << endl;
two.reset(4, 3);
cout << "Show one and two :" << one << " ; "<<two << endl;
cout << "Show temp#4=minus :" << one - two << endl;
one.addh(3);
cout << "Show temp#5=minus :" << one - two << endl;
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++中拷贝构造函数的应用详解
- C++中引用(&)的用法与应用实例分析
- C++使用CriticalSection实现线程同步实例
- C++智能指针实例详解
- 解析C++ 浮点数的格式化输出