#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
// 结构体定义
struct Student {
string name;
int age;
int math;
int chinese;
};
struct School {
string teacher;
string grade;
Student students[40];
};
// 函数声明
void showMenu();
void helloWorldDemo();
void dataTypesDemo();
void operatorsDemo();
void controlStructuresDemo();
void arrayDemo();
void sortingDemo();
void stringDemo();
void functionDemo();
void pointerDemo();
void structDemo();
void searchDemo();
// 排序函数
void bubbleSort(int arr[], int n);
void selectionSort(int arr[], int n);
void bucketSort();
// 查找函数
void binarySearch();
// 函数参数传递演示
void valuePass(int a);
void referencePass(int &a);
void pointerPass(int *a);
// 数组操作函数
void arrayOperations();
int main() {
cout << "=== C++入门基础学习演示系统 ===" << endl;
cout << "基于优快云博客教程实现" << endl;
cout << "================================" << endl << endl;
int choice;
do {
showMenu();
cout << "请选择要演示的功能 (0退出): ";
cin >> choice;
cout << endl;
switch(choice) {
case 1:
helloWorldDemo();
break;
case 2:
dataTypesDemo();
break;
case 3:
operatorsDemo();
break;
case 4:
controlStructuresDemo();
break;
case 5:
arrayDemo();
break;
case 6:
sortingDemo();
break;
case 7:
stringDemo();
break;
case 8:
functionDemo();
break;
case 9:
pointerDemo();
break;
case 10:
structDemo();
break;
case 11:
searchDemo();
break;
case 0:
cout << "感谢使用C++学习演示系统!" << endl;
break;
default:
cout << "无效选择,请重新输入!" << endl;
}
if(choice != 0) {
cout << "\n按回车键继续...";
cin.ignore();
cin.get();
cout << "\n" << string(50, '=') << "\n" << endl;
}
} while(choice != 0);
return 0;
}
void showMenu() {
cout << "?? C++基础知识演示菜单:" << endl;
cout << "1. Hello World 程序" << endl;
cout << "2. 数据类型演示" << endl;
cout << "3. 运算符演示" << endl;
cout << "4. 程序流程结构" << endl;
cout << "5. 数组操作" << endl;
cout << "6. 排序算法" << endl;
cout << "7. 字符串处理" << endl;
cout << "8. 函数使用" << endl;
cout << "9. 指针操作" << endl;
cout << "10. 结构体应用" << endl;
cout << "11. 查找算法" << endl;
cout << "0. 退出程序" << endl;
cout << string(30, '-') << endl;
}
void helloWorldDemo() {
cout << "?? Hello World 程序演示" << endl;
cout << "这是C++的第一个程序:" << endl;
cout << "Hello World!" << endl;
cout << "\n程序结构说明:" << endl;
cout << "? #include<iostream> - 输入输出头文件" << endl;
cout << "? using namespace std - 使用标准命名空间" << endl;
cout << "? int main() - 主函数,程序入口" << endl;
cout << "? cout - 输出语句" << endl;
cout << "? return 0 - 程序正常结束" << endl;
}
void dataTypesDemo() {
cout << "?? 数据类型演示" << endl;
// 基本数据类型
bool boolVar = true;
char charVar = 'A';
int intVar = 42;
float floatVar = 3.14f;
double doubleVar = 3.141592653;
string stringVar = "Hello C++";
cout << "基本数据类型及其值:" << endl;
cout << "bool类型: " << boolVar << " (占用 " << sizeof(boolVar) << " 字节)" << endl;
cout << "char类型: " << charVar << " (占用 " << sizeof(charVar) << " 字节)" << endl;
cout << "int类型: " << intVar << " (占用 " << sizeof(intVar) << " 字节)" << endl;
cout << "float类型: " << floatVar << " (占用 " << sizeof(floatVar) << " 字节)" << endl;
cout << "double类型: " << doubleVar << " (占用 " << sizeof(doubleVar) << " 字节)" << endl;
cout << "string类型: " << stringVar << endl;
// 常量演示
const int CONSTANT_VALUE = 100;
cout << "\n常量演示:" << endl;
cout << "常量值: " << CONSTANT_VALUE << endl;
// 变量输入输出
cout << "\n请输入一个整数: ";
int userInput;
cin >> userInput;
cout << "您输入的数字是: " << userInput << endl;
}
void operatorsDemo() {
cout << "?? 运算符演示" << endl;
int a = 10, b = 3;
cout << "设 a = " << a << ", b = " << b << endl;
// 算术运算符
cout << "\n算术运算符:" << endl;
cout << "a + b = " << (a + b) << endl;
cout << "a - b = " << (a - b) << endl;
cout << "a * b = " << (a * b) << endl;
cout << "a / b = " << (a / b) << endl;
cout << "a % b = " << (a % b) << endl;
// 自增自减
int c = a;
cout << "c++ = " << c++ << " (后自增,c现在是 " << c << ")" << endl;
c = a;
cout << "++c = " << ++c << " (先自增)" << endl;
// 关系运算符
cout << "\n关系运算符:" << endl;
cout << "a == b: " << (a == b) << endl;
cout << "a != b: " << (a != b) << endl;
cout << "a > b: " << (a > b) << endl;
cout << "a < b: " << (a < b) << endl;
cout << "a >= b: " << (a >= b) << endl;
cout << "a <= b: " << (a <= b) << endl;
// 逻辑运算符
cout << "\n逻辑运算符:" << endl;
bool x = true, y = false;
cout << "x && y: " << (x && y) << endl;
cout << "x || y: " << (x || y) << endl;
cout << "!x: " << (!x) << endl;
// 三目运算符
cout << "\n三目运算符:" << endl;
int max = (a > b) ? a : b;
cout << "max(a, b) = " << max << endl;
}
void controlStructuresDemo() {
cout << "?? 程序流程结构演示" << endl;
// if-else 演示
cout << "\n1. if-else 语句演示:" << endl;
int score = 85;
cout << "学生成绩: " << score << endl;
if(score >= 90) {
cout << "成绩等级: 优秀" << endl;
} else if(score >= 80) {
cout << "成绩等级: 良好" << endl;
} else if(score >= 60) {
cout << "成绩等级: 及格" << endl;
} else {
cout << "成绩等级: 不及格" << endl;
}
// switch 演示
cout << "\n2. switch 语句演示:" << endl;
int choice = 2;
cout << "选择技能 " << choice << ": ";
switch(choice) {
case 1:
cout << "一技能" << endl;
break;
case 2:
cout << "二技能" << endl;
break;
case 3:
cout << "三技能" << endl;
break;
default:
cout << "无效技能" << endl;
}
// for 循环演示
cout << "\n3. for 循环演示:" << endl;
cout << "输出1到5: ";
for(int i = 1; i <= 5; i++) {
cout << i << " ";
}
cout << endl;
// while 循环演示
cout << "\n4. while 循环演示:" << endl;
cout << "倒计时: ";
int countdown = 5;
while(countdown > 0) {
cout << countdown << " ";
countdown--;
}
cout << "发射!" << endl;
// do-while 循环演示
cout << "\n5. do-while 循环演示:" << endl;
int num = 1;
cout << "至少执行一次: ";
do {
cout << num << " ";
num++;
} while(num <= 3);
cout << endl;
// 嵌套循环演示
cout << "\n6. 嵌套循环演示 (打印星号矩形):" << endl;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 6; j++) {
cout << "* ";
}
cout << endl;
}
}
void arrayDemo() {
cout << "?? 数组操作演示" << endl;
// 一维数组
cout << "\n1. 一维数组演示:" << endl;
int arr[5] = {10, 20, 30, 40, 50};
cout << "数组元素: ";
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "数组大小: " << sizeof(arr) << " 字节" << endl;
cout << "元素个数: " << sizeof(arr)/sizeof(arr[0]) << endl;
cout << "首地址: " << arr << endl;
// 二维数组
cout << "\n2. 二维数组演示 (学生成绩表):" << endl;
int scores[3][3] = {
{89, 95, 98},
{79, 78, 56},
{90, 99, 93}
};
string names[3] = {"张三", "李四", "王五"};
string subjects[3] = {"语文", "数学", "英语"};
cout << setw(8) << "姓名";
for(int j = 0; j < 3; j++) {
cout << setw(8) << subjects[j];
}
cout << setw(8) << "总分" << endl;
for(int i = 0; i < 3; i++) {
cout << setw(8) << names[i];
int sum = 0;
for(int j = 0; j < 3; j++) {
cout << setw(8) << scores[i][j];
sum += scores[i][j];
}
cout << setw(8) << sum << endl;
}
// 数组操作演示
arrayOperations();
}
void arrayOperations() {
cout << "\n3. 数组增删改查演示:" << endl;
char arr[10] = {'a', 'b', 'c', 'd', 'e'};
int size = 5;
cout << "初始数组: ";
for(int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// 插入操作
char insertChar = 'X';
int insertPos = 2;
for(int i = size; i > insertPos; i--) {
arr[i] = arr[i-1];
}
arr[insertPos] = insertChar;
size++;
cout << "在位置" << insertPos << "插入'" << insertChar << "': ";
for(int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// 删除操作
int deletePos = 1;
for(int i = deletePos; i < size-1; i++) {
arr[i] = arr[i+1];
}
size--;
cout << "删除位置" << deletePos << "的元素: ";
for(int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// 修改操作
arr[0] = 'Z';
cout << "修改第一个元素为'Z': ";
for(int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// 查找操作
char searchChar = 'c';
int foundPos = -1;
for(int i = 0; i < size; i++) {
if(arr[i] == searchChar) {
foundPos = i;
break;
}
}
if(foundPos != -1) {
cout << "字符'" << searchChar << "'在位置: " << foundPos << endl;
} else {
cout << "未找到字符'" << searchChar << "'" << endl;
}
}
void sortingDemo() {
cout << "?? 排序算法演示" << endl;
// 冒泡排序演示
cout << "\n1. 冒泡排序演示:" << endl;
int bubbleArr[] = {64, 34, 25, 12, 22, 11, 90};
int n1 = sizeof(bubbleArr)/sizeof(bubbleArr[0]);
cout << "排序前: ";
for(int i = 0; i < n1; i++) {
cout << bubbleArr[i] << " ";
}
cout << endl;
bubbleSort(bubbleArr, n1);
cout << "排序后: ";
for(int i = 0; i < n1; i++) {
cout << bubbleArr[i] << " ";
}
cout << endl;
// 选择排序演示
cout << "\n2. 选择排序演示:" << endl;
int selectionArr[] = {29, 10, 14, 37, 13};
int n2 = sizeof(selectionArr)/sizeof(selectionArr[0]);
cout << "排序前: ";
for(int i = 0; i < n2; i++) {
cout << selectionArr[i] << " ";
}
cout << endl;
selectionSort(selectionArr, n2);
cout << "排序后: ";
for(int i = 0; i < n2; i++) {
cout << selectionArr[i] << " ";
}
cout << endl;
// 桶排序演示
cout << "\n3. 桶排序演示:" << endl;
bucketSort();
}
void bubbleSort(int arr[], int n) {
for(int i = 0; i < n-1; i++) {
bool swapped = false;
for(int j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = true;
}
}
if(!swapped) break; // 优化:如果没有交换,说明已经有序
}
}
void selectionSort(int arr[], int n) {
for(int i = 0; i < n-1; i++) {
int minIdx = i;
for(int j = i+1; j < n; j++) {
if(arr[j] < arr[minIdx]) {
minIdx = j;
}
}
if(minIdx != i) {
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}
}
void bucketSort() {
const int N = 20;
int bucket[N] = {0};
int data[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
int dataSize = sizeof(data)/sizeof(data[0]);
cout << "原始数据: ";
for(int i = 0; i < dataSize; i++) {
cout << data[i] << " ";
}
cout << endl;
// 统计每个数字出现的次数
for(int i = 0; i < dataSize; i++) {
bucket[data[i]]++;
}
cout << "桶排序结果: ";
for(int i = 0; i < N; i++) {
for(int j = 0; j < bucket[i]; j++) {
cout << i << " ";
}
}
cout << endl;
}
void stringDemo() {
cout << "?? 字符串处理演示" << endl;
// C风格字符串
cout << "\n1. C风格字符串操作:" << endl;
char str1[20] = "Hello";
char str2[20] = "World";
char str3[40];
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
cout << "str1长度: " << strlen(str1) << endl;
// 字符串拷贝
strcpy(str3, str1);
cout << "拷贝str1到str3: " << str3 << endl;
// 字符串连接
strcat(str3, " ");
strcat(str3, str2);
cout << "连接后的str3: " << str3 << endl;
// 字符串比较
cout << "strcmp(str1, str2): " << strcmp(str1, str2) << endl;
// C++ string类
cout << "\n2. C++ string类操作:" << endl;
string s1 = "Hello";
string s2 = "World";
string s3 = s1 + " " + s2;
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
cout << "s3: " << s3 << endl;
cout << "s3长度: " << s3.length() << endl;
// 查找操作
size_t pos = s3.find("World");
if(pos != string::npos) {
cout << "在s3中找到'World',位置: " << pos << endl;
}
// 子字符串
string sub = s3.substr(0, 5);
cout << "s3的前5个字符: " << sub << endl;
}
void functionDemo() {
cout << "?? 函数使用演示" << endl;
cout << "\n1. 参数传递方式演示:" << endl;
int value = 10;
cout << "原始值: " << value << endl;
// 值传递
valuePass(value);
cout << "值传递后: " << value << endl;
// 引用传递
referencePass(value);
cout << "引用传递后: " << value << endl;
// 指针传递
pointerPass(&value);
cout << "指针传递后: " << value << endl;
}
void valuePass(int a) {
a = a * 2;
cout << "函数内值传递: " << a << endl;
}
void referencePass(int &a) {
a = a * 2;
cout << "函数内引用传递: " << a << endl;
}
void pointerPass(int *a) {
*a = *a * 2;
cout << "函数内指针传递: " << *a << endl;
}
void pointerDemo() {
cout << "?? 指针操作演示" << endl;
// 基本指针操作
cout << "\n1. 基本指针操作:" << endl;
int a = 42;
int *p = &a;
cout << "变量a的值: " << a << endl;
cout << "变量a的地址: " << &a << endl;
cout << "指针p的值(a的地址): " << p << endl;
cout << "指针p指向的值: " << *p << endl;
// 修改指针指向的值
*p = 100;
cout << "通过指针修改后,a的值: " << a << endl;
// const修饰指针
cout << "\n2. const修饰指针:" << endl;
int b = 20;
const int *p1 = &a; // 指向常量的指针
int * const p2 = &a; // 常量指针
cout << "const int *p1 = &a; (指向常量的指针)" << endl;
cout << "int * const p2 = &a; (常量指针)" << endl;
// 指针与数组
cout << "\n3. 指针与数组:" << endl;
int arr[] = {1, 2, 3, 4, 5};
int *arrPtr = arr;
cout << "数组元素: ";
for(int i = 0; i < 5; i++) {
cout << *(arrPtr + i) << " ";
}
cout << endl;
cout << "使用指针遍历: ";
for(int *ptr = arr; ptr < arr + 5; ptr++) {
cout << *ptr << " ";
}
cout << endl;
}
void structDemo() {
cout << "??? 结构体应用演示" << endl;
// 基本结构体使用
cout << "\n1. 基本结构体使用:" << endl;
Student stu1;
stu1.name = "张三";
stu1.age = 18;
stu1.math = 95;
stu1.chinese = 88;
cout << "学生信息:" << endl;
cout << "姓名: " << stu1.name << endl;
cout << "年龄: " << stu1.age << endl;
cout << "数学: " << stu1.math << endl;
cout << "语文: " << stu1.chinese << endl;
// 结构体数组
cout << "\n2. 结构体数组:" << endl;
Student students[3] = {
{"李四", 19, 92, 85},
{"王五", 18, 88, 90},
{"赵六", 20, 95, 87}
};
cout << setw(8) << "姓名" << setw(6) << "年龄"
<< setw(8) << "数学" << setw(8) << "语文" << setw(8) << "总分" << endl;
for(int i = 0; i < 3; i++) {
int total = students[i].math + students[i].chinese;
cout << setw(8) << students[i].name
<< setw(6) << students[i].age
<< setw(8) << students[i].math
<< setw(8) << students[i].chinese
<< setw(8) << total << endl;
}
// 结构体指针
cout << "\n3. 结构体指针:" << endl;
Student *stuPtr = &stu1;
cout << "通过指针访问:" << endl;
cout << "姓名: " << stuPtr->name << endl;
cout << "年龄: " << stuPtr->age << endl;
// 嵌套结构体
cout << "\n4. 嵌套结构体(班级管理):" << endl;
School class1;
class1.grade = "高一(3)班";
class1.teacher = "李老师";
class1.students[0] = {"小明", 16, 90, 85};
class1.students[1] = {"小红", 16, 88, 92};
cout << "班级: " << class1.grade << endl;
cout << "班主任: " << class1.teacher << endl;
cout << "学生信息:" << endl;
for(int i = 0; i < 2; i++) {
cout << " " << class1.students[i].name
<< " - 数学:" << class1.students[i].math
<< " 语文:" << class1.students[i].chinese << endl;
}
}
void searchDemo() {
cout << "?? 查找算法演示" << endl;
cout << "\n二分查找演示:" << endl;
binarySearch();
}
void binarySearch() {
int arr[] = {1, 2, 4, 6, 8, 9, 13, 15, 20, 25};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "有序数组: ";
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
int target = 13;
cout << "查找目标: " << target << endl;
int left = 0, right = n - 1;
int steps = 0;
bool found = false;
while(left <= right) {
steps++;
int mid = (left + right) / 2;
cout << "第" << steps << "步: 检查位置" << mid << ",值为" << arr[mid];
if(arr[mid] == target) {
cout << " - 找到目标!" << endl;
cout << "目标" << target << "在数组中的索引位置: " << mid << endl;
found = true;
break;
} else if(arr[mid] < target) {
cout << " - 目标在右半部分" << endl;
left = mid + 1;
} else {
cout << " - 目标在左半部分" << endl;
right = mid - 1;
}
}
if(!found) {
cout << "未找到目标值" << endl;
}
cout << "总共进行了 " << steps << " 步查找" << endl;
} 详细分析