真题总结
约 7274 字大约 24 分钟
2026-11-09
整理目标:不按年份死记题目,而是把历年题拆成可复用的编程结构。遇到新题时,先识别它属于哪个模块,再套用结构并修改数据成员、计算逻辑和输入输出。
适用范围:目前整理的三套 C++ 手写题及相关数学函数问题。
目录
- 三套试题的整体考点
- 模块 A:判断、循环与数字处理
- 模块 B:数组、排序与矩阵
- 模块 C:函数、递归与数值迭代
- 模块 D:数学函数速查与公式转代码
- 模块 E:类与对象
- 模块 F:友元函数与友元类
- 模块 G:继承
- 模块 H:静态成员
- 模块 I:运算符重载
- 模块 J:文件操作
- 历年原题归类
- 如何整理和练习
- 自测清单
1. 三套试题的整体考点
把题目按核心结构归类,可以整理成以下模块:
| 模块 | 核心知识 | 典型题目 |
|---|---|---|
| A. 判断与循环 | if、for、while、break | 成绩等级、猜数字、质数 |
| B. 数字与数组 | 取位、回文、排序、二维数组 | 回文数、6174 黑洞数、对称矩阵 |
| C. 函数 | 普通函数、递归、返回值 | power()、sinFun() |
| D. 数学计算 | sqrt、fabs、泰勒展开、牛顿迭代 | 平方根、正弦近似 |
| E. 类与对象 | 数据成员、构造函数、默认参数、成员函数 | Tree、Desk、Stock |
| F. 友元 | 友元函数、友元成员函数、友元类 | MyPoint、X/Y1/Y2 |
| G. 继承 | 父类、子类、protected、构造函数初始化列表 | Student/UStudent、Employee/Manager |
| H. 静态成员 | static、类外初始化、对象统计 | Student 的 count/sum/ave |
| I. 运算符重载 | operator+、operator- 等 | Fraction 分数类 |
| J. 文件操作 | fstream、打开、写入、关闭 | 6174 黑洞数写入文件 |
复习原则: 不要把每道原题当成完全独立的题。先掌握每个模块的基本模板,再练习题目条件变化。
2. 模块 A:判断、循环与数字处理
2.1 条件判断
核心模板
if (条件1)
{
// 执行操作1
}
else if (条件2)
{
// 执行操作2
}
else
{
// 其他情况
}原题:考试成绩等级
输入 0~100 分:
- 90~100:A
- 60~89:B
- 0~59:C
#include <iostream>
using namespace std;
int main()
{
int score;
cin >> score;
if (score < 0 || score > 100)
{
cout << "输入错误" << endl;
}
else if (score >= 90)
{
cout << "A" << endl;
}
else if (score >= 60)
{
cout << "B" << endl;
}
else
{
cout << "C" << endl;
}
return 0;
}举一反三
把"成绩等级"换成"年龄分类":
if (age < 18)
cout << "未成年";
else if (age < 60)
cout << "成年人";
else
cout << "老年人";识别方法: 题目出现"根据范围分档""满足不同条件输出不同结果",优先想到 if / else if / else。
2.2 循环结构
for:已知循环次数
for (int i = 0; i < n; i++)
{
// 重复执行
}while:条件满足时重复
while (条件)
{
// 重复执行
}do while:先执行一次,再判断
do
{
// 至少执行一次
} while (条件);break:提前结束循环
if (满足结束条件)
{
break;
}原题对应
| 题目 | 循环结构 |
|---|---|
| 输出 1~100 的质数 | while 或 for,常配合双重循环 |
| 猜 1~100 的数字 | while + if + break |
| 数列求和 | for + 累加 |
| 正弦级数求和 | while + 逐项累加 |
| 牛顿迭代求平方根 | do while 或 while + 误差判断 |
2.3 数字拆分
看到"各位数字""个位、十位、百位""数字反转"等关键词,想到取模和整除。
核心模板
while (num > 0)
{
int digit = num % 10; // 取出个位
// 处理 digit
num = num / 10; // 去掉个位
}| 表达式 | 作用 |
|---|---|
num % 10 | 取最后一位 |
num / 10 | 去掉最后一位(整数除法) |
变式一:各位数字之和
int sum = 0;
while (num > 0)
{
sum += num % 10;
num /= 10;
}变式二:统计位数
int count = 0;
if (num == 0)
count = 1;
while (num > 0)
{
count++;
num /= 10;
}变式三:数字反转
int reverse = 0;
while (num > 0)
{
reverse = reverse * 10 + num % 10;
num /= 10;
}变式四:判断回文数
int original = num;
int reverse = 0;
while (num > 0)
{
reverse = reverse * 10 + num % 10;
num /= 10;
}
if (original == reverse)
cout << "是回文数";
else
cout << "不是回文数";2.4 质数判断
质数:大于 1,且只能被 1 和自身整除的整数。
bool isPrime(int n)
{
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}输出 1~100 的质数:
int main()
{
for (int n = 2; n <= 100; n++)
{
if (isPrime(n))
cout << n << " ";
}
return 0;
}举一反三: 把范围 100 改成用户输入的 n,即可输出 1~n 的所有质数。
3. 模块 B:数组、排序与矩阵
3.1 冒泡排序
从小到大排序模板
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}如果把判断条件改成:
if (a[j] < a[j + 1])就是从大到小排序。
举一反三
掌握冒泡排序后,可以解决:
- 数组升序、降序排列。
- 6174 黑洞数中的四位数字排序。
- 成绩排序。
- 找最大值、最小值(也可以用单独遍历实现)。
3.2 二维矩阵是否对称
矩阵对称的条件:
A[i][j]=A[j][i]
前提是方阵,即行数和列数相同。
#include <iostream>
using namespace std;
int main()
{
int n;
int a[50][50];
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
bool flag = true;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[i][j] != a[j][i])
{
flag = false;
break;
}
}
if (!flag)
break;
}
if (flag)
cout << "对称矩阵";
else
cout << "不是对称矩阵";
return 0;
}举一反三:
- 矩阵转置:交换
a[i][j]和a[j][i]。 - 判断单位矩阵:主对角线为 1,其余位置为 0。
- 求矩阵各行、各列之和。
3.3 6174 黑洞数
题目逻辑
输入一个四位正整数,各位数字不能完全相同。反复执行:
- 将四位数字从大到小排列,组成大数。
- 从小到大排列,组成小数。
- 大数减小数。
- 直到得到 6174。
例如:
5432 - 2345 = 3087
8730 - 0378 = 8352
8532 - 2358 = 6174核心代码
int arr[4];
arr[0] = num / 1000;
arr[1] = num / 100 % 10;
arr[2] = num / 10 % 10;
arr[3] = num % 10;
// 从小到大排序
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
int small = arr[0] * 1000 + arr[1] * 100
+ arr[2] * 10 + arr[3];
int big = arr[3] * 1000 + arr[2] * 100
+ arr[1] * 10 + arr[0];
num = big - small;完整程序:
#include <iostream>
using namespace std;
int main()
{
int num;
cin >> num;
if (num < 1000 || num > 9999)
{
cout << "输入有错";
return 0;
}
int a = num / 1000;
int b = num / 100 % 10;
int c = num / 10 % 10;
int d = num % 10;
if (a == b && b == c && c == d)
{
cout << "输入有错";
return 0;
}
while (num != 6174)
{
int arr[4] = {
num / 1000,
num / 100 % 10,
num / 10 % 10,
num % 10
};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
int small = arr[0] * 1000 + arr[1] * 100
+ arr[2] * 10 + arr[3];
int big = arr[3] * 1000 + arr[2] * 100
+ arr[1] * 10 + arr[0];
num = big - small;
cout << big << " - " << small << " = " << num << endl;
}
return 0;
}易错点: 某一轮可能出现 0378 这样的四位表示,但整数变量会把它保存为 378。下一轮用千位、百位、十位、个位拆分时,仍会得到 0、3、7、8,因此计算逻辑正确;如果要求输出时也显示前导零,需要使用格式化输出。
4. 模块 C:函数、递归与数值迭代
4.1 普通函数
核心模板
返回类型 函数名(参数)
{
// 计算过程
return 结果;
}例:求两个数的最大值。
int maxValue(int a, int b)
{
if (a > b)
return a;
return b;
}4.2 递归函数
递归的两个关键点:
- 终止条件:什么时候停止调用自己。
- 递归关系:如何把当前问题变成规模更小的问题。
通用模板
返回类型 fun(参数)
{
if (终止条件)
return 结果;
return fun(参数变化);
}原题:递归计算 x 的 y 次幂
xy=x×xy−1,x0=1
#include <iostream>
using namespace std;
int power(int x, int y)
{
if (y == 0)
return 1;
return x * power(x, y - 1);
}
int main()
{
int x, y;
cin >> x >> y;
cout << power(x, y) << endl;
return 0;
}举一反三一:阶乘
n!=n×(n−1)!,0!=1
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}举一反三二:斐波那契数列
F(n)=F(n−1)+F(n−2)
int fib(int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}举一反三三:1 到 n 求和
S(n)=n+S(n−1),S(0)=0
int sum(int n)
{
if (n == 0)
return 0;
return n + sum(n - 1);
}4.3 数值迭代
迭代题常见结构:
double fun(double x)
{
double current = 初始值;
double next;
do
{
next = 根据公式计算(current);
if (fabs(next - current) < 1e-8)
break;
current = next;
} while (true);
return next;
}重点:每次计算新值,比较新旧值的误差;误差达到要求后停止。
5. 模块 D:数学函数速查与公式转代码
5.1 常用数学函数对照表
使用这些函数通常需要:
#include <cmath>| 数学表达式 | C++ 写法 | 说明 |
|---|---|---|
| x | sqrt(x) | 平方根 |
| x2 | x * x | 平方 |
| xn | pow(x, n) | 幂函数 |
| ex | exp(x) | 指数函数 |
| lnx | log(x) | 自然对数 |
| log10x | log10(x) | 以 10 为底的对数 |
| sinx | sin(x) | 正弦 |
| cosx | cos(x) | 余弦 |
| tanx | tan(x) | 正切 |
| 向上取整 | ceil(x) | 向上取整 |
| 向下取整 | floor(x) | 向下取整 |
注意:
sin()、cos()、tan()的参数默认是弧度,不是角度。log()是自然对数,不是以 10 为底的对数。sqrt()的参数不能为负数(实数范围)。pow(x, n)可以计算幂,但如果题目指定递归或迭代,就要按要求自己实现。
5.2 题目关键词与第一反应
| 题目关键词 | 第一反应 |
|---|---|
| 平方根 | sqrt(),或按题意实现迭代 |
| 绝对值、误差 | fabs() |
| 次方 | pow(),或递归实现 |
| 阶乘 | 自己写循环或递归 |
| 正弦 | sin(),或泰勒展开 |
| 余弦 | cos(),或泰勒展开 |
| 自然对数 | log() |
| 指数 | exp() |
| 最大值 | max() |
| 最小值 | min() |
max()、min() 通常需要:
#include <algorithm>5.3 数学公式转代码的四个步骤
看到数学公式,不要马上写代码。先做四件事:
- 找变量:哪些是输入,哪些是中间量,哪些是结果?
- 找运算:加减乘除、乘方、开方、绝对值分别是什么?
- 找重复关系:是否需要循环、递归或迭代?
- 找停止条件:循环多少次,或者误差小于多少时结束?
5.4 原题:牛顿迭代法求平方根
公式:
xn+1=21(xn+xna)
停止条件:
∣xn+1−xn∣<10−8
代码:
#include <iostream>
#include <cmath>
using namespace std;
double mySqrt(double a)
{
if (a == 0)
return 0;
double x = a;
double next;
do
{
next = 0.5 * (x + a / x);
if (fabs(next - x) < 1e-8)
break;
x = next;
} while (true);
return next;
}
int main()
{
double a;
int num;
cin >> a >> num;
for (int i = 0; i <= num + 1; i++)
{
cout << mySqrt(a + i) << endl;
}
return 0;
}如果输入 4 2,计算:
4,5,6,7
5.5 原题:sin(x) 泰勒展开
数学公式:
sinx=x−3!x3+5!x5−⋯
通项:
(−1)k(2k+1)!x2k+1
可以利用相邻两项的关系,不必每次重新计算幂和阶乘。
#include <iostream>
#include <cmath>
using namespace std;
double sinFun(double x)
{
double sum = 0;
double term = x;
int k = 0;
while (fabs(term) >= 1e-8)
{
sum += term;
k++;
term = -term * x * x
/ ((2 * k) * (2 * k + 1));
}
return sum;
}
int main()
{
double x;
cin >> x;
cout << sinFun(x) << endl;
return 0;
}如果题目要求计算:
sin(x)+sin(x+1)+⋯+sin(x+5)
则在主函数中累加:
double sum = 0;
for (int i = 0; i <= 5; i++)
{
sum += sinFun(x + i);
}
cout << sum << endl;5.6 数学函数识别练习
先自己判断,再对照答案:
- 计算
x的平方根:sqrt(x)。 - 判断两个浮点数之差是否小于精度:
fabs(a-b)。 - 计算 ex:
exp(x)。 - 计算 x5:
pow(x, 5)。 - 计算弧度制的正弦:
sin(x)。
重要提醒: 数学函数只是工具。题目要求"使用牛顿迭代法"时,不能直接用 sqrt() 代替;题目要求"递归求幂"时,也不能直接用 pow() 代替。
6. 模块 E:类与对象
这是三套题中反复出现的核心模块。Tree、Desk、Stock 等题目,本质上都在考类的基本结构。
6.1 类的万能模板
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
int id;
string name;
double score;
public:
// 构造函数
Student(int i, string n, double s)
{
id = i;
name = n;
score = s;
}
// 设置数据
void set(int i, string n, double s)
{
id = i;
name = n;
score = s;
}
// 显示数据
void display()
{
cout << id << endl;
cout << name << endl;
cout << score << endl;
}
};
int main()
{
Student s(1001, "张三", 90);
s.display();
return 0;
}记住基本结构:
class 类名
{
private:
// 数据成员
public:
// 构造函数
// 设置函数
// 显示函数
};6.2 默认参数
原题 Stock:价格默认值为 1.0。
class Stock
{
private:
string code;
string name;
double price;
public:
Stock(string c, string n, double p = 1.0)
{
code = c;
name = n;
price = p;
}
void set(string c, string n, double p)
{
code = c;
name = n;
price = p;
}
void display()
{
cout << code << endl;
cout << name << endl;
cout << price << endl;
}
};调用:
Stock st_a("688001", "中国银行", 48);
Stock st_b("688001", "********");第二个对象省略价格,自动使用默认值 1.0。
6.3 Desk 类的变化
题目要求:桌子类包含 length、width、height、weight,构造函数中 height 默认 1,weight 默认 10;可以设置和显示数据,并计算由 length、width 构成的矩形面积。
#include <iostream>
using namespace std;
class Desk
{
private:
float length;
float width;
float height;
float weight;
public:
Desk(float l, float w, float h = 1, float wt = 10)
{
length = l;
width = w;
height = h;
weight = wt;
}
void set(float l, float w, float h, float wt)
{
length = l;
width = w;
height = h;
weight = wt;
}
float area()
{
return length * width;
}
void display()
{
cout << length << " "
<< width << " "
<< height << " "
<< weight << endl;
cout << "面积:" << area() << endl;
}
};6.4 举一反三:Book 类
题目变成书籍管理:书名、作者、价格,提供构造函数、设置函数、显示函数。
class Book
{
private:
string name;
string author;
double price;
public:
Book(string n, string a, double p)
{
name = n;
author = a;
price = p;
}
void set(string n, string a, double p)
{
name = n;
author = a;
price = p;
}
void display()
{
cout << name << endl;
cout << author << endl;
cout << price << endl;
}
};6.5 类题常见变化
| 题目要求 | 需要增加的结构 |
|---|---|
| 默认参数 | double p = 1.0 |
| 增加成员函数 | void grow() |
| 返回计算结果 | double area() |
| 修改成员数据 | set() |
| 显示数据 | display() |
| 无参构造函数 | Student() {} |
| 构造函数初始化 | Student(...) { ... } |
易错点:
- 构造函数没有返回值类型,不能写
void Student()。 - 类定义结束必须有分号。
private成员不能在类外直接访问。- 默认参数通常从右向左连续设置,不能跳过前面的参数而只给后面的参数默认值。
7. 模块 F:友元函数与友元类
友元的核心:允许指定的外部函数或类访问当前类的私有成员。
7.1 普通友元函数
class A
{
private:
int x;
public:
A(int a)
{
x = a;
}
friend void fun(A &a);
};
void fun(A &a)
{
a.x++;
}注意:fun() 是普通函数,不属于 A 类,但它可以访问 A 的 private 成员。
7.2 友元成员函数
class A;
class B
{
public:
void fun(A *p);
};
class A
{
private:
int x;
public:
friend void B::fun(A *p);
};
void B::fun(A *p)
{
p->x++;
}这里需要先声明 A,再声明 B 的成员函数,之后才能在 A 中声明指定的友元成员函数。
7.3 友元类
class A
{
private:
int x;
friend class B;
};
class B
{
public:
void fun(A &a)
{
a.x++;
}
};friend class B; 表示 B 类的成员函数可以访问 A 的私有成员。
7.4 三种友元对比
| 类型 | 声明方式 | 权限范围 |
|---|---|---|
| 普通友元函数 | friend void fun(A &a); | 指定普通函数 |
| 友元成员函数 | friend void B::fun(A *p); | 指定成员函数 |
| 友元类 | friend class B; | B 类的成员函数 |
7.5 原题:X、Y1、Y2
题目要求:
- X 有私有成员
i。 - Y1 的成员函数
g(X*)是 X 的友元成员函数,使 i 加 1。 - Y2 是 X 的友元类,成员函数
f(X*)使 i 加 5。 - 普通函数
h(X*)是 X 的友元函数,使 i 加 10。
#include <iostream>
using namespace std;
class X;
class Y1;
class Y2;
class Y1
{
public:
void g(X *p);
};
class Y2
{
public:
void f(X *p);
};
class X
{
private:
int i;
public:
X(int a = 0)
{
i = a;
}
void show()
{
cout << "i = " << i << endl;
}
friend void Y1::g(X *p);
friend class Y2;
friend void h(X *p);
};
void Y1::g(X *p)
{
p->i += 1;
}
void Y2::f(X *p)
{
p->i += 5;
}
void h(X *p)
{
p->i += 10;
}
int main()
{
X x(0);
Y1 y1;
y1.g(&x);
x.show();
Y2 y2;
y2.f(&x);
x.show();
h(&x);
x.show();
return 0;
}输出:
i = 1
i = 6
i = 167.6 指针参数怎么理解
X x;
h(&x);x:对象。&x:对象的地址。X *p:指向 X 对象的指针。p->i:通过指针访问对象成员。
下面两种写法等价:
p->i += 10;
(*p).i += 10;7.7 举一反三
原题:友元函数修改 MyPoint 的坐标。
变式:定义 Rectangle 类,包含私有成员 length、width,使用友元函数计算面积。
class Rectangle
{
private:
double length;
double width;
public:
Rectangle(double l, double w)
{
length = l;
width = w;
}
friend double area(Rectangle &r);
};
double area(Rectangle &r)
{
return r.length * r.width;
}8. 模块 G:继承
8.1 继承万能模板
class A
{
protected:
int x;
public:
A(int a)
{
x = a;
}
void show()
{
cout << x << endl;
}
};
class B : public A
{
private:
int y;
public:
B(int a, int b) : A(a)
{
y = b;
}
void display()
{
A::show();
cout << y << endl;
}
};8.2 原题归类
| 题目 | 父类 | 子类 | 新增内容 |
|---|---|---|---|
| Student/UStudent | Student | UStudent | 专业 |
| Employee/Manager | Employee | Manager | 职称 |
8.3 Employee 与 Manager 完整示例
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee
{
protected:
int id;
string name;
string gender;
string department;
double salary;
public:
Employee()
{
id = 0;
name = "";
gender = "";
department = "";
salary = 0;
}
Employee(int i, string n, string g,
string d, double s)
{
id = i;
name = n;
gender = g;
department = d;
salary = s;
}
void display()
{
cout << "工号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "部门:" << department << endl;
cout << fixed << setprecision(2);
cout << "工资:" << salary << endl;
}
};
class Manager : public Employee
{
private:
string title;
public:
Manager() : Employee()
{
title = "";
}
Manager(int i, string n, string g,
string d, double s, string t)
: Employee(i, n, g, d, s)
{
title = t;
}
void display()
{
Employee::display();
cout << "职称:" << title << endl;
}
};
int main()
{
Employee e1(1001, "张三", "男", "技术部", 8000.5);
Manager m1(1002, "李四", "男",
"管理部", 12000.5, "经理");
e1.display();
cout << "----------------" << endl;
m1.display();
return 0;
}8.4 四个必须掌握的语法
| 语法 | 作用 |
|---|---|
class B : public A | B 公有继承 A |
protected | 子类可以访问 |
: A(a) | 调用父类构造函数 |
A::show() | 调用父类成员函数 |
易错点:
- 父类的
private成员不能被子类直接访问。 protected成员可以被子类直接访问。- 子类构造时,先构造父类,再构造子类自己的成员。
- 子类构造函数通过初始化列表调用父类构造函数。
8.5 举一反三:Animal 与 Dog
class Animal
{
protected:
string name;
int age;
public:
Animal(string n, int a)
{
name = n;
age = a;
}
};
class Dog : public Animal
{
private:
string breed;
public:
Dog(string n, int a, string b)
: Animal(n, a)
{
breed = b;
}
};把父类、子类名称和新增成员替换,就可以迁移到员工、学生、动物等题目。
9. 模块 H:静态成员
9.1 静态成员模板
class Student
{
private:
static int count;
public:
Student()
{
count++;
}
static void show()
{
cout << count << endl;
}
};
// 类外初始化
int Student::count = 0;9.2 原题:Student 的 count、sum、ave
题目要求:
count:对象总数。sum:总成绩。ave:平均成绩。show():显示统计信息。
参考代码:
#include <iostream>
using namespace std;
class Student
{
private:
string name;
int stu_no;
double score;
static int count;
static double sum;
public:
Student(string n, int no, double s)
{
name = n;
stu_no = no;
score = s;
count++;
sum += score;
}
static void show()
{
double ave = 0;
if (count > 0)
ave = sum / count;
cout << "人数:" << count << endl;
cout << "总分:" << sum << endl;
cout << "平均分:" << ave << endl;
}
};
int Student::count = 0;
double Student::sum = 0;
int main()
{
Student s1("张三", 1001, 90);
Student s2("李四", 1002, 80);
Student s3("王五", 1003, 70);
Student::show();
return 0;
}9.3 举一反三:统计汽车数量
class Car
{
private:
static int count;
public:
Car()
{
count++;
}
static void show()
{
cout << count << endl;
}
};
int Car::count = 0;记忆: 每个对象独立拥有的数据用普通成员;所有对象共享的数据用 static。
10. 模块 I:运算符重载
10.1 核心模板
class A
{
private:
int x;
public:
A(int a)
{
x = a;
}
A operator+(A &a)
{
return A(x + a.x);
}
};调用:
A a(10);
A b(20);
A c = a + b;10.2 常见运算符
| 运算符 | 重载函数 |
|---|---|
+ | operator+() |
- | operator-() |
* | operator*() |
/ | operator/() |
== | operator==() |
10.3 原题:Fraction 分数类
应包含:
- 分子、分母。
- 构造函数。
- 加、减、乘、除运算。
- 最大公约数。
- 结果约分。
最大公约数可以使用辗转相除法:
int gcd(int a, int b)
{
a = abs(a);
b = abs(b);
while (b != 0)
{
int t = a % b;
a = b;
b = t;
}
return a;
}分数类的基本结构:
class Fraction
{
private:
int num;
int den;
void simplify()
{
int g = gcd(num, den);
if (g != 0)
{
num /= g;
den /= g;
}
if (den < 0)
{
den = -den;
num = -num;
}
}
public:
Fraction(int n = 0, int d = 1)
{
num = n;
den = d;
// 实际程序中还应检查 d != 0
simplify();
}
Fraction operator+(const Fraction &f) const
{
return Fraction(num * f.den + f.num * den,
den * f.den);
}
Fraction operator-(const Fraction &f) const
{
return Fraction(num * f.den - f.num * den,
den * f.den);
}
Fraction operator*(const Fraction &f) const
{
return Fraction(num * f.num, den * f.den);
}
Fraction operator/(const Fraction &f) const
{
// 调用前必须保证 f.num != 0
return Fraction(num * f.den, den * f.num);
}
void display() const
{
cout << num << "/" << den << endl;
}
};完整使用时,需要把 gcd() 放在类定义之前,并且构造函数中应对分母为 0 的情况进行处理。
10.4 举一反三:Money 类
如果 Money 类保存整数金额,要求支持 +:
class Money
{
private:
int amount;
public:
Money(int a)
{
amount = a;
}
Money operator+(const Money &m) const
{
return Money(amount + m.amount);
}
};核心思路: 运算符重载就是定义"两个对象进行某种运算时,应该如何计算并返回结果"。
11. 模块 J:文件操作
11.1 文件操作模板
#include <fstream>
using namespace std;
class A
{
private:
fstream file;
public:
// 构造函数打开文件
A()
{
file.open("test.txt", ios::out);
}
// 写入文件
void write()
{
if (file.is_open())
file << "Hello" << endl;
}
// 析构函数关闭文件
~A()
{
if (file.is_open())
file.close();
}
};11.2 常见文件操作
| 代码 | 作用 |
|---|---|
#include <fstream> | 引入文件流 |
fstream file; | 定义文件对象 |
file.open() | 打开文件 |
file << | 写入文件 |
file >> | 读取文件 |
file.close() | 关闭文件 |
~A() | 析构函数 |
11.3 6174 题目中的文件操作
题目要求:
- 在类的数据成员中定义文件流对象。
- 在构造函数中打开文件。
- 计算过程中写入文件。
- 在析构函数中关闭文件。
关键代码:
private:
fstream file;Blackhole()
{
file.open("Blackhole.txt", ios::out | ios::trunc);
}file << big << " - " << small
<< " = " << num << endl;~Blackhole()
{
if (file.is_open())
file.close();
}11.4 举一反三:成绩写入文件
file << name << " " << score << endl;把原来的计算结果替换成学生姓名和成绩即可。
12. 历年原题归类
以下按目前提供的三套试题内容整理。年份以用户提供的试卷标注为准。
12.1 第一套试题(2023 年)
| 题目 | 核心模块 |
|---|---|
数列 1 + 2*3 + 4*5*6 + ... 求和 | 循环、累加、数列规律 |
| 11~999 的三位回文数字 | 数字处理、回文判断 |
| Tree 类 | 类与对象、构造函数、成员函数 |
| MyPoint 的 Offset 友元函数 | 友元函数、对象引用 |
| n 阶矩阵是否对称 | 二维数组、矩阵 |
| Student 与 UStudent | 继承、构造函数 |
12.2 第二套试题(2024 年)
| 题目 | 核心模块 |
|---|---|
| while 输出 1~100 的质数 | 循环、质数判断 |
| 1~100 猜数字 | while、条件判断、break |
| Desk 类 | 类、默认参数、成员函数、面积计算 |
| sinFun() | 数学函数、泰勒展开、精度控制 |
| Student 统计人数、总分、平均分 | static 静态成员 |
| Fraction 分数类 | 类、运算符重载、最大公约数 |
12.3 第三套试题
| 题目 | 核心模块 |
|---|---|
| 成绩等级 A/B/C | if / else |
| 递归 power(x,y) | 递归函数 |
| Stock 类 | 类、构造函数默认参数、set、display |
| X、Y1、Y2 | 友元函数、友元成员函数、友元类、指针 |
| 牛顿迭代法求平方根 | 数学迭代、fabs、do while |
| Employee 与 Manager | 继承、构造函数、输出格式 |
| 6174 黑洞数 | 数字拆分、数组排序、文件操作、析构函数 |
12.4 高频知识点总结
| 知识点 | 出现情况 | 复习建议 |
|---|---|---|
| 判断与循环 | 多套出现 | 熟练默写 |
| 数字处理 | 多套出现 | 熟练掌握取位公式 |
| 函数 | 多套出现 | 会写参数、返回值 |
| 类与构造函数 | 三套均出现 | 重点掌握 |
| 友元 | 多套出现 | 掌握三种形式 |
| 继承 | 多套出现 | 重点掌握 |
| static | 有出现 | 会初始化、会统计 |
| 运算符重载 | 有出现 | 重点练 Fraction |
| 数学迭代 | 有出现 | 会写误差判断 |
| 文件操作 | 有出现 | 掌握 open、write、close |
13. 如何整理和练习
13.1 不按年份整理,按核心结构整理
建议把笔记建立成以下目录:
C++手写编程复习/
│
├── 01_基础判断与循环.md
├── 02_数字处理与数组.md
├── 03_函数与递归.md
├── 04_数学函数与数值迭代.md
├── 05_类与对象.md
├── 06_友元函数与友元类.md
├── 07_继承.md
├── 08_static静态成员.md
├── 09_运算符重载.md
├── 10_文件操作.md
└── 11_历年题目归类.md13.2 每个模块采用同一套笔记格式
例如"类与对象":
① 核心结构
写出类、数据成员、构造函数、成员函数的完整模板。
② 历年原题
记录 Tree、Desk、Stock 等题目。
③ 常见变化
- 增加默认参数。
- 增加
set()函数。 - 增加计算函数。
- 增加数据成员。
- 增加无参构造函数。
④ 举一反三
把 Stock 改成 Book,把 Tree 改成 Car,重新写一遍。
⑤ 易错点
记录构造函数、访问权限、类定义分号等容易出错的地方。
13.3 每个模块练习三遍
第一遍:看模板写
对照代码,理解每行代码的作用。
第二遍:脱离答案默写
不看答案,从头写出完整程序。
第三遍:改变题目条件
更换类名、数据成员、参数和计算逻辑,检验自己是否真正掌握结构。
不要把"看懂了"当成"会写了"。手写考试最重要的是能独立写出来。
13.4 建议的七天复习顺序
| 天数 | 练习内容 | 目标 |
|---|---|---|
| 第 1 天 | 判断、循环、数字处理 | 能独立写基础程序 |
| 第 2 天 | 数组、排序、矩阵 | 掌握双重循环 |
| 第 3 天 | 普通函数、递归、数学迭代 | 能独立定义函数 |
| 第 4 天 | 类、构造函数、默认参数 | 默写类的完整结构 |
| 第 5 天 | 友元函数、友元类 | 分清三种友元 |
| 第 6 天 | 继承、静态成员 | 掌握父子类关系 |
| 第 7 天 | 运算符重载、文件操作 | 完成综合题 |
14. 自测清单
建议每掌握一个结构,就勾选一次。只有能脱离答案独立写出来,才算真正掌握。
基础语法
函数与数学计算
面向对象
综合应用
最后:考试时的解题思路
拿到一道新题,可以按下面的顺序分析:
- 先看题目在考什么结构。 是循环、函数、类、友元、继承,还是文件操作?
- 确定数据。 输入是什么,数据成员是什么,计算结果是什么?
- 写出核心框架。 先把
main()、函数、类的结构搭起来。 - 填充业务逻辑。 把数学公式、判断条件和计算过程写进去。
- 检查边界和易错点。 例如零、负数、默认参数、精度、文件是否成功打开。
最重要的目标不是背会三套试卷,而是做到:
- 看到类题,想到类的基本结构。
- 看到友元题,想到 friend 的三种写法。
- 看到继承题,想到父类构造函数和
protected。 - 看到数字题,想到
% 10和/ 10。 - 看到递归题,想到终止条件和递归关系。
- 看到数学迭代题,想到新旧值、误差和停止条件。
- 看到文件题,想到打开、写入和关闭。
从背题转变为识别结构,才是面对新题时真正有用的能力。
