C++小白入门
迪丽瓦拉
2024-05-30 07:22:43
0

1.1编写程序四步走:

  • 创建项目

  • 创建文件

  • 编写代码

  • 运行程序

解决方案资源管理器:在新创建的项目下右键“源文件”-添加-“新建项”-“C++文件(.cpp)”,给文件取名

#include 
using namespace std;int main()
{cout << "hello world" << endl;system("pause");return 0;
}

只有第6行是输出,其他是框架。

1.2注释

单行注释:// 描述信息,放在代码上方或语句末尾,对该行代码说明

多行注释:/*描述信息*/,放在代码上方,对该段代码做整体说明

#include 
using namespace std;/*main()函数是一个函数的入口每个程序都必须有一个且有且仅有一个
*/int main()
{//代码的含义就是输出hello worldcout << "hello world" << endl;system("pause");return 0;
}

1.3变量

给一段指定的内存空间起名,方便操作这段内存。数据类型 变量名 = 变量的初始值;

标识符不能是关键词;只能由字母,数字,下划线组成;第一个字符不能是数字;字母区分大小写。

#include 
using namespace std;int main()
{//变量创建的语法:数据类型 变量名 = 变量初始值int a = 10;cout << "a=" << a << endl;system("pause");return 0;
}

1.4常量

用于记录程序中不可更改的数据

两种方式:1.#define 常量名 常量值 2.const 数据类型 常量名 =常量值

#include 
using namespace std;//常量的定义方式:1.#define宏常量 2.const修饰的变量
//1.#define宏常量
#define Day 7int main()
{cout << "一周总共有:" << Day << "天" << endl;//2.const 修饰的变量,也成为常量const int month = 12;cout << "一年总共有:" << month << "个月份" << endl;system("pause");return 0;
}

2.数据类型

意义:给变量分配合适的内存空间

2.1整型

  • short(短整型) 占用2字节 -2^15~2^15-1

  • int(整型) 占用4字节 -2^31~2^31-1

  • long(长整型) -2^31~2^31-1

  • long long (长长整型) 8 字节 -2^61~2^61-1

区别在于所占空间内存不同

2.2sizeof关键字

作用:统计数据类型所占内存的大小

语法:sizeof(数据类型 / 变量)

#include 
using namespace std;int main()
{short num1 = 10;cout << "short所占的内存空间是:" <

2.3浮点型—表示小数

  • 单精度 float 4字节 7位有效数字

  • 双精度 double 8字节 15-16位有效数字(小数点前面的数字也算有效数字)

区别在于表示的有效数字不同

#include 
using namespace std;int main()
{//1.单精度,小数系统会默认是双精度,需在小数后边加f。float f1 = 3.1415926f;cout << "f1=" << f1 << endl;//2.双精度,默认情况下输出一个小数,会显示6位有效数字double d1 = 3.1415926;cout << "d1=" << d1 << endl;//统计float和double占用内存空间cout << "float占用的内存空间是:" << sizeof(float) << endl;cout << "double占用的内存空间是:" << sizeof(double) << endl;//科学计数法,3*10^2,3*0.1^2float f2 = 3e2;cout << "f2=" << f2 << endl;float f3 = 3e-2;cout << "f3=" << f3 << endl;system("pause");return 0;
}

2.4字符型—单个字符

语法:char ch = 'a';只占用1个字节,字符型变量不是把字符本身放到内存中,而是对应的ASCII编码放入存储单元。

#include 
using namespace std;int main()
{//1.字符型创建方式char ch = 'a';cout << ch << endl;//2.字符型变量所占的内存cout << "char字符型所占内存:" << sizeof(char) << endl;//3.字符型变量常见错误//char ch2 = "";//要用单引号//char ch2 = 'abcd';//单引号内只有一个字符//4.字符型变量对应的ASCII编码//a - 97,A-65,cout << (int)ch << endl;system("pause");return 0;
}

2.5转义字符

作用:表示一些不能显示出来的ASCII字符

\n:换行;\t:水平制表,跳到下一个Tab位置;\\:反斜字符\;

#include 
using namespace std;int main()
{//换行符cout << "helloworld\n";//输出反斜杠cout << "\\" << endl;//水平制表符,\t占了8个空间,3个a后面会有5个空格,4个a后面会有4个空格//作用:整齐的输出数据cout << "aa\thelloworld" << endl;cout << "aaa\thelloworld" << endl; system("pause");return 0;
}

2.6字符串类型

两种风格:

  • C风格字符串:char 变量名[ ] = "字符串值"

  • C++风格字符串:string 变量名 = “字符串值”

#include 
using namespace std;
#include  //用C++风格的字符串时,要包含这个头文件int main()
{//1.C风格字符串,注意与字符型区别开char str[] = "helloworld";cout << str << endl;//2.C++风格字符串,包含一个头文件 #includestring str2 = "helloworld";cout << str2 << endl;system("pause");return 0;
}

2.7布尔类型

作用:代表真或假的值。占一个字节大小。

  • true:真(本质是1),只要是非0值都代表真。

  • false:假(本质是0)

#include 
using namespace std;
#include  //用C++风格的字符串时,要包含这个头文件int main()
{//创建布尔数据类型,本质上1代表真,0代表假bool flag = true;//true代表真cout << flag << endl;flag = false;//flase代表假cout << flag << endl;//布尔类型所占内存空间cout << "bool类型所占的内存空间是:" << sizeof(bool) << endl;system("pause");return 0;
}

2.8数据的输入

作用:从键盘上获取数据。

关键字:cin。语法:cin>>变量

#include 
using namespace std;
#include  //用C++风格的字符串时,要包含这个头文件int main()
{//1.整型,ctrl+k+c多行注释//int a = 0;//cout << "请给整型变量a赋值:" << endl;//cin >> a;//cout << "整型变量a=" <<  a << endl;//2.浮点型//float f = 3.14f;//cout << "请给浮点型变量f赋值:" << endl;//cin >> f;//cout << "浮点型变量f= " << f << endl;//3.字符型char ch = 'a';cout << "请给字符型变量ch赋值:" << endl;cin >> ch;cout << "字符型变量ch=" << ch << endl;system("pause");return 0;
}

3.运算符

3.1算术运算符

%——取余,++——递增,--——递减

两个整数相除,结果依然是整数,会将小数部分去除;两个小数不可以做取模运算

3.2赋值运算符

3.3比较运算符

==;!=; <; >; >=; <=;

3.4逻辑运算符

  • !非;a为假,!a为真

  • &&与;都为真,则结果为真

  • ||或;一个为真,结果为真

4.程序流程结构

  • 顺序结构:按顺序执行,不发生跳转

  • 选择结构:依据条件是否满足,有选择的执行相应功能

  • 循环结构:依据条件是否满足,循环多次执行某段代码

4.1选择结构

4.1.1单行格式的if格式

if (条件) {条件满足执行的语句}

#include 
using namespace std;
#include  
int main()
{//选择结构 单行if语句//用户输入分数,若分数>600,视为考上一本大学,输出int score = 0;cout << "请输入一个分数:" << endl;cin >> score;cout << "您输入的分数是:" << score << endl;if (score > 600){cout << "恭喜您考上一本大学" << endl;}//注意:if语句后面不要加分号,否则会和后面的代码分离,{}中的代码都会执行system("pause");return 0;
}

4.1.2多行格式if语句

if (条件) {条件满足执行的语句} else {条件不满足执行的语句}

4.1.3多条件if语句

if(条件1){条件1满足执行的语句} else if (条件2) {条件2满足执行的语句}…else {都不满足执行的语句}

4.1.4嵌套if语句

需求:还需要在一本分数中,>700分考上北大,>600考上人大。

案例:三只小猪比体重

4.1.5三目运算符

语法:表达式1 ?表达式2 :表达式3(若表达式1为真,则执行表达式2的语句,否则执行表达式3)

4.1.6switch语句

switch(表达式)
{case 结果1:执行语句;break;case 结果2:执行语句;break;……default:执行语句;break;
}

if 和switch的区别:switch判断时只能是整型或字符型,不可以是一个区间;结构清晰,执行效率高

4.2循环结构

4.2.1while循环语句

while (循环条件) {循环语句} 只要循环条件结果为真,就执行循环语句。

案例:猜数字

4.2.2 do…while循环语句

do {循环语句} while {循环条件}

与while的区别在于:do…while会先执行一次循环语句,再判断循环条件。

案例:水仙花数

4.2.3for循环

满足循环条件,执行循环语句

for (起始表达式;条件表达式;末尾循环体) {循环语句;}

#include 
using namespace std;int main()
{//for循环,打印数字0-9for(int i = 0; i<10; i++)  //()里囊括了所有{cout << i << endl;}system("pause");return 0;
}

案例:敲桌子

4.2.4嵌套循环

打印10*10的星图

#include 
using namespace std;int main()
{//利用嵌套实现星图//外层执行一次,内层执行一周for (int i = 0; i < 10; i++)   //外层循环{//打印一行星图for (int j = 0; j < 10; j++)   //内层循环{cout << "* ";  //endl的作用是换行}cout << endl;}system("pause");return 0;
}

案例:乘法口诀表

4.3跳转语句

4.3.1break语句-跳出选择结构/循环结构

出现时机:

  • switch语句中,作用是终止case并跳出switch

  • 循环语句中,作用是跳出当前的循环语句

  • 嵌套循环语句中,跳出最近的内层循环语句

4.3.2continue语句

作用:跳出本次循环中余下尚未执行的语句,继续执行下一次循环;break是直接跳出。

#include 
using namespace std;int main()
{//输出0-100的奇数for (int i = 0; i <= 100; i++){if (i % 2 == 0){continue;   //偶数,continue,不进行后面的循环,执行下次循环}cout << i << endl;}system("pause");return 0;
}

4.3.3 goto语句

作用:无条件跳转语句。

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

不推荐使用,易使结构混乱

5.数组

5.1概述

数组,就是一个集合,里面存放了相同类型的数据元素。有两个特点:

  • 数组中每个数据元素都是相同的数据类型

  • 数组是由连续的内存位置组成的

5.2一维数组

三种定义方式:

  • 数据类型 数组名 [数组长度];

  • 数据类型 数组名 [数组长度] = {值1,值2…};

  • 数据类型 数组名 [ ] = {值1,值2…};

#include 
using namespace std;int main()
{//1.int arr[5];arr[0] = 10;arr[1] = 20;arr[2] = 30;arr[3] = 40;arr[4] = 50;cout << arr[0] << endl;  //数组访问小标是从0开始的//2.int arr2[5] = { 10,20,30,40,50 };  //若初始化数据时,没全部填写完,会用0来填补cout << arr2[3] << endl;for (int i = 0; i < 5; i++){cout << arr2[i] << endl;}//3.int arr3[] = { 90,80,70,60,50,40,30,20,10 }; //定义数组时,必须有初始长度for (int i = 0; i < 9; i++){cout << arr3[i] << endl;}system("pause");return 0;
}

5.2.2一维数组数组名

作用:可以统计整个数组在内存中的长度;获取数组在内存中的首地址。

案例:五只小猪称体重

案例:数组元素逆置

将1,3,2,5,4逆置为4,5,2,3,1.(首尾互换)

创建一个临时内存,再首尾互换。起始下标++,末尾下标--,循环

#include 
using namespace std;int main()
{//数组元素逆置int arr[5] = { 1,3,2,5,4 };cout << "元素逆置前:" << endl;for (int i = 0; i < 5; i++){cout << arr[i] << endl;}int start = 0;int end = sizeof(arr) / sizeof(arr[0]) - 1; // 记录起始和结束下标的位置while (start < end){int temp = arr[start];arr[start] = arr[end];arr[end] = arr[temp];  // 起始下标和结束下标的元素互换start++;end--;}cout << "元素逆置后:" << endl;for (int i = 0; i < 5; i++){cout << arr[i] << endl;}system("pause");return 0;
}

5.2.3元素冒泡排序

作用:最常用的排序算法,如淘宝价格排序

  1. 比较相邻元素,若第一个比第二个大,交换它们

  1. 对每一对相邻元素做同样工作,执行完毕后,找到第一个最大值

  1. 重复以上步骤,每次比较次数-1,直到不需要比较。

将数组{4,2,8,0,5,7,1,3,9}进行升序

#include 
using namespace std;int main()
{int arr[9] = { 4,2,8,0,5,7,1,3,9 };cout << "排序前:" << endl;for (int i = 0; i < 9; i++){cout << arr[i] << endl;}cout << endl;for (int i = 0; i < 9 - 1; i++)//排序总轮数 = 元素个数 - 1{for (int j = 0; j < 9 - i - 1; j++) //每轮对比次数 = 元素个数 - 排序轮数 - 1{if (arr[j] > arr[j + 1]){int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}cout << "排序后:" << endl;for (int i = 0; i < 9; i++){cout << arr[i] << endl;}cout << endl;system("pause");return 0;
}

5.3二维数组

5.3.1二维数组数组名

有行有列,通常呈现方式是矩阵,四种定义方式:

  • 数据类型 数组名 [行数] [列数] ;

  • 数据类型 数组名 [行数] [列数] = {{数据1,数据2},{数据3,数据4}};

  • 数据类型 数组名 [行数] [列数] = {数据1,数据2,数据3,数据4};

  • 数据类型 数组名 [ ] [列数] = {数据1,数据2,数据3,数据4};

#include 
using namespace std;int main()
{//1.int arr[2][3];arr[0][0] = 1;arr[0][1] = 2;arr[0][2] = 3;arr[1][0] = 4;arr[1][1] = 5;arr[1][2] = 6;for (int i = 0; i < 2; i++) //外层循环打印行数,内层循环打印列数{for (int j = 0; j < 3; j++){cout << arr[i][j] << endl;}}//2.int arr2[2][3] ={{1,2,3},{4,5,6}};for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++){cout << arr2[i][j] << " ";}cout << endl;}//3.int arr3[2][3] = { 1,2,3,4,5,6 };//4.int arr4[][3] = { 1,2,3,4,5,6 };system("pause");return 0;
}

5.3.2二维数组组名

查看二维数组所占内存空间;获取二维数组首地址。

案例:考试成绩统计

语文

数学

英语

张三

100

100

100

李四

90

50

100

王五

60

70

80

#include 
using namespace std;
#include int main()
{int scores[3][3] ={{100,100,100},{90,50,100},{60,70,80}};string names[3] = { "张三","李四","王五" }; //用string,要用头文件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;
}

6.函数

6.1概述

将一段经常使用的代码封装起来,减少重复代码;一个较大的程序,一般分为若干个程序块,每个模块实现特定功能。

6.2函数的定义

五步骤:

  • 返回值类型

  • 函数名

  • 参数表列

  • 函数体语句

  • return表达式

实现一个加法函数:传入两个整型数据,计算数据相加的结果,并且返回

#include 
using namespace std;
#include int main()
{// 返回值名称 函数名 (参数列表) {函数体语句 return表达式}int add(int num1, int num2){int sum = num1 + num 2;return sum;}system("pause");return 0;
}

6.3函数的调用

语法: 函数名(参数)

#include 
using namespace std;
#include int add(int num1, int num2)  //函数定义时,num1,num2并没有真实数据,只是形式上的参数,简称形参
{int sum = num1 + num2;return sum;
}int main()
{//main()函数中调用add函数int a = 10;int b = 20;int c = add(a, b);  //a和b称为 实际参数,简称实参,当调用函数时,实参的值会传递给形参cout << "c=" << c << endl;a = 100;b = 500;c = add(a, b);cout << "c=" << c << endl;system("pause");return 0;
}

6.4值传递

函数调用时,实参将数值传入给形参。

6.5函数的常见样式

  • 无参无返

  • 有参无返

  • 无参有返

  • 有参有返

#include 
using namespace std;//1.无参无返
void test01()
{cout << "this is test01" << endl;
}//2.有参无返
void test02(int a)
{cout << "this is test02 a =" << a << endl;
}//3.无参有返
int test03()
{cout << "this is test03" << endl;return 1000;
}//4.有参有返
int test04(int a)
{cout << "this is test04 a =" << a << endl;return a;
}int main()
{test01();test02(100);int num1 = test03();cout << "num1= " << num1 << endl;int num2 = test04(10000);cout << "num2=" << num2 << endl;system("pause");return 0;}

6.6函数的声明

作用:前告诉编译器函数存在,main函数在前,定义的函数在后.如果定义的函数在前,main在后,可以不写声明。 函数的声明可以多次,但是函数的定义只能有一次。

#include 
using namespace std;//函数的声明——提前告诉编译器函数存在,main函数在前,定义的函数在后
//声明可以写多次,但定义int max只能出现一次int max(int a, int b);int main()
{int a = 10;int b = 20;cout << max(a, b) << endl;system("pause");return 0;    
}int max(int a, int b)
{return a > b ? a : b;  //两个整型数字比较,返回较大的值
}                                                                   

6.7函数的分文件编写

作用:让代码结构更清晰

4个步骤:

  1. 创建后缀名为.h的头文件:头文件 — 添加 — 新建项

  1. 创建后缀名为.cpp的源文件:源文件 — 添加 — 新建项

  1. 在头文件中写函数的声明

  1. 在源文件中写函数的定义

7.指针

7.1指针变量的定义和使用

作用:通过指针间接访问内存 指针就是一个地址。

7.2指针所占内存空间

指针也是种数据类型,在32位操作系统中,占4个字节。64位下占8个字节。

7.3空指针和野指针

空指针:指针变量指向内存中编号为0的空间;用途:初始化指针变量。空指针指向的内存不可以访问。

相关内容