操作符重载实例
2016-03-13 17:33
309 查看
#include <ctime> #include <iostream> using namespace std; class stopWatch{ public: stopWatch(); void setTime(int newMin, int newSec); stopWatch operator - (stopWatch&); void showTime(); private: int min; int sec; }; stopWatch::stopWatch(){ min = 0; sec = 0; } void stopWatch::setTime(int newMin, int newSec){ min = newMin; sec = newSec; } stopWatch stopWatch::operator-(stopWatch& anotherTime){ stopWatch tempTime; int second; second = min * 60 + sec - (anotherTime.min * 60 + anotherTime.sec); if (second < 0){ second = -second; } tempTime.min = second / 60; tempTime.sec = second % 60; } void stopWatch::showTime(){ if (min > 0){ cout << min << "minutes" << sec << "seconds\n"; } else{ cout << sec << "seconds\n"; } } int main(){ stopWatch startTime, endTime, usedTime; cout << "按回车键开始!"; cin.get(); time_t curtime = time(0); //获取当前系统时间 tm tim = *localtime(&curtime); //根据当前时间获取当地时间 int min, sec; min = tim.tm_min; //得到当前时间的分 sec = tim.tm_sec;// 得到当前时间的秒 startTime.setTime(min, sec); cout << "按回车键结束"; cin.get(); curtime = time(0); //获取当前系统时间 tim = *localtime(&curtime); //根据当前时间获取当地时间 min = tim.tm_min; //得到当前时间的分 sec = tim.tm_sec;// 得到当前时间的秒 endTime.setTime(min, sec); usedTime = endTime - startTime; cout << "用时"; usedTime.showTime(); return 0; }
相关文章推荐
- 面试感悟:3年工作经验程序员应有的技能
- MyList 双链表
- [android] 短信的备份
- linux 权限掩码 umask
- The 8th Zhejiang Provincial Collegiate Programming Contest->A - Ordinal Numbers
- leetcode 121.Best Time to Buy and Sell Stock 股票买卖最佳时间
- 九度题目1194八进制
- 网络流学习笔记
- leetcode:Repeated DNA Sequences
- 第一次上机实验1-3
- oracle的hint总结(二)
- 《Linux内核分析》 第三周 构造一个简单的Linux系统MenuOS
- 蛇形串---------
- Vi Vim编译器设置显示行号
- Java中有关Null的9件事
- 1054. 求平均值 (20)
- 重复导入某一文件
- C++第一次上机实验-1-2
- Effective C++ 条款18
- [转载] 打印三角形数据输出