数据类型
C++定义常量的两种方式:
1、#define 宏常量
#define Day 7
//Day = 14 错误 Day是常量 一旦修改就会出错
2、Const修饰的变量
Const int month = 12;
Month = 24;错误 const修饰的变量也称为常量
常用的编程关键字
创建变量:数据类型 变量名称 = 变量初始值
不要用关键字给变量或者常量写名称
Int int = 10;错误 第二个int不能作为变量名称
标识符命名规则
1、标识符不能是关键字
2、只能由字母、数字、下划线组成
3、第一个字符必须是字母或者下划线
4、标识符中的字母区分大小写
数据类型-整型
意义:给变量分配合适空间
Short 短整型 2字节
Int 整型 4字节
Long 长整型 Windows4字节
Long long 长长整型 8字节
Sizeof关键字
作用:统计数据类型所占内存大小
语法:sizeof(数据类型/变量)
Short num1=10
Cout<<sizeof(short/num1)<<endl;
实型(浮点型)
作用:表示小数
单精度Float 4字节 7位有效数字
双精度Double 8字节 15-16位
字符型
作用:字符型变量显示单个字符
语法:char ch = ‘a’;
转义字符
\n换行
\t 水平制表
\v 垂直制表
字符串型
1、C风格字符串:char 变量名[] = “字符串值”
Char strl[] = “hello world”;
2、C++风格:
头文件加#include
String str2 = “hello world”;
布尔类型
作用:布尔数据类型代表真/假的值
bool类型只有两个值:
True–真(本质是1)
False–假(本质是0)
1、创建bool数据类型
Bool flag = true;
Cout<<flag<<endl;
本质上1代表真 0代表假
运算符
算术运算符 四则运算
%取余
两个数相除除数不可以为0,所以也做不了取模运算
两个小数是不可以做取模运算的
++前置递增 先让变量+1 然后进行表达式运算
++后置递增 先进行表达式运算 再让变量+1
```cpp
#include<iostream>
Using namespace std;
Int main()
{
int a1 = 10;
int b1 = ++a1 * 10;
cout<<"a1="<<a1<<endl;
cout << "b1 = "<< b1 <<endl;
int a2 = 10;
int b2 = a2++ * 10;
cout<<"a2="<<a2<<endl;
cout <<"b2 = "<< b2 <<endl;
}
–前置递减
–后置递减
赋值运算符
#include<iostream>
Using namespace std;
Int main()
{
int a = 10;
a = 100;
cout<<"a="<< a<<endl;
a = 10;
a += 2;//a=a+2
cout << "a="<<a<<endl;
a = 10;
a -= 2;//a=a-2
cout << "a="<<a<<endl;
a = 10;
a *= 2;//a=a*2
cout << "a="<<a<<endl;
a = 10;
a /= 2;//a=a/2
cout << "a="<<a<<endl;
a = 10;
a %= 2;//a=a%2
cout << "a="<<a<<endl;
}
比较运算符
==相等于
逻辑运算符
!非
&&与 两个条件都为真 结果才为真;同真为真,其余为假 0为假其余为真
||或 同假为假 其余为真
逻辑运算符
作用:用于根据表达式的返回值返回真值或者假值
程序流程结构
顺序结构
选择结构
循环结构
选择结构
if语句
1、单行格式if语句{条件满足执行语句}
注意:if条件后面不要加分号
#include<iostream>
Using namespace std;
Int main()
{
int score = 0;
cout<<"qing shu ru fen shu:"<<endl;
cin>>score;
cout <<"nin de fen shu wei:"<<score<<endl;
if(score > 600)
{
cout<<"congratulations!"<<endl;
}
2、多行格式if语句
If(条件)
{
条件满足执行语句
}
Else
{
条件不满足执行语句
}
3、多条件if语句
If(条件){条件满足语句}else if (条件2){条件2满足语句}…else{都不满足执行语句}
#include<iostream>
Using namespace std;
Int main()
{
int score = 0;
cout<<"qing shu ru fen shu:"<<endl;
cin>>score;
cout <<"nin de fen shu wei:"<<score<<endl;
if(score > 600)
{
cout<<"kao shang 1 ben!"<<endl;
}
else if(score > 500)
{
cout<<"kao shang 2 ben"<<endl;
}
else if(score > 400)
{
cout<<"kao shang 3 ben"<<endl;
}
else
{
cout<<"wei kao shang ben ke"<<endl;
}
return 0;
}
嵌套if语句
在if语句中嵌套使用if语句,达到更精确的条件判断
三母运算符
作用:通过三母运算符实现简单的判断
语法:表达式1?表达式2:表达式3
1真执行2 返回2
1假 执行3 返回3
#include<iostream>
Using namespace std;
Int main()
{
int a=10;
int b=5;
int c=4;
c = (a > b ) ? a : b;
cout << c<<endl;
(a > b ? a : b) = 100;
cout << " a =" << a<< endl;
cout<<"b="<<b<<endl;
return 0;
}
switch语句
作用:执行多条条件分支句
Switch(表达式)
{
Case 结果1:执行语句;break;
Case 结果2:执行语句;break;
…
Default:执行语句;break;
}
while循环
While(循环条件){执行语句}
#include<iostream>
Using namespace std;
Int main()
{
int num = 0;
while(num < 11)
{
cout<< num <<endl;
num = num + 1;
}
Return 0;
}
猜数字
#include<iostream>
Using namespace std;
Int main()
{
int val = 0;
while(1)
{
cin >> val;
if(val > num)
{
cout<< "da" <<endl;
}
else if( val < num)
{
cout<< "xiao"<<endl;
}
else
{
cout<<"congratulations"<<endl;
break;
}
}
Return 0;
}
Do … while
满足循环条件,执行循环语句
Do{循环语句}while(循环条件)
与while区别在于会先执行一次循环语句,在判断循环条件
#include<iostream>
Using namespace std;
Int main()
{
int num = 0;
do
{
cout << num<<endl;
num ++;
}
while(num < 10);
return 0;
}
水仙花数
#include<iostream>
Using namespace std;
Int main()
{
int num = 100;
do
{
int a = 0;
int b = 0;
int c = 0;
a = num / 100;
b = num / 10 % 10;
c = num % 10;
if(num == (a * a * a) + (b * b * b ) + ( c * c * c))
{
cout << num << endl;
}
num ++;
}
while(num < 999);
return 0;
}
153 370 371 407
for循环
For(起始表达式;条件表达式;末尾循环体){循环语句;}
循环嵌套
在循环中再嵌套一层循环,解决实际问题
#include<iostream>
Using namespace std;
Int main()
{
int a;
int b;
for(a=0;a<10;a++)
{
for(b=0;b<10;b++)
{
cout<<"*";
}
cout<<endl;
}
Return 0;
}
99乘法表
#include<iostream>
Using namespace std;
Int main()
{
int i = 1;
int j = 1;
for(i=1;i<=9;i++)
{
for(j=1;j <= i;j++)
{
cout<<j << "*"<< i<<"="<<j*i<<endl;
}
cout<<endl;
}
Return 0;
}
跳转语句
Break:跳出选择结构或者循环结构
Continue:在循环语句中,跳出本次循环中余下尚未执行的语句
goto语句
无条件跳转语句
语法:goto 标记
如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
Goto FLAG;
FLAG:
*数组
*:集合 里面存放相同类型的数据元素
1、数组中的每个元素都是相同的数据类型
2、数组是由连续的内存位置组成的
定义方式:
1 数据类型 数组名【数组长度】;
2 数据类型 数组名 【数组长度】 = {值1, 值2…}
3 数据类型 数组名[] = {值1,值2…}
数组的特点:
放在一块连续的内存空间中
数组每个元素都是相同的数据类型
如果在初始化数据时,数据没有全部填写完,会用0填补数据
定义数组时必须有初识长度
一维数组名称的用途
1、可以统计整个数组在内存中的长度 :sizeof(数组名)
统计第一个元素的长度:sizeof(arry[0])
2、可以获取数组在内存中的首地址
Cout<<arry<<endl;
数组名是常量,不能进行赋值操作
Sizeof(arr) / sizeof(arr[0]) -1
数组元素的逆置
#include<iostream>
Using namespace std;
Int main()
{
int arr[5] = {1,2,3,4,5};
cout<< "ni qina: "<<endl;
for(int i = 0;i<5;i++)
{
cout<<arr[i] <<endl;
}
int start = 0;
int end;
end = sizeof(arr) / sizeof(arr[0]) - 1;
while(start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
for(int i = 0;i<5;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
冒泡排序
作用:排序算法,对数组内元素进行排序
#include<iostream>
Using namespace std;
Int main()
{
int arr[9] = {4,2,8,0,5,7,1,3,9};
for(int i = 0; i < 9 - 1; i++)
{
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;
}
}
}
for(int i = 0;i<9;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
二维数组
二维数组的数组名用途
查看二维数组所占内存空间 sizeof
获取二维数组首地址 直接打印
二维数组的应用
函数
将一端经常使用的代码封装,减少重复代码
一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能
函数定义的5个步骤:
1、返回值类型
2、函数名
3、参数列表
4、函数体语句
5、Return语句
语法:
返回值类型 函数名(参数列表)
{
函数体语句
Return表达式
}
函数的调用
功能:使用定义好的函数
语法:函数名(参数)
值传递
就是函数调用时实参将数值传入给形参
值传递时,如果形参发生改变,并不会影响实参
如果函数不需要返回值,声明的时候可以写void 无类型
Return返回值不需要的时候,可以不写return
函数的常见样式
1、无参无返
2、有参无返
3、无参有返
4、有参有返
函数的声明
告诉编辑器函数名称及如何调用函数 函数的实际主体可以单独定义
函数的声明可以多次,但是函数的定义只能有一次
函数的分文件编写
指针
通过指针间接访问内存
内存编号从0开始记录,一般用16进制数字表示
可以利用指针变量保存地址
指针定义的语法:数据类型 *指针变量名
让指针记录变量a的地址
使用指针,可以通过解引用的方式来找到指针指向的内存
指针前加 * 代表解引用 找到指针指向的内存中的数据
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int * p;
p = &a;
cout<<&a<<endl;
cout<<p<<endl;
*p = 1000;
cout<<a<<endl;
cout<<* p<<endl;
return 0;
}
指针所占内存空间
32位操作系统占4字节 64位操作系统占8字节
空指针:指针变量指向内存中的编号为0的空间
用途:初始化指针变量
注意:空指针指向的内存是不可以访问的
0~255之间的编号是系统占用的,因此不可以访问
野指针
指针指向非法的内存空间
在程序中,尽量避免出现野指针
Const修饰指针
const修饰指针 --常量指针
Const * p = &a;
指针的指向可以修改,但是指针指向的值不可以改
*p = 20错误 指向的值不可以改
P = &b正确 指向可以改
const修饰常量 --指针常量
Int * const p = &a;
特点:指针的指向不可以改,指针指向的值可以改
*p = 20正确 指向的值可以改
P = &b错误 指向不可以改
const既修饰指针,又修饰常量
Const int * const p = &a
指针的指向和指向的值都不可以改
指针和数组
利用指针访问数组中元素
#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 * p2 = arr;
for(int i = 0; i < 10; i++)
{
cout<<*p2 <<endl;
p2++;
}
return 0;
}
指针和函数
利用指针作函数参数,可以修改实参的值
修改实参用地址传递 不修改实参用值传递
指针、数组、函数
案例 :封装一个函数,冒泡排序,实现对整型数组的升序排序
例如数组int arr[10] = {4,3,6,9,1,2,10,8,7,5}
用到了冒泡排序
#include<iostream>
using namespace std;
int main()
{
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 printArr(int * arr, int len)
{
for(int i = 0;i<10;i++)
{
cout<<arr[i]<<endl;
}
}
int main()
{
int arr[10] = {4,3,6,9,1,2,10,8,7,5};
int len = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr,len);
printArr(arr,len);
}
结构体
用户自定义的数据类型 允许用户存储不同的数据类型
Struct 结构体名{结构体成员列表};
通过结构体创建变量的3种方式:
Struct 结构体名 变量名
Struct 结构体 变量名 = {成员1值,成员2值…}
定义结构体时顺便创建变量
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
int age;
int score;
}s3;
int main()
{
struct Student s1;
s1.name = "zhangsan";
s1.age = 19;
s1.score = 100;
cout<<s1.name<<s1.age<<s1.score<<endl;
struct Student s2 = {"lisi", 19, 80};
cout<<s2.name<<s2.age<<s2.score<<endl;
s3.name = "wangwu";
s3.age = 18;
s3.score = 90;
cout<<s3.name<<s3.age<<s3.score<<endl;
}
结构体数组
将自定义的结构体放入到数组中方便维护
Struct 结构体名 数组名【元素个数】 = { {}, {}, …{}}
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
int age;
int score;
};
int main()
{
struct Student stuArray[8] = {
{"zhangsan", 18, 100},
{"lisi", 19, 80},
{"wangwu", 10, 70},
};
stuArray[0].name = "zhaoliu";
stuArray[0].age = 19;
stuArray[0].score = 90;
for(int i = 0;i<3;i++)
{
cout<<stuArray[i].name<<stuArray[i].age<<stuArray[i].score<<endl;
}
}
结构体指针
通过指针访问结构体中的成员
利用操作符->可以通过结构体指针访问结构体属性
```cpp
#include<iostream>
#include<string>
Using namespace std;
struct Student{
string name;
int age;
int score;
};
int main()
{
struct Student s = {"zhangsan", 18, 100};
struct Student * p = &s;
cout<<p->name<<p->age<<p->score<<endl;
}
结构体嵌套结构体
结构体中的成员可以是另一个结构体
每个老师辅导一个学员 一个老师的结构体中 记录一个学生的结构体
#include<iostream>
#include<string>
Using namespace std;
struct student{
string name;
int age;
int score;
};
struct teacher{
int id;
string name;
int age;
struct student stu;
};
int main()
{
struct teacher t;
t.id = 3878432;
t.age = 45;
t.name = "zhangsan";
t.stu.name = "lisi";
t.stu.age = 18;
t.stu.score = 100;
cout<<t.id<<t.name<<t.age<<t.stu.name<<t.stu.age<<t.stu.score<<endl;
}
结构体做函数参数
将结构体作为参数向函数中传递
值传递/地址传递
#include<iostream>
#include<string>
Using namespace std;
struct student{
string name;
int age;
int score;
};
void printStudent1(struct student s)
{
cout<<s.name<<s.age<<s.score<<endl;
}
void printStudent2(struct student *p)
{
cout<<p->age<< p->name << p->score<<endl;
}
int main()
{
struct student s;
s.name = "zhangsan";
s.age = 18;
s.score = 90;
printStudent1(s);
printStudent2(&s);
}
结构体中const使用场景
用const来防止误操作
#include<iostream>
#include<string>
Using namespace std;
struct student{
string name;
int age;
int score;
};
void printStudent(student s)
{
cout<<s.name<<s.age<<s.score<<endl;
}
int main()
{
struct student s = {"zhangsan", 15, 100};
printStudent(s);//值传递,可行但是拷贝数据量大
}
//将函数中的形参改为指针,可以减少内存空间,提高效率,而且不会复制新的副本出来
#include<iostream>
#include<string>
Using namespace std;
struct student{
string name;
int age;
int score;
};
void printStudent(const student *s)
{
s->age = 150;
cout<<s->name<<s->age<<s->score<<endl;
}
int main()
{
struct student s = {"zhangsan", 15, 100};
printStudent(&s);
}
#include<iostream>
#include<string>
Using namespace std;
struct Hero
{
string name;
int age;
string sex;
};
int main()
{
struct Hero heroArray[5] =
{
{"liu",23,"nan"},
{"guan",22,"nan"},
{"zhang",20,"nan"},
{"zhao",21,"nan"},
{"diao",19,"nv"},
};
for(int i = 0;i<4;i++)
{
for(int j=0;j<5 - i - 1;j++ )
{
if(heroArray[j].age >heroArray[j + 1].age)
{
struct Hero temp = heroArray[j];
heroArray[j] = heroArray[j+1];
heroArray[j+1] = temp;
}
}
}
for(int i = 0;i<5;i++)
{
cout<<heroArray[i].name<<heroArray[i].age<<heroArray[i].sex <<endl;
}
}