本博客配套视频:黑马程序员 本博客参考博客:https://blog.csdn.net/sunshine_world/article/details/107688106
错误示例:单引号不行,这里不像Python
cout << 'hello world' << endl;作用:给一个指定的内存空间起名,方便操作这段内存 语法:数据类型 变量名 = 初始值;
#include <iostream> using namespace std; int main() { //变量创建的语法:数据类型 变量名 = 变量初始值 int a = 10; cout << "a=" << a << endl; system("pause"); return 0; } a=10 请按任意键继续. . .注意:C++在创建变量时,必须给变量一个初始值,否则会报错
作用:用于记录程序中不可更改的数据
两种定义方法:
#define 常量名 常量值
一般定义在代码的上方,结尾不需要分号 #define Day 7 //意思就是Day以后就代表7例:
#include <iostream> using namespace std; #define Day 7 // 定义在这里,主函数前面 int main() { //Day = 14; 错误,Day不可以修改 cout << "一周共有多少天:" <<Day<< endl; system("pause"); return 0; }const 数据类型 常量名 = 常量值
通常在变量定义前加 const,修饰该变量为常量 注意:需要分号结尾 //const修饰的变量也称为常量 const int day = 7;例
#include <iostream> using namespace std; int main() { const int day = 7; cout << "一周共有多少天:" << day << endl; system("pause"); return 0; } 一周共有多少天:7 请按任意键继续. . .作用: C++中预先保留的单词(标识符)
在定义变量或者常量时候,不要用关键字错误示例:int不能作为变量名
int int = 10;注意:在给变量或者常量起名称时候,不要用C++得关键字,否则会产生歧义
作用:C++规定给标识符(变量、常量)命名时,有一套自己的规则
1.标识符不能是关键字 int int =10; //错误示例 2.标识符只能有字母,数字,下划线组成 int abc = 10; int _abc = 20; int _123abc = 30; 3.第一个字符必须为字母或下划线,不能是数字 //int 123abc = 30; //错误 4.标识符字母区分大小写 int aaa = 40; int AAA = 50;建议:给标识符命名时,争取做到见名知意的效果,方便自己和他人的阅读
C++规定在创建一个变量或者常量时,必须要指定初相应的数据类型,否则无法给变量分配内存
作用:整形变量表示的是整数类型的数据
以下集中都能表示整型,区别在于所占用内存空间不同:
一般使用int即可
#include <iostream> using namespace std; int main() { //1.短整型(-32768~32767) short num1 = 32768; // 超出了范围 //2.整型 int num2 = 32768; //3.长整型 long num3 = 10; //4.长长整型 long long num4 = 10; cout << num1 << endl; cout << num2 << endl; cout << num3 << endl; cout << num4 << endl; system("pause"); return 0; } -32768 32768 10 10 请按任意键继续. . .作用:统计数据类型所占内存大小 语法:sizeof(数据类型/ 变量)
#include <iostream> using namespace std; int main() { //整型:short (2), int (4), long (4), long long (8) //可以利用sizeof求出数据类型占用内存大小 short num1 = 10; cout << "short占用的内存空间为:" << sizeof(short) << endl; cout << "short占用的内存空间为:" << sizeof(num1) << endl; int num2 = 10; cout << "int占用的内存空间为:" << sizeof(int) << endl; long num3 = 10; cout << "long占用的内存空间为:" << sizeof(long) << endl; long long num4 = 10; cout << "long long占用的内存空间为:" << sizeof(num4) << endl; system("pause"); return 0; } short占用的内存空间为:2 short占用的内存空间为:2 int占用的内存空间为:4 long占用的内存空间为:4 long long占用的内存空间为:8 请按任意键继续. . .作用: 用于表示小数
浮点型变量有两种:单精度float、双精度double
两者的区别在于表示的有效数字范围不同
默认情况下输出一个小数会显示6位有效数字
int main() { //默认情况下输出一个小数会显示6位有效数字 float f1 = 3.14f; //通常在float后面多加一个f cout << "f1=" << f1 << endl; float f2 = 3.1415926f; cout << "f2=" << f2 << endl; system("pause"); return 0; } f1=3.14 f2=3.14159 请按任意键继续. . .例2:统计占用的内存空间
#include <iostream> using namespace std; int main() { //统计float和double占用的内存空间 cout << "float占用的内存空间为:" << sizeof(float) << endl; cout << "double占用的内存空间为:" << sizeof(double) << endl; system("pause"); return 0; } float占用的内存空间为:4 double占用的内存空间为:8 请按任意键继续. . .例3:科学计数法
#include <iostream> using namespace std; int main() { //科学计数法 float f2 = 3e2; //3*10^2 cout << "f2=" << f2 << endl; float f3 = 3e-2; //3*10^-2 cout << "f3=" << f3 << endl; system("pause"); return 0; } f2=300 f3=0.03 请按任意键继续. . .作用:可以表示单个字母的数据类型 语法:char ch = 'a';
注意1:单引号,不是双引号
char ch = 'a'; // char ch2 = "b" 双引号错误注意2:单引号里面只能写一个字符,不能写字符串
// char ch2 = 'abcd' 单引号内只能有一个字符 C和C++中字符型变量只占用1个字节字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII码放入对应的存储单元例1:
#include <iostream> using namespace std; int main() { cout << "char字符型变量所占用内存" << sizeof(char) << endl; system("pause"); return 0; } char字符型变量所占用内存1 请按任意键继续. . .例2: 比如a对应97,所以存储97
#include <iostream> using namespace std; int main() { //a - 97 //A - 65 //相差32 //(int)转化为了整型 char ch = 'a'; cout << (int)ch << endl; system("pause"); return 0; } 97 请按任意键继续. . .作用:用于表示一些不能显示出来的ASCII字符 现阶段我们常用的转义字符有: \n \ \t
宽度一共八个,前面有n个a,后面就8-n个空格
aaa hello aa hello aaaa hello 请按任意键继续. . .作用:用于表示一串字符
两种风格:
char 变量名[] = "字符串值";
#include <iostream> using namespace std; int main() { char str1[] = "abc"; cout << "str1=" << str1 << endl; system("pause"); return 0; } str1=abc 请按任意键继续. . .注意:C风格的字符串要用双引号括起来
string 变量名 = "字符串值"; 注意:老版本可能需要包含string头文件
#include <iostream> using namespace std; #include <string> //2019不需要,老版本可能需要这个头文件 int main() { string str1 = "abc"; cout << "str1=" << str1 << endl; system("pause"); return 0; }作用:布尔值数据类型代表真或假的值
truefalsebool类型占1个字节
#include <iostream> using namespace std; int main() { bool flag = true; cout << flag << endl; cout << "bool类型所占用空间" << sizeof(flag) << endl; system("pause"); return 0; } 1 bool类型所占用空间1作用:用于从键盘获取数据 关键字:cin 语法:cin >> 变量 例1:整型赋值
#include <iostream> using namespace std; int main() { int a = 0; // 初始化 cout << "请给整形变量a赋值:" << endl; cin >> a; cout << "赋值后a为:" << a << endl; system("pause"); return 0; } 请给整形变量a赋值: 12 赋值后a为:12 请按任意键继续. . .例2:浮点型赋值
#include <iostream> using namespace std; int main() { float f = 3.14f; cout << "请给浮点型f赋值:" << endl; cin >> f; cout << "赋值后f为:" << f << endl; system("pause"); return 0; } 请给浮点型f赋值: 3.1415 赋值后f为:3.1415 请按任意键继续. . .例3:字符型
#include <iostream> using namespace std; int main() { char ch = 'a'; cout << "请给字符型ch赋值:" << endl; cin >> ch; cout << "赋值后ch为:" << ch << endl; system("pause"); return 0; } 请给字符型ch赋值: b 赋值后ch为:b 请按任意键继续. . .例4:字符串型
#include <iostream> using namespace std; #include <string> int main() { char str1[] = "abc"; string str2 = "abc"; cout << "请给字符串str1,str2赋值:" << endl; cin >> str1; cin >> str2; cout << "赋值后str1为:" << str1 << endl; cout << "赋值后str2为:" << str2 << endl; system("pause"); return 0; } 请给字符串str1,str2赋值: abcd abcde 赋值后str1为:abcd 赋值后str2为:abcde 请按任意键继续. . .例5:bool类型 除了0代表假,其他都为真
#include <iostream> using namespace std; #include <string> int main() { bool flag = false; cout << "请给bool类型flag赋值:" << endl; cin >> flag; cout << "赋值后:" << flag << endl; system("pause"); return 0; } 请给bool类型flag赋值: 2020 赋值后:1 请按任意键继续. . . 请给bool类型flag赋值: 3.14 赋值后:1 请按任意键继续. . .作用:用于执行代码的运算
作用:用于处理四则运算
例1:整数相除只输出整数
#include <iostream> using namespace std; int main() { int a1 = 10; int b1 = 3; cout << a1 / b1 << endl; int a2 = 10; int b2 = 20; cout << a2 / b2 << endl; system("pause"); return 0; } 3 0 请按任意键继续. . .注意:除数不能为0 错误示例:
int a1 = 10; int b1 = 0; //cout << a1 / b1 << endl;例2:小数相除,除不尽则输出小数
#include <iostream> using namespace std; int main() { double d1 = 0.5; double d2 = 0.2; double d3 = 0.1; cout << d1 / d2 << endl; cout << d2 / d3 << endl; system("pause"); return 0; } 2.5 2 请按任意键继续. . .注意:两个小数不能取模 总结:只有整型变量可以进行取模运算
#include <iostream> using namespace std; int main() { int a1 = 10; int b1 = 3; int c1 = 20; int d1 = 0; cout << a1 % b1 << endl; cout << a1 % c1 << endl; //cout << a1 % d1 << endl; system("pause"); return 0; } 1 10 请按任意键继续. . .前置递增 先让变量+1 然后进行表达式的运算 后置递增 先进行表达式的运算 后让变量+1
#include <iostream> using namespace std; int main() { //1、前置递增 int a = 10; ++a; cout << "a=" << a << endl; //2、后置递增 int b = 10; b++; cout << "b=" << b << endl; //3、前置和后置的区别 //前置递增 先让变量+1 然后进行表达式的运算 //后置递增 先进行表达式的运算 后让变量+1 int a2 = 10; int b2 = ++a2 * 10; cout << "a2=" << a2 << endl; cout << "b2=" << b2 << endl; int a3 = 10; int b3 = a3++ * 10; cout << "a3=" << a3 << endl; cout << "b3=" << b3 << endl; system("pause"); return 0; } a=11 b=11 a2=11 b2=110 a3=11 b3=100 请按任意键继续. . .作用:用于将表达式的值赋给变量 这里是和Python一样的,例如: a+=2意思就是a=a+2,其他运算符一个意思。
作用:用于表达式的比较,并返回一个真值或假值
只会输出0(假)和1(真)
#include <iostream> using namespace std; int main() { int a = 1; int b = 2; cout << (a <= b) << endl; cout << (a != b) << endl; cout << (a > b) << endl; system("pause"); return 0; } 1 1 0 请按任意键继续. . .作用:用于根据表达式的值返回真值或假值
只会输出0(假)和1(真)
#include <iostream> using namespace std; int main() { int a = 1; int b = 2; cout << (!a) << endl; cout << (!!a) << endl; cout << (a && b) << endl; cout << (a || b) << endl; system("pause"); return 0; } 0 1 1 1 请按任意键继续. . .C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
顺序结构:程序按顺序执行,不发生跳转选择结构:依据条件是否满足,有选择的执行相应功能循环机构:依据条件是否满足,循环多次执行某段代码四种形式:
单行格式if语句多行格式if语句多条件if语句嵌套if语句例1:单行格式 格式:if(条件){条件满足执行语句}
#include <iostream> using namespace std; int main() { int score = 0; cout << "请输入分数:" << endl; cin >> score; if (score >= 90) // 这里不要加分号 { cout << 'a' << endl; } system("pause"); return 0; } 请输入分数: 92 a 请按任意键继续. . .注意:if条件表达式后不要加分号
例2:多行格式 格式:if(条件){条件满足执行的语句}else{条件不满足执行的语句}
#include <iostream> using namespace std; int main() { int score = 0; cout << "请输入分数:" << endl; cin >> score; if (score >= 90) // 这里不要加分号 { cout << 'a' << endl; } else { cout << 'b' << endl; } system("pause"); return 0; } 请输入分数: 89 b 请按任意键继续. . .例3:多条件if语句 格式:if(条件1){条件1满足执行的语句}else if(条件2){条件2满足执行的语句} ... else{都不满足执行的语句}
#include <iostream> using namespace std; int main() { int score = 0; cout << "请输入分数:" << endl; cin >> score; if (score >= 90) // 这里不要加分号 { cout << 'a' << endl; } else if (score >= 80) { cout << 'b' << endl; } else if (score >= 70) { cout << 'c' << endl; } else { cout << 'd' << endl; } system("pause"); return 0; } 请输入分数: 62 d 请按任意键继续. . .例4:嵌套if语句
#include <iostream> using namespace std; int main() { int score = 0; cout << "请输入分数:" << endl; cin >> score; if (score >= 60) // 这里不要加分号 { cout << "恭喜你及格了" << endl; if (score >= 90) { cout << "你的等级为a" << endl; } else { cout << "你的等级为b" << endl; } } else { cout << "再接再厉" << endl; } system("pause"); return 0; } 请输入分数: 92 恭喜你及格了 你的等级为a 请按任意键继续. . .练习:三只小猪称体重
#include <iostream> using namespace std; int main() { // 三只小猪称体重,判断哪个最重 int a = 0; int b = 0; int c = 0; cout << "请输入小猪a的体重:" << endl; cin >> a; cout << "请输入小猪b的体重:" << endl; cin >> b; cout << "请输入小猪c的体重:" << endl; cin >> c; if ((a > b) && (a > c)) { cout << "小猪a最重" << endl; } else if ((b > a) && (b > c)) { cout << "小猪b最重" << endl; } else if ((c > b) && (c > a)) { cout << "小猪c最重" << endl; } system("pause"); return 0; } 请输入小猪a的体重: 150 请输入小猪b的体重: 200 请输入小猪c的体重: 300 小猪c最重 请按任意键继续. . .作用:通过三目运算符实现简单的判断 语法:表达式1 ? 表达式2 : 表达式3
解释:
如果表达式1的值为真,执行表达式2,并返回表达式2的结果如果表达式1的值为假,执行表达式3,并返回表达式3的结果 #include <iostream> using namespace std; int main() { // 把a、b中最大的赋值给c int a = 10; int b = 20; int c = 0; c = (a > b ? a : b); cout << "c=" << c << endl; // 三目运算符返回的是变量,可以继续进行赋值 (a > b ? a : b) = 100; cout << "a=" << a << endl; cout << "b=" << b << endl; system("pause"); return 0; } c=20 a=10 b=100 请按任意键继续. . .执行多条件分支语句 语法:
switch(表达式) { case 结果1:执行语句; break; case 结果2:执行语句; break; ... default:执行语句; break; }例:
#include <iostream> using namespace std; int main() { int score = 0; cout << "请输入分数:" << endl; cin >> score; switch(score) { case 10: cout << 'a' << endl; break; case 9: cout << 'b' << endl; break; case 8: cout << 'c' << endl; break; case 7: cout << 'd' << endl; break; default: cout << 'f' << endl; break; } system("pause"); return 0; } 请输入分数: 5 f 请按任意键继续. . .if和switch的区别:
switch缺点:判断时候只能是整整或字符型,不可以是一个区间switch优点:结构清晰,执行效率高作用:满足循环条件,执行循环语句 语法:while(循环条件) {循环语句}
解释:只要循环条件的结果为真,就执行循环语句 例1
#include <iostream> using namespace std; int main() { int i = 0; int n = 0; while(i<=5) { n = ++i; cout << n << endl; } system("pause"); return 0; } 1 2 3 4 5 6 请按任意键继续. . .例2
int main() { int j = 0; int m = 0; while (j <= 5) { m = j++; cout << m << endl; } system("pause"); return 0; } 0 1 2 3 4 5 请按任意键继续. . .例1
可是这个版本生成的随机数是固定的,我下次在运行直接猜对
请输入数字: 42 恭喜你猜对了 请按任意键继续. . .例2:解决方法
#include <iostream> using namespace std; #include <ctime> int main() { // 添加随机数种子 作用利用当前系统时间随机生成数,放置每次随机数都一样 srand((unsigned int)time(NULL)); // 猜数字游戏 //rand()%100 //随机生成数 0~99 int num = rand() % 100 + 1; int number = 0; while (number != num) { cout << "请输入数字:" << endl; cin >> number; if (number > num) { cout << "猜测过大" << endl; } else if (number < num) { cout << "猜测过小" << endl; } } cout << "恭喜你猜对了" << endl; system("pause"); return 0; }产生的随机数再也不是42了
请输入数字: 42 猜测过小 请输入数字: 56 猜测过小 请输入数字: 78 猜测过大 请输入数字: 70 猜测过大 请输入数字: 65 猜测过小 请输入数字: 67 猜测过小 请输入数字: 69 猜测过大 请输入数字: 68 恭喜你猜对了 请按任意键继续. . .语法:do{循环语句] while(循环条件); 注意:与while的区别在于do…while会先执行一次循环语句,再判断循环条件
#include <iostream> using namespace std; int main() { int a = 0; do { cout << a << endl; a++; } while (a < 10); system("pause"); return 0; } 0 1 2 3 4 5 6 7 8 9 请按任意键继续. . .案例描述:水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身 例如:1^3 + 5^3 + 3^3 =153 请利用do…while语句,求出所有33位数的水仙花数
#include <iostream> using namespace std; int main() { int n = 100; int a = 0; int b = 0; int c = 0; do { a = n / 100; b = (n - a * 100) / 10; c = n - a * 100 - b * 10; if (n == (a*a*a + b*b*b + c*c*c)) { cout << n << endl; } n++; } while (n < 1000); system("pause"); return 0; } 153 370 371 407 请按任意键继续. . .注意:这里不可以打
n == a ^ 3 + b ^ 3 + c ^ 3;作用:满足循环条件,执行循环语句 语法:for(起始表达式;条件表达式;末尾表达式){循环语句};
#include <iostream> using namespace std; int main() { for (int i = 0; i < 10; i++) { cout << i << endl; } system("pause"); return 0; } 0 1 2 3 4 5 6 7 8 9 请按任意键继续. . .注意:for循环中的表达式,要用分号进行分隔
例2
#include <iostream> using namespace std; int main() { int i = 0; for (; ; ) { if (i >= 10) { break; } cout << i << endl; i++; } system("pause"); return 0; } 0 1 2 3 4 5 6 7 8 9 请按任意键继续. . .案例描述:从1开始数到数字100, 如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。
#include <iostream> using namespace std; int main() { int i = 0; int a = 0; int b = 0; int c = 0; while(i<100) { a = i / 10; b = i - a * 10; c = i / 7; if ((a == 7) || (b == 7) || ((i == c * 7) && (c>0))) { cout << "敲桌子" << endl; } else { cout << i << endl; } i++; } system("pause"); return 0; }
外层循环一次,内层循环一周
作用:用于跳出选择结构或者循环结构 break使用的时机:
出现在switch条件语句中,作用是终止case并跳出switch出现在循环语句中,作用是跳出当前的循环语句出现在嵌套语句中,跳出最近的内层循环语句例1:switch语句中
#include <iostream> using namespace std; int main() { cout << "请选择副本难度" << endl; cout << "1、普通" << endl; cout << "2、中等" << endl; cout << "3、困难" << endl; int select = 0; cin >> select; switch (select) { case 1: cout << "您选择的是普通难度" << endl; break; case 2: cout << "您选择的是中等难度" << endl; break; case 3: cout << "您选择的是困难难度" << endl; break; default: break; } system("pause"); return 0; } 请选择副本难度 1、普通 2、中等 3、困难 1 您选择的是普通难度 请按任意键继续. . .例2:在for循环中
#include <iostream> using namespace std; int main() { for (int i = 0; i < 10; i++) { if (i == 5) { break; } cout << i << endl; } system("pause"); return 0; } 0 1 2 3 4 请按任意键继续. . .例3:在嵌套for循环中退出内层循环
#include <iostream> using namespace std; int main() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (j==5) { break; } cout << "* "; } cout << endl; } system("pause"); return 0; }作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环
#include <iostream> using namespace std; int main() { for (int i = 0; i < 10; i++) { if ((i == 5)||(i == 8)) { continue; } cout << i << endl; } system("pause"); return 0; } 0 1 2 3 4 6 7 9 请按任意键继续. . .注意:continue并没有使整个循环终止,而break会跳出循环
作用:可以无条件跳转语句 语法:goto 标记 解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
#include <iostream> using namespace std; int main() { cout << "1" << endl; goto FLAG; // 标记 cout << "2" << endl; cout << "3" << endl; cout << "4" << endl; FLAG: // 从标记处跳到这里 cout << "5" << endl; system("pause"); return 0; } 1 5 请按任意键继续. . .注意:在程序中不建议使用goto语句,以免造成程序流程混乱
所谓数组,就是一个集合,里面存放了相同类型的数据元素
特点1:数组中每个元素都是相同的数据类型 特点2:数组是由连续的内存位置组成的
一维数组定义的三种方式: 1.数据类型 数组名[数组长度]; 2.数据类型 数组名[数组长度] = {值1,值2,...}; 3.数据类型 数组名[] = {值1,值2,...};
总结1:数组名的命名规范与变量名命名规范一致,不要和变量重名 总结2:数组中下标是从0开始索引
例1:第一种方式
#include <iostream> using namespace std; int main() { int arr[5]; arr[0] = 0; arr[1] = 1; arr[2] = 2; arr[3] = 3; arr[4] = 4; // 访问数组中元素 cout << arr[0] << endl; cout << arr[1] << endl; cout << arr[2] << endl; cout << arr[3] << endl; cout << arr[4] << endl; system("pause"); return 0; } 0 1 2 3 4 请按任意键继续. . .例2:第二种方式
#include <iostream> using namespace std; int main() { int arr[5] = { 0,1,2,3,4 }; // 访问数组中元素 cout << arr[0] << endl; cout << arr[1] << endl; cout << arr[2] << endl; cout << arr[3] << endl; cout << arr[4] << endl; system("pause"); return 0; }输出结果一样
注意:如果初始化数据的时候,没有全部填写完,会用0来填补剩余的数据
int arr[5] = { 0,1 };例3:第三种方式
#include <iostream> using namespace std; int main() { int arr[] = { 0,1,2,3,4,5,6 }; for (int i = 0; i < 7; i++) { // 访问数组中元素 cout << arr[i] << endl; } system("pause"); return 0; } 0 1 2 3 4 5 6 请按任意键继续. . .一维数组名称的用途
可以统计整个数组在内存中的长度可以获取数组在内存中的首地址 #include <iostream> using namespace std; int main() { int arr[] = { 0,1,2,3,4,5,6 }; cout << "整个数组占用的内存空间:"<<sizeof(arr) << endl; cout << "每个元素占用的内存空间:" << sizeof(arr[0]) << endl; cout << "元素个数:" << sizeof(arr) / sizeof(arr[0]) << endl; // 统计元素个数 cout << "数组首地址:" << (int)arr << endl; //(int)强制转化为十进制 cout << "数组中第一个元素地址:" << (int)&arr[0] << endl; cout << "数组中第二个元素地址:" << (int)&arr[1] << endl; system("pause"); return 0; } 整个数组占用的内存空间:28 每个元素占用的内存空间:4 元素个数:7 数组首地址:-1155598808 数组中第一个元素地址:-1155598808 数组中第二个元素地址:-1155598804 请按任意键继续. . .注意:数组名是常量,不可以进行赋值操作 错误示例:
arr = 100注意:数组名是常量,不可以赋值
总结1:直接打印数组名,可以查看数组所占内存的首地址
总结2:对数组名进行sizeof,可以获取整个数组占内存空间的大小
案例描述: 在一个数组中记录了五只小猪的体重,如: int arr[5] = {300, 350, 200, 400, 250}; 找出并打印最重的小猪体重
#include <iostream> using namespace std; int main() { int arr[5] = { 300, 350, 200, 400, 250 }; int max = 0; for (int i = 0; i < 5; i++) { if (arr[i] > max) { max = arr[i]; } } cout << "max=" << max << endl; system("pause"); return 0; } max=400 请按任意键继续. . .案例描述:请申明一个5个元素的数组,并且将元素逆置 (如原数组元素为:1,3,2,5,4;逆置结果为:4,5,2,3,1)
#include <iostream> using namespace std; int main() { int arr[5] = { 1, 3, 2, 5, 4 }; cout << "逆置前:" << endl; for (int i = 0; i < 5; i++) { cout << arr[i]; int start = 0; int end = sizeof(arr) / sizeof(arr[0]) - 1; while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } cout << endl; cout << "逆置后:" << endl; for (int i = 0; i < 5; i++) { cout << arr[i]; } cout << endl; system("pause"); return 0; } 逆置前: 15234 逆置后: 45231 请按任意键继续. . .例2
#include <iostream> using namespace std; int main() { int arr[5] = { 1, 3, 2, 5, 4 }; int arr1[5] = { 1, 3, 2, 5, 4 }; int j = 0; for (int i = 0; i < 5; i++) { cout << arr[i]; j = 4 - i; arr1[j] = arr[i]; } cout << endl; for (int j = 0; j < 5; j++) { cout << arr1[j]; } cout << endl; system("pause"); return 0; } 13254 45231 请按任意键继续. . .作用:最常用的排序算法,对数组内元素进行排序
比较相邻的元素。如果第一个比第二个打,就交换他们两个。对每一对相邻元素做相同的工作,执行完毕后,找到第一个最大值。重复以上的步骤,每次比较次数-1,知道不需要比较排序总轮数 = 元素个数 - 1 每轮对比次数 = 元素个数 - 排序轮数 - 1 从第0轮开始
大的数字在最后面冒出来
#include <iostream> using namespace std; int main() { int arr[9] = { 4,2,8,0,5,7,1,3,9 }; cout << "排序前:" << endl; for (int i = 0; i < 9; i++) { cout << arr[i] << " "; } cout << endl; //开始冒泡排序 //总共排序轮数为 元素个数 - 1 for (int i = 0; i < 9 - 1; i++) { //内层循环对比 次数 = 元素个数 - 当前轮数 - 1 for (int j = 0; j < 9 - i - 1; j++) { //如果第一个数字,比第二个数字大,交换两个数字 if (arr[j] > arr[j + 1]) { int temp = arr[j]; //这三行代码实现这两数的交换 arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } //排序后结果 cout << "排序后:" << endl; for (int i = 0; i < 9; i++) { cout << arr[i] << " "; } cout << endl; system("pause"); return 0; } 排序前: 4 2 8 0 5 7 1 3 9 排序后: 0 1 2 3 4 5 7 8 9 请按任意键继续. . .二维数组就是在一维数组上,多加了一个维度
二维数组的四种定义方式: 1.数据类型 数组名 [行数] [列数]; 2.数据类型 数组名 [行数] [列数] = { {数据1,数据2},{数据3,数据4}}; 3.数据类型 数组名 [行数] [列数] = { 数据1,数据2,数据3,数据4}; 4.数据类型 数组名 [] [列数] = { 数据1,数据2,数据3,数据4};
例1:第一种定义
#include <iostream> using namespace std; int main() { int arr[2][2]; arr[0][0] = 0; arr[0][1] = 1; arr[1][0] = 2; arr[1][1] = 3; cout << arr[0][0] << endl; cout << arr[0][1] << endl; cout << arr[1][0] << endl; cout << arr[1][1] << endl; system("pause"); return 0; } 0 1 2 3 请按任意键继续. . .例2:第二种定义
#include <iostream> using namespace std; int main() { int arr[2][2] = { {1,2},{3,4} }; cout << arr[0][0] << endl; cout << arr[0][1] << endl; cout << arr[1][0] << endl; cout << arr[1][1] << endl; system("pause"); return 0; }例3:for嵌套输出二维数组
#include <iostream> using namespace std; int main() { int arr[2][3] = { {1,2,3},{4,5,6} }; //外层循环打印行数 //内层循环打印列数 for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << arr[i][j] << " "; } cout << endl; } system("pause"); return 0; } 1 2 3 4 5 6 请按任意键继续. . .例1:
#include <iostream> using namespace std; int main() { int arr[2][3] = { {1,2,3},{4,5,6} }; cout << "int类型二维数组占用的内存空间大小:" << sizeof(arr) << endl; double arr1[2][3] = { {1,2,3},{4,5,6} }; cout << "double类型二维数组占用的内存空间大小:" << sizeof(arr1) << endl; system("pause"); return 0; } int类型二维数组占用的内存空间大小:24 double类型二维数组占用的内存空间大小:48 请按任意键继续. . .例2
#include <iostream> using namespace std; int main() { int arr[2][3] = { {1,2,3},{4,5,6} }; cout << "二维数组占用的内存空间大小:" << sizeof(arr) << endl; cout << "二维数组第一行占用的内存空间大小:" << sizeof(arr[0]) << endl; cout << "二维数组第一个数据占用的内存空间大小:" << sizeof(arr[0][0]) << endl; cout << "二维数组多少个数据:" << sizeof(arr) / sizeof(arr[0][0]) << endl; cout << "二维数组行数:" << sizeof(arr) / sizeof(arr[0]) << endl; cout << "二维数组列数:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl; cout << "十六进制二维数组首地址" << arr << endl; cout << "十进制二维数组首地址" << (int)arr << endl; cout << "二维数组第一行首地址" << (int)arr[0] << endl; cout << "二维数组第二行首地址" << (int)arr[1] << endl; //相差12,隔着三个数据 cout << "二维数组第一个元素首地址" << (int)&arr[0][0] << endl; cout << "二维数组第二个元素首地址" << (int)&arr[0][1] << endl; system("pause"); return 0; } 二维数组占用的内存空间大小:24 二维数组第一行占用的内存空间大小:12 二维数组第一个数据占用的内存空间大小:4 二维数组多少个数据:6 二维数组行数:2 二维数组列数:3 十六进制二维数组首地址000000367812F768 十进制二维数组首地址2014508904 二维数组第一行首地址2014508904 二维数组第二行首地址2014508916 二维数组第一个元素首地址2014508904 二维数组第二个元素首地址2014508908 请按任意键继续. . .总结1:二维数组名就是这个数组的首地址
总结2:对二维数组名进行sizeof时,可以获取整个二维数组占用的内存空间大小
考试成绩统计: 案例描述:有三名同学(张三,李四,王五),在一次考试中的成绩分别如下表,请分别输出三名同学的总成绩
#include <iostream> using namespace std; int main() { int scores[3][3] = { {100,100,100}, {90,50,100}, {60,70,80} }; string names[3] = { "张三", "李四", "王五" }; for (int i = 0; i < 3; i++) { int num = 0; for (int j = 0; j < 3; j++) { num += scores[i][j]; } cout << names[i] << "的总分为:" << num << endl; } system("pause"); return 0; } 张三的总分为:300 李四的总分为:240 王五的总分为:210 请按任意键继续. . .作用:将一段经常使用的代码封装起来,减少重复代码
一个较大的程序,一般分为若干个程序块,每个程序实现特定的功能。
函数的定义一般主要有5个步骤: 1、返回值类型 2、函数名 3、参数列表 4、函数体语句 5、return表达式
语法:
返回值类型 函数名 (参数列表) { 函数体语句 return表达式 } 返回值类型 :一个函数可以返回一个值。在函数定义中函数名:给函数起个名称参数列表:使用该函数时,传入的数据函数体语句:花括号内的代码,函数内需要执行的语句return表达式: 和返回值类型挂钩,函数执行完后,返回相应的数据例1:定义加法函数
int add(int num1, int num2) { int sum = num1 + num2; return sum; }功能:使用定义好的函数 语法:函数名 (参数)
例1:调用add函数
#include <iostream> using namespace std; int add(int num1, int num2) //形参 { int sum = num1 + num2; return sum; } int main() { //main函数中调用add函数 int c = add(10, 20); //实参 cout << c << endl; system("pause"); return 0; } 30 请按任意键继续. . .例1:实现两个值交换的函数
#include <iostream> using namespace std; //值传递 //定义函数,实现两个数字进行交换函数 //如果函数不需要返回值,声明可以写void类型 void swap(int num1, int num2) { cout << "交换前:num1=" << num1 << ", num2=" << num2 << endl; int temp = num1; num1 = num2; num2 = temp; cout << "交换后:num1=" << num1 << ", num2=" << num2 << endl; return; } int main() { int a = 10; int b = 20; cout << "交换前:a=" << a << ", b=" << b << endl; swap(a, b); cout << "交换后:a=" << a << ", b=" << b << endl; system("pause"); return 0; } 交换前:a=10, b=20 交换前:num1=10, num2=20 交换后:num1=20, num2=10 交换后:a=10, b=20 请按任意键继续. . .常见的函数样式有4种: 1.无参无返 2.有参无返 3.无参有返 4.有参有返
例1:第一种,无参无返
#include <iostream> using namespace std; void test01() { cout << "Hello world!" << endl; } int main() { test01(); system("pause"); return 0; } Hello world! 请按任意键继续. . .例2:第二种,有参无返
#include <iostream> using namespace std; void test02(int a) { cout << "a=" << a << endl; } int main() { int a; cout << "请输入a:" << endl; cin >> a; test02(a); system("pause"); return 0; } 请输入a: 5 a=5 请按任意键继续. . .例三:第三种,无参有返
#include <iostream> using namespace std; int test03() { cout << "This is test03." << endl; return 1000; } int main() { int num1 = test03(); cout << "num1=" << num1 << endl; system("pause"); return 0; } This is test03. num1=1000 请按任意键继续. . .例四:第四种,有参有返
#include <iostream> using namespace std; int test04(int a) { cout << "This is test04." << endl; return a; } int main() { int num2 = test04(1000); cout << "num2=" << num2 << endl; system("pause"); return 0; } This is test04. num2=1000 请按任意键继续. . .作用:告诉编码器函数名称及如何调用函数。函数的实际主体可以单独定义
函数的声明可以多次,但是函数的定义只能有一次 #include <iostream> using namespace std; int max(int a, int b); //函数声明 ,定义的函数就可以写在主函数后面了 int main() { cout << max(10, 20) << endl; system("pause"); return 0; } //函数的声明 //比较函数,实现两个整型数字的比较,返回较大的值 int max(int a, int b) { return a > b ? a : b; } 20 请按任意键继续. . .作用:让代码结构更加清晰
函数份文件编写一般有4个步骤
创建后缀名.h的头文件创建后缀名cpp的源文件在头文件中写函数的声明在源文件中写函数的定义例1:实现两个数的交换,这例使用分文件编写 1、 swap.h头文件
#include <iostream> using namespace std; //函数声明 void swap(int num1, int num2);2、swap.cpp源文件
#include "swap.h" void swap(int num1, int num2) { cout << "交换前:num1=" << num1 << ", num2=" << num2 << endl; int temp = num1; num1 = num2; num2 = temp; cout << "交换后:num1=" << num1 << ", num2=" << num2 << endl; return; }3、first_temp.cpp源文件(主文件)
#include <iostream> using namespace std; #include "swap.h" int main() { swap(10, 20); system("pause"); return 0; }
指针的作用:可以通过指针间接访问内存
内存编码是从0开始记录的,一般用十六进制数字表示可以利用指针变量保存地址指针变量定义语法:数据类型 * 变量名
#include <iostream> using namespace std; int main() { //1、定义指针 int a = 10; //指针定义的语法:数据类型 * 指针变量 int* p; //让指针记录变量a的地址 p = &a; cout << "a的地址为:" << &a << endl; cout << "指针p为:" << p << endl; //其实指针就是地址 //2、使用指针 //可以通过解引用的方式来找到指针指向的内存 //指针前加 * 代表解引用,找到指针指向的内存数据 *p = 1000; cout << "a=" << a << endl; cout << "*p=" << *p << endl; //不改变地址,只改变了地址存的数据 cout << "a的地址为:" << &a << endl; cout << "指针p为:" << p << endl; system("pause"); return 0; } a的地址为:0000006FE5EFF9F4 指针p为:0000006FE5EFF9F4 a=1000 *p=1000 a的地址为:0000006FE5EFF9F4 指针p为:0000006FE5EFF9F4 请按任意键继续. . .总结1: 我们可以通过 & 符号 获取变量的地址
总结2:利用指针可以记录地址
总结3:对指针变量解引用,可以操作指针指向的内存
提问:指针也是种数据类型,那么这种数据类型占用多少内存空间?
在32位操作系统下占用4个字节空间在64位操作系统下占用8个字节空间 #include <iostream> using namespace std; int main() { int a = 10; //int* p; //p = &a; int* p = &a; cout << "sizeof(p) =" << sizeof(p) << endl; cout << "sizeof (int*) =" << sizeof (int*) << endl; cout << "sizeof (double*) =" << sizeof(double*) << endl; cout << "sizeof (char*) =" << sizeof(char*) << endl; system("pause"); return 0; } sizeof(p) =8 sizeof (int*) =8 sizeof (double*) =8 sizeof (char*) =8 请按任意键继续. . .空指针:指针变量指向内存中编号为0的空间 用途:初始化指针变量 注意:空指针指向的内存是不可以访问的
#include <iostream> using namespace std; int main() { //空指针 //1、空指针用于给指针变量进行初始化 int* p = NULL; //2、空指针是不可以进行访问的 //0~255之间的内存编号是系统占用的,因此不可以访问 //*p = 100; //错误 system("pause"); return 0; }以下代码会报错
#include <iostream> using namespace std; int main() { //野指针 //在程序中,尽量避免出现野指针 int* p = (int*)0x1100; cout << *p << endl; system("pause"); return 0; }注意:空指针和野指针都不是我们申请的空间,因此不要访问。
const修饰指针有三种情况:
const修饰指针 —常量指针const修饰常量 —指针常量const即修饰指针,又修饰常量第一种:常量指针 相当于int* p是常数
const int* p = &a特点:指针的指向可以修改,但是指针指向的值不可以修改
*p = 20; //错误 p = &b; //正确例1:
#include <iostream> using namespace std; int main() { int a = 10; int b = 10; const int* p = &a; //*p = 20; //错误 p = &b; //正确 system("pause"); return 0; }第二种:指针常量 相当于p(地址)是常数
int* const p = &a特点:指针的指向不可以改,指针指向的值可以改
*p = 20; //正确 p = &b; //错误例2:
#include <iostream> using namespace std; int main() { int a = 10; int b = 10; int* const p = &a; *p = 20; //正确 //p = &b; //错误 system("pause"); return 0; }第三种:即修饰指针又修饰常量
const int* const p = &a;特点:指针的指向和指针指向的值都不可以修改
*p = 20; //错误 p = &b; //错误
作用:利用指针访问数组中元素
例:利用指针遍历数组
#include <iostream> using namespace std; int main() { //指针和数组 //利用指针访问数组中的元素 int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; cout << "第一个元素为:" << arr[0] << endl; int* p = arr; //arr就是数组的首地址 cout << "利用指针来访问第一个元素:" << *p << endl; p++; //向后偏移四个字节 cout << "利用指针来访问第二个元素:" << *p << endl; cout << "利用指针遍历数组:" << endl; int* p2 = arr; for (int i = 0; i < 10; i++) { cout << *p2 << " "; p2++; } cout << endl; system("pause"); return 0; } 第一个元素为:1 利用指针来访问第一个元素:1 利用指针来访问第二个元素:2 利用指针遍历数组: 1 2 3 4 5 6 7 8 9 10 请按任意键继续. . .作用:利用指针作函数参数,可以修改实参的值
p是存地址 *p是存数值
#include <iostream> using namespace std; void swap02(int* p1, int* p2) //两个形参为地址 { int temp = *p1; //里面交换地址存储的值 *p1 = *p2; *p2 = temp; } int main() { int a = 10; int b = 20; cout << "传递前:" << endl; cout << "a=" << a << endl; cout << "b=" << b << endl; //地址传递 swap02(&a, &b); cout << "传递后:" << endl; cout << "a=" << a << endl; cout << "b=" << b << endl; system("pause"); return 0; } 传递前: a=10 b=20 传递后: a=20 b=10 请按任意键继续. . .案例描述:封装一个函数,利用冒泡法排序,实现对整型数组的升序排序 例如数组:int arr[10] = {4,3,6,9,1,2,10,8,7,5};
#include <iostream> using namespace std; void bubbleSort(int* arr, int len) //第一个形参:地址 第二个形参:长度 { for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - i - 1; j++) { //如果j>j+1的值,交换数字 if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } //打印数组 void printArray(int* arr, int len) { for (int i = 0; i < len; i++) { cout << arr[i] << " "; } cout << endl; } int main() { //1、创建一个数组 int arr[10] = { 4,3,6,9,1,2,10,8,7,5 }; int len = sizeof(arr) / sizeof(arr[0]); //2、创建函数,实现冒泡排序 bubbleSort(arr, len); //3、打印排序后的数组 printArray(arr, len); system("pause"); return 0; } 1 2 3 4 5 6 7 8 9 10 请按任意键继续. . .结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
语法:
struct 结构体名 { 结构体成员列表 }通过结构体创建变量的方式有三种:
struct 结构体名 变量名struct 结构体名 变量名 = {成员1值, 成员2值...}定义结构体时顺便创建变量 #include <iostream> using namespace std; #include <string> //1、创建学生数据类型:学生包括(姓名,年龄,分数) //自定义数据类型,一些类型集合组成的一个类型 //语法:struct 类型名称 {成员列表} struct Student { //成员列表 //姓名 string name; //年龄 int age; //考试分数 int score; }s3; //顺便创建结构体变量 int main() { //2、通过学生类型创建具体学生 // 2.1 struct Student s1; //struct关键字可以省略 //Student s1; struct Student s1; //给s1属性赋值,通过.访问结构体变量中的属性 s1.name = "张三"; s1.age = 18; s1.score = 100; cout << "姓名:" << s1.name << ", 年龄:" << s1.age << ", 分数:" << s1.score << endl; // 2.2 struct Student s2 = {...} struct Student s2 = { "李四", 19, 80 }; cout << "姓名:" << s2.name << ", 年龄:" << s2.age << ", 分数:" << s2.score << endl; // 2.3 在定义结构体时顺便创建结构体的变量 s3.name = "王五"; s3.age = 20; s3.score = 60; cout << "姓名:" << s3.name << ", 年龄:" << s3.age << ", 分数:" << s3.score << endl; system("pause"); return 0; } 姓名:张三, 年龄:18, 分数:100 姓名:李四, 年龄:19, 分数:80 姓名:王五, 年龄:20, 分数:60 请按任意键继续. . . 总结1:定义结构体时的关键字是struct,不可省略总结2:创建结构体变量时,关键字struct可以省略总结3:结构体变量利用操作符 ‘’.’’ 访问成员作用:将自定义的结构体放入到数组中方便维护 语法:struct 结构体 数组名[元素个数] = { {}, {}, {} }
#include <iostream> using namespace std; #include <string> //结构体数组 //1、定义结构体 struct Student { //姓名 string name; //年龄 int age; //分数 int score; }; int main() { //2、创建结构体数组 struct Student stuArray[3] = { {"张三", 18, 100}, {"李四", 28, 99}, {"王五",38, 66} }; //3、给结构体数组中的元素赋值 stuArray[2].name = "赵六"; //4、遍历结构体数组 for (int i = 0; i < 3; i++) { cout << "姓名:" << stuArray[i].name << ", 年龄:" << stuArray[i].age << ", 分数:" << stuArray[i].score << endl; } system("pause"); return 0; } 姓名:张三, 年龄:18, 分数:100 姓名:李四, 年龄:28, 分数:99 姓名:赵六, 年龄:38, 分数:66 请按任意键继续. . .作用:通过指针访问结构体中的成员
利用操作符->可以通过结构体指针访问结构体属性 #include <iostream> using namespace std; #include <string> struct Student { string name; //姓名 int age; //年龄 int score; //分数 }; int main() { //1、创建学生结构体变量 Student s = { "张三", 18, 100 }; //2、通过指针指向结构体变量 Student* p = &s; cout << "姓名:" << p->name << ", 年龄:" << p->age << ", 分数:" << p->score << endl; system("pause"); return 0; } 姓名:张三, 年龄:18, 分数:100 请按任意键继续. . .作用:结构体中的成员可以是另一个个结构体
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
#include <iostream> using namespace std; #include <string> //学生结构体定义 struct student { //成员列表 string name; //姓名 int age; //年龄 int score; //分数 }; //教师结构体定义 struct teacher { //成员列表 int id; //职工编号 string name; //教师姓名 int age; //教师年龄 struct student stu; //子结构体 学生 }; int main() { struct teacher t1; t1.id = 10000; t1.name = "老王"; t1.age = 40; t1.stu.name = "张三"; t1.stu.age = 18; t1.stu.score = 100; cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl; cout << "辅导学员 姓名: " << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl; system("pause"); return 0; } 教师 职工编号: 10000 姓名: 老王 年龄: 40 辅导学员 姓名: 张三 年龄:18 考试分数: 100 请按任意键继续. . .作用:将结构体作为参数向函数中传递
传递方式有两种:
值传递地址传递 #include <iostream> using namespace std; #include <string> struct student { string name; //姓名 int age; //年龄 int score; //分数 }; //值传递 void printStudent(student stu) { stu.age = 28; cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl; } //地址传递 void printStudent2(student* stu) { stu->age = 28; cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl; } int main() { student stu = { "张三",18,100 }; //值传递 printStudent(stu); cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl; cout << endl; //地址传递 printStudent2(&stu); cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl; system("pause"); return 0; } 子函数中 姓名:张三 年龄: 28 分数:100 主函数中 姓名:张三 年龄: 18 分数:100 子函数中 姓名:张三 年龄: 28 分数:100 主函数中 姓名:张三 年龄: 28 分数:100 请按任意键继续. . .总结:如果不想修改主函数中的数据,用值传递,反之用地址传递
作用:用const来防止误操作
#include <iostream> using namespace std; #include <string> struct student { string name; //姓名 int age; //年龄 int score; //分数 }; //const使用场景 void printStudent(const student* stu) //加const防止函数体中的误操作 { //stu->age = 100; //不能修改,因为加了const cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl; } int main() { student stu = { "张三",18,100 }; printStudent(&stu); system("pause"); return 0; } 姓名:张三 年龄:18 分数:100 请按任意键继续. . .案例描述: 学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
1、设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员 2、学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值 3、最终打印出老师数据以及老师所带的学生数据。
#include <iostream> using namespace std; #include <string> #include <ctime> struct Student { string name; //学生姓名 int score; //分数 }; struct Teacher { string name; //老师姓名 Student sArray[5]; //创建5名学生数组 }; //给老师和学生赋值的函数 void allocateSpace(Teacher tArray[], int len) { string tName = "教师"; string sName = "学生"; string nameSeed = "ABCDE"; //给老师赋值 for (int i = 0; i < len; i++) { tArray[i].name = tName + nameSeed[i]; //老师A,老师B,老师C //给老师带的学生赋值 for (int j = 0; j < 5; j++) { tArray[i].sArray[j].name = sName + nameSeed[j]; //学生A,学生B,学生C.... tArray[i].sArray[j].score = rand() % 61 + 40; //成绩随机生成,40~100 } } } //打印输出信息 void printTeachers(Teacher tArray[], int len) { for (int i = 0; i < len; i++) //遍历老师 { cout << tArray[i].name << endl; //输出老师姓名 for (int j = 0; j < 5; j++) //遍历老师带的学生 { cout << "\t姓名:" << tArray[i].sArray[j].name << " 分数:" << tArray[i].sArray[j].score << endl; } } } int main() { srand((unsigned int)time(NULL)); //随机数种子 分数随机需要这个 Teacher tArray[3]; //创建3名老师数组 int len = sizeof(tArray) / sizeof(tArray[0]); allocateSpace(tArray, len); //创建数据 printTeachers(tArray, len); //打印数据 system("pause"); return 0; } 教师A 姓名:学生A 分数:70 姓名:学生B 分数:87 姓名:学生C 分数:86 姓名:学生D 分数:66 姓名:学生E 分数:48 教师B 姓名:学生A 分数:87 姓名:学生B 分数:51 姓名:学生C 分数:50 姓名:学生D 分数:75 姓名:学生E 分数:74 教师C 姓名:学生A 分数:46 姓名:学生B 分数:61 姓名:学生C 分数:50 姓名:学生D 分数:44 姓名:学生E 分数:74 请按任意键继续. . .案例描述: 1、设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。 2、通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
五名英雄信息如下:
{"刘备",23,"男"}, {"关羽",22,"男"}, {"张飞",20,"男"}, {"赵云",21,"男"}, {"貂蝉",19,"女"},例:
#include <iostream> using namespace std; #include <string> #include <ctime> //英雄结构体 struct hero { string name; //姓名 int age; //年龄 string sex; //性别 }; //冒泡排序,升序 void bubbleSort(hero arr[], int len) { for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - 1 - i; j++) { if (arr[j].age > arr[j + 1].age) { hero temp = arr[j]; //hero相当于一个类型,想当于int arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } //打印数组 void printHeros(hero arr[], int len) { for (int i = 0; i < len; i++) { cout << "姓名:" << arr[i].name << " 性别:" << arr[i].sex << " 年龄:" << arr[i].age << endl; } } int main() { //创建数组存放5名英雄 struct hero arr[5] = { {"刘备",23,"男"}, {"关羽",22,"男"}, {"张飞",20,"男"}, {"赵云",21,"男"}, {"貂蝉",19,"女"}, }; //int len = sizeof(arr) / sizeof(hero); //获取数组元素个数 int len = sizeof(arr) / sizeof(arr[0]); //获取数组元素个数 bubbleSort(arr, len); //排序 printHeros(arr, len); //打印 system("pause"); return 0; } 姓名:貂蝉 性别:女 年龄:19 姓名:张飞 性别:男 年龄:20 姓名:赵云 性别:男 年龄:21 姓名:关羽 性别:男 年龄:22 姓名:刘备 性别:男 年龄:23 请按任意键继续. . .