基本格式:

1
2
3
4
5
6
7
#include<iostream>
using namespace std;
int main() //main函数是程序入口,必须有且仅有一个
{
system("pause");
return 0;
}

输出函数cout <<

1
2
3
4
5
cout << "输出内容" << endl;
//不同类型的输出值需要用"<<"隔开
cout << "a = " << a << endl;
cout << (int)ch <<endl;//将ch变量转化为int类型输出
//输出变量值

输入函数cin >>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//格式cin >> 变量
//整型
int a = 0;
cout <<"请给整型变量a赋值" << endl;
cin >> a;
cout << "整型变量a为" << a << endl;

float b = 3.14f;
cout << "请给浮点型变量b赋值" << endl;
cin >> b;
cout << "浮点型变量b为" << b << endl;

char ch = 'a';
cout <<"请给字符变量ch赋值" << endl;
cin >> ch;
cout << "字符变量ch为" << ch << endl;

#include <string>
string str = "hello";
cout <<"请给字符串变量str赋值" << endl;
cin >> str;
cout << "字符串变量str为" << str << endl;

bool flag = false;
cout <<"请给布尔型变量flag赋值" << endl;
cin >> flag;
cout << "布尔型变量flag为" << flag << endl;
//bool类型变量只要赋值非零即表示true

注释

1
2
3
4
5
//单行注释
/*
多行
注释
*/

变量使用(方面管理内存空间)

1
2
//语法:数据类型 变量名 = 变量初始值;
int a = 10;

定义常量两种方法

1
2
3
4
5
6
7
8
9
10
//1.#define 宏常量:
//#define 常量名 常量值
#define Day 7
cout << "一周有:" << Day << "天" <<endl;
//2.const修饰的变量:
//格式: const数据类型 常量名 = 常量值
int month = 12;
month = 24;//可修改
const int month = 12;
month = 24;//出现报错不可修改

标识符/关键字:

不能用来给变量或常量起名称

标识符命名规则:

不能是关键字;只能由字母、数字、下划线组成,第一个字符必须是字母或下划线;区分大小写;最好见名知义

数据类型(给变量分配合适的内存空间)

整型

1.整型int:4字节 -2^312^31-1
2.短整型short:2字节 -2^15(-32768)
2^15-1(32767)
3.长整型long:Windows4字节,Linux(32位)4字节,(64位)8字节
4.长长整型:longlong:8字节

实型(浮点型)

1.单精度float:4字节,有效数字7位

2.双精度double:8字节,有效数字15~16位

3.科学计数法:

1
2
3
4
float num1 = 3.14;
float num2 = 3.14f;//浮点数后加上f可以让电脑识别出为单精度,不然会以双精度接收
//但是默认都是以最高六位小数输出
float num3 =3e2;//3 * 10 ^ 2输出为300

字符型

1
2
3
4
5
6
7
//char 变量名 = ’ ’;只能有一个字符
//字符型变量占用一个字节,并不是把字符本身放入内存中存储,而是将对应的ASCLL编码放入储存单元
char ch = 'a';
cout << ch << endl;
cout << "char字符型变量所占内存" << sizeof(char) << endl;
cout << (int)ch << endl;//输出为97为a的ASCLL码,A65
//常见错误:单引号,一个字符

字符串

1
2
3
4
5
6
7
//string 变量名 = " ";
//注意双引号,C语言是用char函数,变量名后加上[]
//C++则需要包含一个“#include <string>”的头文件
char str1[] = "helloworld";
cout << str1 << endl;
string str2 = "helloworld";
cout << str2 << endl;

布尔类型bool

bool只有两个值,true——1,false——2,占用1字节

1
2
3
bool flag = true;
cout << flag << endl;//输出为1
cout <<sizeof(bool) << endl;

sizeof:统计数据类型所占内存大小

1
2
3
//sizeof( 数据类型或变量)
cout <<sizeof(int) << endl;
cout <<sizeof(num1) << endl;

转义字符:用于表示不能显示出来的ASCLL字符

1.\n:换行

2.\\:反斜杠(\无法单独输出)

3.\t:水平制表符(输出对齐)

1
2
3
4
5
cout << "hello world\n" << endl;
cout << "\\" << endl;
cout << "aaaa\thelloworld" << endl;
cout << "aaaaaa\thelloworld" << endl;
cout << "aa\thelloworld" << endl;

运算符(执行代码的运算)

算数运算符

1、+ 正号/加

2、- 负号/减

3、* 乘

4、/ 除

5、% 取模、取余

6、++ 前置递增、后置递减

7、– 前置递减、后置递减

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
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;//整数相除结果依然为整数,去除小数部分,除数不为零

double d1 = 0.5;
double d2 = 0.25;
cout << d1 / d2 << endl;//小数相除,结果可以是整数也可以是小数

cout << a1 % b1 <<endl;//小数不可以进行取余运算,零不可以作除数

//前置递增/递减++a:先让变量递增然后进行表达式运算
int a2 = 10;
int b2 = ++a2 * 10;//++a2 = 11

int a3 = 10;
int c3 = a3++ * 10;//a3++ = 10 运算完a3 = 11
cout << "b2 = " << b2 << endl;
cout << "c3 = " << c3 << endl;

//后置递增/递减a++:先进行表达式运算再让变量递增
system("pause");
return 0;
}

赋值运算符

1、赋值=

2、+=: a = a + …

3、-=: a = a - …

4、*=: a = a * …

5、/=: a = a / …

6、%=: a = a % …

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;

比较运算符

1、==等于

2、!=不等于

3、<小于

4、>大于

5、<=小于等于

6、>=大于等于

1
2
3
4
5
6
7
8
int a = 10;
int 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;

逻辑运算符

1、!非

2、&&与

3、||或

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a = 10;
cout << !a << endl;//0,对10(true)取反
cout << !!a << endl;//1

int b = 10;
int c = 10;
cout c< ( b && c ) << endl;//1
int c = 0;
cout << ( b && c ) << endl;//0

int d = 10;
int e = 0;
cout << ( d || e ) << endl;//1
d = 0;
cout << ( d || e ) << endl;//0

程序流程结构

顺序结构

选择结构

if语句

三种形式:

1、单行格式

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
//输入分数,如果大于600,显示考上一本大学
int score = 0;
cin >> score;
cout << "您输入的分数为" << score << endl;
if (score > 600)
{
//如果if后加上;则会导致if语句始终成立
cout << "恭喜您考上一本" << endl;
}
system ("pause");
return 0;
}

2、多行格式if else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
int main(){
//场景:输入分数,大于600输出考上一本,小于输出未考上一本
int score = 0;
cout << "请输入您的分数" << endl;
cin >> score;
cout << "您输入的分数为" << score << endl;
if (score > 600)
{
cout << "恭喜考上一本" << endl;
}
else
{
cout << "未考上一本" << endl;
}
system ("pause");
return 0;
}

3、多条件格式if-else if-else if-else多条件判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
int main(){
//输入分数,大于600考上一本,大于500考上二本,大于400考上三本,最后没考上本科
int score = 0;
cout << "请输入分数" << endl;
cin >> score;
if (score > 600)
{
cout << "您的分数为" << score << ",考上一本" << endl;
}
else if ( score > 500)
{
cout << "您的分数为" << score << ",考上二本" << endl;
}
else if (score > 400)
{
cout << "您的分数为" << score << ",考上三本" << endl;
}
else
{
cout << "您的分数为" << score << ",未考上本科" << endl;
}
system("pause");
return 0;
}

4、嵌套格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//输入分数,大于600考上一本,大于500考上二本,大于400考上三本,最后没考上本科
//大于600中,大于700考入清华,大于650考入北大,大于600考入人大
#include<iostream>
using namespace std;
int main(){
int score = 0;
cout << "请输入分数" << endl;
cin >> score;
if (score > 600)
{
cout << "您的分数为" << score << ",考上一本" << endl;
if (score > 700)
{
cout << "您的分数为" << score << ",考上清华" << endl;
}
else if (score > 650)
{
cout << "您的分数为" << score << ",考上北大" << endl;
}
else
{
cout << "您的分数为" << score << ",考上人大" << endl;
}
}
else if (score >500)
{
cout << "您的分数为" << score << ",考上二本" << endl;
}
else if (score > 400)
{
cout << "您的分数为" << score << ",考上三本" << endl;
}
else
{
cout << "您的分数为" << score << ",未考上本科" << endl;
}
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//三只小猪,输入各自体重,输出最重的
#include<iostream>
using name space std;
int main(){
int a = 0;
int b = 0;
int c = 0;
cout << "请输入第一只小猪的体重" << endl;
cin >> a;
cout << "请输入第二只小猪的体重" << endl;
cin >> b;
cout << "请输入第三只小猪的体重" << endl;
cin >> c;
if (a > b)
{
if (a > c)
{
cout << "第一只小猪最重,体重为" << a << endl;
}
else
{
cout << "第三只小猪最重,体重为" << c << endl;
}
else
{
if (b > c)
{
cout << "第二只小猪最重,体重为" << b << endl;
}
else
{
cout << "第三只小猪最重,体重为" << c << endl;
}
}
system("pause");
return 0;
}

三目运算符

结构:表达式1?表达式2:表达式3

若表达式1为true,则执行表达式2,并返回2的结果,若表达式1为false,则执行表达式3,并返回3的结果

返回的是变量值,可以继续赋值

1
2
3
4
5
6
7
8
9
10
11
12
//将a或b中较大的赋值给c
#include<iostream>
using namespace std;
int main(){
int a = 0;
int b = 10;
int c = 0;
c = a > b ? a : b;
//(a > b ? a : b) = 100;
system("pause");
return 0;
}

switch语句

语法:

switch (表达式){case 结果1;break;case 结果2;break;default:最后结果;break;}

缺点:判断的时候只能是整型或者字符型,不能是一个区间

优点:结构清晰,执行效率高

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
//给电影打分,经典9-10,非常好7-8,一般5-6,烂片<5
int main()
{
int score = 0;
cout << "请输入电影分数" << endl;
cin >> score;
cout << "您打的分数为" << score << endl;
switch(score)
{
case 9 || 10://貌似只能用一个或,多了会报错"case 标签值已出现在开关中"
cout << "您认为是经典电影" << endl;
break;//退出当前分支,否则会一次向下执行
case 7 || 8:
cout << "您认为是非常好的电影" << endl;
break;
case 5 || 6:
cout << "您认为是一般电影" << endl;
break;
default:
cout << "您认为是很烂的电影" << endl;
}
system("pause");
return 0;
}

循环结构

while循环

格式:while (循环条件) {循环语句}//条件为1时循环不结束,可以使用break退出循环

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
//打印1-9共10个数字
int main(){
int num1 = 0;
while (num1 < 10)
{
cout << num1 << endl;
num1++;
}
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//猜数字游戏,输入数字,对照1——100随机数,输出偏大或偏小,重新猜测,猜对了输出胜利结束
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
//rand()%100;
//生成0 - 99 的随机数
//会发现每次都是42,可以添加一个随机数的种子,从系统时间获取随机数
srand((unsigned int)time(Null));
//使用时添加<ctime>的头文件
int ran = rand()%100 + 1;
int num = 0;
cout << "请输入数字" << endl;
cin >> num;
while (ran != num)
{
if (num > ran)
{
cout << "偏大" << endl;
cin >> num;
}
if (num < ran)
{
cout << "偏小" << endl;
cin >> num;
}
}
cout << "恭喜你猜对啦!" << endl;

system("pause");
return 0;
}

do-while循环语句

与while的差别是会先执行一次循环内容,再判断循环条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main()
{
//输出0-9
int num = 0;
do
{
cout << num << endl;
num++
}
while (n < 10);
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//水仙花数:三位数,各位数的三次方之和等于它本身,例如1^3+5^3+3^3=153
//找出所有水仙花数
#include<iostream>
using namespace std;
int main()
{
int num = 100;
int num1 = 0;
int num2 = 0;
int num3 = 0;
while (num < 1000)
{
num1 = num % 10;
num2 = num / 10 % 10;
num3 = num / 100;//整除只有整数
if (num == num1 * num1 * num1 + num2 * num2 * num2 + num3 * num3 * num3)
{
cout << num << endl;
}
num++;
}

system("pause");
return 0;
}

for循环

格式:for(起始表达式(创建变量i、赋值等);条件表达式(循环条件);末尾表达式(结束循环后的表达式)){ 循环语句;}

注意表达式之间用;分隔,结构清晰,常用

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int main()
{
//输出0-9
for( int i = 0; i < 10; i++)//起始表达式int i = 0 可以提到前面,条件表达式可以用break放在
{
cout << i << endl;
}
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//敲桌子游戏:从1开始到100,如果数字个位含有7或者十位含有7或者是7的倍数,我们显示“敲桌子”,其他数字直接输出
#include<iostream>
using namespace std;
int main()
{
for( int i = 1; i < 101; i++)
{
if ( i % 7 == 0 || i / 10 == 7 || i % 10 = 7)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}

嵌套循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//打印10*10的*阵
#include<iostream>
using namespace std;
int main()
{
for(int j = 0; j < 10; j++)//外层执行一次,内层执行一周
{
for(int i = 0; i < 10; i++)//如果前后都是用变量i,则后面输出使用到i时就近输出内层的i
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//打印乘法口诀表
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i < 11; i++)
{
for (int j = 1; j <= i ; j++)
{
cout << j << "*" << i << "=" << j * i << "\t";// \t 可以实现对齐
}
cout << endl;
}
system("pause");
return 0;
}

跳转语句

break语句

1、switch条件语句中终止case并跳出switch

2、循环语句中跳出当前循环语句

3、嵌套循环中跳出最近的内层循环语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;
int main()
{
//switch语句
cout << "请选择副本难度前的序号" << endl;
cout << "1、简单" << endl;
cout << "2、普通" << endl;
cout << "3、困难" << endl;
int con = 0;
cin >> con;
switch(con)
{
case 1
cout << "您选择的是简单" << endl;
break;//退出case分支,如果不退出会输出其他结果
case 2
cout << "您选择的是普通" << endl;
break;
case 3
cout << "您选择的是困难" << endl;
break;
default:
break;
}
system("pause");
return 0;
}

continue语句

作用:在循环语句中,跳过本次循环中余下未执行的语句,继续执行下次循环(终止循环进入下次循环)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 100; i++)
{
//奇数输出,偶数不输出
if (i % 2 ==0)
{
continue;
}
cout << i << endl;
}
system("pause");
return 0;
}

goto语句

作用:可以无条件跳转语句,但是影响代码逻辑结构,不方便其他人阅读

语法:goto 标记;标记;

如果标记的名称存在,执行到goto语句时,会跳转到标记的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
int main()
{
int i = 0;
cout << i + 1 << endl;
goto FLAG;
cout << i + 2 << endl;
FLAG;
cout << i + 3 << endl;
system("pause");
return 0;
}

数组

定义方法:

1、数据类型 数组名[数组长度];//数组下标从0开始索引

2、数据类型 数组名[数组长度] = {值1, 值2 ……};//初始数据不完全赋值,会用0来填补空缺

3、数据类型 数组名[ ] = {值1, 值2 ……};//不标注长度,自动识别

特点:

1、放在一块连续的内存空间中

2、数组中每个元素数据类型相同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
int main()
{
int arr1[5];
arr1[0] = 10;
cout << arr1[0] << endl;

int arr2[5] = { 10, 20, 30, 40};
cout << arr2[0] << endl;

for ( int i = 0; i <5; i++)
{
cout << arr2[i] << endl;
}

arr[] = {10, 20 ,30, 40, 50, 60, 70};
system("pause");
return 0;
}

数组名的用途:

1、可以统计整个数组在内存中的长度

2、可以获取数组在内存中的首地址

//数组名是常量,不可以进行赋值操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main()
{
int arr1[5] = {1, 2, 3, 4, 5};
cout << "整个数组占用的内存空间为" << sizeof(arr1) << endl;
cout << "每个元素占用的内存空间为" << sizeof(arr1[0]) << endl;
//每一个元素为int类型,4个字节,一个数组5个元素,20个字节

cout << "数组类型首地址为" << arr1 << endl;//获取数组在内存中的首地址,是一串采用16进制的数字
cout << "数组类型首地址为" << (int)arr1 << endl;//获取数组在内存中的首地址,是一串采用16进制的数字
cout << "数组第一个元素的首地址" << (int)&arr1[0] <<endl;
cout << "数组第二个元素的首地址" << (int)&arr1[1] <<endl;
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
int main()
{
//五只小猪称体重,输出最大的体重
int arr[5] = {300, 350, 400, 200, 250};
int max = 0;
for (int i = 0; i < 5; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
cout << "最大的体重为" <<max << endl;
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//将数组元素逆置
#include<iostream>
using namespace std;
int main()
{
int arr1[5] = {1, 2, 3, 4, 5};
int temp = 0;
int left = 0;//起始元素下标
int right = sizeof(arr1)/sizeof(arr1[0]) - 1;//末尾元素下标
for (; left < right; left++, right--)
{
temp = arr1[left];
arr1[left] = arr1[right];
arr1[right] = temp;
}
for (int i = 0; i < sizeof(arr1)/sizeof(arr1[0]); i++)
{
cout << arr1[i] << endl;
}
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//将数组元素逆置
#include<iostream>
using namespace std;
int main()
{
int arr1[5] = {1, 2, 3, 4, 5};
int num = sizeof(arr1)/sizeof(arr1[0]);
int arr2[num - 1];
for (int i = 0; i < num; i++)
{
arr2[num - 1 - i] = arr1[i];
}

for (int j = 0; j < num; j++)
{
cout << arr2[j] << endl;
}
system("pause");
return 0;
}