Time类中的运算符重载
问题及代码:
/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:李盈盈 * 完成日期:2015年 05 月 09 日 * 版 本 号:v1.0 * * 问题描述:定义对时间对象的自增和自减一目运算符,并定义Time类中的<<和>>运算符重载,实现时间的输入和输出。* 输入描述:输入两个时间。* 程序输出:按要求输出。*/
#include <iostream>
using namespace std;
class CTime
{
private:unsigned short int hour;unsigned short int minute;unsigned short int second;
public:CTime(int h=0,int m=0,int s=0);void setTime(int h,int m,int s);friend istream &operator>>(istream &in,CTime &t);friend ostream &operator<<(ostream &out,CTime t);bool operator > (CTime &t);bool operator < (CTime &t);bool operator >= (CTime &t);bool operator <= (CTime &t);bool operator == (CTime &t);bool operator != (CTime &t);CTime operator+(CTime &c);CTime operator-(CTime &c);CTime operator+(int s);CTime operator-(int s);CTime operator++(int);CTime operator++();CTime operator--(int);CTime operator--();CTime operator+=(CTime &c);CTime operator-=(CTime &c);CTime operator+=(int s);CTime operator-=(int s);
};
CTime::CTime(int h,int m,int s)
{hour=h;minute=m;second=s;
}
void CTime::setTime(int h,int m,int s)
{hour=h;minute=m;second=s;
}
istream &operator>>(istream &in,CTime &t)
{char ch1,ch2;while(1){cout<<"请输入时间(hh:mm:ss) ";cin>>t.hour>>ch1>>t.minute>>ch2>>t.second;if (ch1==':' && ch2==':')if (t.hour>-1 && t.hour<24 && t.minute>-1 && t.minute<60 && t.second>-1 && t.second<60)break;cerr<<"时间格式不正确! 请重新输入";}return in;
}
ostream &operator<<(ostream &out,CTime t)
{out<<t.hour<<':'<<t.minute<<':'<<t.second;return out;
}
bool CTime::operator > (CTime &t)
{if (hour>t.hour)return true;if (hour<t.hour)return false;if (minute>t.minute)return true;if (minute<t.minute)return false;if (second>t.second)return true;return false;
}bool CTime::operator < (CTime &t)
{if (hour<t.hour)return true;if (hour>t.hour)return false;if (minute<t.minute)return true;if (minute>t.minute)return false;if (second<t.second)return true;return false;
}
bool CTime::operator == (CTime &t)
{if (*this<t || *this>t)return false;return true;
}
bool CTime::operator != (CTime &t)
{if (*this==t) return false;return true;
}
bool CTime::operator >= (CTime &t)
{if (*this<t) return false;return true;
}
bool CTime::operator <= (CTime &t)
{if (*this>t) return false;return true;
}
CTime CTime::operator + (CTime &t)
{int h,m,s;s=second+t.second;m=minute+t.minute;h=hour+t.hour;if (s>59){s-=60;m++;}if (m>59){m-=60;h++;}while (h>23) h-=24;CTime t0(h,m,s);return t0;
}
CTime CTime::operator+(int s)
{int ss=s%60;int mm=(s/60)%60;int hh=s/3600;CTime t0(hh,mm,ss);return *this+t0;
}
CTime CTime::operator - (CTime &t)
{int h,m,s;s=second-t.second;m=minute-t.minute;h=hour-t.hour;if (s<0){s+=60;m--;}if (m<0){m+=60;h--;}while (h<0) h+=24;CTime t0(h,m,s);return t0;
}
CTime CTime::operator-(int s)
{int ss=s%60;int mm=(s/60)%60;int hh=s/3600;CTime t0(hh,mm,ss);return *this-t0;
}
CTime CTime::operator++(int)
{CTime t=*this;*this=*this+1;return t;
}
CTime CTime::operator++()
{*this=*this+1;return *this;
}
CTime CTime::operator--(int)
{CTime t=*this;*this=*this-1;return t;
}
CTime CTime::operator--()
{*this=*this-1;return *this;
}
CTime CTime::operator+=(CTime &c)
{*this=*this+c;return *this;
}
CTime CTime::operator-=(CTime &c)
{*this=*this-c;return *this;
}
CTime CTime::operator+=(int s)
{*this=*this+s;return *this;
}
CTime CTime::operator-=(int s)
{*this=*this-s;return *this;
}int main()
{CTime t1,t2,t;cout<<"t1为:";cin>>t1;cout<<"t2为:";cin>>t2;cout<<"下面比较两个时间大小:\n";if (t1>t2) cout<<"t1>t2"<<endl;if (t1<t2) cout<<"t1<t2"<<endl;if (t1==t2) cout<<"t1=t2"<<endl;if (t1!=t2) cout<<"t1≠t2"<<endl;if (t1>=t2) cout<<"t1≥t2"<<endl;if (t1<=t2) cout<<"t1≤t2"<<endl;cout<<endl;cout<<"t1= "<<t1<<endl;cout<<"t2= "<<t2<<endl;cout<<"t=t1++"<<endl;t=t1++;cout<<"t= "<<t<<" t1= "<<t1<<endl;cout<<"t=++t1"<<endl;t=++t1;cout<<"t= "<<t<<" t1= "<<t1<<endl;cout<<"t1+t2= "<<t1+t2<<endl;cout<<"t1-t2= "<<t1-t2<<endl;cout<<"t1+2000= "<<t1+2000<<endl;cout<<"t1-5000= "<<t1-5000<<endl;return 0;
}
运行结果:
Time类中的运算符重载相关推荐
- 第十三周项目一-分数类中的运算符重载
/**Copyright(c)2016,烟台大学计算机与控制工程学院*All rights reserved*文件名称:123.cpp*作 者:王蕊*完成日期:2016年5月25日*版 本 号:v1. ...
- 第十二周项目二-Time类中的运算符重载
/**Copyright(c)2016,烟台大学计算机与控制工程学院*All rights reserved*文件名称:123.cpp*作 者:王蕊*完成日期:2016年5月24日*版 本 号:v1. ...
- 项目3-分数类中的运算符重载
/* * Copyright (c) 2011, 烟台大学计算机学院 * All rights reserved. * 作 者:王静 * 完成日期:2013 年 5 月 5 日 * 版 本 号:v1. ...
- /项目3-分数类中的运算符重载
/* * Copyright (c) 2011, 烟台大学计算机学院 * All rights reserved. * 作 者:王静 * 完成日期:2013 年 4 月 24 日 * 版 本 号:v1 ...
- 8-2 实现Time类中的运算符重载
/* * 作 者: 霍雨佳 * 完成日期:2014 年4月15日 * 版 本 号:v1.0 * 问题描述:实现Time类中的运算符重载 * 样例输入: * 样例输出: * 项目要求:实现Time类中的 ...
- 实现复数类中的运算符重载(含有double类型)
/* * 作 者: 霍雨佳 * 完成日期:2014 年4月15日 * 版 本 号:v1.0 * 问题描述:实现复数类中的运算符重载. * 样例输入: * 样例输出: * 问题分析:一个定义完整的类,是 ...
- 8-3 实现分数类中的运算符重载
/* * 作 者: 霍雨佳 * 完成日期:2014 年4月15日 * 版 本 号:v1.0 * 问题描述:实现分数类中的运算符重载 * 样例输入: * 样例输出: * 项目要求:实现分数类中的运算符重 ...
- 第十二周上机实践项目 项目1-实现复数类中的运算符重载 (2)
问题及代码: [项目-实现复数类中的运算符重载] (1)请用类的成员函数,定义复数类重载运算符+.-.*./,使之能用于复数的加减乘除 class Complex { public:Complex() ...
- 第7周-项目1-完整实现复数类中的运算符重载-扩展+、-、*、/运算符的功能
问题及代码: /* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:number.cpp *作 者:单昕昕 *完成日期:20 ...
- 第十二周-实现复数类中的运算符重载
/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:main.cpp *作 者:李德彪 *完成时间:2016年5月23日 * ...
最新文章
- 文件系统与NoSQL分布式存储技术对比
- 003_Redis配置
- complementary prior
- 网络性能测试工具iperf详细使用图文教程【转载】
- 计算机学校的奖项,2017年度国家科学技术奖各奖项公示
- 求某一维度的最大值_高中数学函数求最值常用方法总结
- PCS7 Step7块破解 解锁 FC FB s7canopener S7 Blocks Unlock
- html浮动垂直居中对齐,css如何设置垂直居中对齐?
- xlsxwriter设置列宽_Python3之excel操作xlsxwriter模块
- 华为交换机端口vlan详解
- 金蝶kis专业版公网访问_金蝶KIS远程服务器端和远程客户端配置说明
- 《人工智能狂潮》读后感——什么是人工智能?(一)
- 2021年12月苹果开发者证书配置
- day01 Python基础
- Delphi访问网络共享文件夹
- 音准听力测试软件app,听音练耳app神器考试用
- 手机中PDF格式转换PPT操作方法
- html文件是一种使用超文本标记语言,超文本标记语言HTML HTML(Hyper Text Markup Language,.ppt...
- 嵌入式Qt开发环境搭建
- Cesium快速入门
热门文章
- mysql中floa类型数据和mysql命令
- VMWare16Pro 调整中文
- Ubuntu 16.04安装有道词典
- 万能密码HTTP基本方法 —— 【WUST-CTF2020】admin
- 爱码物联SaaS|一物一码技术如何助力线下流量营销?
- AIGC、ChatGPT、GPT系列?我的认识
- 10万ip网站 服务器,我做电影站如何在半年内实现日IP10万(转载)
- androidtv gms包_Android之GMS自我总结
- [Android App] 内涵段子最新5.5.5 去广告去更新修改版
- Three.js学习笔记---我和小伙伴都惊呆了