您的位置:首页 > 其它

第九周 【项目2-Time类中的运算符重载(续)】

2015-05-13 08:34 453 查看
问题描述:

在Time类中的运算符重载基础上

(1)定义对时间对象的自增和自减一目运算符


<span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;"><strong>	//一目运算符的重载
	CTime operator++(int);//后置++,下一秒
	CTime operator++();//前置++,下一秒,前置与后置返回值不一样
	CTime operator--( int);//后置--,前一秒
	CTime operator--();//前置--,前一秒</strong></span>


(2)定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。



代码:

main.cpp

#include <iostream>
#include "CTime.h"
using namespace std;
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<<"时间格式不正确! 请重新输入\n";
    }
    return cin;
}
ostream &operator<<(ostream &out,CTime t) {
    out<<t.hour<<':'<<t.minute<<':'<<t.second;
    return out;
}
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;
}


CTime.h

#ifndef CTIME_H
#define CTIME_H
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);
    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 &t);
    CTime operator-(CTime &t);
    CTime operator+(int s);
    CTime operator-(int s);
    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--();//前置--,前一秒
	friend istream &operator>>(istream& in,CTime& c);
	friend ostream &operator<<(ostream& out,const CTime& c);
};
#endif // CTIME_H


CTime.cpp

#include <iostream>
#include "CTime.h"
using namespace std;
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;
}
void CTime::display()  {
    cout<<hour<<':'<<minute<<':'<<second<<'\12';
}
bool CTime::operator>(CTime & s){
    if (hour>s.hour) return true;
    if (hour<s.hour) return false;
    if (minute>s.minute) return true;
    if (minute<s.minute) return false;
    if (second>s.second) return true;
    return false;
}
bool CTime::operator<(CTime & s){
    if (hour<s.hour) return true;
    if (hour>s.hour) return false;
    if (minute<s.minute) return true;
    if (minute>s.minute) return false;
    if (second<s.second) return true;
    return false;
}
bool CTime::operator>=(CTime & s){
    if(*this<s)return false;
    else return true;
}
bool CTime::operator<=(CTime & s){
    if(*this>s)return false;
    else return true;
}
bool CTime::operator==(CTime &s){
    if(hour==s.hour&&minute==s.minute&&second==s.second)
        return true;
    else return false;
}
bool CTime::operator!=(CTime &s) {
    if (*this==s) return false;
    return true;
}
CTime CTime::operator+(CTime &s){
    CTime t;
    int a,b,c;
    a=hour+s.hour;
    b=minute+s.minute;
    c=second+s.second;
    if(c>=60){
        b++;
        c%=60;
    }
    if(b>=60){
        a++;
        b%=60;
    }
    if(a>=24){
        a%=24;
    }
    t.setTime(a,b,c);
    return t;
}
CTime CTime::operator-(CTime &s){
    CTime t;
    int a,b,c;
    a=hour-s.hour;
    b=minute-s.minute;
    c=second-s.second;
    if(c<0){
        c+=60;
        b--;
    }
    if(b<0){
        b+=60;
        a--;
    }
    if(a<0){
        a+=24;
    }
    t.setTime(a,b,c);
    return t;
}
CTime CTime::operator+(int s){
    CTime t(s%60,(s/60)%60,s/3600);
    return *this+t;
}
CTime CTime::operator-(int s){
    CTime t(s%60,(s/60)%60,s/3600);
    return *this-t;
}
CTime CTime::operator+=(CTime &s){
    *this=*this+s;
    return *this;
}
CTime CTime::operator-=(CTime &s){
    *this=*this-s;
    return *this;
}
CTime CTime::operator+=(int s){
    *this=*this+s;
    return *this;
}
CTime CTime::operator-=(int s){
    *this=*this-s;
    return *this;
}
CTime CTime::operator++(int n){
    CTime c=*this;
    *this=*this+1;
    return c;
}
CTime CTime::operator++(){
    *this=*this+1;
    return *this;
}
CTime CTime::operator--(int n){
    CTime t=*this;
    *this=*this-1;
    return t;
}
CTime CTime::operator--(){
    *this=*this-1;
    return *this;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: