第8周实验报告2
来源:互联网 发布:淘宝买家信誉怎么刷 编辑:程序博客网 时间:2024/05/16 11:27
实验目的:实现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):hour(h), minute(m), second(s){ }; void setTime(int h,int m,int s); void display(); //比较运算符(二目)的重载 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);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15 CTime operator-(CTime &c);//对照+理解 CTime operator+(int s);//返回s秒后的时间 CTime operator-(int s);//返回s秒前的时间 //一目运算符的重载 CTime operator++(int);//后置++,下一秒 CTime operator++();//前置++,下一秒 CTime operator--(int);//后置--,前一秒 CTime operator--();//前置--,前一秒 //赋值运算符的重载 CTime operator+=(CTime &c); CTime operator-=(CTime &c); CTime operator+=(int s);//返回s秒后的时间 CTime operator-=(int s);//返回s秒前的时间 }; //下面实现所有的运算符重载代码。 //为简化编程,请注意通过调用已有函数,利用好各函数之间的关系 void CTime::setTime(int h,int m,int s) { hour = h; minute = m; second = s; } void CTime::display() { cout << hour << ":" << minute << ":" << second <<endl; } //比较运算符(二目)的重载 //对 “>”的定义 bool CTime::operator > (CTime &t)//将时分化成秒,再比较大小 { if(hour * 3600 + minute * 60 + second > t.hour * 3600 + t.minute * 60 + second) { return true; } else { return false; } } //对 “<”的定义 bool CTime::operator < (CTime &t)//将时分化成秒,再比较大小 { if(hour * 3600 + minute * 60 + second < t.hour * 3600 + t.minute * 60 + second) { return true; } else { return false; } } //对 “<=”的定义 bool CTime::operator <= (CTime &t)//将时分化成秒,再比较大小 { if(hour * 3600 + minute * 60 + second <= t.hour * 3600 + t.minute * 60 + second) { return true; } else { return false; } } //对 “>=”的定义 bool CTime::operator >= (CTime &t)//将时分化成秒,再比较大小 { if(hour * 3600 + minute * 60 + second >= t.hour * 3600 + t.minute * 60 + second) { return true; } else { return false; } } //对 “==”的定义 bool CTime::operator == (CTime &t)//时分秒都要相等 { if(hour == t.hour && minute == t.minute && second == t.second) { return true; } else { return false; } } //对 “!=”的定义 bool CTime::operator != (CTime &t) { if(hour != t.hour || minute != t.minute || second != t.second) { return true; } else { return false; } } //二目运算符的重载 //返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15 CTime CTime::operator+(CTime &c)//先转换成秒,再相加,然后输出 { int T; T = hour * 3600 + minute *60 + second + c.hour * 3600 + c.minute * 60 + c.second; hour = T / 3600; T = T % 3600; minute = T / 60; second = T % 60; return (*this); } //返回c所规定的时、分、秒后的时间 CTime CTime::operator-(CTime &c)//先转换成秒,再相减,然后输出 { int T; T = hour * 3600 + minute *60 + second - c.hour * 3600 - c.minute * 60 - c.second; hour = T / 3600; T = T % 3600; minute = T / 60; second = T % 60; return (*this); } //返回s秒后的时间 CTime CTime::operator+(int s) { second = second + s; if(second >= 60) { minute = minute + second /60; second = second % 60; if(minute >= 60) { hour = hour + minute / 60; minute = minute % 60; if(hour >= 24) { hour = hour % 24; } } } return (*this); } //返回s秒前的时间 CTime CTime::operator-(int s) { int T; T = hour * 3600 + minute *60 + second - s; hour = T / 3600; T = T % 3600; minute = T / 60; second = T % 60; return (*this); } //一目运算符的重载 //后置++,下一秒 CTime CTime::operator++(int) { CTime temp = (*this); int T; T = hour * 3600 + minute *60 + second + 1; hour = T / 3600; T = T % 3600; minute = T / 60; second = T % 60; return temp; } //前置++,下一秒 CTime CTime::operator++() { int T; T = hour * 3600 + minute *60 + second + 1; hour = T / 3600; T = T % 3600; minute = T / 60; second = T % 60; return (*this); } //后置--,下一秒 CTime CTime::operator--(int) { CTime temp = (*this); int T; T = hour * 3600 + minute *60 + second - 1; hour = T / 3600; T = T % 3600; minute = T / 60; second = T % 60; return temp; } //前置--,下一秒 CTime CTime::operator--() { int T; T = hour * 3600 + minute *60 + second - 1; hour = T / 3600; T = T % 3600; minute = T / 60; second = T % 60; return (*this); } CTime CTime::operator+=(CTime &c) { CTime c1; c1 = (*this) + c; return c1; } CTime CTime::operator-=(CTime &c) { CTime c1; c1 = (*this) - c; return c1; } CTime CTime::operator+=(int s) { CTime c1; c1 = (*this) + s; return c1; } CTime CTime::operator-=(int s) { CTime c1; c1 = (*this) - s; return c1; } void main() { CTime t1(8,20,25), t2(11,20,50), t; cout << "t1为:"; t1.display(); cout << "t2为:"; t2.display(); cout << "比较两个时间大小:" << 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; if (t1 <= t2) cout << "t1 ≤ t2" << endl; cout<<endl; t = t1; t = t + t2; cout << "t1 + t2 = "; t.display(); t = t1; t = t - t2; cout << "t1 - t2 = "; t.display(); t = t1; t = t + 35; cout<< "t1 + 35 = "; t .display(); t = t2; t = t - 55; cout<< "t2 - 55 = " ; t.display(); t = t1; t = t ++; cout << endl; cout << "t1++ = "; t.display(); t = t1; t = ++t; cout << "++t1 = "; t.display(); t = t2; t = t --; cout << "t2-- = "; t.display(); t = t2; t = --t; cout << "--t1 = "; t.display(); t = t1; t +=t2; cout << "t1 += t2,t1 = " ; t.display(); t = t2; t -= t1; cout << "t2 -= t1,t2 = " ; t.display(); t = t1; t += 5; cout << "t1 += 5 = " ; t.display(); t = t1; t -= 6; cout << "t1 -= 6 = " ; t.display(); system("pause"); }
截图;
代码太长了,虽然不难,但打的很辛苦!
- 第8周实验报告1-2
- 第8周实验报告2
- 第8周实验报告2
- 第8周实验报告2
- 第8周实验报告
- 第8周实验报告1-1
- 第8周实验报告1
- 第8周实验报告1
- 第8周实验报告任务一
- 第8周实验报告1-3
- 第8周实验报告任务二
- 第8周实验报告任务三
- 第8周实验报告1
- 第8周实验报告3
- 第8周实验报告3
- 第8周实验报告3
- 第8周实验报告4
- 第2周实验报告2
- 在c#中调用c++里导出的DLL之相关说明
- Java执行Linux命令并返回命令结果
- 重定向 1>&2 2>&1
- 第八周任务二:实现Time 类中的运算符重载
- 程序员素质要求
- 第8周实验报告2
- 如何在DOS窗口中显示UTF-8字符
- g++参数说明
- Android系统中 setprop,getprop,watchprops命令的使用
- UDP数据包大小问题
- BQ27501驱动和用户空间交互数据
- UvaOJ 10014 - Simple calculations
- pushlet 传递页面request参数
- 编程技术面试的五大要点