有一个时间类(Time),成员变量有:小时(hours)、分钟(minutes),补充 Time 类的代码,重载加号运算符,使得程序能够正确运行。
键盘输入两个正整数,分别为小时 h 和分钟 m。要求分钟 m 范围为 0 - 59
输出两个 Time 对象(t1 和 t2)相加后的时间结果,通过调用 show() 输出。
1 10
3 30
2 50
5 10
#include <iostream> using namespace std; class Time { public: int hours; // 小时 int minutes; // 分钟 Time() { hours = 0; minutes = 0; } Time(int h, int m) { this->hours = h; this->minutes = m; } void show() { cout << hours << " " << minutes << endl; } // write your code here...... Time operator+(Time &t){ Time res; res.minutes = this -> minutes + t.minutes; res.hours = this -> hours + t.hours + res.minutes/60; res.minutes %= 60; return res; } }; int main() { int h, m; cin >> h; cin >> m; Time t1(h, m); Time t2(2, 20); Time t3 = t1 + t2; t3.show(); return 0; }
#include <iostream> using namespace std; class Time { public: int hours; // 小时 int minutes; // 分钟 Time() { hours = 0; minutes = 0; } Time(int h, int m) { this->hours = h; this->minutes = m; } void show() { cout << hours << " " << minutes << endl; } // write your code here...... Time operator+(Time &t){ int temp = t.minutes + minutes; int h = temp / 60; this->hours += t.hours + h; this->minutes = temp % 60; return *this; } }; int main() { int h, m; cin >> h; cin >> m; Time t1(h, m); Time t2(2, 20); Time t3 = t1 + t2; t3.show(); return 0; }
#include <iostream> using namespace std; class Time { public: int hours; // 小时 int minutes; // 分钟 Time() { hours = 0; minutes = 0; } Time(int h, int m) { this->hours = h; this->minutes = m; } void show() { cout << hours << " " << minutes << endl; } // write your code here...... Time operator+(const Time& t) { Time answer(this->hours + t.hours, this->minutes + t.minutes); while (answer.minutes >= 60) { answer.minutes -= 60; answer.hours++; } answer.hours %= 24; return answer; } }; int main() { int h, m; cin >> h; cin >> m; Time t1(h, m); Time t2(2, 20); Time t3 = t1 + t2; t3.show(); return 0; }
#include <iostream> using namespace std; class Time { public: int hours; // 小时 int minutes; // 分钟 Time() { hours = 0; minutes = 0; } Time(int h, int m) { this->hours = h; this->minutes = m; } void show() { cout << hours << " " << minutes << endl; } // write your code here...... Time& operator + (Time t2) { hours = hours + t2.hours; minutes = minutes + t2.minutes; if (minutes > 59) { minutes = minutes - 60; hours = hours + 1; } return *this; } }; int main() { int h, m; cin >> h; cin >> m; Time t1(h, m); Time t2(2, 20); Time t3 = t1 + t2; t3.show(); return 0; }
#include <iostream> using namespace std; class Time { public: int hours; // 小时 int minutes; // 分钟 Time() { hours = 0; minutes = 0; } Time(int h, int m) { this->hours = h; this->minutes = m; } void show() { cout << hours << " " << minutes << endl; } // write your code here...... Time operator+(Time &t) { int minutes1=this->minutes + t.minutes; int hours1=this->hours+t.hours; while(minutes1>=60) { minutes1=minutes1-60; hours1++; } return Time(hours1,minutes1); } }; int main() { int h, m; cin >> h; cin >> m; Time t1(h, m); Time t2(2, 20); Time t3 = t1 + t2; t3.show(); return 0; }
#include<iostream> using namespace std; #include <string> class Time { private: int hours, minutes; public: Time() { this->hours = 0; this->minutes = 0; } Time(int hours, int minutes) { this->hours = hours; this->minutes = minutes; } Time operator+ (Time& t) { Time temp; if (this->minutes+t.minutes >= 60) { temp.minutes = this->minutes + t.minutes - 60; temp.hours = this->hours + t.hours + 1; return temp; } else { temp.minutes = this->minutes + t.minutes; temp.hours = this->hours + t.hours; return temp; } } void showTime() { cout << this->hours << " " << this->minutes << endl; } }; void test01() { int h, m; cin >> h >> m; Time t1(h, m); Time t2(2, 20); Time t3 = t1 + t2; t3.showTime(); } int main() { test01(); return 0; }
#include <iostream> using namespace std; class Time { int hours; int minutes; public: Time(int hours, int minutes): hours(hours), minutes(minutes) {}; const Time operator+(const Time& t) { int tminutes = this->minutes + t.minutes; int thours = this->hours + t.hours; if (tminutes >= 60) { thours++; tminutes -= 60; } return Time(thours, tminutes); } friend void prHM(const Time& t) { cout << t.hours << " " << t.minutes; } }; int main() { int hours; int minutes; cin >> hours >> minutes; Time t1(hours, minutes); /*还得猜测给定的是2小时20分*/ Time t2(2, 20); prHM(t1 + t2); } // 64 位输出请用 printf("%lld")
#include <iostream> using namespace std; class Time{ private: int hours; int minutes; public: void show(); Time(); void set_Time(int h, int m); void set_Time(Time &t1, Time &t2); }; Time::Time(){ //构造函数,参数初始化固定 this->hours = 2; this->minutes = 20; } void Time::set_Time(int h , int m){ //设置参数 this->hours = h; this->minutes = m; } void Time::set_Time(Time &t1, Time &t2){ //重载,设置参数 this->hours = t1.hours + t2.hours; this->minutes = t1.minutes + t2.minutes; if(this->minutes >= 60){ this->hours ++; this->minutes -= 60; } } void Time::show(){ //打印 cout << this->hours << " " << this->minutes << endl; } int main() { int h, m; Time t1, t2, t3; cin >> h >> m; t2.set_Time(h, m); t3.set_Time(t1, t2); t3.show(); }