现在的位置: 首页 > 综合 > 正文

请老师给我改一下程序,我实在是不知道错哪了,花了好长时间都没运行出来,任务二

2013年10月17日 ⁄ 综合 ⁄ 共 5556字 ⁄ 字号 评论关闭
#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为: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)
{
 second=second+60*minute+3600*hour;
 t.second=t.second+60*t.minute+3600*t.hour;
 if(second>t.second)
  return true;
 else
  return false;
}
bool CTime::operator < (CTime &t)
{
 CTime t2;
 t2.hour=hour;
 t2.minute=minute;
 t2.second=second;
 if(t2>t)
  return true;
 else
  return false;
}
bool CTime::operator >= (CTime &t)
{
 second=second+60*minute+3600*hour;
 t.second=t.second+60*t.minute+3600*t.hour;
 if(second>=t.second)
  return true;
 else
  return false;
}
bool CTime::operator <= (CTime &t)
{
 CTime c2;
 c2.hour=hour;
 c2.minute=minute;
 c2.second=second;
 
 if(c2>=t)
  return true;
 else
  return false;
}
bool CTime::operator == (CTime &t)
{
 CTime c2;
 c2.hour=hour;
 c2.minute=minute;
 c2.second=second;
 
 if(!(c2>t)&&!(c2<t))
  return true;
 else
  return false;
}
bool CTime::operator != (CTime &t)
{
 second=second+60*minute+3600*hour;
 t.second=t.second+60*t.minute+3600*t.hour;
 if(second==t.second)
  return true;
 else
  return false;
}
//二目运算符的重载
CTime CTime::operator+(CTime &c)//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
{
 CTime c1;
 unsigned short int m;
 m= (hour * 3600 + minute * 60 + second) + (c.hour * 3600 + c.minute * 60 + c.second);
 c1.hour=m/3600;
 if(c1.hour>24)
 c1.hour=c1.hour%24;
 c1.minute=m/60;
 if(c1.minute>59)
 c1.minute=c1.minute%60;
 c1.second=m%60;
 return c1;
}

CTime CTime::operator-(CTime &c)
{
 CTime c1;
 unsigned short int m;
 m=second+60*minute+3600*hour-(c.second+60*c.minute+3600*c.hour);
 if(m<0)
 {
  m=-m;

 }
 c1.hour=m/3600;
 if(c1.hour>24)
 c1.hour=c1.hour%24;
 c1.minute=m/60;
 if(c1.minute>59)
 c1.minute=c1.minute%60;
 c1.second=m%60;
 return c1;
}
//返回s秒后的时间 
CTime CTime::operator+(int s)
{
 CTime c1;
  int m;
 m=second+60*minute+3600*hour+s;
 c1.hour=m/3600;
 if(c1.hour>24)
 c1.hour=c1.hour%24;
 c1.minute=m/60;
 if(c1.minute>59)
 c1.minute=c1.minute%60;
 c1.second=m%60;
 return c1;
 
}
//返回s秒前的时间
CTime CTime::operator-(int s)
{
 CTime c1;
 int m;
 m=second+60*minute+3600*hour-s;
 if(c1.second<0)
 {
  m=-m;

 }
 c1.hour=m/3600;
 if(c1.hour>24)
 c1.hour=c1.hour%24;
 c1.minute=m/60;
 if(c1.minute>59)
 c1.minute=c1.minute%60;
 c1.second=m%60;
 return c1;
 
}
//一目运算符的重载
//后置++,下一秒
CTime CTime::operator++(int)
{
 CTime temp(*this);
 second=second+60*minute+3600*hour;
 second++;
  second=second%60;
     minute=second/60;
  if(minute>59)
  minute=minute%60;
 hour=second/3600;
 if(hour>24)
  hour=hour%24;
 return temp;
}
//前置++,下一秒
CTime CTime:: operator++()
{
 second=second+60*minute+3600*hour;
 ++second;
  second=second%60;
     minute=second/60;
  if(minute>59)
  minute=minute%60;
 hour=second/3600;
 if(hour>24)
  hour=hour%24;
 return *this;
}
//后置--,前一秒
CTime CTime::operator--(int)
{
 CTime temp(*this);
 second=second+60*minute+3600*hour;
 second--;
  second=second%60;
     minute=second/60;
 hour=second/3600;
 return temp;
}
//前置--,前一秒
CTime CTime::operator--()
{
 CTime temp(*this);
 second=second+60*minute+3600*hour;
 --second;
  second=second%60;
     minute=second/60;
 hour=second/3600;
 return temp;
}
//赋值运算符的重载     
CTime CTime::operator+=(CTime &c)
{
 CTime c1;
 c1.second=second+60*minute+3600*hour+c.second+60*c.minute+3600*c.hour;
 c1.second=c1.second%60;
 c1.minute=c1.second/60;
 if(c1.minute>59)
  c1.minute=c1.minute%60;
 c1.hour=c1.second/3600;
 if(c1.hour>24)
  c1.hour=c1.hour%24;
 return c1;
 
}
CTime CTime::operator-=(CTime &c)
{
 CTime c1;
 c1.second=second+60*minute+3600*hour-(c.second+60*c.minute+3600*c.hour);
 if(c1.second<0)
 {
  cout<<"时间不能为负数"<<endl;
  system("pause");  
        exit(0);  

 }
 c1.second=c1.second%60;
 c1.minute=c1.second/60;
 if(c1.minute>59)
  c1.minute=c1.minute%60;
 c1.hour=c1.second/3600;
 if(c1.hour>24)
  c1.hour=c1.hour%24;
 return c1;
}
//返回s秒后的时间
CTime CTime::operator+=(int s)
{
 CTime c1;
 c1.second=second+60*minute+3600*hour+s;
 c1.second=c1.second%60;
 c1.minute=c1.second/60;
 if(c1.minute>59)
  c1.minute=c1.minute%60;
 c1.hour=c1.second/3600;
 if(c1.hour>24)
  c1.hour=c1.hour%24;
 return c1;
 
}
//返回s秒前的时间
CTime CTime::operator-=(int s)
{
 CTime c1;
 c1.second=second+60*minute+3600*hour-s;
 if(c1.second<0)
 {
  cout<<"时间不能为负数"<<endl;
  system("pause");  
        exit(0);  

 }
 c1.second=c1.second%60;
 c1.minute=c1.second/60;
 if(c1.minute>59)
  c1.minute=c1.minute%60;
 c1.hour=c1.second/3600;
 if(c1.hour>24)
  c1.hour=c1.hour%24;
 return c1;
 
}
void main()
{
 CTime t1(8,20,25),t2(11,20,50),t;

 cout<<"t1为:";

 t1.display();

 cout<<"t2为:";

 t2.display();

 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;
 //下面自行设计对其他运算符的重载的测试
   t = t1 + t2;

  cout<<"t1+t2=";

  t.display();

  cout<<"t2-t1=";

    t=t2-t1;

  t.display();

  int s=5; 

  t=t1+s;

  cout<<"t1"<<' '<<s<<"秒后的时间为:";

  t.display();

  int n=5; 

  t=t1-n;

  cout<<n<<"秒前的时间为:";

  t.display();

  t1++;

  cout<<"后置++,t1下一秒的时间为:";

  t1.display();

  ++t1;

  cout<<"前置++,t1下一秒的时间为:";

  t1.display();

  t2--;

  cout<<"后置--,t2下一秒的时间为:";

  t2.display();

  --t2;

  cout<<"前置--,t2下一秒的时间为:";

  t2.display();

  t1+=t2;

  cout<<"t1+=t2"<<"为:";

  t1.display();

  t2-=t1;

  cout<<"t2-=t1"<<"为:";

  t2.display();

  int m=10;

  t1+=m;

  cout<<"t1+=m"<<"为:";

  t1.display();

  t1-=t2;

  cout<<"t1-=t2"<<"为:";

  t1.display();

  system("pause");

}

抱歉!评论已关闭.