打卡第三十四天

计算时间相减
 一、
1.定义一个时间类,小时和分钟是其两个私有成员数据。

2.输入一个起始时间和一个结束时间(起始时间早于结束时间),通过运算符重载-(减号),计算这两个时间相隔多少分钟

二、
三、

#include<iostream>
using namespace std;
class Clock{
private:
int hours;
int minutes;
public:
Clock(int a=0,int b=0):hours(a),minutes(b){};
void get(int ,int );
friend int operator-(const Clock &a,const Clock &b);
};
void Clock::get(int b,int c){
hours=b;
minutes=c;
}
int operator-(const Clock &a,const Clock &b){
int count=0;
count+=(b.hours-a.hours)*60+b.minutes-a.minutes;
return count;
}
int main(){
Clock c1,c2;
int a1,a2,a3,a4;
while(1){
cin>>a1>>a2>>a3>>a4;
if(a1==0&&a2==0&&a3==0&&a4==0){
break;
}
c1.get(a1,a2);
c2.get(a3,a4);
cout<<c1-c2<<endl;
}
return 0;
}

四、

#include<iostream>
using namespace std;
class Clock{
private:
int hours;
int minutes;
public:
Clock(int a=0,int b=0):hours(a),minutes(b){};
void get(int ,int );
friend int operator-(const Clock &a,const Clock &b);
};
void Clock::get(int b,int c){
hours=b;
minutes=c;
}
int operator-(const Clock &a,const Clock &b){
int count=0;
count+=(b.hours-a.hours)*60+b.minutes-a.minutes;
return count;
}
int main(){
Clock c1,c2;
int a1,a2,a3,a4;
while(1){
cin>>a1>>a2>>a3>>a4;
if(a1==0&&a2==0&&a3==0&&a4==0){
break;
}
c1.get(a1,a2);
c2.get(a3,a4);
cout<<c1-c2<<endl;
}
return 0;
}

posted @ 2023-05-25 09:28  欧吼吼  阅读(16)  评论(0编辑  收藏  举报