—笔记续更—
作用:用于执行代码的运算
用于处理四则运算
int a = 5, b = 20;
cout << a % b << endl;
用于将表达式的值赋给变量
=
+= : a+=2 → a=a+2;
-=
*=
/=
%=
用于表达式的比较,并返回一个真值或假值
==
!=
<
<=
>=
用于根据表达式的值返回真值或假值
!
&&
||
顺序结构、选择结构、循环结构
int a = 0;
cout << "请输入分值:" << endl;
cin >> a;
if (a >= 480)
{
cout << "恭喜获得好成绩!" << endl;
}
else
{
cout << "很遗憾没能获得好成绩,继续加油!" << endl;
}
完整多条件、嵌套if语句例子:
#include
using namespace std;
int main()
{
int score = 0;
cout << "请输入分值:" << endl;
cin >> score;
cout << "您输入的分值是:" << score << endl;
if (score > 100 || score <= 0)
{
cout << "您的输入有误,请重新输入!" << endl;
}
else if (score >= 90)
{
cout << "恭喜获得:优!" << endl;
if (score >= 97)
{
cout << "你将获得奖学金:1000元" << endl;
}
else if (score >= 95)
{
cout << "你将获得奖学金:500元" << endl;
}
else
cout << "你将获得奖学金:100元" << endl;
}
else if (score >= 80)
{
cout << "恭喜获得:良!" << endl;
}
else if (score >= 70)
{
cout << "恭喜获得:中!" << endl;
}
else if (score >= 60)
{
cout << "恭喜获得:及格" << endl;
}
else
{
cout << "很遗憾您挂科了,继续加油吧!" << endl;
}
system("pause");
return 0;
}
语法:表达式1 ? 表达式2 :表达式3
解释:
如果表达式1为真,执行表达式 2,并返回表达式2的结果
如果表达式1为假,执行表达式3,并返回表达式3的结果
int a = 0;
int b = 0;
int c = 0;
int inter = 0;
int max = 0;
cout << "请输入a的值:" << endl;
cin >> a;
cout << "请输入b的值:" << endl;
cin >> b;
cout << "请输入c的值:" << endl;
cin >> c;
cout << "a的值为:" << a << endl;
cout << "b的值为:" << b << endl;
cout << "c的值为:" << c << endl;
inter = (a > b ? a : b);
max = (inter > c ? inter : c);
cout << "最大值是:" << max << endl;
执行多条件分支语句
语法:
switch(判断条件)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
case 结果3:执行语句;break;
...
default:执行语句;break;
}
switch和if的区别:
语法:while(循环条件){循环语句}
//输出整数 0-9
int num =0;
while (num < 10)
{
cout << num <<endl;
num++;
}
例子:
随机数:rand()%100 //生成随机数0-99 (伪随机数)
添加随机种子数:
- time 系统时间头文件包含 #iclude<ctime >
- 利用系统时间随机生成
srand((unsigned int )time(NULL));
```cpp
while (1)
{
int num = rand() % 100 + 1;
if (n <= 10)
{
cout << "随机数为:" << num << endl;
n = n + 1;
}
else
{
break;
}
}
猜数字游戏:
#include
using namespace std;
#include
int main()
{
srand((unsigned int)time(NULL));//生成随机数
int num = rand() % 100 + 1;
cout << "叮叮叮~ 来猜数啦!" << endl;
int n = 1;
int val = 0;//猜数字
while (1) //循环体
{
cin >> val;
cout << "您输入的数值为:" << val << endl;
if (val > num)
{
cout << "您猜大了" << endl;
}
else if (val < num)
{
cout << "您猜小了" << endl;
}
else
{
cout << "恭喜您猜对了" << endl;
break;
}
}
system("pause");
return 0;
}
语法:do{循环语句} while(循环条件);
区别:do…while 先执行一次循环语句,再判断循环条件
do…while例:
do
{
cout << num << endl;
num = num +1;
}
while (num < 20);//while后有;
while例:
while (num < 40)
{
cout << num << endl;
num = num + 1;
}
找水仙花数例:
#include
using namespace std;
int main()
{
int num = 100;
do //打印水仙花100-999的数值
{
int a = 0;//个位
int b = 0;//十位
int 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 << endl;
}
num++;
}
while (num < 1000);
system("pause");
return 0;
}
语法:for(其实表达式;条件表达式;末尾循环体){循环语句;}
for例:
int main()
{
for (int a = 0; a < 10; a++)
{
cout << a << endl;
}
system("pause");
return 0;
}
int main()
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 10; j++)
{
cout << "@#$ ";
}
cout << endl;
}
system("pause");
return 0;
}
九九乘法表:
int main() //嵌套循环 乘法表
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j<<"×"<<i<<"="<<j*i<<" ";
}
cout << endl;
}
system("pause");
return 0;
}
break使用的方式:
在continue循环语句中,跳出本次循环中余下未执行的语句,继续执行下一次循环。
作用:可以无条件跳出语句(不常用)
语法: goto标记
1 xxx
2 xxx
goto Flag;
3 xxx
4 xxx
Flag:
5 xxx
一个集合,存放同类型数据元素
1.数据类型 数组名 [数组长度];
int arr[5];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
2.数据类型 数组名 [数组长度] = { 值1,值2 …};
int arr2[5] = { 1,2,3,4,5 };
3.数据类型 数组名 [ ] = {值1,值2 …};
int arr3[] = { 1,2,3,4,5 };
数组名用途
1、可以通过数组名统计整个数组占用内存大小
//sizeof(arr)/sizeof(arr[0])
2、可以通过数组名查看数组首地址
cout << "查看数组首地址:" << (int)arr << endl;
cout << "查看数组第一个地址:" << (int)&arr[0] << endl;
//数组名是常量,不能赋值
数组找最大值例:
int arr4[] = { 1,2,50,4,10,1 };
int max = 0;
for(int i=0;i<5;i++)
if (arr4[i] > max)
{
max = arr4[i];
}
cout << "最大值为:" << max << endl;
数组逆置例:
int arr5[] = { 1,2,3,4,5 };
cout << "数组逆置前:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr5[i];
}
cout << endl;
int start = 0;
int end = sizeof(arr5) / sizeof(arr5[0]) - 1;
for (;start<end;)//实现元素互换
{
int temp = arr5[start];
arr5[start] = arr5[end];
arr5[end] = temp;
start++;
end--;
}
cout << "数组逆置后:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr5[i];
}
cout << endl;
int arr6[10] = { 4,80,10,8,1,2,3,6,5,9 };
cout << "冒泡前:" << endl;
for (int i = 0; i < 10; i++)
{
cout << arr6[i] << " ";
}
cout << endl;
cout << "冒泡后:" << endl;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10 - i - 1; j++)
{
if (arr6[j] > arr6[j+1])
{
int temp = arr6[j];
arr6[j] = arr6[j+1];
arr6[j+1] = temp;
}
}
}
for (int i = 0; i < 10; i++)
{
cout << arr6[i] << " ";
}
cout << endl;
1.数据类型 数组名[行数][列数];
2.数据类型 数组名[行数][列数] = {{数据1,数据2},{数据3,数据4}};
int arr1[2][3] =
{
{1,2,3},
{2,4,6}
};
cout << "该数组为:" << endl;
for (int i = 0; i < 2;i++)
{
for (int j = 0; j < 3;j++)
{
cout << arr1[i][j] << " ";
}
cout << endl;
}
3.数据类型 数组名[行数][列数] = { 数据1,数据2,数据3,数据4 };
4.数据类型 数组名[ ][列数] = { 数据1,数据2,数据3,数据4 };
int arr2[3][3] =
{
{100,100,100},
{90,50,100},
{60,70,80}
};
string name[3] = { "张三","李四","王五" };
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum = sum + arr2[i][j];
}
cout << name[i] << "的成绩为:" << sum << endl;
}
返回值类型 函数名 (参数列表)
{
函数体语句
return表达式
}
int add(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
功能:使用定义好的函数
语法:函数名(参数)
示例:
//定义加法函数
//函数定义的时候,num1和num2并没有真实数据,只是形式参数,简称形参
int add(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
int main()
{
//main函数中调用add函数
int A = 1;
int B = 5;
//函数调用语法;函数名称(参数)
//A,B称为实际参数,简称实参
//调用函数时,实参的值传递给形参
int c = add(A, B);
cout << "c的值为:" << c << endl;
system("pause");
return 0;
}
void swap(int num1, int num2)
{
cout << "交换前:" << endl;
cout << "num1=" << num1 << endl;
cout << "num2=" << num2 << endl;
int temp = num1;
num1 = num2;
num2 = temp;
cout << "交换后:" << endl;
cout << "num1=" << num1 << endl;
cout << "num2=" << num2 << endl;
}
int main()
{
int a = 15;
int b = 30;
swap(a, b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
return 0;
}
1.无参无返
2.有参无返
3.无参有返
4.有参有返
//1.无参无返
void test1()
{
cout << "this is test1" << endl;
}
//2.有参无返
void test2(int num1)
{
cout << " this is test2 num1=" << num1 << endl;
}
//3.无参有返
int test3()
{
cout << " this is test3 :" ;
return 50;
}
//4.有参有返
int test4(int num2)
{
cout << "this is test4 num3=" << num2 << endl;
return num2;
}
int main()
{
//1.无参无返函数调用
test1();
//2.有参无返函数调用
test2(200);
//3.无参有返函数调用
int a2=test3();
cout << a2 << endl;
//4.有参有返函数调用
int a3 = test4(500);
cout << "a3=" << a3 << endl;
system("pause");
return 0;
}
用途:告诉编译器函数名称及如何调用函数,函数的实际主体可以单独定义
(提前告诉编译器函数存在,可以将函数放置在int main之后,即函数往后写)
用途:让代码结构更清晰
函数分文件编写4步骤:
1.创建后缀名为.h的头文件
例:
文件名:test4.h
2.创建后缀名为.cpp的源文件
文件名:test4.cpp
3.在头文件中写函数的声明
#include
using namespace std;
int test4(int num2);
4.在源文件中写函数的定义
#include"test4.h"
int test4(int num2)
{
cout << "this is test4 num3=" << num2 << endl;
return num2;
}
新文件函数调用,开头声明:
#include
using namespace std;
#include
#include"test4.h"
用途:可以通过指针间接访问内存
定义语法:数据类型 * 变量名
//1、定义指针
int a = 1;
//指针定义语法:数据类型 * 指针变量名
int * p;
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "p的地址为:" << p << endl;
//2、使用指针
//解引用找到指向内存
*p = 7;
cout << "a=" << a << endl;
cout << "*p=" << *p << endl;
//指针所占内存空间
int main()
{
int a = 10;
int* p = &a;
cout << "sizeof (int *)=" << sizeof(int*) << endl;
cout << "sizeof(double *)=" << sizeof(double*) << endl;
system("pause");
return 0;
1.空指针:指针变量指向内存中编号为0的空间
用途:初始化指针变量
注意:空指针内存不可访问
//空指针
int * p3 = NULL;
//*p3 = 5;(会报错)
2.野指针
//野指针
int* p4 = (int*)0x1100;
//访问野指针报错
cout << *p << endl;
三种情况:
1.const修饰指针 —常量指针
2.const修饰常量 —指针常量
3.const既修饰指针,又修饰常量
1.常量指针:指针的指向可以改,指针指向的值不可以改
int a=3;
int cinst * p=&a;
2.指针常量:指针的指向不可以改,指针指向的常量可以改
int a=3;
int cinst * p=&a;
3.const既修饰指针,又修饰常量
作用:利用指针访问数组中元素
//指针和数组
//利用指针访问数组中的元素
int main()
{
int arr[10] = { 1.2,3,4,5,6,7,8,9,10 };
int* p1 = arr;
cout << "数组首地址:" << arr[0] << endl;
cout << "指针解引用:" << *p1 << endl;
for (int i = 0; i <9; i++)
{
//cout << arr[i] << endl;
cout << *p1 << endl;
p1++;
}
system("pause");
return 0;
}
1、值传递
void swap(int num1, int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
cout << "swap01 a=" << num1 << endl;
cout << "swap02 b=" << num2 << endl;
}
int main()
{
int a = 1;
int b = 2;
swap(a, b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
return 0;
}
2、地址传递
void swap01(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
cout << "swap01 *p1=" << *p1 << endl;
cout << "swap02 *p2=" << *p2 << endl;
}
int main()
{
int a = 1;
int b = 2;
swap01(&a, &b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
return 0;
}
综合应用:
//冒泡排序函数
void bubbleSort(int * arr, int len)
{
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
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)
{
cout << "利用函数做数组的冒泡排序:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
}
int main()
{
//1.创建数组
int arr[5] = { 4,1,5,2,9};
int len = sizeof(arr) / sizeof(arr[0]);
//2.创建函数,使用冒泡法
bubbleSort(arr, len);
//3.打印输出
printarray(arr, len);
system("pause");
return 0;
}
结构体属于用户自定义的数据类型,允许用户储存不同的数据类型
语法:struct 结构体名称 {结构体成员列表};
通过结构体创建变量三种方式:
struct student
{
string name;
int age;
int score;
}s3;
int main()
{
//1.struct 结构体名 变量名
struct student s1;
s1.name = "wfh";
s1.age = 23;
s1.score = 90;
cout << "姓名:" << s1.name << " 年龄:" << s1.age
<< " 分数:" << s1.score << endl;
//2.struct 结构体名 变量名 = {成员1值,成员2值 ...}
struct student s2 = { "ydn",22,92 };
cout << "姓名:" << s2.name << " 年龄:" << s2.age
<< " 分数:" << s2.score << endl;
//3顺便创建结构体变量
s3.name = "lzx";
s3.age = 22;
s3.score = 95;
cout << "姓名:" << s3.name << " 年龄:" << s3.age
<< " 分数:" << s3.score << endl;
system("pause");
return 0;
}
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构名称 数组名[元素个数] = { {},{},… {} }
struct student
{
string name;
int age;
int score;
};
int main()
{
struct student stuarray[3] =
{
{ "wfh",23,90 },
{ "ydn",22,91 },
{ "lzx",22,92 }
};
stuarray[1].age = 50;
stuarray[1].score = 100;
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << stuarray[i].name << " 年龄:" << stuarray[i].age
<< " 分数:" << stuarray[i].score << endl;
}
system("pause");
return 0;
}
作用:通过指针访问结构体中的成员
语法:利用操作->可以通过结构体访问结构体属性
for (int i = 0; i < 3; i++)
{
struct student* p = &stuarray[i];
cout << "姓名:" << p -> name << " 年龄:" << p -> age
<< " 分数:" << p-> score << endl;
}
作用:结构体中的成员可以是另一个结构体
(如:一个老师辅导一名学生,在一个老师的结构体中,记录一个学生的结构体)
struct student
{
string name;
int age;
int score;
};
//结构体嵌套结构体
struct teacher
{
string name;
int id;
int age;
struct student stu;
};
int main()
{
//创建老师
struct teacher t;
t.name = "龙腾老师";
t.id = 1001;
t.age = 50;
t.stu ={ "wfh",23,90 };
cout << "老师信息: "<< endl <<"姓名:" << t.name << " 编号:" <<t.id
<< " 年龄:" << t.age << endl;
cout << "学生信息: " << endl << "姓名:" << t.stu.name << " 年龄:" << t.stu.age
<< " 分数:" << t.stu.score << endl;
system("pause");
return 0;
}
作用:将结构体作为参数向函数中传递
传递方式:
struct student
{
string name;
int age;
int score;
};
//1.值传递
void printstu1(struct student s1)
{
s1.age = 50;
cout<<"值传递 子函数中s1:"<<endl<< "姓名:" << s1.name << " 年龄:" << s1.age
<< " 分数:" << s1.score << endl;
}
//2.地址传递
void printstu2(struct student *p)
{
p->age = 100;
cout << "地址传递 子函数中s1:" << endl << "姓名:" << p->name << " 年龄:" << p->age
<< " 分数:" << p->score << endl;
}
int main()
{
student s1 = { "千域",23,90 };
//printstu1(s1);//形参改变实参不变
printstu2(&s1);//形参改变实参也改变
cout << "主函数中s1:" << endl <<"姓名:" << s1.name << " 年龄:" << s1.age
<< " 分数:" << s1.score << endl;
system("pause");
return 0;
}
作用:防止误操作
(值传递-复制 耗内存;地址传递-指针 仅占用4字节)
void printstu2(const struct student *p)
{
p->age = 100; //加入const之后,一旦有修改操作就会报错,可以防止误操作
cout << "地址传递 子函数中s1:" << endl << "姓名:" << p->name << " 年龄:" << p->age
<< " 分数:" << p->score << endl;
}
student s1 = { "千域",23,90 };
printstu2(&s1);
struct student
{
string sname;
int score;
};
struct teacher
{
string tname;
struct student sarray[5];
};
//定义函数
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() % 51+50;//50至100
tarray[i].sarray[j].score = random;
}
}
}
//打印输出
void printAll(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 << " 考试分数: " << tarray[i].sarray[i].score << endl;
}
}
}
int main()
{
//随机数种子
srand((unsigned int)time (NULL));
teacher tarray[3];
int len = sizeof(tarray) / sizeof(tarray[0]);
allocatespace(tarray, len);
printAll(tarray, len);
system("pause");
return 0;
}
以上,补充