假期2.7
一、填空题
1、在下列程序的空格处填上适当的字句,使输出为:0,2,10。
#include <iostream>
#include <math.h>
class Magic
{double x;
public:
Magic(double d=0.00):x(fabs(d))
{}
Magic operator+(_ _const Magic& c_
_____)
{
return Magic(sqrt(x*x+c.x*c.x));
}
___ friend std::ostream&
____operator<<(ostream & stream,const Magic & c)
{ stream<<c.x;
return stream;
}
};
int main()
{Magic ma;
cout<<ma<<", "<<Magic(2)<<", "<<ma+Magic(-6)+
Magic(-8)<<endl;
}
二、编程题
1、 定义复数类的加法与减法,使之能够执行下列运算:
Complex a(2,5),b(7,8),c(0,0);
c=a+b;
c=4.1+a;
c=b+5.6;
#include <iostream>
using namespace std;
class Complex {
public:
Complex(double real = 0.0, double imaginary = 0.0) {
this->real = real;
this->imaginary = imaginary;
}
Complex operator+(const Complex& other) const {
double resultReal = real + other.real;
double resultImaginary = imaginary + other.imaginary;
return Complex(resultReal, resultImaginary);
}
Complex operator-(const Complex& other) const {
double resultReal = real - other.real;
double resultImaginary = imaginary - other.imaginary;
return Complex(resultReal, resultImaginary);
}
friend Complex operator+(double num, const Complex& complex) {
double resultReal = num + complex.real;
double resultImaginary = complex.imaginary;
return Complex(resultReal, resultImaginary);
}
friend Complex operator+(const Complex& complex, double num) {
return num + complex;
}
friend Complex operator-(double num, const Complex& complex) {
double resultReal = num - complex.real;
double resultImaginary = -complex.imaginary;
return Complex(resultReal, resultImaginary);
}
friend Complex operator-(const Complex& complex, double num) {
return -num + complex;
}
void print() const {
cout << real << " + " << imaginary << "i" << endl;
}
private:
double real;
double imaginary;
};
int main() {
Complex a(2, 5);
Complex b(7, 8);
Complex c(0, 0);
c = a + b;
c.print();
c = 4.1 + a;
c.print();
c = b + 5.6;
c.print();
return 0;
}
2、 编写一个时间类,实现时间的加、减、读和输出。
#include <iostream>
using namespace std;
class Time {
public:
Time(int h = 0, int m = 0, int s = 0) {
hours = h;
minutes = m;
seconds = s;
}
void setTime(int h, int m, int s) {
hours = h;
minutes = m;
seconds = s;
}
void getTime(int& h, int& m, int& s) const {
h = hours;
m = minutes;
s = seconds;
}
void printTime() const {
cout << hours << ":" << minutes << ":" << seconds << endl;
}
Time operator+(const Time& other) const {
int totalSeconds = seconds + other.seconds;
int carryMinutes = totalSeconds / 60;
int remainingSeconds = totalSeconds % 60;
int totalMinutes = minutes + other.minutes + carryMinutes;
int carryHours = totalMinutes / 60;
int remainingMinutes = totalMinutes % 60;
int totalHours = hours + other.hours + carryHours;
return Time(totalHours, remainingMinutes, remainingSeconds);
}
Time operator-(const Time& other) const {
int totalSeconds = seconds - other.seconds;
int borrowMinutes = 0;
if (totalSeconds < 0) {
totalSeconds += 60;
borrowMinutes = 1;
}
int totalMinutes = minutes - other.minutes - borrowMinutes;
int borrowHours = 0;
if (totalMinutes < 0) {
totalMinutes += 60;
borrowHours = 1;
}
int totalHours = hours - other.hours - borrowHours;
return Time(totalHours, totalMinutes, totalSeconds);
}
private:
int hours;
int minutes;
int seconds;
};
int main() {
Time t1(10, 30, 45);
Time t2(2, 15, 20);
Time sum = t1 + t2;
Time diff = t1 - t2;
cout << "t1: ";
t1.printTime();
cout << "t2: ";
t2.printTime();
cout << "Sum: ";
sum.printTime();
cout << "Difference: ";
diff.printTime();
return 0;
}
3、 增加操作符,以允许人民币与double型数相乘。
friend money operator*(const money&,double);
friend money operator*(double,const money&);
注意:两个money对象不允许相乘。
#include <iostream>
using namespace std;
class Money {
private:
double amount;
public:
Money(double amt = 0.0) : amount(amt) {}
friend Money operator*(const Money& money, double num) {
return Money(money.amount * num);
}
friend Money operator*(double num, const Money& money) {
return Money(money.amount * num);