第八周实验报告2.0

来源:互联网 发布:哈雷尔体测数据 编辑:程序博客网 时间:2024/05/29 13:21

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 复数类中的运算符重载
* 作 者: 任小宁
* 完成日期: 2012 年 04 月10 日
* 版 本 号:201158504431

* 对任务及求解方法的描述部分
* 问题描述:实现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:15CTime 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:15CTime 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");  }



原创粉丝点击