题目1、
6-1 时间相加
(给出题目描述)
代码清单:
#include <iostream>
using namespace std;
class Time {
private:
int hours,minutes, seconds;
public:
Time(int h=0, int m=0, int s=0);
Time operator + (Time &);
void DispTime();
};
Time::Time(int h, int m, int s)
{
this->hours = h;
this->minutes = m;
this->seconds = s;
}
Time Time::operator+(Time &t)
{
Time tm4;
int a = 0, b = 0;
tm4.seconds = (this->seconds + t.seconds)%60;
a = (this->seconds + t.seconds) / 60;
tm4.minutes = (this->minutes + t.minutes + a) % 60;
b= (this->minutes + t.minutes + a) / 60;
tm4.hours = this->hours + t.hours + b;
return tm4;
}
void Time::DispTime()
{
cout << hours << "h:" << minutes << "m:" << seconds << "s"<<endl;
}
/* 请在这里填写答案 */
int main() {
Time tm1(8,75,50),tm2(0,6,16), tm3;
tm3=tm1+tm2;
tm3.DispTime();
return 0;
}
运行结果截图
题目2
(给出题目描述)
7-1 复数类的运算
代码清单:
…………………………..
………………………….
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double r = 0, double i = 0) :real(r), imag(i) { }
Complex operator+(Complex &c) const;//重载双目运算符'+'
Complex operator-=(Complex &c ); //重载双目运算符'-='
friend Complex operator-(Complex& c,Complex &cc);//重载双目运算符'-'
void Display() const;
private:
double real;
double imag;
};
Complex Complex::operator+(Complex& c)const
{
Complex c4;
c4.real = this->real + c.real;
c4.imag = this->imag + c.imag;
return c4;
}
Complex Complex::operator-=(Complex& c)
{
this->real -= c.real;
this->imag -= c.imag;
return *this;
}
Complex operator-(Complex& c,Complex &cc)
{
Complex c4;
c4.real = c.real - cc.real;
c4.imag = c.imag - cc.imag;
return c4;
}
void Complex::Display() const
{
cout << "(" << real << ", " << imag << ")" << endl;
}
int main()
{
double r, m;
cin >> r >> m;
Complex c1(r, m);
cin >> r >> m;
Complex c2(r, m);
Complex c3 = c1 + c2;
c3.Display();
c3 = c1 - c2;
c3.Display();
c3 -= c1;
c3.Display();
return 0;
}
运行结果截图
题目3
(给出题目描述)
7-2 日程安排(多重继承+重载)
代码清单:
…………………………..
………………………….
#include<iostream>
using namespace std;
class Date{
protected:
int year;
int month;
int day;
public:
Date(int a,int b,int c):year(a),month(b),day(c){};
};
class Time{
protected:
int hour;
int minute;
int second;
public:
Time(int a,int b,int c):hour(a),minute(b),second(c){};
};
class Schedule:public Date,public Time{
private:
int ID;
public:
Schedule(int a=0,int b=0,int c=0,int d=0,int e=0,int f=0,int g=0):ID(a),Date(b,c,d),Time(e,f,g){};
bool operator <(const Schedule &s2){
if(year<s2.year){
return true;
}else if(year>s2.year){
return false;
}else if(month<s2.month){
return true;
}else if(month>s2.month){
return false;
}else if(day<s2.day){
return true;
}else if(day>s2.day){
return false;
}else if(hour<s2.hour){
return true;
}else if(hour>s2.hour){
return false;
}else if(minute<s2.minute){
return true;
}else if(minute>s2.minute){
return false;
}else if(second<s2.second){
return true;
}else if(second>s2.second){
return false;
}else{
return true;
}
};
void display(){
cout<<"The urgent schedule is No."<<ID<<": "<<year<<"/"<<month<<"/"<<day<<" "<<hour<<":"<<minute<<":"<<second;
}
};
int main(){
Schedule d1,d2;
int a,b,c,d,e,f,g;
char ch;
cin>>a>>b>>ch>>c>>ch>>d>>e>>ch>>f>>ch>>g;
d1=Schedule(a,b,c,d,e,f,g);
cin>>a;
while(a){
cin>>b>>ch>>c>>ch>>d>>e>>ch>>f>>ch>>g;
d2=Schedule(a,b,c,d,e,f,g);
if(d2.operator<(d1)){
d1=d2;
}
cin>>a;
}
d1.display();
return 0;
}
运行结果截图