
课程目标
- 理解数组的概念和用途
- 掌握一维数组的定义、初始化和访问
- 学会数组遍历和经典应用
- 掌握字符串的基本操作
- 理解批量数据处理的思想
- 为后续算法学习打下基础
第一部分:数组的概念(40分钟)
1.1 什么是数组?
生活比喻:
- 储物柜:一排编号的储物格,每个格子可以放东西
- 学生座位表:按学号排列的座位,每个位置坐一个学生
- 火车车厢:连续的车厢,每节车厢有编号和乘客
1.2 为什么需要数组?
没有数组的困境:
cpp
// 要存储5个学生的成绩
int score1, score2, score3, score4, score5;
cin >> score1 >> score2 >> score3 >> score4 >> score5;
// 要计算平均分
double average = (score1 + score2 + score3 + score4 + score5) / 5.0;
// 如果要处理100个学生呢?需要100个变量!
使用数组的便利:
cpp
// 使用数组存储5个学生的成绩
int scores[5];
for (int i = 0; i < 5; i++) {
cin >> scores[i];
}
// 计算平均分
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += scores[i];
}
double average = sum / 5.0;
1.3 数组的特点
- 相同类型:数组中所有元素的数据类型相同
- 连续存储:元素在内存中连续存放
- 固定大小:数组一旦创建,大小不能改变
- 索引访问:通过下标(索引)访问元素
第二部分:一维数组的基本操作(60分钟)
2.1 数组的定义和初始化
定义数组:
cpp
数据类型 数组名[数组大小];
示例:
cpp
#include <iostream>
using namespace std;
int main() {
// 定义数组的不同方式
int numbers1[5]; // 定义包含5个整数的数组(未初始化)
int numbers2[5] = {1, 2, 3, 4, 5}; // 定义并初始化
int numbers3[] = {1, 2, 3, 4, 5}; // 自动推断大小为5
int numbers4[5] = {1, 2}; // 前两个元素初始化,其余为0
// 访问数组元素
numbers1[0] = 10; // 给第一个元素赋值
cout << numbers2[2] << endl; // 输出第三个元素:3
return 0;
}
2.2 数组的访问和遍历
数组索引:
- 索引从0开始,不是从1开始!
- 有效索引范围:0 到 数组大小-1
- 访问越界是常见错误
cpp
#include <iostream>
using namespace std;
int main() {
int scores[5] = {85, 92, 78, 96, 88};
// 逐个访问
cout << "第一个学生成绩: " << scores[0] << endl;
cout << "第三个学生成绩: " << scores[2] << endl;
// 使用循环遍历数组
cout << "所有成绩: ";
for (int i = 0; i < 5; i++) {
cout << scores[i] << " ";
}
cout << endl;
// 修改数组元素
scores[1] = 95; // 修改第二个学生的成绩
cout << "修改后的第二个成绩: " << scores[1] << endl;
return 0;
}
2.3 数组大小和边界检查
cpp
#include <iostream>
using namespace std;
int main() {
const int SIZE = 5; // 使用常量定义数组大小
int numbers[SIZE] = {10, 20, 30, 40, 50};
// 正确遍历
cout << "正确遍历: ";
for (int i = 0; i < SIZE; i++) {
cout << numbers[i] << " ";
}
cout << endl;
// 错误:访问越界(危险!)
// cout << numbers[5] << endl; // 索引5不存在!
// cout << numbers[-1] << endl; // 索引-1不存在!
// 使用sizeof计算数组大小
int byteSize = sizeof(numbers); // 整个数组的字节数
int elementSize = sizeof(numbers[0]); // 每个元素的字节数
int elementCount = byteSize / elementSize; // 元素个数
cout << "数组总字节数: " << byteSize << endl;
cout << "每个元素字节数: " << elementSize << endl;
cout << "元素个数: " << elementCount << endl;
return 0;
}
第三部分:数组的经典应用(80分钟)
3.1 求最大值和最小值
cpp
#include <iostream>
using namespace std;
int main() {
const int SIZE = 7;
int temperatures[SIZE] = {25, 28, 30, 22, 26, 29, 27};
// 找最高温度
int maxTemp = temperatures[0]; // 假设第一个是最大值
for (int i = 1; i < SIZE; i++) {
if (temperatures[i] > maxTemp) {
maxTemp = temperatures[i];
}
}
// 找最低温度
int minTemp = temperatures[0]; // 假设第一个是最小值
for (int i = 1; i < SIZE; i++) {
if (temperatures[i] < minTemp) {
minTemp = temperatures[i];
}
}
cout << "一周温度: ";
for (int i = 0; i < SIZE; i++) {
cout << temperatures[i] << "°C ";
}
cout << endl;
cout << "最高温度: " << maxTemp << "°C" << endl;
cout << "最低温度: " << minTemp << "°C" << endl;
return 0;
}
3.2 求和与平均值
cpp
#include <iostream>
using namespace std;
int main() {
const int SIZE = 5;
double expenses[SIZE] = {45.5, 28.0, 63.2, 19.8, 52.1};
// 计算总支出
double total = 0;
for (int i = 0; i < SIZE; i++) {
total += expenses[i];
}
// 计算平均支出
double average = total / SIZE;
// 找出高于平均值的支出
cout << "所有支出: ";
for (int i = 0; i < SIZE; i++) {
cout << expenses[i] << "元 ";
}
cout << endl;
cout << "总支出: " << total << "元" << endl;
cout << "平均支出: " << average << "元" << endl;
cout << "高于平均值的支出: ";
for (int i = 0; i < SIZE; i++) {
if (expenses[i] > average) {
cout << expenses[i] << "元 ";
}
}
cout << endl;
return 0;
}
3.3 计数和统计
cpp
#include <iostream>
using namespace std;
int main() {
const int SIZE = 10;
int scores[SIZE] = {85, 92, 78, 45, 96, 88, 62, 75, 91, 53};
int excellent = 0; // 优秀(90-100)
int good = 0; // 良好(80-89)
int pass = 0; // 及格(60-79)
int fail = 0; // 不及格(0-59)
// 统计各个等级的人数
for (int i = 0; i < SIZE; i++) {
if (scores[i] >= 90) {
excellent++;
} else if (scores[i] >= 80) {
good++;
} else if (scores[i] >= 60) {
pass++;
} else {
fail++;
}
}
cout << "成绩统计结果:" << endl;
cout << "优秀(90-100): " << excellent << "人" << endl;
cout << "良好(80-89): " << good << "人" << endl;
cout << "及格(60-79): " << pass << "人" << endl;
cout << "不及格(0-59): " << fail << "人" << endl;
// 计算百分比
cout << "优秀率: " << (excellent * 100.0 / SIZE) << "%" << endl;
cout << "及格率: " << ((excellent + good + pass) * 100.0 / SIZE) << "%" << endl;
return 0;
}
3.4 数组排序(选择排序)
cpp
#include <iostream>
using namespace std;
int main() {
const int SIZE = 6;
int numbers[SIZE] = {64, 25, 12, 22, 11, 5};
cout << "排序前: ";
for (int i = 0; i < SIZE; i++) {
cout << numbers[i] << " ";
}
cout << endl;
// 选择排序算法
for (int i = 0; i < SIZE - 1; i++) {
// 找到从i到末尾的最小元素的索引
int minIndex = i;
for (int j = i + 1; j < SIZE; j++) {
if (numbers[j] < numbers[minIndex]) {
minIndex = j;
}
}
// 交换当前元素和最小元素
int temp = numbers[i];
numbers[i] = numbers[minIndex];
numbers[minIndex] = temp;
// 显示每一轮排序的结果
cout << "第" << i + 1 << "轮: ";
for (int k = 0; k < SIZE; k++) {
cout << numbers[k] << " ";
}
cout << endl;
}
cout << "排序后: ";
for (int i = 0; i < SIZE; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
3.5 数组搜索
cpp
#include <iostream>
using namespace std;
int main() {
const int SIZE = 8;
int numbers[SIZE] = {23, 45, 67, 12, 89, 34, 56, 78};
int target;
cout << "数组内容: ";
for (int i = 0; i < SIZE; i++) {
cout << numbers[i] << " ";
}
cout << endl;
cout << "请输入要查找的数字: ";
cin >> target;
// 线性搜索
bool found = false;
int position = -1;
for (int i = 0; i < SIZE; i++) {
if (numbers[i] == target) {
found = true;
position = i;
break; // 找到后立即退出循环
}
}
if (found) {
cout << "找到了!数字 " << target << " 在数组中的位置是: " << position << endl;
} else {
cout << "没有找到数字 " << target << endl;
}
return 0;
}
第四部分:字符串基础(60分钟)
4.1 什么是字符串?
字符串的概念:
- 字符串是字符的序列
- 在C++中,字符串可以用字符数组或string类表示
- 字符串以空字符
\0结尾
4.2 字符数组表示字符串
cpp
#include <iostream>
using namespace std;
int main() {
// 方式1:字符数组(C风格字符串)
char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char str2[] = "Hello"; // 自动添加\0
char name[20];
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
// 输入字符串
cout << "请输入你的名字: ";
cin >> name;
cout << "你好, " << name << "!" << endl;
// 遍历字符数组
cout << "字符串字符: ";
for (int i = 0; str2[i] != '\0'; i++) {
cout << str2[i] << " ";
}
cout << endl;
return 0;
}
4.3 string类的基本使用
cpp
#include <iostream>
#include <string> // 必须包含string头文件
using namespace std;
int main() {
// string类的定义和初始化
string str1 = "Hello";
string str2("World");
string str3;
// 字符串赋值
str3 = "C++";
// 字符串连接
string greeting = str1 + " " + str2 + "!";
cout << greeting << endl;
// 字符串输入
string name;
cout << "请输入你的全名: ";
cin >> name; // 遇到空格停止
cout << "你好, " << name << "!" << endl;
// 使用getline读取整行
cin.ignore(); // 清除之前的换行符
cout << "请重新输入你的全名: ";
getline(cin, name);
cout << "你好, " << name << "!" << endl;
return 0;
}
4.4 字符串常用操作
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Hello C++ Programming";
// 字符串长度
cout << "字符串: " << text << endl;
cout << "长度: " << text.length() << endl;
// 访问单个字符
cout << "第一个字符: " << text[0] << endl;
cout << "最后一个字符: " << text[text.length() - 1] << endl;
// 字符串比较
string str1 = "apple";
string str2 = "banana";
if (str1 == str2) {
cout << str1 << " 等于 " << str2 << endl;
} else if (str1 < str2) {
cout << str1 << " 在 " << str2 << " 前面" << endl;
} else {
cout << str1 << " 在 " << str2 << " 后面" << endl;
}
// 子字符串
string substring = text.substr(6, 3); // 从位置6开始,取3个字符
cout << "子字符串: " << substring << endl;
// 查找子字符串
int position = text.find("C++");
if (position != string::npos) {
cout << "找到 'C++' 在位置: " << position << endl;
} else {
cout << "没有找到 'C++'" << endl;
}
return 0;
}
第五部分:字符串应用示例(60分钟)
5.1 字符串统计
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
cout << "请输入一段文本: ";
getline(cin, text);
int letterCount = 0, digitCount = 0, spaceCount = 0, otherCount = 0;
// 统计各种字符的个数
for (int i = 0; i < text.length(); i++) {
char ch = text[i];
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
letterCount++;
} else if (ch >= '0' && ch <= '9') {
digitCount++;
} else if (ch == ' ') {
spaceCount++;
} else {
otherCount++;
}
}
cout << "\n=== 字符统计结果 ===" << endl;
cout << "总字符数: " << text.length() << endl;
cout << "字母个数: " << letterCount << endl;
cout << "数字个数: " << digitCount << endl;
cout << "空格个数: " << spaceCount << endl;
cout << "其他字符: " << otherCount << endl;
return 0;
}
5.2 字符串反转
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "请输入一个字符串: ";
getline(cin, str);
cout << "原始字符串: " << str << endl;
// 方法1:创建新字符串
string reversed1;
for (int i = str.length() - 1; i >= 0; i--) {
reversed1 += str[i];
}
cout << "反转字符串(方法1): " << reversed1 << endl;
// 方法2:原地反转
string reversed2 = str; // 复制原字符串
int left = 0, right = reversed2.length() - 1;
while (left < right) {
// 交换左右字符
char temp = reversed2[left];
reversed2[left] = reversed2[right];
reversed2[right] = temp;
left++;
right--;
}
cout << "反转字符串(方法2): " << reversed2 << endl;
return 0;
}
5.3 回文判断
cpp
#include <iostream>
#include <string>
#include <cctype> // 用于tolower函数
using namespace std;
int main() {
string str;
cout << "请输入一个字符串: ";
getline(cin, str);
// 预处理:转换为小写,移除非字母数字字符
string processed;
for (char ch : str) {
if (isalnum(ch)) { // 如果是字母或数字
processed += tolower(ch);
}
}
// 判断是否是回文
bool isPalindrome = true;
int left = 0, right = processed.length() - 1;
while (left < right) {
if (processed[left] != processed[right]) {
isPalindrome = false;
break;
}
left++;
right--;
}
cout << "\"" << str << "\" ";
if (isPalindrome) {
cout << "是回文!" << endl;
} else {
cout << "不是回文。" << endl;
}
return 0;
}
5.4 简单加密解密
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
int key;
cout << "请输入要加密的文本: ";
getline(cin, text);
cout << "请输入加密密钥(1-25): ";
cin >> key;
// 加密
string encrypted = text;
for (int i = 0; i < encrypted.length(); i++) {
char ch = encrypted[i];
if (isalpha(ch)) {
char base = islower(ch) ? 'a' : 'A';
encrypted[i] = (ch - base + key) % 26 + base;
}
}
cout << "加密结果: " << encrypted << endl;
// 解密
string decrypted = encrypted;
for (int i = 0; i < decrypted.length(); i++) {
char ch = decrypted[i];
if (isalpha(ch)) {
char base = islower(ch) ? 'a' : 'A';
decrypted[i] = (ch - base - key + 26) % 26 + base;
}
}
cout << "解密结果: " << decrypted << endl;
return 0;
}
第六部分:综合应用项目(60分钟)
6.1 学生成绩管理系统
cpp
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int MAX_STUDENTS = 50;
int main() {
string names[MAX_STUDENTS];
int scores[MAX_STUDENTS];
int studentCount = 0;
int choice;
do {
// 显示菜单
cout << "\n=== 学生成绩管理系统 ===" << endl;
cout << "1. 添加学生成绩" << endl;
cout << "2. 显示所有成绩" << endl;
cout << "3. 查找学生成绩" << endl;
cout << "4. 统计成绩分析" << endl;
cout << "5. 成绩排序" << endl;
cout << "0. 退出系统" << endl;
cout << "请选择操作: ";
cin >> choice;
switch (choice) {
case 1: {
// 添加学生成绩
if (studentCount >= MAX_STUDENTS) {
cout << "学生数量已满!" << endl;
break;
}
cout << "请输入学生姓名: ";
cin >> names[studentCount];
cout << "请输入" << names[studentCount] << "的成绩: ";
cin >> scores[studentCount];
studentCount++;
cout << "添加成功!" << endl;
break;
}
case 2: {
// 显示所有成绩
if (studentCount == 0) {
cout << "还没有学生数据!" << endl;
break;
}
cout << "\n=== 所有学生成绩 ===" << endl;
cout << left << setw(15) << "姓名" << setw(10) << "成绩" << "等级" << endl;
cout << string(35, '-') << endl;
for (int i = 0; i < studentCount; i++) {
string grade;
if (scores[i] >= 90) grade = "优秀";
else if (scores[i] >= 80) grade = "良好";
else if (scores[i] >= 70) grade = "中等";
else if (scores[i] >= 60) grade = "及格";
else grade = "不及格";
cout << left << setw(15) << names[i]
<< setw(10) << scores[i]
<< grade << endl;
}
break;
}
case 3: {
// 查找学生成绩
if (studentCount == 0) {
cout << "还没有学生数据!" << endl;
break;
}
string searchName;
cout << "请输入要查找的学生姓名: ";
cin >> searchName;
bool found = false;
for (int i = 0; i < studentCount; i++) {
if (names[i] == searchName) {
cout << "找到学生: " << names[i] << endl;
cout << "成绩: " << scores[i] << endl;
found = true;
break;
}
}
if (!found) {
cout << "没有找到学生: " << searchName << endl;
}
break;
}
case 4: {
// 统计成绩分析
if (studentCount == 0) {
cout << "还没有学生数据!" << endl;
break;
}
int sum = 0, maxScore = scores[0], minScore = scores[0];
int excellent = 0, good = 0, medium = 0, pass = 0, fail = 0;
for (int i = 0; i < studentCount; i++) {
sum += scores[i];
if (scores[i] > maxScore) maxScore = scores[i];
if (scores[i] < minScore) minScore = scores[i];
if (scores[i] >= 90) excellent++;
else if (scores[i] >= 80) good++;
else if (scores[i] >= 70) medium++;
else if (scores[i] >= 60) pass++;
else fail++;
}
double average = static_cast<double>(sum) / studentCount;
cout << "\n=== 成绩统计分析 ===" << endl;
cout << "学生总数: " << studentCount << endl;
cout << "平均分: " << fixed << setprecision(2) << average << endl;
cout << "最高分: " << maxScore << endl;
cout << "最低分: " << minScore << endl;
cout << "优秀(90-100): " << excellent << "人 ("
<< (excellent * 100.0 / studentCount) << "%)" << endl;
cout << "良好(80-89): " << good << "人 ("
<< (good * 100.0 / studentCount) << "%)" << endl;
cout << "中等(70-79): " << medium << "人 ("
<< (medium * 100.0 / studentCount) << "%)" << endl;
cout << "及格(60-69): " << pass << "人 ("
<< (pass * 100.0 / studentCount) << "%)" << endl;
cout << "不及格(0-59): " << fail << "人 ("
<< (fail * 100.0 / studentCount) << "%)" << endl;
break;
}
case 5: {
// 成绩排序(按成绩降序)
if (studentCount == 0) {
cout << "还没有学生数据!" << endl;
break;
}
// 使用选择排序按成绩降序排列
for (int i = 0; i < studentCount - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < studentCount; j++) {
if (scores[j] > scores[maxIndex]) {
maxIndex = j;
}
}
// 交换成绩
int tempScore = scores[i];
scores[i] = scores[maxIndex];
scores[maxIndex] = tempScore;
// 交换姓名
string tempName = names[i];
names[i] = names[maxIndex];
names[maxIndex] = tempName;
}
cout << "成绩排序完成!" << endl;
break;
}
case 0:
cout << "感谢使用学生成绩管理系统!" << endl;
break;
default:
cout << "无效选择,请重新输入!" << endl;
}
} while (choice != 0);
return 0;
}
练习与作业
基础练习(必做)
练习1:数组基础操作
创建一个包含10个整数的数组,完成以下操作:
- 从键盘输入10个数字
- 计算数组元素的总和和平均值
- 找出最大值和最小值及其位置
- 将数组元素逆序存放
练习2:字符串处理
编写程序处理字符串:
- 输入一个字符串,统计其中元音字母的个数
- 将字符串中的所有空格替换为下划线
- 判断字符串是否包含数字
- 将字符串中的大写字母转换为小写
练习3:成绩分析
使用数组存储一个班级的成绩(最多50人):
- 输入成绩(输入-1结束)
- 计算平均分、最高分、最低分
- 统计各分数段人数
- 按成绩从高到低排序
挑战练习(选做)
挑战1:矩阵运算
实现3×3矩阵的基本运算:
- 矩阵加法
- 矩阵乘法
- 矩阵转置
- 求矩阵对角线元素和
挑战2:字符串压缩
实现简单的字符串压缩算法:
- 将连续重复的字符压缩为"字符+次数"
- 例如:"aaabbc"压缩为"a3b2c1"
- 实现解压缩功能
挑战3:简单投票系统
模拟选举投票系统:
- 存储候选人姓名和得票数
- 实现投票功能
- 统计投票结果
- 找出获胜者
实验任务
任务1:数组边界测试
测试数组越界访问的后果:
cpp
int arr[5] = {1,2,3,4,5};
// 尝试访问arr[5], arr[6], arr[-1]等
// 观察程序行为并记录结果
任务2:字符串内存测试
测试不同字符串初始化方式的内存使用:
cpp
char str1[] = "Hello";
char str2[10] = "Hello";
string str3 = "Hello";
// 使用sizeof测试各字符串的大小
任务3:排序算法比较
实现冒泡排序和选择排序,比较:
- 代码复杂度
- 执行效率
- 交换次数
学习总结
今天学到了:
- ✅ 数组概念:相同类型元素的集合
- ✅ 数组操作:定义、初始化、访问、遍历
- ✅ 数组应用:求最值、求和、计数、排序
- ✅ 字符串基础:字符数组和string类
- ✅ 字符串操作:长度、连接、比较、查找
- ✅ 批量处理:使用循环处理数组和字符串
关键技能:
- 数组设计:合理选择数组大小和数据类型
- 循环遍历:使用循环处理数组元素
- 算法思维:实现排序、搜索等基本算法
- 字符串处理:文本数据的各种操作
下一课预告:
下一节课我们将学习函数与递归初步,包括函数的定义、调用、参数传递,让代码更加模块化和可复用!