C++primer plus第六版课后编程题答案11.4
来源:互联网 发布:淘宝开店计划书 编辑:程序博客网 时间:2024/06/05 05:04
mytime.h
#ifndef MYTIME_h_#define MYTIME_h_#include <iostream>//为了方便我全部写成inline函数了class Time{private:int hours;int minutes;public:Time(){hours=minutes=0;};Time(int h,int m=0){hours=h;minutes=m;};void Reset(int h=0,int m=0){hours=h;minutes=m;};Time operator+(const Time &t)const{int jinwei=0;int newh=hours+t.hours;int newm=minutes+t.minutes;if(newm>60){newm=newm%60;jinwei=1;}newh+=jinwei;return Time(newh,newm);};Time operator+(double m)const//重载+{int jinwei=0;int newm=m+minutes;if(newm>60){newm=newm%60;jinwei=1;}int newh=hours+jinwei;return Time(newh,newm);};Time operator-(const Time &t)const{int jinwei=0;int newh=hours-t.hours;int newm=minutes-t.minutes;if(newm<0){newm=minutes-t.minutes+60;jinwei=-1;}newh+=jinwei;return Time(newh,newm);};Time operator*(double n)const{double totalsource=hours*60+minutes;//原来的分钟数double totalnow=totalsource*n;int newh=totalnow/60;int newm=int(totalnow)%60;//求余必须为intreturn Time(newh,newm);};friend Time operator*(double n,const Time &t){return t*n;};friend Time operator-(const Time &t,double m){double total=t.hours*60+t.minutes;int now=total-m;int newh=now/60;int newm=now%60;//求余必须为intreturn Time(newh,newm);};friend Time operator+(double m,const Time &t){return t+m;}friend std::ostream &operator<<(std::ostream &os,const Time &t){os<<t.hours<<" hours,"<<t.minutes<<" minutes."<<std::endl;return os;};};#endif
main114.cpp
#include<iostream>#include"myTime.h"using namespace std;void main114(){Time t1;Time t2(6,9);Time t3(7);Time t4(4,59);/*构造测试cout<<t1<<endl;cout<<t2<<endl;cout<<t3<<endl;*/Time t5=t1+t2;//cout<<t5<<endl;//t5=t2+t4;//cout<<t5<<endl;//t5=t2+10;//cout<<t5<<endl;//t5=t4*2;t5=t2-t4;cout<<t5<<endl;cin.get();}
0 0
- C++primer plus第六版课后编程题答案11.4
- C++primer plus第六版课后编程练习答案11.4
- C++primer plus第六版课后编程题答案 6.1
- C++primer plus第六版课后编程题答案 6.2
- C++primer plus第六版课后编程题答案 6.3
- C++primer plus第六版课后编程题答案 6.4
- C++primer plus第六版课后编程题答案 6.5
- C++primer plus第六版课后编程题答案 6.6
- C++primer plus第六版课后编程题答案 6.7
- C++primer plus第六版课后编程题答案 6.8
- C++primer plus第六版课后编程题答案 6.9
- C++primer plus第六版课后编程题答案7.1
- C++primer plus第六版课后编程题答案7.2
- C++primer plus第六版课后编程题答案7.3
- C++primer plus第六版课后编程题答案7.4
- C++primer plus第六版课后编程题答案7.5
- C++primer plus第六版课后编程题答案7.6
- C++primer plus第六版课后编程题答案 7.7
- android解析xml文件的方式(其二)SAX
- [置顶]秒杀大部分公司的面试题,精心整理合集,值得收藏(javascript篇)
- NET 2.0 基础类库中的范型
- AM335X-LED驱动-ioremap
- 【OFFICE】Office 2010 取消弹出宏安全性设置错误的提示的技巧
- C++primer plus第六版课后编程题答案11.4
- 用数组实现插入排序
- android解析xml文件的方式(其三)PULL
- CodeBlocks输出中文乱码的解决方法
- 独立玩家越来越少
- shell脚本中如何获取命令的参数(1)
- 随机森林——Random Forests
- homework
- 用ASP检查MDaemon系统管理员登录