问题及代码:
运行结果:
知识点总结:
运行结果:
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:Time.cpp
*作 者:单昕昕
*完成日期:2015年4月25日
*版 本 号:v1.0
*问题描述:实现Time类中的运算符重载
*程序输入:无。
*程序输出:Time。
*/
#include<iostream>
#include<cmath>
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);
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);
//二目的加减运算符的重载
//返回t规定的时、分、秒后的时间
//例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15
CTime operator+(CTime &t);
CTime operator-(CTime &t);//对照+理解
CTime operator+(int s);//返回s秒后的时间
CTime operator-(int s);//返回s秒前的时间
//二目赋值运算符的重载
CTime operator+=(CTime &c);
CTime operator-=(CTime &c);
CTime operator+=(int s);//返回s秒后的时间
CTime operator-=(int s);//返回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;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
//二目的比较运算符重载
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)
{
return !(*this < t);
}
bool CTime::operator <= (CTime &t)
{
return !(*this > t);
}
bool CTime::operator == (CTime &t)
{
return !((*this > t)||(*this < t));
}
bool CTime::operator != (CTime &t)
{
return ((*this > t)||(*this < t));
}
//二目的加减运算符的重载
//返回t规定的时、分、秒后的时间
//例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15
CTime CTime::operator+(CTime &t)
{
CTime time;
int h,m,s;
h=hour+t.hour;
m=minute+t.minute;
s=second+t.second;
int count=0;
int i=s;
if(s>=60)
{
while(i>=60)
{
i%=60;
count=(s-i)/60;
}
m+=count;
s=i;
}
int j=m;
count=0;
if (m>=60)
{
while(j>=60)
{
j%=60;
count=(m-j)/60;
}
h+=count;
m=j;
}
int k=h;
count=0;
if (h>=24)
{
while(k>=24)
{
k%=24;
count=(h-k)/24;
}
h=k;
}
time.hour=h;
time.minute=m;
time.second=s;
return time;
}
CTime CTime::operator-(CTime &t)//对照+理解
{
CTime time;
int h,m,s;
h=hour-t.hour;
m=minute-t.minute;
s=second-t.second;
if(s<0)
{
s+=60;
m--;
}
if(m<0)
{
m+=60;
h--;
}
if(h<0)
h+=24;
time.hour=h;
time.minute=m;
time.second=s;
return time;
}
CTime CTime::operator+(int s)//返回s秒后的时间
{
int h,m,sec;
sec=s%60;
m=(s/60)%60;
h=s/3600;
CTime time(h,m,sec);
return *this+time;
}
CTime CTime::operator-(int s)//返回s秒前的时间
{
int h,m,sec;
sec=s%60;
m=(s/60)%60;
h=s/3600;
CTime time(h,m,sec);
return *this-time;
}
//二目赋值运算符的重载
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)//返回s秒后的时间
{
int h,m,sec;
sec=(second+s)%60;
m=minute+(s/60)%60;
h=hour+s/3600;
CTime time(h,m,sec);
*this=*this+s;
return *this;
}
CTime CTime::operator-=(int s)//返回s秒前的时间
{
*this=*this-s;
return *this;
}
//下面定义用于测试的main()函数
int main()
{
CTime t1(10,41,30),t2(6,42,23),t;
cout<<"t1为:";
t1.display();
cout<<"t2为:";
t2.display();
cout<<"————————————————————"<<endl;//分割线
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;
cout<<"下面对两个时间进行计算:"<<endl;
t=t1+t2;
cout<<"t1+t2:";
t.display();
t=t1-t2;
cout<<"t1-t2:";
t.display();
t=t1+333;
cout<<"t1+333s:";
t.display();
t=t2-2222;
cout<<"t2-2222s:";
t.display();
t1+=t2;
cout<<"t1+=t2:";
t1.display();
t1-=t2;
cout<<"t1-=t2:";
t1.display();
t1+=3333;
cout<<"t1+=3333s:";
t1.display();
t2-=222;
cout<<"t2-=222s:";
t2.display();
return 0;
}
运行结果:
知识点总结:
运算符重载。
学习心得:
真是要写哭了。。。这个破程序我码了改了整了快三个小时。。。
下面附上为什么写这么长时间的原因:
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:Time.cpp
*作 者:单昕昕
*完成日期:2015年4月25日
*版 本 号:v1.0
*问题描述:实现Time类中的运算符重载
*程序输入:无。
*程序输出:Time。
*/
#include<iostream>
#include<cmath>
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);
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);
//二目的加减运算符的重载
//返回t规定的时、分、秒后的时间
//例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15
CTime operator+(CTime &t);
CTime operator-(CTime &t);//对照+理解
CTime operator+(int s);//返回s秒后的时间
CTime operator-(int s);//返回s秒前的时间
//二目赋值运算符的重载
CTime operator+=(CTime &c);
CTime operator-=(CTime &c);
CTime operator+=(int s);//返回s秒后的时间
CTime operator-=(int s);//返回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;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
//二目的比较运算符重载
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)
{
return !(*this < t);
}
bool CTime::operator <= (CTime &t)
{
return !(*this > t);
}
bool CTime::operator == (CTime &t)
{
return ((*this > t)||(*this < t));
}
bool CTime::operator != (CTime &t)
{
return !((*this > t)||(*this < t));
}
//二目的加减运算符的重载
//返回t规定的时、分、秒后的时间
//例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15
CTime CTime::operator+(CTime &t)
{
CTime time;
time.hour=hour+t.hour;
time.minute=minute+t.minute;
time.second=second+t.second;
int count=0;
int i=time.second;
if(time.second>=60)
{
while(i>=60)
{
i%=60;
count=(time.second-i)/60;
}
time.minute+=count;
time.second=i;
}
int j=time.minute;
count=0;
if (time.minute>=60)
{
while(j>=60)
{
j%=60;
count=(time.minute-j)/60;
}
time.hour+=count;
time.minute=j;
}
int k=time.hour;
count=0;
if (time.hour>=24)
{
while(k>=24)
{
k%=24;
count=(time.hour-k)/24;
}
time.hour=k;
}
return time;
}
CTime CTime::operator-(CTime &t)//对照+理解
{
CTime time;
if(hour>t.hour)
{
time.hour=hour-t.hour;
time.minute=minute-t.minute;
time.second=second-t.second;
if(time.second<0)
{
time.minute--;
time.second+=60;
}
if(time.minute<0)
{
time.hour--;
time.minute+=60;
}
}
else
{
time.hour=t.hour-hour;
time.minute=t.minute-minute;
time.second=t.second-second;
if(time.second<0)
{
time.minute--;
time.second+=60;
}
if(time.minute<0)
{
time.hour--;
time.minute+=60;
}
}
return time;
}
CTime CTime::operator+(int s)//返回s秒后的时间
{
CTime time;
second+=s;
int count=0;
int i=second;
if(second>=60)
{
while(i>=60)
{
i%=60;
count=(second-i)/60;
}
minute+=count;
second=i;
}
int j=minute;
count=0;
if (minute>=60)
{
while(j>=60)
{
j%=60;
count=(minute-j)/60;
}
hour+=count;
minute=j;
}
int k=hour;
count=0;
if (hour>=24)
{
while(k>=24)
{
k%=24;
count=(hour-k)/24;
}
hour=k;
}
time.hour=hour;
time.minute=minute;
time.second=second;
return time;
}
CTime CTime::operator-(int s)//返回s秒前的时间
{
CTime time;
second-=s;
int count=0;
int i=fabs(second);
if(second>=60)
{
while(i>=60)
{
i%=60;
count=(second-i)/60;
}
minute-=count;
second=i;
}
int j=minute;
count=0;
if (minute>=60)
{
while(j>=60)
{
j%=60;
count=(minute-j)/60;
}
hour-=count;
minute=j;
}
int k=hour;
count=0;
if (hour>=24)
{
while(k>=24)
{
k%=24;
count=(hour-k)/24;
}
hour=k;
}
time.hour=hour;
time.minute=minute;
time.second=second;
return time;
}
//二目赋值运算符的重载
CTime CTime::operator+=(CTime &c)
{
this->hour=this->hour+c.hour;
this->minute=this->minute+c.minute;
this->second=this->second+c.second;
int count=0;
int i=this->second;
if(this->second>=60)
{
while(i>=60)
{
i%=60;
count=(this->second-i)/60;
}
this->minute+=count;
this->second=i;
}
int j=this->minute;
count=0;
if (this->minute>=60)
{
while(j>=60)
{
j%=60;
count=(this->minute-j)/60;
}
this->hour+=count;
this->minute=j;
}
int k=this->hour;
count=0;
if (this->hour>=24)
{
while(k>=24)
{
k%=24;
count=(this->hour-k)/24;
}
this->hour=k;
}
return *this;
}
CTime CTime::operator-=(CTime &c)
{
if(this->hour>c.hour)
{
this->hour=this->hour-c.hour;
this->minute=this->minute-c.minute;
this->second=this->second-c.second;
if(this->second<0)
{
this->minute--;
this->second+=60;
}
if(this->minute<0)
{
this->hour--;
this->minute+=60;
}
}
else
{
this->hour=c.hour-this->hour;
this->minute=this->minute-c.minute;
this->second=this->second-c.second;
if(this->second<0)
{
this->minute--;
this->second+=60;
}
if(this->minute<0)
{
this->hour--;
this->minute+=60;
}
}
return *this;
}
CTime CTime::operator+=(int s)//返回s秒后的时间
{
this->second+=s;
int count=0;
int i=this->second;
if(this->second>=60)
{
while(i>=60)
{
i%=60;
count=(this->second-i)/60;
}
this->minute+=count;
this->second=i;
}
int j=this->minute;
count=0;
if (this->minute>=60)
{
while(j>=60)
{
j%=60;
count=(this->minute-j)/60;
}
this->hour+=count;
this->minute=j;
}
int k=this->hour;
count=0;
if (this->hour>=24)
{
while(k>=24)
{
k%=24;
count=(this->hour-k)/24;
}
this->hour=k;
}
this->hour=hour;
this->minute=minute;
this->second=second;
return *this;
}
CTime CTime::operator-=(int s)//返回s秒前的时间
{
this->second-=s;
int count=0;
int i=fabs(this->second);
if(this->second>=60)
{
while(i>=60)
{
i%=60;
count=(this->second-i)/60;
}
this->minute-=count;
this->second=i;
}
int j=this->minute;
count=0;
if (this->minute>=60)
{
while(j>=60)
{
j%=60;
count=(this->minute-j)/60;
}
this->hour-=count;
this->minute=j;
}
int k=this->hour;
count=0;
if (this->hour>=24)
{
while(k>=24)
{
k%=24;
count=(this->hour-k)/24;
}
this->hour=k;
}
this->hour=hour;
this->minute=minute;
this->second=second;
return *this;
}
//下面定义用于测试的main()函数
int main()
{
CTime t1(10,41,30),t2(6,42,23),t;
cout<<"t1为:";
t1.display();
cout<<"t2为:";
t2.display();
cout<<"————————————————————"<<endl;//分割线
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;
cout<<"下面对两个时间进行计算:"<<endl;
t=t1+t2;
cout<<"t1+t2:";
t.display();
t=t1-t2;
cout<<"t1-t2:";
t.display();
t=t1+333;
cout<<"t1+333s:";
t.display();
t=t2-2222;
cout<<"t2-2222s:";
t.display();
t1+=t2;
cout<<"t1+=t2:";
t1.display();
t1-=t2;
cout<<"t1-=t2:";
t1.display();
t1+=3333;
cout<<"t1+=3333s:";
t1.display();
t2-=222;
cout<<"t2-=222s:";
t2.display();
return 0;
}
运行结果:
先不说我这段有直奔500行去的程序行数。。看见运行结果我就有想撞墙的冲动。。
因为。。那个数据类型啊啊啊!!无符号整型啊啊啊~!!!
然后我就开始改。。发现这真是牵一发而动全身啊!
然后我又翻了翻书重新理了下思路,发现算法可以改进很多很多很多(此处略去N个很多)。。
然后那个行数就唰唰唰往下掉啊。。
终于出来了最前面那段200多行的程序。。
我。。我真是欲哭无泪悲喜交加。。
以后再也不要对着电脑想思路了,还是要回归最开始学C++的时候,对着纸笔想思路啊啊啊。