编写一个时间类,实现时间对象的加、减运算并输出。求大神解救,到底哪里错了啊?
#include"istream.h"classtime{private:inthour,minute,second;public:time(inth=0,intm=0,...
#include "istream.h"
class time
{
private :
int hour ,minute,second;
public :
time(int h=0,int m=0,int s=0)
{
hour=h; minute=m; second=s;
}
void settime();
void display();
time operator +(time&);
time operator -(time&);
};
void time :: settime ()
{
cin>>hour>>minute>>second;
if(hour>24||hour<0||minute>60||minute<0||second>60||second<0)
cout<<"error"<<endl;
else cin>>hour>>minute>>second;
}
void time:: display()
{
cout<<hour<<":"<<minute<<":"<<second;
}
time time::operator +(time &a)
{
int h,m,s;
h=(a.hour+(minute+a.minute+(a.second+second)/60)/60)24;
m=(a.minute+(a.second+second)/60)%60;
s=(a.second+second)%60;
return time(h,m,s);
}
void mian()
{
time a,b,c;
cout<<"请输入a的时间"<<endl;
a.settime();
cout<<"请输入b的时间"<<endl;
b.settime();
cout<<"a的时间为:"<<endl;
a.display();
cout<<"b的时间为:"<<endl;
b.display();
c=a+b;
c.display();
} 展开
class time
{
private :
int hour ,minute,second;
public :
time(int h=0,int m=0,int s=0)
{
hour=h; minute=m; second=s;
}
void settime();
void display();
time operator +(time&);
time operator -(time&);
};
void time :: settime ()
{
cin>>hour>>minute>>second;
if(hour>24||hour<0||minute>60||minute<0||second>60||second<0)
cout<<"error"<<endl;
else cin>>hour>>minute>>second;
}
void time:: display()
{
cout<<hour<<":"<<minute<<":"<<second;
}
time time::operator +(time &a)
{
int h,m,s;
h=(a.hour+(minute+a.minute+(a.second+second)/60)/60)24;
m=(a.minute+(a.second+second)/60)%60;
s=(a.second+second)%60;
return time(h,m,s);
}
void mian()
{
time a,b,c;
cout<<"请输入a的时间"<<endl;
a.settime();
cout<<"请输入b的时间"<<endl;
b.settime();
cout<<"a的时间为:"<<endl;
a.display();
cout<<"b的时间为:"<<endl;
b.display();
c=a+b;
c.display();
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询