C++入门基础篇
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++入门基础篇,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含40066字,纯文字阅读大概需要58分钟。
内容图文
![C++入门基础篇](/upload/InfoBanner/zyjiaocheng/606/1ebabbd55afc4968b4f4fbbcf1575ccc.jpg)
C++入门基础
002第一个C++程序C++书写helloworld
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = { "Hello Word" };
cout << str << endl;
system("pause");
return 0;
}
003程序的注释-单行注释和多行注释
//单行注释
/*
多行注释,注意多行注释不可以嵌套
*/
004变量-变量的使用-变量的意义
//变量的使用和变量的意义
//变量存在的意义:方便我们管理内存空间
//变量创建的语法:数据类型 变量名 = 变量初始值;
变量的声明,如果想声明一个变量而非定义它,就在变量的名前添加关键字extern,而不是显示的初始化变量:
extern int i ; //声明i而非定义i
int j ; //声明j并定义j;
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = { "Hello Word" };
cout << str << endl;
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
005常量-常量与变量的区别-常量的使用
//常量
//作用:用于记录程序中不可更改的数据
//C++定义常量的两种方式
1、#define 宏常量#define 常量名 常量值
2、const修饰的变量 const 数据类型 变量名 = 常量值
通常在变量定义前加关键字const,修饰变量为常量,不可修改
#include<iostream>
#include<string>
using namespace std;
#define Day 7
int main()
{
//Day = 14; //错误,Day是常量,一旦修改就会报错
cout << "一周总共有:" << Day << "天" << endl;
//const 修饰的变量
const int month = 12;
//month = 24; //错误,const修饰的变量也称为常量
cout << "一年有:" << month << "个月" << endl;
system("pause");
return 0;
}
006关键字-C++常用的编程关键字
作用:关键字是C++中预先保留的单词(标识符)
在定义变量或者常量的时候,不要用关键字。
#include<iostream>
#include<string>
using namespace std;
int main()
{
//创建变量:数据类型 变量名称 = 变量初始值
//不要用关键字给变量或常量起名称
//int int = 10; //错误,第二个int是关键字,不可以作为变量的名称
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
007标识符命名规则
标识符命名规则
作用:C++规定标识符(变量、常量)命名时,有一套自己的规则
- 标识符不能是关键字
- 标识符只能由字母、数字、下划线组成
- 第一个字符必须为字母或下划线
- 标识符中字母区分大小写
建议:给标识符命名时,争取坐到见名知意的效果
008数据类型-整型
C++规定在创建一个变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存
作用:整型变量表示的是整数类型的数据
c++中能够表示整型的类型有以下几种方式,区别在于所占的内存空间不同:
#include<iostream>
#include<string>
using namespace std;
//语法:数据类型 变量名 = 变量的初始值
//数据类型存在的意义:给变量分配合适的内存空间
int main()
{
//整型
short num1 = 10; //1、短整型(-32768 ~ 32767)
int num2 = 10; //2、整型
long num3 = 10; //3、长整型
long long num4 = 10; //4、长长整型
cout << "num1 =" << num1 << endl;
cout << "num2 =" << num2 << endl;
cout << "num3 =" << num3 << endl;
cout << "num4 =" << num4 << endl;
system("pause");
return 0;
}
009数据类型-sizeof关键字
作用:利用sizeof关键字可以统计数据类型所占内存大小
语法:sizeof(数据类型/变量)
#include<iostream>
#include<string>
using namespace std;
int main()
{
short num1 = 10;
cout << "short占用的内存空间为:" << sizeof(short) << endl;
int num2 = 10;
cout << "int占用的内存空间为:" << sizeof(int) << endl;
long num3 = 10;
cout << "long占用的内存空间为:" << sizeof(long) << endl;
long num4 = 10;
cout << "long long占用的内存空间为:" << sizeof(long long) << endl;
system("pause");
return 0;
}
//short < int <=long <= long long
010数据类型-实型
作用:用于表示小数
浮点型变量分为两种:
1、单精度float
2、双精度double
两者的区别在于表示的有效数字范围不同
#include<iostream>
#include<string>
using namespace std;
int main()
{
//默认情况下输出一个小数,会显示出6位有效数字
float f1 = 3.1415926f;
cout << "f1 = " << f1 << endl;
double d1 = 3.1415926;
cout << "d1 = " << d1 << endl;
//统计float 和double的占用的内存空间
cout << "float占用的内存空间为:" << sizeof(float) << endl;
cout << "double占用的内存空间为:" << sizeof(double) << endl;
//科学计数法
float f2 = 3e3f; //3*10^3
cout << "f2 = " << f2 << endl;
float f3 = 3e-2f; //3*0.1^2
cout << "f3 = " << f3 << endl;
system("pause");
return 0;
}
011数据类型-字符型
作用:字符型变量用于显示单个字符
//语法:
char ch = 'a';
- note1:在显示字符变量时,用单引号将字符括起来,不要用双引号
- note:单引号内只能有一个字符,不可以是字符串
C和C++中字符型变量只占用1个字节
字符型比变量并不是把字符本身放在内存中存储,而是将对应得ASCII码放入存储单元
#include<iostream>
//#include<string>
using namespace std;
int main()
{
//1、字符型变量的创建方式
char ch = 'a'; //a- 97
char ch4 = 'A'; //A- 65
cout << "ch = " << ch << endl;
//2、字符型变量所占的内存大小
cout << "char型字符串所占的内存空间:" << sizeof(char) << endl;
//3、字符型变量常见的错误
//char ch2 = "b"; //创建字符型变量时候,要用单引号
char ch3 = 'abcd'; //创建字符型变量的时候单引号只能有一个字符
cout << ch3 << endl;
//4、字符型变量对应的ASCII码
cout << (int)ch << endl;
cout << (int)ch4 << endl;
system("pause");
return 0;
}
012数据类型-转义字符
作用:用于表示一些不能显示出来的ASCII字符
现阶段我们常用的转义字符有:\n \ \t
#include<iostream>
#include<string>
using namespace std;
int main()
{
//转义字符
cout << "hello world\n"; //换行符 \n
cout << "\\" << "\n"; // 反斜杠 "\\"
//水平制表符 \t 作用可以整齐的输出数据
cout << "aaa\thello world" << endl;
cout << "aaaaa\thello world" << endl;
cout << "a\thello world" << endl;
system("pause");
return 0;
}
013数据类型-字符串类型
字符串类型
作用:用于表示一串字符
两种风格:
- C风格字符串:char 变量名[] = "字符串值"
- C++风格字符串:string 变量名 = “字符串”
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、C风格的字符串
//注意: char 字符串名 []
//注意2: 等号后面要用双引号,
char str1[] = "hello world";
cout << str1 << endl;
string str2 = "hello world!!!";
cout << str2 << endl;
system("pause");
return 0;
}
014数据类型-布尔类型
作用:布尔数据类型代表真或假的值
bool 类型只有两个值:
- true -----真(本质是1)
- false-----假(本质是0)
bool类型占1个字节的大小
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、创建bool数据类型
bool flag = true;
cout << flag << endl;
flag = false;
cout << flag << endl;
//2、查看bool类型所占内存空间
cout << "bool类型所占的内存空间:" << sizeof(bool) << endl;
system("pause");
return 0;
}
015数据类型-数据输入
作用:用于从键盘获取数据
关键字:cin
语法:cin>>变量
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a;
cout << "请给整型变量赋值:" << endl;
cin >> a;
cout << "a = " <<a<< endl;
system("pause");
return 0;
}
016运算符-算术运算符-加减乘除运算
作用:用于执行代码的运算
算术运算符
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a1 = 10;
int b1 = 3;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1* b1 << endl;
//两个整数相除,结果依然是整数,将小数部分去掉
cout << a1 / b1 << endl;
system("pause");
return 0;
}
017运算符-算术运算符-取模运算
#include<iostream>
#include<string>
using namespace std;
int main()
{
//取模运算的本质是求余数
int a1 = 10;
int b1 = 3;
cout << 10 % 3 << endl;
int a2 = 10;
int b2 = 20;
cout << a2%b2 << endl;
//两个小数是不可以做取模运算的
system("pause");
return 0;
}
018运算符-算术运算符-递增递减
作用:用于处理四则运算
#include<iostream>
#include<string>
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然后进行表达式运算
int a2 = 10;
int b2 = ++a2 * 10;
cout << "a2 = " << a2 << endl;
cout << "b2 = " << b2 << endl;
//后置递增,先进行表达式运算,后让变量+1
int a3= 10;
int b3 =a3++ * 10;
cout << "a3 = " << a3 << endl;
cout << "b3 = " << b3 << endl;
system("pause");
return 0;
}
019运算符-赋值运算符
作用:用于将表达式的值赋给变量
赋值运算符包括以下几个符号:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//赋值运算符
// =
int a = 10;a = 100;
cout << "a = " << a << endl;
// +=
a = 10; a += 2;
cout << "a = " << a << endl;
// -=
a = 10; a -= 2;
cout << "a = " << a << endl;
// *=
a = 10; a *= 2;
cout << "a = " << a << endl;
// /=
a = 10; a /= 2;
cout << "a = " << a << endl;
// %=
a = 10; a %= 2;
cout << "a = " << a << endl;
system("pause");
return 0;
}
020运算符-比较运算符
作用:用于表达式的比较,并返回一个真值或假值
比较运算符有以下符号:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//比较运算符
// ==
int a = 10, b = 20;
cout << (a == b) << endl;
// !=
cout << (a != b) << endl;
// >
cout << (a > b) << endl;
// <
cout << (a < b) << endl;
// >=
cout << (a <= b) << endl;
// <=
cout << (a >= b) << endl;
system("pause");
return 0;
}
021运算符-逻辑运算符-非
作用:用于根据表达式的值返回真值或假值
逻辑运算符有以下符号:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//逻辑运算符 非!
int a = 10;
//在C++中除了0都为真
cout << !a << endl;
cout << !!a << endl;
system("pause");
return 0;
}
022运算符-逻辑运算符-与
#include<iostream>
#include<string>
using namespace std;
int main()
{
//逻辑运算符 与 &&
int a = 10, b = 10;
cout << (a&&b) << endl;
a = 0, b = 10;
cout << (a&&b) << endl;
a = 0, b = 0;
cout << (a&&b) << endl;
system("pause");
return 0;
}
023运算符-逻辑运算符-或
#include<iostream>
#include<string>
using namespace std;
int main()
{
//逻辑运算符 或 ||
int a = 10, b = 10;
cout << (a||b) << endl;
a = 0, b = 10;
cout << (a||b) << endl;
a = 0, b = 0;
cout << (a||b) << endl;
system("pause");
return 0;
}
024程序流程结构-选择结构-单行if语句
C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
- 顺序结构:程序按顺序执行,不发生跳转
- 选择结构:依据条件是否满足,有选择的执行相应的功能
- 循环结构:依据条件是否满足,循环多次执行某段代码
选择结构:
if语句
作用:执行满足条件的语句
if语句的三种形式
- 单行格式if语句
- 多行格式if语句
- 多条件的if语句
#include<iostream>
#include<string>
using namespace std;
int main()
{
//选择结构 单行if语句
//用户输入分数,如果分数大于600,视为考上了一本大学,在屏幕上输出
//1、用户输入一个分数
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
//2、打印用户输入的分数
cout << "您输入的分数为:" << score << endl;
//3、判断分数是否大于600,如果大于,那么输出
if (score > 600)
cout << "恭喜您考上了一本大学!!!" << endl;
system("pause");
return 0;
}
025程序流程结构-选择结构-多行if语句
#include<iostream>
#include<string>
using namespace std;
int main()
{
//选择结构 多行if语句
//用户输入分数,如果分数大于600,视为考上了一本大学,在屏幕上输出,
//如果没考考试一本大学打印没考上
//1、用户输入一个分数
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
//2、打印用户输入的分数
cout << "您输入的分数为:" << score << endl;
//3、判断分数是否大于600,如果大于,那么输出,否则打印未考上一本
if (score > 600)
cout << "恭喜您考上了一本大学!!!" << endl;
else
cout << "继续努力,继续加油!!!" << endl;
system("pause");
return 0;
}
026程序流程结构-选择结构-多条件if语句
#include<iostream>
#include<string>
using namespace std;
int main()
{
//选择结构 多条件if语句
//用户输入分数,如果分数大于600,视为考上了一本大学,在屏幕上输出,
//如果大于500,视为考上了二本大学
//如果大于400,视为考上了三本大学
//如果小于等于400,视为未考上本科,在屏幕上输出
//1、用户输入一个分数
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
//2、打印用户输入的分数
cout << "您输入的分数为:" << score << endl;
//3、判断分数是否大于600,如果大于,那么输出,否则打印未考上一本
if (score > 600)
cout << "恭喜您考上了一本大学!!!" << endl;
else if (score > 500)
cout << "恭喜您考上了二本大学!!!" << endl;
else if (score > 400)
cout << "恭喜您考上了三本大学!!!!" << endl;
else
cout << "再接再厉,继续努力" << endl;
system("pause");
return 0;
}
027程序流程结构-选择结构-嵌套if语句
嵌套if语句:在if语句中,可以嵌套使用if语句,达到更精确的条件判断
案列需求:
- 提示用户输入一个高考考试分数,根据分数做如下判断
- 分数如果大于600分视为考上一本,大于500分视为考上二本,大于400考上三本,其余视为未考上
- 在一本分数中,如果大于700分,考入北大,大于650,考入清华,大于600考入人大
#include<iostream>
#include<string>
using namespace std;
int main()
{
//选择结构 嵌套if语句
//1、用户输入一个分数
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
//2、打印用户输入的分数
cout << "您输入的分数为:" << score << endl;
//3、判断分数是否大于600,如果大于,那么输出,否则打印未考上一本
if (score > 600){
if (score > 700)
cout << "恭喜您考上了北大!!!" << endl;
else if (score > 650)
cout << "恭喜您考上了清华!!!" << endl;
else
cout << "恭喜您考上人大!!!" << endl;
}
else if (score > 500)
cout << "恭喜您考上了二本大学!!!" << endl;
else if (score > 400)
cout << "恭喜您考上了三本大学!!!!" << endl;
else
cout << "再接再厉,继续努力" << endl;
system("pause");
return 0;
}
028测序流程结构-选择结构案例-三只小猪称体重
练习案列:
三只小猪称体重
有三只小猪ABC,请分别输入三只小猪的体重,并判断那只小猪最重。
1、先判断A和B谁重
A重
让A和C比较
A重 结果是A最重
C重 结果是C最重
B重
让B和C比较
B重 结果是B最重
C重 结果是C最重
#include<iostream>
#include<string>
using namespace std;
int main()
{
//三只小猪称体重,判断哪只最重
//1、创建三只小猪的体重变量
int num1 = 0, num2 = 0, num3 = 0;
//2、让用户输入三只小猪的体重
cout << "请输入小猪A的体重:" << endl;
cin >> num1;
cout << "请输入小猪B的体重:" << endl;
cin >> num2;
cout << "请输入小猪C的体重:" << endl;
cin >> num3;
cout << "请输入小猪A的体重:" << num1 << endl;
cout << "请输入小猪B的体重:" << num2 << endl;
cout << "请输入小猪C的体重:" << num3 << endl;
//3、判断那只最重
if (num1 > num2){//A>B
if (num1 > num3)
cout << "三只小猪A最重" << endl;
else
cout << "三只小猪C最重" << endl;
}
else{
if (num2 > num3) //B比C重
cout << "小猪B最重:" << endl;
else
cout << "小猪C最重" << endl;
}
system("pause");
return 0;
}
029程序流程结构-选择结构-三目运算符
三目运算符
作用:通过三目运算符实现简单的判断
语法:表达式1?表达式2 : 表达式3
解释:
如果表达式1的值为真,执行表达式2,并返回表达式2的结果
如果表达式1的值为假,执行表达式3,并返回表达式3的结果
#include<iostream>
#include<string>
using namespace std;
int main()
{
//三目运算符
//创建三个变量a,b,c
//将a和b做比较,将变量大的值赋给变量c
int a = 10, b = 20, c = 0;
c = (a > b ? a : b);
cout << "c = " << c << endl;
//在C++中三目运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << "a = " << a << endl;
cout << "b =" << b << endl;
system("pause");
return 0;
}
030程序流程结构-选择结构-switch语句
作用:执行多条件分支语句
语法:
switch(表达式){
case 结果1:执行语句;break;
case 结果2:执行语句;break;
...
default:执行语句;break;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
//switch语句
//给电影进行打分 10 ~ 9 经典 8 ~ 7 非常好 6 ~ 5 一般 5以下 烂片
int score = 0;
cout << "请给电影打分:" << endl;//1、提示用户给电影评分
cin >> score;
cout << "您打的分数为:" << score << endl;//2、用户开始进行打分
switch (score){ //3、根据用户输入的分数来提示用户最后的结果
case 10:
cout << "您认为是经典电影" << endl; break;
case 9:
cout << "您认为是经典电影" << endl; break;
case 8:
cout << "您认为是电影非常好" << endl; break;
case 7:
cout << "您认为是电影非常好" << endl; break;
case 6:
cout << "您认为是电影一般" << endl; break;
case 5:
cout << "您认为是电影一般" << endl; break;
default:
cout << "您认为是电影是个烂片" << endl; break;
}
system("pause");
return 0;
}
if和switch区别?
switch缺点,判断时候只能是整型或者字符型,不可以是一个区间
switch优点,结构清晰,执行效率高
注意1:switch语句中表达式类型只能是整型或者字符型
注意2:case里如果没有break,那么程序会一直向下执行
总结:与if语句比,对于多条件判断时,switch结构清晰,执行效率高,缺点是switch不可以判断区间
031程序流程结构-循环结构-while语句
while循环语句
作用:满足条件,执行循环语句
语法:while(循环条件){循环语句}
解释:只要循环条件的结果为真,就执行循环语句
#include<iostream>
#include<string>
using namespace std;
int main()
{
//while循环
//在屏幕中打印0 ~ 9
int num = 0;
while (num <10){
cout << num++ << " ";
}
cout << endl;
system("pause");
return 0;
}
注意:在执行循环语句的时候,程序必须提供跳出循环的出口,否则会出现死循环。
032程序流程结构-循环结构案例-猜数字
while循环练习案例:猜数字
案列描述:系统随机生成一个1到100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果才对恭喜玩家胜利,并退出游戏。
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
int main()
{
//添加随机数种子,作用是利用当前系统生成随机数,防止每次随机数都一样
srand((unsigned int)time(nullptr));
//1、系统生成随机数
int num = rand() % 100 +1; //rand()%100+1生成0+1 ~ 99+1随机数
//2、玩家进行猜测
int val = 0;
while (1){
cin >> val;
//3、判断玩家的猜测
if (val > num)
cout << "猜测过大" << endl;
else if (val < num)
cout << "猜测过小" << endl;
else{
cout << "恭喜您猜对了!!!" << endl;
break;
}
}
//猜对 退出游戏
//猜错 提示猜的结果 过大或者过小, 重新返回第2步
system("pause");
return 0;
}
033程序流程结构-循环结构-dowhile语句
作用:满足循环条件,执行循环语句
语法:do{循环语句}while(循环条件)
注意:与while的区别在于do …while会先执行一次循环语句,在判断循环条件
#include<iostream>
#include<string>
using namespace std;
int main()
{
//do ...while语句
//在屏幕中输出0到9这10个数字
int num = 0;
do{
cout << num++ << " ";
} while (num < 10);
cout << endl;
system("pause");
return 0;
}
034程序流程结构-循环结构案例-水仙花数
练习案例:水仙花数
案例描述:水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身
例如:1^3 + 5 ^3 + 3 ^3 = 153
请用do …while语句,求出所有3位数中的水仙花数
分析:
1、将所有的三位数进行输出(100 ~ 999)
2、在所有的三位数中找到水仙花数
水仙花数
获取个位数 153%10 = 3 对数字取模于10可以获取到个位
获取十位数 (153/10)%10 = 5 先整除于10,得到两位数,再取模于10,得到十位数
获取百位数 153/100 直接整除于100,获取到百位
判断 各位^3 +十位 ^3 +百位 ^3 = itself
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、打印所有的三位数
int num = 100;
do{
int a = 0, b = 0, c = 0;
a = num % 10; //个位
b = num / 10 % 10; //十位数
c = num / 100; //百位数
if (a*a*a+ b*b*b + c*c*c == num){
cout << num<< " ";
}
num++;
} while (num < 1000);
cout << endl;
//2、从所有的三位数字中找到水仙花数
system("pause");
return 0;
}
036程序流程结构-循环结构-for循环
作用:满足循环条件,执行循环语句
语法:for(起始条件;条件表达式;末尾循环体){循环语句;}
#include<iostream>
#include<string>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
cout << i << " ";
cout << endl;
system("pause");
return 0;
}
036程序流程结构-循环结构-敲桌子
练习案例:敲桌子
案例描述:从1开始数到数字100,如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余之间打印输出。
1、先输出1到100这些数字
2、从这100个数字中找到特殊的数字,改位“敲桌子”
7的倍数 (7 14 21 28 …) %7 ==0
个位有7 (7 17 27 37…) % 10 == 7
十位有7 (70 71 71 73 …) /10 = 7
#include<iostream>
#include<string>
using namespace std;
int main()
{
//敲桌子的案例
//1、先输出1 ~ 100数字
for (int i = 1; i <= 100; i++){
if (i%7 ==0||i%10 == 7||i/10==7)
cout << "敲桌子" << endl;
else
cout << i << " ";
}
cout << endl;
//2、从100个数字中找到特殊数字,打印敲桌子
system("pause");
return 0;
}
037程序流程结构-嵌套循环
作用:在循环中再嵌套一层循环,解决一些实际问题
#include<iostream>
#include<string>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++){
for (int i = 0; i < 10; i++)
cout << "*" << " ";
cout << endl;
}
system("pause");
return 0;
}
038流程循环结构-嵌套循环案例-乘法口诀表
#include<iostream>
#include<string>
using namespace std;
int main()
{
for (int i =1; i < 10; i++){
for (int j = 1; j <= i; j++)
cout << j << " * " <<i <<" = " << i*j <<" ";
cout << endl;
}
system("pause");
return 0;
}
039程序流程结构-跳转语句-break语句
作用:用于跳出选择结构或者循环结构
break使用的时机
- 出现在switch条件语句中,作用是终止case并跳出switch
- 出现在循环语句中,作用是跳出当前的循环语句
- 出现在嵌套循环中,跳出最近的内层循环语句
#include<iostream>
#include<string>
using namespace std;
int main()
{
//break的使用时机
//1、出现在switch语句中
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;
default:
break;
}
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
//break的使用时机
//2、出现在循环语句中
for (int i = 0; i < 10; i++){
if (i == 5){
break;
}
cout << i << " ";
}
cout << endl;
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
//break的使用时机
//3、出现在嵌套循环语句中
for (int i = 0; i < 10; i++){
for (int j = 0; j < i; j++){
if (j == 5)
break;
cout << "*" << " ";
}
cout << endl;
}
system("pause");
return 0;
}
040程序流程结构-跳转语句-continue
作用:在循环语句中,跳出本次循环中余下尚未执行的语句,继续执行下一次循环。
continue并没有是整个循环终止,而break会跳出循环
#include<iostream>
#include<string>
using namespace std;
int main()
{
//continue 执行到本行,就不在执行后面的代码了,而执行下一次循环
for (int i = 0; i <= 50; i++){
//如果是奇数输出,偶数不输出
if (i % 2 == 0){
continue;//可以筛选条件,执行到此就不再向下执行,执行下一次循环
//break; //break会退出循环,而continue不会
}
cout << i << " ";
}
cout << endl;
system("pause");
return 0;
}
041程序流程结构-跳转语句-goto
·作用:可以无条件跳转语句
语法:goto标记;
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
#include<iostream>
#include<string>
using namespace std;
int main()
{
//goto语句
cout << "1、XXXXXX" << endl;
cout << "2、XXXXXX" << endl;
goto FLAG;
cout << "3、XXXXXX" << endl;
cout << "4、XXXXXX" << endl;
FLAG:
cout << "5、XXXXXX" << endl;
system("pause");
return 0;
}
042数组-一维数组定义方式
数组:所谓数组,就是一个集合,里面存放了相同类型的数据元素
特点1:数组中的每个数据元素都是相同的数据类型
特点2:数组中由连续的内存位置组成的
一维数组
一位数组的定义方式:
- 数据类型 数组名[数组长度];
- 数据类型 数组名[数组长度] = {值1,值2,…};
- 数组类型 数组名[ ] = {值1,值2,…};
#include<iostream>
#include<string>
using namespace std;
static void pritnArray(int *arr){
for (int i = 0; i < 5; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
//1. 数据类型 数组名[数组长度];
int arr[5];
arr[0] = 10, arr[1] = 20, arr[2] = 30, arr[3] = 40, arr[4] = 50;
pritnArray(arr);
//2. 数据类型 数组名[数组长度] = { 值1,值2,...... };
//如果在初始化数据时候,没有全部填写完成,会用0来填补剩余的数据
int arr2[5] = { 10, 20, 30, 40 };
pritnArray(arr2);
//3. 数组类型 数组名[] = { 值1,值2,...... };
int arr3[] = { 10, 20, 30, 40, 50 };
pritnArray(arr3);
system("pause");
return 0;
}
043数组-一维数组-数组名
一维数组名称的用途:
1、可以统计整个数组在内存中的长度
2、可以获取数组在内存中的首地址
#include<iostream>
#include<string>
using namespace std;
int main()
{
//数组名用途
//1、可以通过数组统计整个数组占用内存的大小
int arr[5] = { 1, 2, 3, 4, 5 };
cout << "整个数组所占内存空间:"<< sizeof(arr) << endl;
cout << "每个元素所占内存空间:"<< sizeof(arr[0]) << endl;
cout << "数组中元素个数为:" << sizeof(arr) / sizeof(arr[0]) << endl;
//2、可以通过数组名查看数组的首地址
cout << "数组的首地址为:" << (int)arr << endl;
cout << "数组中第一个元素的地址为:" << (int)&arr[0] << endl;
cout << "数组中第二个元素的地址为:" << (int)&arr[1] << endl;
//数组名是常量,不可以进行赋值操作
//arr = 100;
system("pause");
return 0;
}
044数组-一维数组案例-五只小猪称体重
练习案例1:五只小猪称体重
在一个数组中记录了五只小猪的 体重,如:int arr[5] = {300,350,200,400,250};
找出并打印最重的小猪的体重
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、创建5只小猪体重的数组
int arr[5] = { 300, 350, 200, 400, 250 };
//2、从数组中找到最大值
int max = 0; //先认定一个最大值为0
for (int i = 0; i < 5; i++){
if (max < arr[i])
max = arr[i];
}
//3、打印最大值
cout << "max = " << max << endl;
system("pause");
return 0;
}
045数组-一维数组案例-元素逆置
案例练习:数组元素逆置
案例描述:声明一个5个元素的数组,并且将元素逆置.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int start = 0; //记录起始下标位置
int end = sizeof(arr) / sizeof(arr[0]) - 1; //记录结束下标位置
for (int i = 0; i < (start + end) / 2; i++){
if (start < end){
swap(arr[start], arr[end]);
start++;
end--;
}
}
cout << "数组逆置后为:" << endl;
for (int i = 0; i < 5; i++){
cout << arr[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
046数组-一维数组-冒泡排序
作用:最常用的排序算法,对数组内元素进行排序
1、比较相邻的元素,如果第一个比第二个大,就交换它们两个
2、对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值
3、重复以上的步骤,每次比较次数-1,直到不需要比较
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[8] = { 1, 4, 5, 6, 3, 2, 8, 9 };
for (int i = 0; i < 8-1; i++){
for (int j = 0; j < 8 - i -1; j++)
swap(arr[j], arr[j + 1]);
}
for (int i = 0; i < 8; i++)
cout << arr[i] << " ";
cout << endl;
system("pause");
return 0;
}
047数组-二维数组-定义方式
二维数组就是在一维数组上,多加一个维度
二维数组定义方式
- 数据类型 数组名[行数][列数]
2.数据类型 数组名[行数][列数] = {{数据1,数据2},{数据3,数据4}}; - 数据类型 数组名[行数][列数] = { 数据1,数据2,数据3,数据4};
- 数据类型 数组名 [ ][列数] = {数据1,数据2,数据3,数据4};
#include<iostream>
#include<string>
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;
}
048数组-二维数组-数组名
查看二维数组所占内存空间
获取二维数组首地址
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
//1、统计查看占用内存空间大小
cout << "二维数组占用的内存空间:" << sizeof(arr) << endl;
cout << "二维数组的第一行元素所占的空间:" << sizeof(arr[0]) << endl;
cout << "二维数组第一个元素占用的内存空间:" << sizeof(arr[0][0]) << endl;
cout << "二维数组的行数为:" << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "二维数组所占的列数为:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
//2、可以查看二维数组的首地址
cout << "二维数组首地址为:" << (int)arr << endl;
cout << "二维数组中第一行的首地址" << (int)arr[0] << endl;
cout << "二维数组中第一行第一个元素的首地址" << (int)&arr[0][0] << endl;
system("pause");
return 0;
}
049数组-二维数组案例-考试成绩统计
二维数组应用案例
考试成绩统计:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//二维数组案例-考试成绩统计
//1、创建二维数组
int scores[3][3] = { { 100, 100, 100 }, { 90, 50, 100 }, { 60, 70, 80 } };
string names[3] = { "张三", "李四", "王五" };
//2、统计每个人的总分
for (int i = 0; i < 3; i++){
int sum = 0; //统计分数总和变量
for (int j = 0; j < 3; j++){
sum += scores[i][j];
}
cout <<names[i]<< "的总分为:" << sum << endl;
}
system("pause");
return 0;
}
050函数-函数的定义
作用:将一段经常使用的代码封装起来-减少重复代码的
一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能
函数的定义
函数的定义一般主要有5个步骤:
1、返回值类型
2、函数名
3、参数列表
4、函数体语句
5、return 表达式
#include<iostream>
#include<string>
using namespace std;
int add(int num1, int num2){
int sum = num1 + num2;
return sum;
}
int main()
{
int a = 10, b = 20;
cout << add(a, b) << endl;
system("pause");
return 0;
}
051函数-函数的调用
功能:使用定义好的函数
语法:函数名(参数)
052函数-值传递
所谓值传递,就是函数调用时实参将数值传入给形参
值传递,如果形参发生,并不会影响实参
#include<iostream>
#include<string>
using namespace std;
//值传递
//定义函数,实现两个数字进行交换的函数
//如果函数不需要返回值,声明的时候可以写void
void swap(int a, int b){
cout << "交换前:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
int temp = a;
a = b;
b = temp;
cout << "交换后:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
int main()
{
int a = 10, b = 20;
cout << "main交换前:" << endl;
cout << "main a = " << a << endl;
cout << "main b = " << b << endl;
swap(a, b);
//当我们做值传递的时候,函数的形参发生改变,并不会影响实参
cout << "main交换后:" << endl;
cout << "main a = " << a << endl;
cout << "main b = " << b << endl;
system("pause");
return 0;
}
053函数-常见的样式
常见的函数样式有4种
1、无参无返
2、有参无返
3、无参有返
4、有参有返
#include<iostream>
#include<string>
using namespace std;
//函数的常见样式
static void test01(){//1、无参无返
cout << "this is test01()" << endl;
}
static void test02(int a){//2、有参无返
cout << "this is test02 a = " << a << endl;
}
static int test03(){//3、无参有返
cout << "this is test03()" << endl;
return 12;
}
static int test04(int a){//4、有参有返
cout << "this is test04 a = " << a << endl;
return 0;
}
int main()
{
test01();
test02(10);
int num1 =test03();
cout << num1 << endl;
test04(20);
system("pause");
return 0;
}
054函数-函数的声明
作用:告诉编译器函数名称及如何调用函数,函数的实际主体可以单独定义
函数的声明可以多次,但函数的定义只能有一次。
#include<iostream>
#include<string>
using namespace std;
//函数声明
//比较函数,实现两个整型的数字进行比较,返回较大的值
//提前告诉编译器函数的存在,可以利用函数的声明
//声明可以有多次
extern int max(int a, int b);
extern int max(int a, int b);
extern int max(int a, int b);
int main()
{
int a = 10, b = 20;
cout << max(a, b)<<endl;
system("pause");
return 0;
}
extern int max(int a, int b){
return a > b ? a : b;
}
055函数-函数的分文件编写
作用:让代码结构更加清晰
函数分文件编写一本有4个步骤
- 创建后缀名为.h的头文件
- 创建后缀名为.cpp的源文件
- 在头文件种写函数的声明
- 在源文件中写函数的定义
055myswap.h
#include<iostream>
using namespace std;
//函数的声明
void swap(int a, int b);
055myswap.cpp
#include"055myswap.h"
//函数的定义
void swap(int a, int b){
int temp = a;
a = b;
b = temp;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
055.cpp
#include<iostream>
#include<string>
#include"055myswap.h"
using namespace std;
//函数的分文件编写
//实现一个两个数字交换的函数
int main()
{
int a = 10, b = 20;
swap(a, b);
system("pause");
return 0;
}
056指针-指针的定义和使用
指针的作用:可以通过指针间接访问内存
- 内存编号是从0开始记录的,一般用十六进制数字表示
- 可以利用指针比哪里保存地址
指针的变量定义语法:数据类型 *变量名;
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a = 10;
//1、定义一个指针
int *p;
//让指针记录变量a的地址
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "指针p为:" << p << endl;
//2、使用指针
//可以通过解引用的方式来找到指针指向内存中的数据
*p = 1000;
cout << " a= " << a << endl;
cout << "*p =" << *p << endl;
system("pause");
return 0;
}
057指针-指针所占内存空间
提问:指针也是种数据类型,那么这种数据类型占用多少内存空间?
#include<iostream>
#include<string>
using namespace std;
int main()
{
//指针所占的内存空间的大小
int a = 10;
int *p = &a;
//在32位系统下,指针是占4个字节空间大小,不管是什么数据类型
//在64位系统下,指针是占8个字节空间大小,不管是什么数据类型
cout << "sizeof(int*) =" << sizeof(int*) << endl;
cout << "sizeof(dobule*) =" << sizeof(double*) << endl;
cout << "sizeof(float*) =" << sizeof(float*) << endl;
cout << "sizeof(short*) =" << sizeof(short*) << endl;
system("pause");
return 0;
}
058指针-空指针
空指针:指针变量指向的内存中编号为0的空间
用途:初始化指针变量
注意:空指针指向的内存是不可以访问的
#include<iostream>
#include<string>
using namespace std;
int main()
{
//空指针
//1、空指针用于给指针变量进行初始化
int* p = nullptr;
//2、空指针是不可以进行访问的
//0 ~ 255之间的编号是系统占用的,因此不可以访问
*p = 100;
system("pause");
return 0;
}
059指针-野指针
野指针:指针变量指向非法的内存空间
空指针和野指针都不是我们申请的空间,因此不要访问。
060指针-const修饰指针
const修饰指针有三种情况:
- const修饰指针 ----常量指针
- const修饰常量 ----指针常量
- const修饰指针,又修饰常量
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、const修饰指针 常量指针
int a = 10, b = 20;
const int *p = &a; //特点是指针指向的值不可以改,指针的指向可以改
//*p = 20;//错误
p = &b;
//2、const修饰常量 指针常量
int *const p2 = &a;//特点是指针的指向是不可以改,指针指向的值可以改
*p2 = b; //正确
//p2 = &b; //错误
//3、const修饰指针和常量
const int * const p3 = &a;//指针的指向和指针指向的值都不可以修改
//*p3 = 100; //错误
//p3 = &b; //错误
system("pause");
return 0;
}
061指针-指针和数组
作用:利用指针访问数组中元素
#include<iostream>
#include<string>
using namespace std;
int main()
{
//指针和数组
//利用指针访问数组中的元素
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int *p = arr; //arr就是数组的首地址
cout << "利用指针访问第一个元素:" << *p << endl;
p++; //让指针向后偏移4个字节
cout << "利用指针访问第二个元素:" << *p << endl;
cout << "利用指针遍历数组" << endl;
auto p2 = arr;
for (int i = 0; i < 10; i++){
cout << *p2 << " ";
p2++;
}
cout << endl;
system("pause");
return 0;
}
062指针-指针和函数
作用:利用指针做函数参数,可以修改实参的值
#include<iostream>
#include<string>
using namespace std;
static void swap01(int a, int b){
int temp = a;
a = b;
b = temp;
}
static void swap02(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
//指针和函数
//1、值传递
int a = 10, b = 20;
swap01(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
//2、地址传递
swap02(&a, &b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
总结:如果不想修改实参,就用值传递,如果想修改实参,就用地址传递。
063指针-指针配合数组和函数案例
案例封装:封装一个函数,利用冒泡排序,实现对整数型数组的升序排序
int arr[10] = {1,3,4,5,6,2,7,9,8,0};
#include<iostream>
#include<string>
using namespace std;
static void bubbleSort(int *arr,int n){
for (int i = 0; i < n - 1; i++){
for (int j = 0; j < n - i - 1; j++){
if (arr[j] >arr[j + 1])
swap(arr[j], arr[j + 1]);
}
}
}
static void printArray(int*arr,int n){
for (int i = 0; i < n; i++){
cout << *arr << " ";
arr++;
}
cout << endl;
}
int main()
{
//1、先创建数组
int arr[10] = { 1, 3, 4, 5, 6, 2, 7, 9, 8, 0 };
int len = sizeof(arr) / sizeof(arr[0]);
//2、创建函数,实现冒泡排序
bubbleSort(arr, len);
//3、打印排序后的数组
printArray(arr, len);
system("pause");
return 0;
}
064结构体-结构体定义和使用
结构体的基本概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
结构体的定义和使用
语法:struct 结构体{结构体成员列表};
通过结构体创建变量的方式有三种:
- struct结构体名 变量名
- struct结构体名 变量名 = {成员1值,成员2值…}
- 定义结构体是顺便创建变量
#include<iostream>
#include<string>
using namespace std;
//自定义的数据类型,就是数据类型的集合的一个类型
struct Student{
string m_Name;
int m_Age;
int m_Score;
}s3;
int main()
{
//2.1 struct Student s1;
Student s1;
s1.m_Name = "张三";
s1.m_Age = 20;
s1.m_Score = 100;
cout << "姓名:" << s1.m_Name << "\t年龄:" << s1.m_Age << "\t分数:" << s1.m_Score << endl;
//2.2 struct Student s2 = { ... }
Student s2 = { "李四", 18, 90 };
cout << "姓名:" << s2.m_Name << "\t年龄:" << s2.m_Age << "\t分数:" << s2.m_Score << endl;
//2.3 在定义结构体时顺便创建结构体变量
s3.m_Name = "王五";
s3.m_Age = 25;
s3.m_Score = 100;
cout << "姓名:" << s3.m_Name << "\t年龄:" << s3.m_Age << "\t分数:" << s3.m_Score << endl;
system("pause");
return 0;
}
065结构体-结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体 数组名[元素个数] = { { },{ },… { }};
#include<iostream>
#include<string>
using namespace std;
//结构体数组
//1、定义结构体
struct Student{
string m_Name;
int m_Age;
int m_Score;
};
int main()
{
//2、创建结构体数组
Student stuArray[3] =
{
{ "张三", 18, 100 },
{ "王五", 19, 90 },
{ "李四", 15, 99 }
};
//3、给结构体数组中的元素赋值
stuArray[2].m_Name = "赵六";
stuArray[2].m_Age = 80;
stuArray[2].m_Score = 80;
//4、遍历结构体数组
for (int i = 0; i < 3; i++){
cout << "姓名:" << stuArray[i].m_Name
<< "\t年龄:" << stuArray[i].m_Age
<< "\t分数:" << stuArray[i].m_Score <<endl;
}
system("pause");
return 0;
}
066结构体-结构体指针
作用:通过指针访问结构体中的成员
- 利用操作符->可以通过结构体指针访问结构体属性
#include<iostream>
#include<string>
using namespace std;
//结构体指针
//1、定义结构体
struct Student{
string m_Name;
int m_Age;
int m_Score;
};
int main()
{
//1、创建结构体变量
Student s = { "张三", 18, 100 };
//2、通过指针指向结构体变量
Student *sp = &s;
//3、通过指针访问结构体变量中的数据
cout << "姓名:" << sp->m_Age
<< "\t年龄:" << (*sp).m_Age
<< "\t分数:" << sp->m_Score << endl;
system("pause");
return 0;
}
067结构体-结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
#include<iostream>
#include<string>
using namespace std;
//1、定义结构体
struct Student{
string m_Name;
int m_Age;
int m_Score;
};
//定义老师的结构体
struct Teacher{
int id;
string name;
int age;
Student stu;
};
int main()
{
Teacher t;
t.id = 100;
t.age = 50;
t.name = "老王";
t.stu.m_Name = "小王";
t.stu.m_Age = 20;
t.stu.m_Score = 100;
cout << "老师的姓名: " << t.name << "\t老师的编号:" << t.id
<< "\t老师的年龄:" << t.age << "\n" << "老师辅导的学生姓名:" << t.stu.m_Name
<< "\t学生的年龄:" << t.stu.m_Age << "\t学生的考试分数:" << t.stu.m_Score << endl;
system("pause");
return 0;
}
068结构体-结构体做函数参数
作用:将结构体作为参数向函数中传递
传递的方式有:
- 值传递
- 地址传递
#include<iostream>
#include<string>
using namespace std;
//1、定义结构体
struct Student{
string m_Name;
int m_Age;
int m_Score;
};
//1、值传递
static void printStudent1(Student s){
s.m_Age = 100;
cout << "子函数1中 姓名:\t" << s.m_Name <<
"\t 年龄:" << s.m_Age << "\t 分数:" << s.m_Score << endl;
}
//2、地址传递
static void printStudent2(const Student *s){
cout << "子函数2中 姓名:\t" << s->m_Name <<
"\t 年龄:" << (*s).m_Age << "\t 分数:" << s->m_Score << endl;
}
int main()
{
//结构体做函数参数
//将学生传入到一个参数中,打印学生身上的所有信息
//创建结构体变量
Student s = { "张三", 18, 100 };
cout << "main中函数打印 姓名\t " << s.m_Name <<
"\t 年龄:" << s.m_Age << "\t 分数:" << s.m_Score << endl;
printStudent1(s);
//printStudent2(&s);
system("pause");
return 0;
}
总结:如果不想修改主函数中的数据,用值传递,反之用地址传递
069结构体-结构体中const使用场景
作用:用const 来防止误操作
将函数中的形参改为指针,可以减少内存空间,而且不会复制除新的副本出来
#include<iostream>
#include<string>
using namespace std;
//1、定义结构体
struct Student{
string m_Name;
int m_Age;
int m_Score;
};
//将函数中的形参改为指针,可以减少内存空间,而且不会复制除新的副本出来
static void printStudent(const Student *s){
//s->m_Age = 100; //错误,禁止修改,防止误操作
cout << "子函数2中 姓名:\t" << s->m_Name <<
"\t 年龄:" << (*s).m_Age << "\t 分数:" << s->m_Score << endl;
}
int main()
{
//创建结构体变量
Student s = { "张三", 18, 100 };
//通过函数打印结构体变量信息
printStudent(&s);
system("pause");
return 0;
}
070结构体-结构体案例1
案例描述:
学校正在做毕业设计,每个老师带5个学生,总共有3名老师,需求如下
设计学生和老师的结构体,其中老师的结构体中,有老师 姓名和一个存放5名学生的数组作为成员,学生的成员有姓名、考试分数、创建数组存放3名老师,通过函数给每个老师及所带学生赋值最重打印出老师数据以及老师所带学生的数据。
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
//学生的结构体
struct Student{
string sName;
int sScore;
};
//老师的结构体定义
struct Teacher{
string tName;
Student sArray[5];
};
//给老师和学生赋值的函数
static void allocateSpace(Teacher *tArray, int len){
string nameSeed = "ABCDE";
for (int i = 0; i < len; i++){//给老师赋值
tArray[i].tName = "Teacher_";
tArray[i].tName += nameSeed[i];
//通过循环给每个老师的学生赋值
for (int j = 0; j < 5; j++){
tArray[i].sArray[j].sName = "Student_";
tArray[i].sArray[j].sName += nameSeed[j];
int random = rand() % 61 + 40; //40~99
tArray[i].sArray[j].sScore = random;
}
}
}
//打印所有信息
static void printInfor(Teacher *tArray, int len){
for (int i = 0; i < len; i++){
cout << "老师的姓名:" << tArray[i].tName << endl;
for (int j = 0; j < 5; j++){
cout << "\t学生的姓名:" << tArray[i].sArray[j].sName
<< "\t考试的分数" << tArray[i].sArray[j].sScore << endl;
}
}
}
int main()
{
//随机数种子
srand((unsigned int)time(nullptr));
//1、创建3名老师的数组
Teacher tArray[3];
//2、通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
//3、打印所有老师及所带的学生信息
printInfor(tArray, len);
system("pause");
return 0;
}
071结构体-结构体案例2
案例描述:
设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。
通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果
#include<iostream>
#include<string>
using namespace std;
//1、设计英雄的结构体
struct Hero{
string name;
int age;
string sex;
};
static void bubbleSort(Hero *heroArray, int len){
for (int i = 0; i < len - 1; i++){
for (int j = 0; j < len - i - 1; j++){
if (heroArray[j].age > heroArray[j + 1].age)
swap(heroArray[j], heroArray[j + 1]);
}
}
}
static void printHeroArray(Hero *heroArray, int len){
for (int i = 0; i < len; i++){
cout << "英雄的姓名:" << heroArray[i].name << "英雄的年龄:" << heroArray[i].age
<< "英雄的性别:" << heroArray[i].sex << endl;
}
}
int main()
{
//2、创建数组存放5个英雄
Hero heroArray[5] =
{
{ "刘备", 54, "男" },
{ "关于", 39, "男" },
{ "张飞", 30, "男" },
{ "赵云", 36, "男" },
{ "貂蝉", 19, "女" }
};
int len = sizeof(heroArray) / sizeof(heroArray[0]);
/*for (int i = 0; i < len; i++){
cout << "姓名:" << heroArray[i].name << "\t年龄:" <<
heroArray[i].age << "\t性别:" << heroArray[i].sex << endl;
}*/
//3、对数组进行排序,按照年龄进行升序排列
bubbleSort(heroArray, len);
//4、将排序后的打印输出
printHeroArray(heroArray, len);
system("pause");
return 0;
}
内容总结
以上是互联网集市为您收集整理的C++入门基础篇全部内容,希望文章能够帮你解决C++入门基础篇所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。