Ospverload
Ospverload
md 2025-01-06
#include <iostream>
#include <cmath>
// 有理数类
class Rational {
private:
int numerator; // 分⼦
int denominator; // 分⺟
public:
// 默认构造函数,创建分⼦为 0,分⺟为 1 的有理数 (即 0/1)
Rational() : numerator(0), denominator(1) {}
// 构造函数,创建指定分⼦和分⺟的有理数,并进⾏约分
Rational(int num, int den) : numerator(num), denominator(den) {
if (den == 0) {
cerr << "错误: 分⺟不能为零。" << endl;
numerator = 0;
denominator = 1;
} else {
int gcd = findGCD(abs(num), abs(den)); // 计算最⼤公约数,进⾏约分
numerator /= gcd;
denominator /= gcd;
}
}
// 计算最⼤公约数(辗转相除法)
int findGCD(int a, int b) {
if (b == 0) {
return a;
} else {
return findGCD(b, a % b);
}
}
// 重载 + 运算符,实现有理数与双精度数的加法
friend Rational operator+(const Rational& r, double d) {
// 将双精度数转换为有理数形式,⽅便计算
return Rational(r.numerator + d * r.denominator, r.denominator);
}
// 重载 - 运算符,实现有理数与双精度数的减法
friend Rational operator-(const Rational& r, double d) {
return Rational(r.numerator - d * r.denominator, r.denominator);
}
1/4
ospverload.md 2025-01-06
// 重载 * 运算符,实现有理数与双精度数的乘法
friend Rational operator*(const Rational& r, double d) {
return Rational(r.numerator * d, r.denominator);
}
// 重载 / 运算符,实现有理数与双精度数的除法
friend Rational operator/(const Rational& r, double d) {
if (d == 0) {
cerr << "错误: 除数不能为零。" << endl;
return Rational(0, 1);
}
return Rational(r.numerator, r.denominator * d);
}
// 重载 << 运算符,⽤于输出有理数对象
friend ostream& operator<<(ostream& os, const Rational& r) {
os << r.numerator << "/" << r.denominator;
return os;
}
// 重载 >> 运算符,⽤于输入有理数对象
friend istream& operator>>(istream& is, Rational& r) {
char c;
is >> r.numerator >> c >> r.denominator;
return is;
}
// 显⽰有理数
void Display() const {
cout << numerator << "/" << denominator << endl;
}
};
// 复数类
class Complex {
private:
double real; // 实部
double imag; // 虚部
2/4
ospverload.md 2025-01-06
public:
// 默认构造函数,创建实部为 0,虚部为 0 的复数
Complex() : real(0), imag(0) {}
// 构造函数,创建指定实部和虚部的复数
Complex(double r, double i) : real(r), imag(i) {}
// 重载 + 运算符,实现复数与双精度数的加法
friend Complex operator+(const Complex& c, double d) {
return Complex(c.real + d, c.imag);
}
// 重载 - 运算符,实现复数与双精度数的减法
friend Complex operator-(const Complex& c, double d) {
return Complex(c.real - d, c.imag);
}
// 重载 * 运算符,实现复数与双精度数的乘法
friend Complex operator*(const Complex& c, double d) {
return Complex(c.real * d, c.imag * d);
}
// 重载 / 运算符,实现复数与双精度数的除法
friend Complex operator/(const Complex& c, double d) {
if (d == 0) {
cerr << "错误: 除数不能为零。" << endl;
return Complex(0, 0);
}
return Complex(c.real / d, c.imag / d);
}
// 重载 << 运算符,⽤于输出复数对象
3/4
ospverload.md 2025-01-06
// 重载 >> 运算符,⽤于输入复数对象
friend istream& operator>>(istream& is, Complex& c) {
char sign;
is >> c.real >> sign >> c.imag;
if (sign == '-') {
c.imag = -c.imag;
}
return is;
}
// 显⽰复数
void Display() const {
cout << real << (imag >= 0 ? "+" : "") << imag << "i" << endl;
}
};
int main() {
// 测试有理数运算
Rational r1(1, 2);
cout << "r1: " << r1 << endl;
cout << "r1 + 3.5 = " << r1 + 3.5 << endl;
cout << "3.5 + r1 = " << 3.5 + r1 << endl;
cout << "r1 - 3.5 = " << r1 - 3.5 << endl;
cout << "3.5 - r1 = " << 3.5 - r1 << endl;
cout << "r1 * 3.5 = " << r1 * 3.5 << endl;
cout << "3.5 * r1 = " << 3.5 * r1 << endl;
cout << "r1 / 3.5 = " << r1 / 3.5 << endl;
cout << "3.5 / r1 = " << 3.5 / r1 << endl;
// 测试复数运算
Complex c1(2, 3);
cout << "c1: " << c1 << endl;
cout << "c1 + 3.5 = " << c1 + 3.5 << endl;
cout << "3.5 + c1 = " << 3.5 + c1 << endl;
cout <
```
4/4