这是我大一下学期的编程入门记录。它不单独作为项目统计,更像是从“会写语句”走向“能完成一个小程序”的起点。下面的题目与代码按当时题目名称整理为复盘版,方便之后重新学习和练手。

返回项目实践 · 002 面向对象程序设计 · 003 数据结构与算法课程设计

1、课程信息

项目 内容
学习阶段 大一下学期
主要语言 C++
课程定位 语法、流程控制、函数与基础程序设计
关键词 输入输出、分支循环、函数拆分、字符串处理

2、题目与训练点

编号 题目 主要训练内容 现在回看
A16 发工资 输入输出、算术运算、流程控制 从明确规则到计算结果,适合练习变量、条件和格式化输出
A30 数字拆分 整数运算、循环、边界处理 重点是取余、整除和数字位数处理,能训练基础算法意识
A57 房价 条件判断、数据计算 用条件分支表达分段规则,避免把公式和判断写散
B07 猜成语 字符串处理、交互、中文编码 比数值题更接近小程序,涉及输入、提示、判断和编码兼容

3、A16 发工资

3.1 题目描述

输入若干名员工的工资信息,每名员工包含姓名、基本工资、奖金和扣款。计算每名员工的实发工资,并统计本次发放工资总额。

实发工资计算公式:

1
实发工资 = 基本工资 + 奖金 - 扣款

3.2 输入输出

输入格式:

1
2
3
4
n
name1 base1 bonus1 deduction1
name2 base2 bonus2 deduction2
...

输出格式:

1
2
3
姓名 实发工资
...
Total: 总额

示例:

1
2
3
4
3
Alice 3000 500 120
Bob 2800 300 80
Cindy 3500 800 200

输出:

1
2
3
4
Alice 3380.00
Bob 3020.00
Cindy 4100.00
Total: 10500.00

3.3 完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Employee {
string name;
double base;
double bonus;
double deduction;

double salary() const {
return base + bonus - deduction;
}
};

int main() {
int n;
cin >> n;

vector<Employee> employees(n);
for (int i = 0; i < n; i++) {
cin >> employees[i].name
>> employees[i].base
>> employees[i].bonus
>> employees[i].deduction;
}

double total = 0.0;
cout << fixed << setprecision(2);

for (const Employee& employee : employees) {
double currentSalary = employee.salary();
total += currentSalary;
cout << employee.name << " " << currentSalary << endl;
}

cout << "Total: " << total << endl;
return 0;
}

4、A30 数字拆分

4.1 题目描述

输入一个非负整数,拆分出它的每一位数字,输出数字位数、各位数字之和以及逆序后的数字。

这个题适合练习整除 /、取余 %、循环和 0 的边界情况。

4.2 输入输出

输入:

1
12340

输出:

1
2
3
4
digits: 1 2 3 4 0
count: 5
sum: 10
reverse: 4321

4.3 完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
long long number;
cin >> number;

if (number < 0) {
cout << "Please input a non-negative integer." << endl;
return 0;
}

vector<int> digits;
if (number == 0) {
digits.push_back(0);
} else {
long long temp = number;
while (temp > 0) {
digits.push_back(temp % 10);
temp /= 10;
}
reverse(digits.begin(), digits.end());
}

int sum = 0;
long long reversedNumber = 0;

cout << "digits:";
for (int digit : digits) {
cout << " " << digit;
sum += digit;
reversedNumber = reversedNumber * 10 + digit;
}
cout << endl;

reverse(digits.begin(), digits.end());
reversedNumber = 0;
for (int digit : digits) {
reversedNumber = reversedNumber * 10 + digit;
}

cout << "count: " << digits.size() << endl;
cout << "sum: " << sum << endl;
cout << "reverse: " << reversedNumber << endl;

return 0;
}

5、A57 房价

5.1 题目描述

输入房屋面积、每平方米单价、楼层和装修等级,估算总房价。设定规则如下:

条件 调整
7~12 层 基础房价增加 3%
13 层及以上 基础房价增加 5%
精装修 每平方米增加 1500 元
简装修 每平方米增加 800 元
毛坯 不增加装修费用

总价计算公式:

1
2
3
基础房价 = 面积 × 单价 × 楼层系数
装修费用 = 面积 × 装修单价
总价 = 基础房价 + 装修费用

5.2 输入输出

输入:

1
89.5 12000 18 fine

输出:

1
2
3
base: 1127700.00
decoration: 134250.00
total: 1261950.00

5.3 完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

double getFloorRate(int floor) {
if (floor >= 13) {
return 1.05;
}
if (floor >= 7) {
return 1.03;
}
return 1.00;
}

double getDecorationPrice(const string& level) {
if (level == "fine") {
return 1500.0;
}
if (level == "simple") {
return 800.0;
}
return 0.0;
}

int main() {
double area;
double unitPrice;
int floor;
string decoration;

cin >> area >> unitPrice >> floor >> decoration;

if (area <= 0 || unitPrice <= 0 || floor <= 0) {
cout << "Invalid input." << endl;
return 0;
}

double base = area * unitPrice * getFloorRate(floor);
double decorationFee = area * getDecorationPrice(decoration);
double total = base + decorationFee;

cout << fixed << setprecision(2);
cout << "base: " << base << endl;
cout << "decoration: " << decorationFee << endl;
cout << "total: " << total << endl;

return 0;
}

6、B07 猜成语

6.1 题目描述

程序内置若干成语和提示。用户最多猜 5 次,每次输入一个成语。猜中则输出成功;猜错则给出剩余次数,次数用完后输出答案。

这个题目主要练习字符串、循环、交互提示和中文输入。不同编译器对中文控制台输入支持不完全一致,如果中文显示异常,可以先把成语换成拼音或英文单词测试程序逻辑。

6.2 输入输出

示例:

1
2
3
4
5
提示:形容做事有恒心,坚持不懈。
请输入成语:半途而废
猜错了,还剩 4 次。
请输入成语:持之以恒
恭喜你,猜对了!

6.3 完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Idiom {
string answer;
string hint;
};

int main() {
vector<Idiom> idioms = {
{"持之以恒", "形容做事有恒心,坚持不懈。"},
{"画蛇添足", "比喻多此一举,反而坏事。"},
{"亡羊补牢", "比喻出了问题后及时补救。"},
{"水滴石穿", "比喻力量虽小,只要坚持也能成功。"}
};

int index = 0;
int maxTryCount = 5;
string guess;

cout << "提示:" << idioms[index].hint << endl;

for (int i = 1; i <= maxTryCount; i++) {
cout << "请输入成语:";
cin >> guess;

if (guess == idioms[index].answer) {
cout << "恭喜你,猜对了!" << endl;
return 0;
}

cout << "猜错了,还剩 " << maxTryCount - i << " 次。" << endl;
}

cout << "很遗憾,正确答案是:" << idioms[index].answer << endl;
return 0;
}

7、当时真正练到的能力

7.1 从题目规则到程序流程

这几道题都不复杂,但它们把最基本的编程习惯压得很实:先读懂输入输出,再拆条件,再写循环或函数,最后用样例和边界值检查程序。现在看,这是后面做课程设计、算法题和小系统前必须经过的一层地基。

7.2 中文输入与交互

“猜成语”留下的印象最深,因为它不再只是算一个数字,而是要让程序和人来回交互。中文输入、字符串比较、提示语和异常输入都会影响体验。这个问题也提醒我:程序能运行是一回事,用户能顺畅使用又是另一回事。

8、个人复盘

这门课最值得保留的不是把几道题包装成项目,而是记录自己如何从语法练习过渡到完整程序:开始关注命名、函数拆分、输入校验、边界情况和可读性。之后整理历史资料时,如果能找到原始代码,我会把它作为“当时写法”保留,而不是用后来重写的代码替代它。