week18
week18
week18
md 2025-01-05
12
1.
#include <iostream>
class Complex {
public:
Complex(float x = 0, float y = 0) : x(x), y(y) {} // 使⽤成员初始化列表
private:
float x;
float y;
};
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
return 0;
}
2.
#include <iostream>
class Complex {
private:
float x;
float y;
public:
Complex(float x = 0, float y = 0) : x(x), y(y) {} // 使⽤成员初始化列表
int main() {
2 / 19
week18.md 2025-01-05
Complex d = b + 5.6;
d.Display();
Complex e = 4.1 + a;
e.Display();
return 0;
}
3.
#include <iostream>
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
// 显⽰分数
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>
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
// 拷⻉构造函数
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 p3 = p1 + p2;
cout << "p1 + p2: ";
p3.display();
return 0;
}
13
1.
#include <iostream>
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; // 使⽤重载的 + 运算符直接计算两个分数的和
return 0;
}
2.
#include <iostream>
#include <cmath>
class Point {
public:
// 默认构造函数,使⽤成员初始化列表初始化坐标为 (0, 0)
Point() : x(0), y(0) {}
// 带参数的构造函数,使⽤成员初始化列表初始化坐标
Point(double x, double y) : x(x), y(y) {}
private:
double x, y; // 存储点的坐标
};
// 带参数的构造函数,使⽤成员初始化列表初始化基类和 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
return 0;
}
3.
#include <iostream>
#include <string>
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; // 员⼯年龄
};
protected:
int sales; // 销售业绩
};
protected:
int members; // 管理⼈数
};
int main() {
SaleManager x("张三", 32, 128, 8);
x.Display(); // 显⽰销售经理的信息
return 0;
}
14
1.
//26-蔚元利-10231510082
#include <iostream>
#include <string>
class Integer {
protected:
int x;
public:
Integer() : x(0) {} // ⽆参构造函数,初始化 x 为 0
10 / 19
week18.md 2025-01-05
public:
Rational() : Integer(), y(1) {} // ⽆参构造函数,初始化 x 为 0,y 为 1
Rational(int x, int y) : Integer(x), y(y) {} // 有参构造函数
public:
Complex() : Integer(), y(0) {} // ⽆参构造函数,初始化 x 为 0,y 为 0
Complex(int x, int y) : Integer(x), y(y) {} // 有参构造函数
int main() {
Rational x(9, 19);
Complex z(3, -8);
ptr1->Display(); // 输出有理数
ptr2->Display(); // 输出复数
return 0;
}
2.
//26-蔚元利-10231510082
#include <iostream>
#include <cmath>
11 / 19
week18.md 2025-01-05
class Point2D {
public:
double x, y;
class Point3D {
public:
double x, y, z;
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>
// 构造函数在类内定义
12 / 19
week18.md 2025-01-05
// 成员函数在类外定义
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);
}
int main() {
Complex<float> z1(2.1, 5.3), z2(1.9, -2.3);
Complex<int> a1(2, 5), a2(1, -2);
return 0;
}
15
1.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
13 / 19
week18.md 2025-01-05
int main() {
// 设置随机数种⼦
srand(time(0));
// 随机⽣成矩阵 A 的元素
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
A[i][j] = (double)rand() / RAND_MAX; // ⽣成 [0, 1] 之间的随机数
}
}
fout_binary.write((char*)A, sizeof(A));
fout_binary.close();
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>
// 复数类
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;
return 0;
}
3.
#include <iostream>
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
}
}
// 赋值运算符重载,实现深度拷⻉
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];
}
// 析构函数,释放动态分配的内存
~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
}
}
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
return 0;
}
4.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <random>
#include <string>
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();
return 0;
}```
19 / 19