0% found this document useful (0 votes)
2 views19 pages

week18

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

week18.

md 2025-01-05

12
1.

#include <iostream>

using namespace std;

class Complex {
public:
Complex(float x = 0, float y = 0) : x(x), y(y) {} // 使⽤成员初始化列表

void Display() const { // 使⽤ const 修饰符,表⽰函数不修改对象


cout << x << (y >= 0 ? "+" : "") << y << "i" << endl;
}

Complex operator+=(const Complex& c) { // 使⽤ const 引⽤提⾼效率


x += c.x;
y += c.y;
return *this;
}

friend Complex operator/(const Complex& c1, const Complex& c2); // 使⽤ const


引⽤

private:
float x;
float y;
};

Complex operator/(const Complex& c1, const Complex& c2) {


float abs = c2.x * c2.x + c2.y * c2.y;
if (abs == 0) { // 处理除数为 0 的情况
cerr << "Error: Division by zero." << endl;
return Complex(0, 0);
}
float a = (c1.x * c2.x + c1.y * c2.y) / abs;
float b = (c1.y * c2.x - c1.x * c2.y) / abs;
return Complex(a, b);
}

int main() {
Complex z1(2.1, -5.4), z2(7.5, 3.2);
Complex z3 = z2 / z1;
z3.Display();

z3 += z1;
z3.Display();

// 额外测试:创建并输出⼀个复数

1 / 19
week18.md 2025-01-05

Complex z4(1.5, -2.0);


cout << "z4 = " << z4 << endl;

return 0;
}

2.

#include <iostream>

using namespace std;

class Complex {
private:
float x;
float y;

public:
Complex(float x = 0, float y = 0) : x(x), y(y) {} // 使⽤成员初始化列表

Complex operator+(const Complex& c) const {


return Complex(x + c.x, y + c.y);
}

Complex operator=(const Complex& c) { // 赋值运算符重载


x = c.x;
y = c.y;
return *this;
}

Complex operator=(float r) { // 赋值运算符重载(将实数赋值给复数)


x = r;
y = 0;
return *this;
}

Complex operator+(float r) const {


return Complex(x + r, y);
}

friend Complex operator+(float r, const Complex& c);

void Display() const {


cout << x << (y >= 0 ? "+" : "") << y << "i" << endl;
}
};

Complex operator+(float r, const Complex& c) {


return Complex(r + c.x, c.y);
}

int main() {
2 / 19
week18.md 2025-01-05

Complex a(2.1, 5.7), b(7.5, 8);


Complex c = a + b;
c.Display();

c = 3.5; // 将实数 3.5 赋值给复数 c


c.Display();

Complex d = b + 5.6;
d.Display();

Complex e = 4.1 + a;
e.Display();

return 0;
}

3.

#include <iostream>

using namespace std;

class Rational {
public:
// 构造函数,初始化分⼦和分⺟,并进⾏约分
Rational(int x = 0, int y = 1) {
if (y == 0) {
cerr << "Error: 分⺟不能为零。" << endl;
x = 0;
y = 1;
} else {
int gcd = findGCD(x, y);
this->x = x / gcd;
this->y = y / gcd;
}
}

// 重载⼤于运算符
bool operator>(const Rational& r) const {
// 比较两个分数的⼤⼩
return x * r.y > y * r.x;
}

// 重载等于运算符
bool operator==(const Rational& r) const {
// 判断两个分数是否相等
return x * r.y == y * r.x;
}

// 重载⼩于运算符
bool operator<(const Rational& r) const {
// 判断两个分数的⼤⼩
3 / 19
week18.md 2025-01-05

return x * r.y < y * r.x;


}

// 显⽰分数
void Display() const {
cout << x << "/" << y << endl;
}

private:
int x; // 分⼦
int y; // 分⺟

// 计算最⼤公约数(辗转相除法)
int findGCD(int a, int b) {
if (b == 0) {
return a;
}
return findGCD(b, a % b);
}
};

int main() {
Rational a(2, 3);
Rational b(4, 5);

cout << "a > b ? " << (a > b ? "true" : "false") << endl;
cout << "a == b ? " << (a == b ? "true" : "false") << endl;
cout << "a < b ? " << (a < b ? "true" : "false") << endl;

return 0;
}

4.

#include <iostream>

using namespace std;

class PolyInt {
private:
int* a; // 指向系数数组的指针
int n; // 多项式的次数

public:
// 默认构造函数,创建零多项式
PolyInt() : n(0) {
a = new int[1];
a[0] = 0;
}

// 构造函数,根据系数数组和次数创建多项式
PolyInt(int* b, int c) : n(c) {
4 / 19
week18.md 2025-01-05

a = new int[n + 1];


for (int i = 0; i <= n; ++i) {
a[i] = b[i];
}
}

// 拷⻉构造函数
PolyInt(const PolyInt& other) : n(other.n) {
a = new int[n + 1];
for (int i = 0; i <= n; ++i) {
a[i] = other.a[i];
}
}

// 赋值运算符重载,实现深拷⻉
PolyInt& operator=(const PolyInt& other) {
if (this != &other) { // 防⽌⾃赋值
delete[] a; // 释放原有的内存
n = other.n;
a = new int[n + 1];
for (int i = 0; i <= n; ++i) {
a[i] = other.a[i];
}
}
return *this;
}

// 多项式加法
PolyInt operator+(const PolyInt& other) const {
int max_degree = max(n, other.n);
PolyInt result(max_degree);
for (int i = 0; i <= n; ++i) {
result.a[i] += a[i];
}
for (int i = 0; i <= other.n; ++i) {
result.a[i] += other.a[i];
}
return result;
}

// 显⽰多项式
void display() const {
for (int i = n; i >= 0; --i) {
if (a[i] != 0) {
if (i < n && a[i] > 0) {
cout << " + ";
} else if (i < n && a[i] < 0) {
cout << " - ";
}
if (abs(a[i]) != 1 || i == 0) {
cout << abs(a[i]);
}
if (i > 1) {
cout << "x^" << i;
5 / 19
week18.md 2025-01-05

} else if (i == 1) {
cout << "x";
}
}
}
cout << endl;
}

// 析构函数
~PolyInt() {
delete[] a;
}

private:
int* a; // 指向系数数组的指针
int n; // 多项式的次数
};

int main() {
int arr1[] = {1, 0, 2, 4};
int arr2[] = {2, 0, 4, 3, 0, 5};
int size1 = sizeof(arr1) / sizeof(arr1[0]) - 1;
int size2 = sizeof(arr2) / sizeof(arr2[0]) - 1;

PolyInt p1(arr1, size1);


PolyInt p2(arr2, size2);

cout << "p1: ";


p1.display();
cout << "p2: ";
p2.display();

PolyInt p3 = p1 + p2;
cout << "p1 + p2: ";
p3.display();

return 0;
}

13
1.

#include <iostream>

using namespace std;

// 计算最⼤公约数 (Greatest Common Divisor) 的函数


int gcd(int a, int b) {
int c;

6 / 19
week18.md 2025-01-05

c = (a > b) ? a % b : b % a; // 计算 a 和 b 中较⼤的数对较⼩的数取余
while (c != 0) {
// 辗转相除法:将较⼤的数替换为较⼩的数,较⼩的数替换为余数,重复直到余数为 0
a = b;
b = c;
c = (a > b) ? b % a : a % b;
}
return b; // 返回最⼤公约数
}

// 整数类
class Integer {
public:
// 构造函数,默认值为 0
Integer(int a = 0) : a(a) {}

// 获取整数的值
int getA() const {
return a;
}

private:
int a; // 存储整数的值
};

// 分数类,继承⾃整数类
class Fraction : public Integer {
public:
// 构造函数,默认分⺟为 1
Fraction(int a = 0, int b = 1) : Integer(a), b(b) {}

// 显⽰分数
void Display() {
cout << a << "/" << b << endl;
}

protected:
int b; // 分⺟

// 重载 + 运算符,实现分数相加
friend Fraction operator+(const Fraction& x, const Fraction& y) {
int gcd_val = gcd(x.b, y.b); // 计算两个分⺟的最⼤公约数
int lcm = (x.b * y.b) / gcd_val; // 计算两个分⺟的最⼩公倍数
int num = (x.getA() * (lcm / x.b)) + (y.getA() * (lcm / y.b)); // 计算分⼦
return Fraction(num, lcm); // 返回相加后的分数
}
};

int main() {
Fraction x(2, 3), y(-7, 5);
Fraction z = x + y; // 使⽤重载的 + 运算符直接计算两个分数的和

cout << "z = ";


z.Display(); // 显⽰结果分数
7 / 19
week18.md 2025-01-05

return 0;
}

2.

#include <iostream>
#include <cmath>

using namespace std;

class Point {
public:
// 默认构造函数,使⽤成员初始化列表初始化坐标为 (0, 0)
Point() : x(0), y(0) {}

// 带参数的构造函数,使⽤成员初始化列表初始化坐标
Point(double x, double y) : x(x), y(y) {}

// 计算与另⼀个点的距离,声明为 const 函数,表⽰不修改对象的状态


double dist(const Point& p) const {
double xx = p.x - this->x;
double yy = p.y - this->y;
return sqrt(xx * xx + yy * yy);
}

private:
double x, y; // 存储点的坐标
};

class Point3D : public Point {


public:
// 默认构造函数,使⽤成员初始化列表初始化基类和 z 坐标
Point3D() : Point(), z(0) {}

// 带参数的构造函数,使⽤成员初始化列表初始化基类和 z 坐标
Point3D(double x, double y, double z) : Point(x, y), z(z) {}

// 计算与另⼀个三维点的距离,声明为 const 函数
double dist(const Point3D& p) const {
double xx = p.x - this->x;
double yy = p.y - this->y;
double zz = p.z - this->z;
return sqrt(xx * xx + yy * yy + zz * zz);
}

private:
double z; // 存储 z 坐标
};

int main() {
Point A1, A2(4, 5.6);
8 / 19
week18.md 2025-01-05

Point3D B1, B2(4, 5.6, 7.8);

cout << "|A1-A2|=" << A1.dist(A2) << endl;


cout << "|B1-B2|=" << B2.dist(B1) << endl; // 调⽤ B2 的 dist() ⽅法计算距离

return 0;
}

3.

#include <iostream>
#include <string>

using namespace std;

class Employee {
public:
// 默认构造函数,初始化姓名为空字符串,年龄为 0
Employee() : name(""), age(0) {}

// 构造函数,初始化姓名和年龄
Employee(const string& name, int age) : name(name), age(age) {}

// 虚拟函数,⽤于显⽰员⼯信息
virtual void Display() const {
cout << "姓名: " << name << ", 年龄: " << age << endl;
}

protected:
string name; // 员⼯姓名
int age; // 员⼯年龄
};

class Saleman : virtual public Employee {


public:
// 构造函数,初始化姓名、年龄和销售业绩
Saleman(const string& name, int age, int sales)
: Employee(name, age), sales(sales) {}

// 覆盖基类的 Display() 函数,显⽰销售员信息


void Display() const override {
cout << "销售员: 姓名: " << name << ", 年龄: " << age << ", 销售业绩: " <<
sales << endl;
}

protected:
int sales; // 销售业绩
};

class Manager : virtual public Employee {


public:
// 构造函数,初始化姓名、年龄和管理⼈数
9 / 19
week18.md 2025-01-05

Manager(const string& name, int age, int members)


: Employee(name, age), members(members) {}

// 覆盖基类的 Display() 函数,显⽰经理信息


void Display() const override {
cout << "经理: 姓名: " << name << ", 年龄: " << age << ", 管理⼈数: " <<
members << endl;
}

protected:
int members; // 管理⼈数
};

class SaleManager : public Saleman, public Manager {


public:
// 构造函数,初始化所有成员变量
SaleManager(const string& name, int age, int sales, int members)
: Employee(name, age), Saleman(name, age, sales), Manager(name, age,
members) {}

// 覆盖基类的 Display() 函数,显⽰销售经理信息


void Display() const override {
cout << "销售经理: 姓名: " << name << ", 年龄: " << age
<< ", 销售业绩: " << sales << ", 管理⼈数: " << members << endl;
}
};

int main() {
SaleManager x("张三", 32, 128, 8);
x.Display(); // 显⽰销售经理的信息

return 0;
}

14
1.

//26-蔚元利-10231510082
#include <iostream>
#include <string>

using namespace std;

class Integer {
protected:
int x;

public:
Integer() : x(0) {} // ⽆参构造函数,初始化 x 为 0

10 / 19
week18.md 2025-01-05

Integer(int x) : x(x) {} // 有参构造函数,初始化 x

virtual void Display() = 0; // 纯虚函数


};

class Rational : public Integer {


private:
int y;

public:
Rational() : Integer(), y(1) {} // ⽆参构造函数,初始化 x 为 0,y 为 1
Rational(int x, int y) : Integer(x), y(y) {} // 有参构造函数

void Display() override {


cout << x << "/" << y << endl;
}
};

class Complex : public Integer {


private:
int y;

public:
Complex() : Integer(), y(0) {} // ⽆参构造函数,初始化 x 为 0,y 为 0
Complex(int x, int y) : Integer(x), y(y) {} // 有参构造函数

void Display() override {


cout << x << (y >= 0 ? "+" : "-") << abs(y) << "i" << endl;
}
};

int main() {
Rational x(9, 19);
Complex z(3, -8);

Integer *ptr1 = &x;


Integer *ptr2 = &z;

ptr1->Display(); // 输出有理数
ptr2->Display(); // 输出复数

return 0;
}

2.

//26-蔚元利-10231510082
#include <iostream>
#include <cmath>

using namespace std;

11 / 19
week18.md 2025-01-05

class Point2D {
public:
double x, y;

Point2D(double x, double y) : x(x), y(y) {}

double dist(const Point2D& other) const {


return sqrt(pow(x - other.x, 2) + pow(y - other.y, 2));
}
};

class Point3D {
public:
double x, y, z;

Point3D(double x, double y, double z) : x(x), y(y), z(z) {}

double dist(const Point3D& other) const {


return sqrt(pow(x - other.x, 2) + pow(y - other.y, 2) + pow(z - other.z,
2));
}
};

template <typename T>


double mydist(const T& p1, const T& p2) {
return p1.dist(p2);
}

int main() {
Point2D A1(1.2, 3.4), A2(5.6, 7.8);
Point3D B1(1.2, 3.4, 5.6), B2(9.8, 7.6, 5.4);

cout << "Distance between A1 and A2: " << mydist(A1, A2) << endl;
cout << "Distance between B1 and B2: " << mydist(B1, B2) << endl;

return 0;
}

3.

//26-蔚元利-10231510082
#include <iostream>
#include <cmath>

using namespace std;

template <typename T>


class Complex {
public:
T real, imag;

// 构造函数在类内定义
12 / 19
week18.md 2025-01-05

Complex() : real(0), imag(0) {}


Complex(T r, T i) : real(r), imag(i) {}

// 成员函数在类外定义
double Magnitude() const;
Complex operator+(const Complex& other) const;
void Display() const;
};

// 成员函数实现
template <typename T>
double Complex<T>::Magnitude() const {
return sqrt(real * real + imag * imag);
}

template <typename T>


Complex<T> Complex<T>::operator+(const Complex<T>& other) const {
return Complex(real + other.real, imag + other.imag);
}

template <typename T>


void Complex<T>::Display() const {
cout << real << (imag >= 0 ? "+" : "-") << abs(imag) << "i" << endl;
}

int main() {
Complex<float> z1(2.1, 5.3), z2(1.9, -2.3);
Complex<int> a1(2, 5), a2(1, -2);

cout << "z1 + z2 = ";


(z1 + z2).Display();
cout << "Magnitude of z1 + z2: " << (z1 + z2).Magnitude() << endl;

cout << "a1 + a2 = ";


(a1 + a2).Display();
cout << "Magnitude of a1 + a2: " << (a1 + a2).Magnitude() << endl;

return 0;
}

15
1.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

13 / 19
week18.md 2025-01-05

using namespace std;

int main() {
// 设置随机数种⼦
srand(time(0));

// 定义⼀个 6x6 的⼆维数组,⽤于存储矩阵 A


double A[6][6];

// 随机⽣成矩阵 A 的元素
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
A[i][j] = (double)rand() / RAND_MAX; // ⽣成 [0, 1] 之间的随机数
}
}

// (a) 将矩阵 A 写入文本文件 output.txt (追加模式)


fstream fstrm("output.txt", ios::out | ios::app);

for (int i = 0; i < 6; i++) {


for (int j = 0; j < 6; j++) {
fstrm << setw(8) << fixed << setprecision(6) << A[i][j] << " ";
}
fstrm << endl;
}
fstrm.close();

// (b) 将矩阵 A 写入⼆进制文件 fout01.dat


fstream fout_binary("fout01.dat", ios::binary);

fout_binary.write((char*)A, sizeof(A));
fout_binary.close();

// (c) 从⼆进制文件 fout01.dat 读取前 12 个数据,构成矩阵 B


fstream fin_binary("fout01.dat", ios::binary);

double B[2][6];
fin_binary.read((char*)(B), sizeof(B));
fin_binary.close();

// 输出矩阵 B
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 6; j++) {
cout << setw(8) << fixed << setprecision(6) << B[i][j] << " ";
}
cout << endl;
}

return 0;
}

2.

14 / 19
week18.md 2025-01-05

#include <iostream>
#include <complex>

using namespace std;

// 复数类
class Complex {
public:
double real, imag;

// 构造函数
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

// 重载输出运算符 <<
friend ostream& operator<<(ostream& os, const Complex& c) {
os << c.real;
if (c.imag >= 0) {
os << "+";
}
os << c.imag << "i";
return os;
}

// 重载输入运算符 >>
friend istream& operator>>(istream& is, Complex& c) {
char plusOrMinus;
is >> c.real >> plusOrMinus >> c.imag;
if (plusOrMinus == '-') {
c.imag = -c.imag;
}
return is;
}
};

int main() {
Complex c1(3, 4), c2;

cout << "c1 = " << c1 << endl; // 输出:c1 = 3+4i

cout << "请输入⼀个复数(例如 3+4i): ";


cin >> c2;
cout << "c2 = " << c2 << endl;

return 0;
}

3.

#include <iostream>

using namespace std;


15 / 19
week18.md 2025-01-05

class PolyInt {
public:
// 默认构造函数,创建 0 多项式(即常数项为 0)
PolyInt() : n(0) {
// 分配⼀个元素的空间,⽤于存储常数项
a = new int[1];
a[0] = 0;
}

// 构造函数,创建 n 次多项式,所有系数初始化为 0
PolyInt(int n) : n(n) {
// 分配 n+1 个元素的空间,⽤于存储 n 次多项式的系数
a = new int[n + 1];
for (int i = 0; i <= n; ++i) {
a[i] = 0; // 将所有系数初始化为 0
}
}

// 构造函数,根据给定的次数 n 和系数数组 a 创建多项式


PolyInt(int n, int *a) : n(n) {
// 分配 n+1 个元素的空间
this->a = new int[n + 1];
// 复制系数数组到对象内部
for (int i = 0; i <= n; ++i) {
this->a[i] = a[i];
}
}

// 赋值运算符重载,实现深度拷⻉
PolyInt& operator=(const PolyInt& p) {
// 如果是⾃赋值,直接返回⾃⾝
if (this == &p) return *this;

// 释放原有的内存空间
delete[] a;

// 复制次数和系数
n = p.n;
a = new int[n + 1];
for (int i = 0; i <= n; ++i) {
a[i] = p.a[i];
}

return *this; // 返回当前对象


}

// 计算多项式在给定点 x 的值(使⽤ Horner 算法)


int operator()(const int x) {
int result = a[n]; // 从最⾼次项开始计算
for (int i = n - 1; i >= 0; --i) {
result = result * x + a[i]; // Horner 算法⾼效计算多项式值
}
return result;
16 / 19
week18.md 2025-01-05

// 析构函数,释放动态分配的内存
~PolyInt() {
delete[] a;
}

private:
int n; // 多项式的次数
int *a; // 指向系数数组的指针

// 声明友元函数,以便输出运算符可以访问私有成员
friend ostream& operator<<(ostream& out, const PolyInt& p);

// 声明友元函数,以便乘法运算符可以访问私有成员
friend PolyInt operator*(const PolyInt& p, const PolyInt& q);
};

// 输出运算符重载
ostream& operator<<(ostream& out, const PolyInt& p) {
for (int i = p.n; i >= 0; --i) {
if (p.a[i] != 0) { // 仅输出非零项
if (i < p.n) { // 不是最⾼次项
out << ((p.a[i] > 0) ? " + " : " - ");
// 判断系数是否为正,输出相应的符号
if (abs(p.a[i]) != 1 || i == 0) {
// 如果系数绝对值不为 1 或者为常数项,输出系数
out << abs(p.a[i]);
}
} else {
out << p.a[i]; // 输出最⾼次项的系数
}
if (i > 1) {
out << "x^" << i; // 输出 x 的幂次(⼤于 1 次)
} else if (i == 1) {
out << "x"; // 输出 x 的⼀次幂
}
}
}
return out;
}

// 乘法运算符重载
PolyInt operator*(const PolyInt& p, const PolyInt& q) {
int m = p.n + q.n; // 计算乘积多项式的最⾼次数
int *c = new int[m + 1]; // 分配空间存储乘积多项式的系数
for (int i = 0; i <= m; ++i) {
c[i] = 0;
} // 初始化系数数组为 0

// 计算乘积多项式的系数
for (int i = 0; i <= p.n; ++i) {
for (int j = 0; j <= q.n; ++j) {
c[i + j] += p.a[i] * q.a[j];
17 / 19
week18.md 2025-01-05

}
}

return PolyInt(m, c); // 创建并返回乘积多项式对象


}

int main() {
PolyInt p(3, new int[]{1, 2, 0, 4}); // 创建多项式 p(x) = 1 + 2x + 4x^3
PolyInt q(5, new int[]{2, 0, -4, 0, -3, 1}); // 创建多项式 q(x) = 2 - 4x^2 -
3x^3 + x^5

PolyInt s = p * q; // 计算乘积多项式 s(x)

cout << "p(x) = " << p << endl;


cout << "q(x) = " << q << endl;
cout << "s(x) = " << s << endl;

cout << "p(2) = " << p(2) << endl;


cout << "s(2) = " << s(2) << endl;

return 0;
}

4.

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <random>
#include <string>

using namespace std;

int main() {
// 文件名
string filename = "namelist.txt";

// 打开文件,以读取模式打开
ifstream infile(filename);
if (!infile.is_open()) {
cerr << "⽆法打开文件:" << filename << endl;
return 1;
}

// 读取名单到vector中
vector<string> names;
string name;
while (getline(infile, name)) {
names.push_back(name);
}
infile.close();
18 / 19
week18.md 2025-01-05

// 设置奖项⼈数
int N = names.size(); // 总⼈数
int N1 = 1; // ⼀等奖⼈数
int N2 = 5; // ⼆等奖⼈数
int N3 = 10; // 三等奖⼈数

// 验证输入是否合法
if (N3 <= N2 || N2 <= N1 || N3 + N2 + N1 >= N) {
cout << "奖项⼈数设置不合法!请重新输入。" << endl;
return 1;
}

// 随机数引擎
random_device rd;
mt19937 gen(rd());

// 打乱名单顺序
shuffle(names.begin(), names.end(), gen);

// 打开输出文件,以追加模式写入
ofstream outputFile("winners.txt", ios::out | ios::app);
if (!outputFile.is_open()) {
cerr << "⽆法打开输出文件" << endl;
return 1;
}

// 依次抽取奖项并写入文件
outputFile << "⼀等奖:" << endl;
for (int i = 0; i < N1; ++i) {
outputFile << names[i] << endl;
}
outputFile << "⼆等奖:" << endl;
for (int i = N1; i < N1 + N2; ++i) {
outputFile << names[i] << endl;
}
outputFile << "三等奖:" << endl;
for (int i = N1 + N2; i < N1 + N2 + N3; ++i) {
outputFile << names[i] << endl;
}

outputFile.close();

cout << "抽奖结果已保存到 winners.txt 文件中。" << endl;

return 0;
}```

19 / 19

You might also like