1 初识C++
1.1 第一个C++程序
编写程序步骤:
- 创建项目
- 创建文件
- 编写程序
- 运行程序
#include <iostream>
using namespace std;
int main()
{
cout<<"hellloworld"<<endl;//输出函数cout
system("pause");
return 0;
}
1.2 注释
1 单行注释
#include <iostream>
using namespace std;
int main()
{
//多用于一段代码之后
system("pause");
return 0;
}
2 多行注释
/*1111111
11111111*/
编译器在编译代码时,会忽略注释
1.3 变量
变量存在的意义:方便管理内存空间。
变量书写的格式:数据类型 变量名 =变量初始值;
#include <iostream>
using namespace std;
int main()
{
int variable = 0;
cout<<"a="<<a<<endl;
system("pause");
return 0;
}
1.4 常量
常量的作用:用于记录程序中不可改变的数据
C++定义常量的两种方式
1.#define 宏常量 :通常在文件上方定义,表示一个常量
#define 常量名 常量值
(一旦修改就会报错)
2.const 修饰的变量:通常在定义变量定义前加关键字const,修饰该变量为常量,不可修改。
const 数据类型 常量名 = 常量值;
#include <iostream>
using namespace std;
#define Week 7;
int main()
{
const int year = 12;
/*Week = 8;会报错,define定义的宏常量不可修改
year = 13;也会报错,const 修饰的变量称为常量,也不可以修改*/
system("pause");
return 0;
}
1.5 关键字(标识符)

侵删
1.6 标识符命名规则
- 标识符不能是关键字
- 标识符只能由字母,数字,下划线组成
- 标识符的第一位字符只能是字母或下划线
- 标识符中区分大小写
2 数据类型
给变量分配合适的内存空间
2.1 整型
表示的是整数类型的值
| 数据类型 | 占用空间 | 取值范围 |
| short(短整型) |
2字节 | (-2^15~2^15 -1) |
| int(整型) | 4字节 | (-2^31~2^31 -1) |
| long(长整型) | Windows为4字节,Linux中为4字节(32位),8字节(64位) | (-2^31~2^31 -1) |
| longlong(长长整型) | 8字节 | (-2^63~2^63 -1) |
2.2 sizeof关键字
#include <iostream>
using namespace std;
int main()
{
int variable = 1;
cout << "int占用空间为" << sizeof(variable) << endl;
cout << "int占用空间为" << sizeof(int) << endl;
system("pause");
return 0;
}
2.3 实性(浮点型)
| 数据类型 | 占用空间 | 有效数字范围 |
| float(单精度) | 4字节 | 七位有效数字 |
| double(双精度) | 8字节 | 十五~十六位有效数字 |
2.4 字符型
用于显示单个字符
char ch = 'a';
- 显示字符型变量时,用单引号将字符括起来,不要用双引号
- 单引号内只能有一个字符,不能出现字符串
- 字符型仅占用一个字节
- 字符型变量并不是把字符放进内存中存储,而是将对应的ACSII编码放进储存单元
2.5 转义字符

侵删
值得注意的转义字符有 : \n \t \\
#include <iostream>
using namespace std;
int main()
{
//换行符\n
cout << "aaa\n" << endl;
//输出反斜杠\ 需要输出两个\\
cout << "\\" << endl;
//水平制表符\t
cout << "aa\thelloworld" << endl;
cout << "aaaa\thelloworld" << endl;
cout << "aaaaaa\thelloworld" << endl;
system("pause");
return 0;
}

2.6字符串
#include <iostream>
#include <string>//必须包含的一个头文件
using namespace std;
int main()
{
string str = "abcdefg";
system("pause");
return 0;
}
2.7布尔数据类型 bool
布尔数据类型代表真或假的值
bool类型只有两个值:
- ture——真(1)
- false——假(0)
仅占用一个字节
#include <iostream>
using namespace std;
int main()
{
bool flag = ture;
//此时输出flag的值为1
cout << "flag的值\n" << flag << endl;
flag = false;
//此时输出flag的值为0
cout << "flag的值" << flag << endl;
//本质上1代表真的值,0代表假的值
system("pause");
return 0;
}
2.8 数据的输入
用于从键盘输入数据
#include <iostream>
using namespace std;
#include <string>
int main()
{
//整型
cout << "请给整型变量赋值\n" << endl;
int a = 0;
cin >> a;
cout << "a=" << a << endl;
//浮点型
cout << "请给浮点型变量赋值\n" << endl;
float b = 3.14f;
cin >> b;
cout << "b=" << b << endl;
//字符型
cout << "请给字符型变量赋值\n" << endl;
char ch = 'a';
cin >> ch;
cout << "ch=" << ch << endl;
//字符串
cout << "请给字符串型变量赋值\n" << endl;
string d = "asdf";
cin >> d;
cout << "d=" << d << endl;
//布尔型 需注意给布尔型变量赋值时,需要赋值0/1,不能赋值true/false
system("pause");
return 0;
}
3 运算符
用于执行代码的运算
| 运算符类型 | 作用 |
| 算术运算符 | 用于处理加减乘除(四则运算) |
| 赋值运算符 | 将表达式的值赋给变量 |
| 比较运算符 | 用于表达式的比较,返回的值为真值或假值 |
| 逻辑运算符 | 根据表达式的值返回真值还是假值 |
3.1 算术运算符
| 运算符 | 术语 | 示例 | 结果 |
| + | 正号 | a=+3 | 3 |
| - | 负号 | a=-3 | -3 |
| + | 加号 | a=3+3 | 6 |
| - | 减号 | a=3-3 |
0 |
| * | 乘号 | a=3*3 | 9 |
| / | 除号 | a=3/3 | 1 |
| % | 取余 | a=10%3 | 1 |
| ++ | 前置递增 | a=3,b=++a | b=4 |
| ++ | 后置递增 | a=3,b=a++ | b=3 |
| -- | 前置递减 | a=3,b=--a | b=2 |
| -- | 后置递减 | a=3,b=a-- | b=3 |
#include <iostream>
using namespace std;
#include <string>
int main()
{
//1.加减乘除
//1.1加减
int a = 1;
int b = 2;
cout << a + b << endl;
cout << a - b << endl;
//1.2乘除
cout << a * b << endl;
//当a<b时,由于之前定义的是int整型变量,输出的结果也是整形,于是cout输出为0
cout << a / b << endl;
//若在输出时,将输出的数据的类型从整形改为实型,此时便能输出小数
cout << (double)a / b << endl;
//除数不能为零
//2.取模运算
int c = 10;
int d = 20;
cout << c % b << endl;
//取余是取除法中余数,且小数之间不能取余
cout << c % d << endl;
//若被取余数小于取余数,则取余结果为他本身
//能整除的数也为零
//3.递增递减
//3.3前置后置的区别
//前置是先进行加一的操作,然后再执行表达式
//后置是先执行表达式,在执行加一的操作
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;
system("pause");
return 0;
}

3.2 赋值运算符
#include <iostream>
using namespace std;
#include <string>
int main()
{
int a = 10;
cout << "a=" << a << endl;
//赋值运算符
//=
a = 100;
cout << "a=" << a << endl;
//+=
a = 10;
a += 2;//a=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;
}

3.3比较运算符
| 运算符 | 术语 | 示例 | 结果 |
| == | 相等于 | 1==1 | 1 |
| != | 不相等 | 1!=1 | 0 |
| < | 小于 | 1<2 | 1 |
| > | 大于 | 1>2 | 0 |
| >= | 大于等于 | 1>=2 | 0 |
| <= | 小于等于 | 1<=2 | 1 |
#include <iostream>
using namespace std;
#include <string>
int main()
{
int a = 10;
int b = 11;
cout << (a==b) << endl;
//小括号的作用是可以让计算机优先处理括号里的内容
system("pause");
return 0;
}

3.4 逻辑运算符
| 运算符 | 术语 | 示例 | 结果 |
| ! | 非 | !a | 若a为假,则非a为真;若a为真,则非a为假 |
| && | 与 | a&&b | 若a与b都为真,则结果为真,其余为假 |
| || | 或 | a||b | a与b至少有一个为真,其余为假 |
4 程序流程结构
C++中最基本的三种结构:顺序结构,选择结构,循环结构
- 顺序结构:程序按顺序执行,不执行跳转
- 选择结构:根据条件是否满足,有选择的执行相应功能
- 循环结构:根据条件是否满足,循环多次执行某段代码
4.1 选择结构
根据条件是否满足,有选择的执行相应功能
4.1.1 if语句
执行满足条件的语句
if语句的三种形式:
- 单行格式if语句
- 多行格式if语句
- 多条件的if语句
4.1.1.1 单行格式if语句

单行if语句的流程图
#include <iostream>
using namespace std;
int main()
{
//让学生输入自己的期末分数,若大于60分,即显示及格,若小于60分,显示不及格
cout << "请输入你的期末分数" << endl;
int scores = 0;
cin >> scores;
cout << "您的考试分数为" << scores << endl;
if(scores>60)
{
cout << "恭喜你通过了考试,成绩及格" << endl;
}
else
{
cout << "未通过考试,考试成绩不及格" << endl;
}
//上一处使用的是多行if语句
/*if(scores>60)
{
cout << "合格" << endl;
}
if(scores<60)
{
cout << "不合格" << endl;
}
此处使用的是单行if语句*/
system("pause");
return 0;
}

4.1.1.2 多行if语句

多行if语句流程图
4.1.1.3 多条件if语句

多条件if语句流程图
#include <iostream>
using namespace std;
int main()
{
//判断用户输入的分数为什么分段
int score = 0;
cout << " Please input your text scores." << endl;
cin >> score;
if (score >= 90)
{
cout << "You passed the exam,your score level is A";
}
else if (score >= 80)
{
cout << "You passed the exam,your score level is B";
}
else if (score >= 70)
{
cout << "You passed the exam,your score level is C";
}
else if (score >= 60)
{
cout << "You passed the exam,your score level is D";
}
else if (score < 60)
{
cout << "You did not pass the exam,your score level is E";
}
system("pause");
return 0;
}

4.1.1.3嵌套if语句
#include <iostream>
using namespace std;
int main()
{
//给出一批百分制成绩,输出对应的成绩等级A、B、C、D、E。
// 90分以上为A,80-89分为B,70-79分为C,60-69分为D,60分以下为E。
//1.定义成绩等级
int score = 0;//用户初始成绩
int A = 89;//大于89为a
int B = 79;//大于79为b
int C = 69;//大于69为c
int D = 59;//大于59为d
//其余为e
//2.输入成绩
cout << "请输入您的成绩" << endl;
cin >> score;
cout << "您的成绩为" << score << endl;
//3.进行成绩分类
if (score > A)
{
cout << "您的成绩为A等级" << endl;
}
else if (score < A)
{
if (score > B)
{
cout << "您的成绩为B等级" << endl;
}
else if (score < B)
{
if (score > C)
{
cout << "您的成绩为C等级" << endl;
}
else if (score < C)
{
if (score > D)
{
cout << "您的成绩为D等级" << endl;
}
else
{
cout << "您的成绩不合格,为E等级" << endl;
}
}
}
}
system("pause");
return 0;
}

用嵌套if语句改写的成绩判断程序
案例:三只小猪称体重
有三只小猪,分别输入他们的重量,并判断谁最重
#include <iostream>
using namespace std;
int main()
{
//三只小猪称体重,分别为A,B,C
float A = 0;
float B = 0;
float C = 0;
//1.分别输入三只小猪的体重,并告诉用户他们的体重
cout << "请输入第一只小猪的体重 " << endl;
cin >> A;
cout << "小猪A的体重是" << A << endl;
cout << "请输入第二只小猪的体重" << endl;
cin >> B;
cout << "小猪B的体重为" << B << endl;
cout << "请输入第三只小猪的体重" << endl;
cin >> C;
cout << "小猪C的体重为" << C << endl;
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;
}
4.1.2 三目运算符
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
int c = 0;
a > b ? c = a : c = b;
system("pause");
return 0;
}
(if)?(执行if后的语句):(else)
4.1.3 switch语句
#include <iostream>
using namespace std;
int main()
{
/*
给电影打分
若评分为十到九,则评价为经典
若评分为八到七,则评价为好片
若评分为六到五,则评价为一般
若评分为五以下,则评价为烂片
*/
//让用户输入分数
int score = 0;
cout << "请你输入对该影片的评分" << endl;
cin >> score;
switch (score)
{
case 10 :
cout << "You think this movie is classical"; break;
case 9:
cout << "You think this movie is classical"; break;
case 8:
cout << "You think this movie is good"; break;
case 7:
cout << "You think this movie is good"; break;
case 6:
cout << "You think this movie is nomal"; break;
case 5:
cout << "You think this movie is nomal"; break;
default :
cout << "You think this movie is shit"; break;
}
system("pause");
return 0;
}
switch语句相较于if语句的优缺点:
- 缺点:只能判断整型或字符型,不能判断一个区间
- 优点:结构清晰,运行效率高
- PS:case语句后面必须加上break关键字(退出当前分支),否则会一直执行下去
案例:根据输入的星期几来输出英文
#include <iostream>
using namespace std;
int main()
{
int day = 0;
//1.告诉用户输入星期几
cout << "请输入星期几" << endl;
cin >> day;
switch (day)
{
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuseday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
}
system("pause");
return 0;
}
4.1.3 选择结构例题
案例1:奇偶判断
#include <iostream>
using namespace std;
int main()
{
//1.让用户输入一个数
int variable = 0;
cout << "请输入一个数字" << endl;
cin >> variable;
//2.判断是奇数还是偶数,奇数不能被2整除,偶数可以被2整除
if (variable % 2 == 0)
{
cout << "您输入的数字为偶数" << endl;
}
else
{
cout << "您输入的数字为奇数" << endl;
}
system("pause");
return 0;
}
案例2:闰年判断
#include <iostream>
using namespace std;
int main()
{
//① 能被 4 整除但不能被 100 整除;② 能被 400 整除。满足任一条件即为闰年。
//1.让用户输入年份
int year = 0;
cout << "请输入年份" << endl;
cin >> year;
cout << "您输入的年份为" << year << endl;
//2.判断是否为闰年
if (year % 4 == 0 && year % 100 != 0 )
{
cout << "您输入的年份为闰年" << endl;
}
else if(year%400==0)
{
cout << "您输入的年份为闰年" << endl;
}
else
{
cout << "您输入的年份不为闰年" << endl;
}
system("pause");
return 0;
}
案例3:简易计算器
#include <iostream>
using namespace std;
int main()
{
//题目:用户输入两个数字和一个运算符(+、-、*、/),
//程序根据运算符计算并输出结果。
//1.让用户分别输入两个数字,以及一个运算符
int num1 = 0;
int num2 = 0;
char ope = 'a';
cout << "请你输入第一个数字" << endl;
cin >> num1;
cout << "请你输入第二个数字" << endl;
cin >> num2;
cout << "请你输入运算符" << endl;
cin >> ope;
//2.判断运算符类型
switch (ope)
{
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
break;
case '/':
if (num2 == 0)
{
cout << "除数不能为0" << endl;
}
else
{
cout << num1 << " / " << num2 << " = " << (double) num1 / num2 << endl;
}
break;
case '%':
cout << num1 << " % " << num2 << " = " << num1 % num2 << endl;
break;
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;;
break;
default :
cout << "按正确的格式输入" << endl;
}
system("pause");
return 0;
}

cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
案例:输入三条边长,判断是否能构成三角形
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//三角形的两边之差小于第三边,两边之和大于第三边
//1.让用户分别输入三条边a,b,c
float a = 0;
float b = 0;
float c = 0;
cout << "请你输入第一条边的数值" << endl;
cin >> a;
cout << "a=" << a << endl;
cout << "请你输入第二条边的数值" << endl;
cin >> b;
cout << "b=" << b << endl;
cout << "请你输入第三条边的数值" << endl;
cin >> c;
cout << "c=" << c << endl;
//2.定义一个布尔型变量,若为三角形则为真
bool tri = (a+b>c)&&(a+c>b)&&(b+c>a);
if (tri = 1)
{
if (a == b == c)
{
cout << "这是一个等边三角形" << endl;
}
else if ((a == b) || (a == c) || (b == c))
{
cout << "这是一个等腰三角形" << endl;
}
else
{
cout << "这是一个三角形" << endl;
}
}
else
{
cout << "无法构成三角形" << endl;
}
system("pause");
return 0;
}
运用abs函数时应加上#include <cmath>头文件
4.2 循环语句
4.2.1 while
#include <iostream>
using namespace std;
int main()
{
int a = 0;
while (a <= 9)
{
cout << a++ << endl;
}
system("pause");
return 0;
}
满足循环条件便执行语句
while(1)————一直循环
PS:避免死循环
案例:猜随机数(rand函数)
#include <iostream>
#include <ctime>//time所需头文件
using namespace std;
//猜数字
int main()
{
//添加随机数种子,让系统根据时间生成随机数,防止随机数每次都一样
srand((unsigned int)time(NULL));
//1.系统1生成一个一到一百的随机数
int num = rand() % 100 + 1;
//2.用户猜测
int gue = 0;
cout << "请输入1~100任意随机数" << endl;
while (gue != num)
{
cin >> gue;
if (gue > num)
{
cout << "您输入的数字大于系统生成的随机数" << endl;
}
else if(gue < num)
{
cout << "您输入的数字小于系统生成的随机数" << endl;
}
}
cout << "恭喜您猜对的系统生成的随机数" << endl;
system("pause");
return 0;
}
rand()%100————指生成0~99的随机数
srand((unsighed int) time (NULL))随机数种子
4.2.2 do while
do while 与 while 的区别是:
do while 会先执行一次代码再判断循环条件
案例:水仙花数
#include <iostream>
using namespace std;
//水仙花数:三位数的每位三次方相加等于它本身
int main()
{
int num = 100;
do
{
int hun = num / 100;
int ten = num%100/10;
int one = num % 10;
int sum = hun * hun * hun + ten * ten * ten + one * one * one;
if (sum == num)
{
cout << "这是一个水仙花数:" << num << endl;
}
num++;
} while (num<1000);
system("pause");
return 0;
}
4.2.3 for 循环
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 9; i++)
//(起始表达式;条件表达式;末尾循环体)
{
cout << i << endl;
}
system("pause");
return 0;
}
案例:敲桌子
#include <iostream>
using namespace std;
//敲桌子,从1~100,遇到含有七,七的倍数,输出
int main()
{
for (int i = 1; i < 101; i++)
{
if(i%7==0)
{
cout << i << " ";
continue;
}
if (i % 10 == 7 || i / 10 == 7)
{
cout << i << " ";
}
}
cout << endl;
system("pause");
return 0;
}
4.2.4 嵌套循环
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
for (int n = 0; n < 10; n++)
{
cout << "*" << " ";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
案例:乘法口诀表
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i < 10; i++)
{
for (int n = 1; n < 10; n++)
{
if (( i == 3 && n == 3) || (i == 4 && n == 3))
{
cout << " ";
}
if (n > i)
{
continue;
}
else
{
cout << n << "*" << i << "=" << i * n << " ";
}
}
cout << endl;
}
system("pause");
return 0;
}
4.2.5 循环结构例题
案例:统计 1 到 100 中偶数的个数
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for (int i = 1; i < 101; i++)
{
if (i % 2 == 0)
{
cout << i << " ";
sum++;
}
}
cout << endl << "总共出现了" << sum << "个偶数" << endl;
system("pause");
return 0;
}
案例:计算n的阶乘
#include <iostream>
using namespace std;
int main()
{
//1.让用户输入一个正整数n
cout << "请输入一个正整数" << endl;
int n = 0;
cin >> n;
int fac = 1;
for (; n > 0; n--)
{
fac = fac * n;
}
cout << "这是你输入的正整数的阶乘:" << fac << endl;
system("pause");
return 0;
}
案例:打印直角三角形、等腰三角形(* 组成)
#include <iostream>
using namespace std;
int main()
{
//1.让用户输入三角形的行数
int row = 0;
cout << "请输入三角形的行数(正整数)" << endl;
cin >> row;
cout << "这是直角三角形的图形" << endl;
//打印的行数
for (int i = 0; i < row; i++)
{
//打印的个数
for (int n = 0; n <= i; n++)
{
cout << "*";
}
cout << endl;
}
cout << "这是等腰三角形" << endl;
//打印的行数
for (int x = 0; x < row; x++)
{
//打印的空格
for (int y = 0; y < row - x - 1; y++)
{
cout << " ";
}
//打印的*号
for (int z = 0; z <= 2 * x; z++)
{
cout << "*" ;
}
cout << endl;
}
system("pause");
return 0;
}
4.3 跳转语句
4.3.1 break语句
使用时机:
- 在switch选择结构中,作用于终止case,并跳出switch语句
- 跳出循环语句
- 在嵌套循环结构中,用于跳出最近一个循环语句
案例:输入正确的密码(嵌套循环中的运用)
#include <iostream>
using namespace std;
int main()
{
int core = 123456;
int input = 0;
int i = 0;
while (input != core)
{
cout << "请输入密码:";
cin >> input;
if (input == core)
{
cout << "密码正确" << endl;
break;
}
cout << endl << "密码错误,还有" << 2 - i << "次机会" << endl;
i++;
if (i > 2)
{
cout << "输入多次错误密码,验证失败" << endl;
break;
}
}
system("pause");
return 0;
}
4.3.2 continue语句
跳过本次循环中未执行的语句,执行下一循环
案例:计算 1-100 中所有奇数的和
#include <iostream>
using namespace std;
int main()
{
//计算 1-100 中所有奇数的和
int sum = 0;
for (int i = 1; i < 101; i++)
{
if (i % 2 == 0)
{
continue;
}
else
{
sum = sum + i;
}
}
cout << "1-100 中所有奇数的和为:"<< sum << endl;
system("pause");
return 0;
}
4.3.3 goto语句
无条件的跳转代码
案例:处理错误输入
#include <iostream>
using namespace std;
int main()
{
//让用户输入1~10的数
int num = 0;
FLAG:
cout << "请输入一个在1~10之间的数" << endl;
cin >> num;
if (num > 10 || num < 1)
{
cout << "错误输入" << endl;
goto FLAG;
}
cout << "您输入的数字为" << num << endl;
system("pause");
return 0;
}
1万+

被折叠的 条评论
为什么被折叠?



