C++教程(2023-1-30)

第一章:C++介绍

1.第一个c++程序

c++程序:
编写c++程序的步骤: 创建项目  创建文件 编写代码 运行程序
//
// Created by ~风轻云淡~ on 2022/12/19.
//
#include "iostream"
using namespace std;
int main(){
    
    cout<<"helloWorld"<<std::endl;
    
    system("pause");

    
    return 0;

}

2.注释:在代码中加一些说明和解释 方便自己和其他程序员阅读代码
	两种格式:单行注释 //  通常放在一行代码的上方 对该代码说明
	
			多行注释
				/**/ t通常放在一段代码的上方
				
				
	//
// Created by ~风轻云淡~ on 2022/12/19.
//
#include "iostream"
using namespace std;
int main(){

    cout<<"helloWorld"<<std::endl;

    system("pause");
//单行注释

/*
 * 多行注射
 * */
    return 0;

}



//
// Created by ~风轻云淡~ on 2022/12/19.
//
#include "iostream"
using namespace std;
int main(){

    cout<<"helloWorld"<<std::endl;

    system("pause");
//单行注释

/*
 * 多行注射
 * */
    return 0;

    /*
     * main是一个程序的入口 每个程序都必须有这么一个函数 有且仅有一个
     * 
     * */
    
}


	

2.变量

变量存在的作用:方便我们管理内存空间

变量创建的语法:数据类型 变量名=变量的初始值;
				int   a	 =100;		


//
// Created by ~风轻云淡~ on 2022/12/19.
//

#include "iostream"
using namespace std;
int main(){
//    变量创建的语法 :


//变量创建的语法:
int  a=10;
cout <<"a="<<a<<endl;


    system("pause");
    return 0;

}




3.常量

作用:用于记录程序中不可更改的数据
c++定义常量的两种方式
1.#define 宏常量 :#define 常量名 常量值
		通常在文件的上方定义 表示一个常量
2.const 修饰的变量 const 数据类型 常量名=常量值
 通常在变量定义前加关键字const 修饰变量为常量 不可修改
 
 
 #include <iostream>

/*常量的定义方式:
 *      #define 宏常量
 *          2.const修饰的变量
 * */

//1.#define  N
#define Day 7
using namespace std;


int main() {

    cout<<"一周有:"<<Day<<"天"<<endl;
    
///2.const修饰的变量  也是常量
const int m=12;

cout<<"m的值为:"<<m<<endl;
    return 0;
}

 

4.编程常用的关键字

与java  python 中的一样:
class  return  int  float double 

因此不能使用关键字给变量或常量起名字
    
    
    

5.标识符命名规则

标识符:1.标识符不能是关键字
		int int=10  这样子不允许
	2.标识符只能由字母 下划线 数字组成
		int abc=10  int  _abc=20
	3.标识符中字母区分大小写
    		
	4.标识符是区分大小写的

第二章:数据类型

1.常见数据类型


#include <iostream>

#define Day 7
using namespace std;


int main() {

// 数据类型存在的意义:给变量分配合适的内存空间  区别就是占用的内存空间不同

/*
 * short  短整型
 *int 整型 
 * long 长整型
 * 
 * */

//1.短整型:short
short num1=10;

//2.整型 int
int num2=10;

//3.长整型long
long num3=100;

//4.双精度
double num4=900;

//5.浮点型
float num5=90000;

    return 0;
}



2.sizeof关键字

#include <iostream>

#define Day 7
using namespace std;


int main() {

// 数据类型存在的意义:给变量分配合适的内存空间  区别就是占用的内存空间不同

/*
 * short  短整型
 *int 整型
 * long 长整型
 *
 * */

//1.短整型:short
short num1=10;

//2.整型 int
int num2=10;

//3.长整型long
long num3=100;

//4.双精度
double num4=900;

//5.浮点型
float num5=90000;



//利用sizeof求出数据类型的内存占用大小
//语法:sizeof(数据类型/变量)
cout<<"num1数据类型是" <<sizeof(num1) <<endl;

cout<< "num2的数据类型是:"<<sizeof (num2)<< endl;



    return 0;
}

3.浮点型

float  浮点型
double  双精度
#include <iostream>

#define Day 7
using namespace std;


int main() {



//1.短整型:short
short num1=10;

//2.整型 int
int num2=10;

//3.长整型long
long num3=100;

//4.双精度
double num4=900;

//5.浮点型  默认情况下输出一个小数  会显示出6位有效数字
float num5=90.56f;

cout<< "num5="<< num5 <<endl;


//科学技术发:
float x=3e2;//3*10^2
cout<< "x的值为!"<< x<< endl;







    return 0;
}

4.字符型

char ch='a'  作用:用于显示单个字符
#include <iostream>

#define Day 7
using namespace std;


int main() {


//1.创建字符型变量
char ch='a';
cout<<"转换一下" << ch+92<<endl;

//注意:不能使用双引号创建
//创建的时候单引号内只能有一个字符
//char j='dhdh';   这个就是错误的

//字符对应的ASCII码:a 97  A-65


    return 0;
}

5.转义字符

转义字符

		\a
		\b
		\f
		\n
		\t
		#include <iostream>

#define Day 7
using namespace std;


int main() {


//1.换行符  \n
cout<< "你好\n  你好\n" <<endl;
//    2.水平制表符 \t  整齐输出数据
cout<< "asfdasf\thjhjgjgh\t"<<endl;
    return 0;
}

		

6.字符串类型

#include <iostream>

#define Day 7
using namespace std;

//使用c++风格的字符串要包含的头文件
#include "string"
int main() {

//    1.C语言字符串  cha   注意;char 字符串名[]
//等号后面要使用双引号包含起来
    char  str[]="hello word";

//    2.c++版本字符串
string string1="hello World";
cout<<string1 <<endl;
    

}

7.布尔类型

#include <iostream>

#define Day 7
using namespace std;

//使用c++风格的字符串要包含的头文件
#include "string"
int main() {

//布尔值:判断对错


bool flag= true;
cout<<flag<<endl;//输出1

bool j= false;
cout<<j<<endl;//输出0

}

8.数据的输入

作用:从键盘上获取数据
	关键字:cin>>'值'
	
		#include <iostream>

#define Day 7
using namespace std;

//使用c++风格的字符串要包含的头文件
#include "string"
int main() {

//1.整型
int a=0;
cout<<"请输入a的值"<<endl;
cin>>a;
cout<<"a的值为:"<<a<<endl;

//    2.浮点型
    float b=0;
    cout<<"请输入a的值"<<endl;
    cin>>b;
    cout<<"a的值为:"<<b<<endl;
//3.字符型
    char c='a';
    cout<<"请输入a的值"<<endl;
    cin>>a;
    cout<<"a的值为:"<<c<<endl;

//4.字符串类型
    string str="hello";
    cout<< "请输入"<< endl;
    cin>>str;
    cout<<"str的值为:"<<str<<endl;
}

第三章:运算符

1.算数,取模运算符

#include <iostream>

#define Day 7
using namespace std;

//使用c++风格的字符串要包含的头文件
#include "string"
int main() {

//1.加减乘除
int a1=10;
int a2=45;
cout<<a1+a2<<endl;
cout<<a1-a2<<endl;
cout<<a1*a2<<endl;
cout<<a1/a2<<endl;//两个指数相除 将小数部分去除

cout<<"你好"<<endl;

//2.取模运算:求两个相除的余数
cout<<a1%a2<<endl;
两个小数之间是不可以做取模运算的


    return 0;
}

2.算数运算符

前置递增
后置递增


前置递减
后置递减
#include <iostream>

#define Day 7
using namespace std;

//使用c++风格的字符串要包含的头文件
#include "string"
int main() {




//    1.前置递增
int a=10;
++a;//让变量+1
cout<<"啊的值"<<a<<endl;


//2.后置递增
int b=10;
b++;//让变量+1
cout<<"b=?"<<b<<endl;

/*
 * 前置和后置的区别:1前置递增 先让变量+1 如何在进行表达式运算
 *   后置递增 先进行表达式运算 后让变量+1
 * */

int a2=10;
int b2=++a2*10;
cout<<"a2"<<a2<<endl;







    return 0;
}

3.赋值运算符

=  赋值
+= 加等于
-= 减等于
*=乘等于
#include <iostream>


using namespace std;
int main() {

//赋值运算符
//
// 1. =
int a=10;
a=100;
cout<<a<<endl;

//2.+=
a=10;
a+=2;//a=a+2
cout<<"a的值为"<<a<<endl;


// 3.   *=
a*=2;
cout<<"a*=2为"<<a<<endl;

//4.  /=
a=90;
a /=2;//a=a/2
cout<<"a的值为"<<a<<endl;



    return 0;
}

4.比较运算符

#include <iostream>


using namespace std;
int main() {

/*比较运算符 :用于表达式的比较 并返回一个真值或假值
 *
 *
 *      ==相等
 *      !=不等于
 *         <小于
 *         >大于
 *         <=小于大于
 *         >= 大于大于
 *
 * */


int a=10;
int b=20;
cout<<(a==b)<<endl;//输出0

cout<<(2>90)<<endl;//注意:在c++中使用小括号来提高程序执行的优先级

cout<<(a>b)<<endl;//输出0


    return 0;
}

5.逻辑运算符

#include <iostream>


using namespace std;
int main() {

/*
 * 逻辑运算符:用于工具表达式的值返回真值或者假值
 *
 *      ! 非
 *      && 与
 *      || 或
 *
 * */

//逻辑运算符 非

    int a=10;
    cout<<!(!a)<<endl;//先算!a 然后再整体取反 就是输出1

//    与运算
bool n= true;
bool m= false;
cout<<!(m&&n)<<endl;
//在计算机中只要是非0数字都是真  a=1 a=-1 等等都是真  只有a=0的时候才是假

cout<<!(a)<<endl;


//或运算||

cout<<(m||n)<<endl;
    return 0;
}

第四章:程序流程语句

1.选择结构

三种运行结构:顺序结构 选择结构 循环结构
顺序结构:程序按照顺序执行 不发生跳转
选择结构:依照条件执行 是否满足 有选择的执行
循环结构:依照条件是否满足 循环多次执行某段代码

1.if语句
  作用:执行满足条件的语句
  单行格式的if语句
		#include <iostream>
using namespace std;
int main() {
    int a=10;
//    1.用户输入的分数
int score=0;

cout<<"请输入分数"<<endl;
cin>>score;
//2.打印用户输入的分数
cout<<"分数是:"<<score<<endl;
//3.判断房租是否大于600 大于就输出
//注意:if后面不要加分号
    if (score>600){
        cout<<"恭喜你考上了大学!"<<endl;

    }

    


    if (a<20){

        cout<<"你好"<<a<<endl;
        a++;
    }

    return 0;
}

2.多行格式的if语句

#include <iostream>
using namespace std;
int main() {

//多行 if语句
int score=0;
cout<<"请输入分数"<<endl;
cin>>score;

cout<<"你输入的分数"<<score<<endl;

    if (score>600){//如果大于600分就执行下面大括号

        cout<<"分数是"<<score<<"恭喜你录取了~·"<<endl;
    } else{//不大于600分就执行下面的代码


        cout<<"很遗憾 请耐心等待"<<endl;
    }




    return 0;
}

3.多条件语句

#include <iostream>
using namespace std;
int main() {

//多条件if语句:
//输入考上分数 如果大于600 分 就输出考上了 一本
//如果大于500 就输出二本大学
//如果大于400 就输出三本大学
//如果小于400 就输出 还未录取 请等待
int score=0;

cout<<"请输入分数"<<endl;
cin>>score;


if(score>600){
    cout<<"恭喜你考上了一本大学!"<<endl;
} else if(score>500&&score<<600){
    cout<<"恭喜你考上了二本大学"<<endl;

} else if(score>400&&score<500){
    cout<<"恭喜你考上了三本大学"<<endl;

} else{

    cout<<"很遗憾 你没有大学上了"<<endl;
}

    return 0;
}

4.嵌套if语句

#include <iostream>
using namespace std;
int main() {




//    用户输入分数
int score=0;
cout<<"请用户输入分数"<<endl;

cin>>score;


    if (score>700){

//        大于700还要分出考上北大或者清华
        if (score>750){
            cout<<"恭喜你 考上了北大"<<endl;
        } else if(score>710&&score<=759){

            cout<<"恭喜你 考上了人民大学"<<endl;
        } else if(score>=710&&score<=710){
            cout<<"恭喜你 考上了复旦大学"<<endl;
        }

    } else if(score <700&&score>500){

        cout<<"恭喜你考了一本"<<endl;

    } else if(score>400&&score<=500){
        cout<<"恭喜你考了二本"<<endl;
    }



    return 0;
}

#include <iostream>

using namespace std;
int main() {

//    创建三个变量
int num1=0;
int num2=0;
int num3=0;


//让用户输入内容:
        cout<<"请输入num1的值"<<endl;
        cin>>num1;

        cout<<"请输入num2的值"<<endl;
        cin>>num2;

        cout<<"请输入num3的值"<<endl;
        cin>>num3;
        
    if (num1>num2){

        if (num1>num3){

            cout<<"num2最大"<<num2<<endl;
        } else{
            cout<<"num3最大"<<num3<<endl;
        }

    } else{
        if (num2>num3){

            cout<<"num2最大"<<num2<<endl;
        }

    }
    
    return 0;
}

5.三目运算符

#include <iostream>
using namespace std;
int main() {

    /*
     * 作用:通过三目运算符实现简单的判断
     *          语法:表达式1?表达式2:表达式3  如果表达式1位真 则执行表达式2 并且返回表达式2的结果  以此类推
     *
     * */


    /*三目运算符
     * */

//    将a和b做义工比较  将变量大的值赋值给c
int a,b,c;
a=10;
b=10;
c=0;
          c= (a>b?a:b) ;
          cout<<"值为:"<<c<<endl;

//    创建三个变量 a b c
//在c++中三目运算符返回的是变量 可以继续赋值
    system("pause");
    return 0;
}

6.Switch语句

Switch:执行多条件分支语句




#include <iostream>


using namespace std;
int main() {

//    switch语句

//1.提示用户输入
cout<<"请给电影打分"<<endl;

//2.用户开始评分
int  score=0;
cin>>score;
cout<<"你打的分数是"<<score<<endl;

//4.根据用户输入的分数的不同来输出最后的结果
    switch (score) {
        case 10:
            cout<<"您认为是经典"<<endl;
            break;//退出这个分支

        case 9:
            cout<<"你认为是经典"<<endl;
            break;//退出这个分支

        case 8:
            cout<<"烂片"<<endl;
            break;
    }


    std::cout << "程序结束" << std::endl;
    return 0;
}


switch缺点:判断时候只能是整型或者字符型 不可以是一个区间
    		优点:结构清晰 执行效率高

第五章:循环结构

1.While循环语句

#include <iostream>

using namespace std;
int main() {

    /*
     *      1.While语句:
     *              作用满足循环条件  执行循环语句
     *                  语法: while(循环条件){循环语句}
     *
     *
     *
     * 主要循环条件一种为真就一直执行循环语句
     *
     *
     *
     *
     *
     *
     * */




// 在控制台输出0到9这个数字
    int num=0;
    while (num<10){//在while中填入循环条件
        cout<<num<<endl;//在while中书写代码体
        num++;
    }

    while (1){//在书写程序的时候  一定要杜绝死循环
        cout<<num<<endl;
        num++;
    }

    std::cout << "Hello, World!" << std::endl;
    return 0;
}

2.猜数字游戏

#include <iostream>

using namespace std;
#include "ctime"
int main() {
//    添加一个随机数的种子  作用利用当前系统时间生成随机数  防止每次随机数都一样
    srand((unsigned  int) time(NULL))


//    1.系统要生成一个随机数

int num= rand()%100;//0到99之间的随机数
//cout<<num<<endl;


//2.玩家进行猜测
int val=0;//玩家输入的数据


    while (1){

        cin>>val;
        if (val>num){
            cout<<"猜测过大"<<endl;

        } else if{

            cout<<"猜小了"<<endl;
        }else{

            cout<<"猜对了"<<endl;
            break;//在循环中使用break来退出
        }


    }


//3.判断玩家的猜测

//猜对  退出游戏
//猜错  提示信息

    return 0;
}

3.do…while语句

#include <iostream>

using namespace std;
int main() {

    /*
     *      do...while()语句
     *          作用:满足循环条件  执行循环语句
     *          语法:do{循环语句}  while(循环条件)
     *      注意:与while的区别在于 do...while会先执行因此循环语句  在判断循环条件
     *      先执行后判断
     *
     * */



//    示例:
int num=1;

    do {
        cout<<"计算机科学与技术学院"<<endl;
        cout<<num<<endl;
        num++;
    } while (num<100);
}

4.练习案例

#include <iostream>

using namespace std;
int main() {






//    1.将所有的三位数输出

int num=100;

int a=0;
int c=0;
int b=0;
    do {
a=num%10;
b=num/10%10;
c=num/100;
//2.在所有三位数中寻找水仙花数
        if (a*a*a+b*b*b+c*c*c==num){
            cout<<num<<endl;//3.输出水仙花数
        }

        num++;
    } while (num<1000);



















    std::cout << "Hello, World!" << std::endl;
    return 0;
}

5.for循环语句

#include <iostream>

using namespace std;
int main() {

//    for循环语句:作用先满足循环条件 执行循环语句
//语法:for(起始表达式;条件表达式;末尾循环体){循环语句}

//示例:
    for (int i = 0; i < 100; ++i) {
        cout<<i<<endl;
    }
    
}

6.嵌套循环

#include <iostream>

using namespace std;
int main() {

/*      嵌套循环:
 *              外层执行一次  内存执行一周
 *
 * */


    for (int i = 0; i <10 ; ++i) {

        for (int j = 0; j < 10; ++j) {
            cout<<"-"<<endl;
        }

        cout<< endl;
    }

}

7.跳转语句

#include <iostream>

using namespace std;
int main() {


    /*
     *      break语句:
     *              作用跳出选择结构或者循环结构
     *      使用:
     *              出现在Switch语句中 作用就是跳出当前的循环语句
     *              出现在嵌套循环中  跳出最近的内层循环语句
     *
     *
     * */

//    break的使用

//1.出现在Switch语句


int num=0;
cout<<"请选择难度"<<endl;
cout<<"1.普通"<<endl;
cout<<"2.困难"<<endl;
cout<<"3.最难"<<endl;
cin>>num;

    switch (num) {


        case 1:
            cout<<"普通"<<endl;
            break;

        case 2:
            cout<<"困难"<<endl;
            break;
        case 3:
            cout<<"最难"<<endl;
            break;
    }



//2.出现在循环语句

    for (int i = 0; i < 100; ++i) {


        if (i==7){//如果i=7 就退出
            
            cout<<"程序退出"<<endl;
            cout<<i<<endl;
        }
    }


//3.出现在嵌套循环语句
    for (int i = 0; i < 100; ++i) {
        for (int j = 0; j <i ; ++j) {

            if (i==j){
                cout<<"退出循环"<<endl;
                break;
                
            }
            
        }
    }


}
#include <iostream>

using namespace std;
int main() {



//    跳转语句:continue语句:  执行到这一行代码就执行下一次的代码

    for (int i = 0; i < 10; ++i) {

        if (i%2==0){
            cout<<"偶数是"<<i<<endl;
            continue;//直接跳出语句 可以作为筛选条件   这个并没有使整个循环终止  而break直接跳出
        } else if(i%2!=0){

            cout<<"奇数是"<<i<<endl;

        }
        


        cout<<i<<endl;

    }






}
#include <iostream>

using namespace std;
int main() {

    
    /*
     * goto语句:  goto 标记;  如果标记存在 则执行到goto语句的时候会跳转到标记的位置
     * 
     * */

    goto label;
    cout<<"你好"<<endl;
    
    cout<<"你好"<<endl;
    
    label:;
    
    
}

第五章:数组

1.一维数组

#include <iostream>

using namespace std;
int main() {
    /*
     * 数组特点:放在也快连续的内存空间中  数组中每个元素都是相同数据类型
     *
     *
     * */


//    1.方式一:
        int arr1[10];
        arr1[0]=1;//给数组中的元素赋值

//        访问数组元素
cout<<arr1[0]<<endl;

   /* for (int i = 0; i < 100; ++i) {
        arr1[i]=i;
        cout<<arr1[i]<<endl;
    }
*/

//2.方式二:
int  arr2[5]={10,22,22,34,34};
    for (int i = 0; i < 5; ++i) {

        cout<<arr2[i]<<endl;
    }

//如果在初始化的时候没有全部填写完毕 则使用0来补齐


//3.方式三:
int arr3[]={9,1,2,3,4,5,67,8,8,9};
//定义数组的时候 必须有初始长度
cout<<arr3[4]<<endl;

}

2.一维数组的数组名称

#include <iostream>

using namespace std;
int main() {

//一维数组名称的用途;1.可以统计整个数组在内存中的大小  2.可以获取数组在内存中的首地址

int a[5]={1,2,3,4,5};

//统计数组长度 sizeof()
cout<<"数组长度"<< sizeof(a)<<endl;


//获取数组中元素的首地址
cout<<a<<endl;



//查看某个元素的首地址
cout<<&a[4]<<endl;


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




    std::cout << "Hello, World!" << std::endl;
    return 0;
}
#include <iostream>

using namespace std;
int main() {


//    元素倒置
int arr[]={1,2,3,4,5,6,7,8};


//        实现数组元素的倒置
//1.创建数组

cout<<"逆序前的数组为"<<endl;
    for (int i = 0; i < 8; ++i) {

        cout<<arr[i]<<endl;
    }

    
//    先记录一下起始下标的位置

int start=0;
    int end=sizeof (arr) /sizeof (arr[0])-1;
    
    int temp=arr[start];


    while (start<end){
        arr[start]=arr[start];
        arr[start]=arr[end];
        arr[end]=temp;
        
        
//        下标更新
        start++;
        end--;
    }




    cout<<"逆序后的数组为"<<endl;
    for (int i = 0; i < 8; ++i) {

        cout<<arr[i]<<endl;
    }


//记录结束下标的位置

//起始下标与结束下标互换




//2.实现逆序数组



//3,打印逆序后的数组





}
#include <iostream>

using namespace std;
int main() {
    /*冒泡排序思路:
     *          1.比较相邻的数据 如果第一个对于第二个就交换
     *          2.对每一对相邻元素做同样的工作 执行完毕后找到其最大值
     *          3.重复以上操作
     *
     *      排序的总论数=元素个数-1
     *          每轮对比的次数=元素个数-排序轮数
     *
     * */


    int arr[]={4,2,3,0,6,7,8,9};


    for (int i = 0; i < 9-1; ++i) {
        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;

            }
        }

    }


    for (int i = 0; i < 8; ++i) {
        cout<<"排序后:"<<arr[i]<<endl;
    }


}

3.二维数组

#include <iostream>

using namespace std;
int main() {

/*
 * 二维数组:
 *
 * */


//方式一:
int arr[2][3];

arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;

        arr[1][0]=2;

//        外层循环打印行数  内层循环打印列数
    for (int i = 0; i < 2; ++i) {

        for (int j = 0; j < 3; ++j) {
            cout<<"\n"<<arr[i][j]<<endl;
        }
    }

    
    
    
//    2.方式二:
int arr2[2][3]={
        {1,2,3},
        {2,345}
    };
    
    
    
//    3.方式三:
int arr3[2][4]={123,232,232,231,23,12,212,232};


//4.方式四:
int arr4[][3]={123,456};
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

第六章:函数

1.函数的定义

#include <iostream>

using namespace std;
//返回值列表  函数名  参数列表  (参数列表)  {  函数体  return 返回值}


int main() {
    
    /*
     * 函数:将一段经常使用的代码封装起来 减少代码的重复
     *      
     *      
     *      函数的定义一般有五步:
     *              1-- 返回值类型
     *              2-- 函数名
     *              3--参数列表
     *               4.函数体语句
     *               5.return 表达式
     *      
     *      返回值类型 函数名(参数列表)
     *                  {
     *                  函数体语句
     *                  
     *                  return 表达式
     *                  
     *                  }
     * */
    
    
  
    
    
}

double function(int a,int b){


    for (int j=1;j<100;j++){
        
        cout<<"你好"<<endl;
        
    }
    return a+b;
}

2.函数调用

#include <iostream>

using namespace std;
//返回值列表  函数名  参数列表  (参数列表)  {  函数体  return 返回值}



int add(int a,int b){


    int c=a+b;
    return c;
}

int function(int v,int l){

    int m=v+ (++v)+ (++l) +l++;
    return m;
}
int main() {

   /*
    *
    *
    * 函数调用:
    *           main中调用
    *
    *       在函数定义的并没有实际参数  称为形参数
    *               在调用的时候  才会有实参来传
    *
    *       注意:定义函数的时候  定义的函数要写在main函数的上面  不能在下面
    * 
    * */

   int a=10;
   int b=100;
//   传递参数
int m=add(a,b);

cout<<"两个值为:"<<m<<endl;



cout<<"-------------------------"<<endl;


int v=90;

int l=70;

  int n= function(v,l);
cout<<"前置++和后置++:"<<n<<endl;
}

3.值传递

#include <iostream>

using namespace std;
//返回值列表  函数名  参数列表  (参数列表)  {  函数体  return 返回值}



int add(int a,int b){


    int c=a+b;
    return c;
}

int function(int v,int l){

    int m=v+ (++v)+ (++l) +l++;
    return m;
}


void swap(int num1,int num2){

//    定义一个临时变量
        int temp;

        temp=num1;
        num1=num2;
        num2=temp;

        cout<<"交换完成后:"<<num1<< num2<<endl;

//    不需要返回值
}

int main() {

    /*值传递:实现两个数字进行交换函数
     *              如果函数不需要返回值  可以在函数名之前书写一个void
     *
     * */
int a=100;
int b=900;
        swap(a,b);//因为这个函数没有返回值  所以不需要定义其他变量来接收这个值
//        当我们做值传递的时候 形参发送改变  并不会影响实参


}




4.参数的常见样式

#include <iostream>

using namespace std;




/*
 *      函数的常见样式:
 *              1.无参无返
 *              2.有参无返
 *              3.无参有返
 *              4.有参有返
 *
 *
 * */
//1.无参无返
void test1(){
    cout<<"Hello 计算机"<<endl;
}



//2.有参无返
void test2(int a){
    cout<<"有参无返"<<a<<endl;
}

//3.无参有返

int test3(){

    return 1000;

}

//4.有参有返的
int test4(int a ,int b){
    return a+b;
}
int main() {
    test1();
    test2(1);
int x=    test3();//因为有返回值  所以可以定义变量来接收
        cout<<x<<endl;
        int y= test4(12,34);
        cout<<y<<endl;
}



5.函数的声明

#include <iostream>

using namespace std;
/*
 * 函数的声明:告诉编译器函数名称及如何调用函数 函数的实际主体可以单独定义
 *      注意:函数的声明可以定义多次  但是函数的定义只能有一次
 *
 *
 * */


//声明函数:  因为之前的代码  函数定义在main后面就报错 可以提前告诉编译器 函数是存在的

int max(int a,int b);//函数的声明

//函数声明可以由多次  但是函数的定义只能有一次

int main() {

  int max1=  max(12,45);
cout<<max1<<endl;

}


int max(int a,int b){

    return a>b? a:b;
}

6.函数的分文件编写

swap.h


#include <iostream>
using namespace std;
//函数的声明
void swap(int a,int b);

swap.cpp

#include "swap.h"
//函数定义:
void swap(int a,int b){
    int temp=a;
    a=b;
    b=temp;
    cout<<a<<b<<endl;
}

#include "iostream"
using namespace std;
#include "swap.h"
/*
 *      函数的分文件编写:
 *                  作用:让代码结构更加清晰
 *                                  函数分文件编写一般有四个步骤
 *                                  1.创建后缀名为.h的头文件
 *                                   2.创建后缀名为.cpp的文件
 *                                   3.在头文件中写函数的声明
 *                                   4.在原文件中写函数的定义
 *
 *
 * */
//把swap的头文件包含过来即可使用
int main() {

int a=1;
int b=3;
    swap(a,b);

//1.创建.h为后缀名的头文件
//2.创建.cpp后缀的原文件
//3.在头文件中写函数的声明
//4.在原文件中写函数的定义

    system("pause");
    return 0;

}
cmake_minimum_required(VERSION 3.21)
project(Pr08)

set(CMAKE_CXX_STANDARD 14)

add_executable(Pr08 main.cpp swap.h swap.cpp)//注意 在分文件编写的时候 一定要把文件的添加 加上 不然会无法识别

第七章:指针

1.指针的定义及使用

#include <iostream>

using namespace std;
int main() {

/*
 *  指针的基本概念:
 *              真正的作用:可以通过指针间接访问内存
 *
 *                  内存编号是从0开始记录的 一般使用十六进制数来表示
 *                     可以用指针变量保存地址
 *
 *
 *            1.定义一个指针
 *
 *
 *            2.。使用指针
 *
 *
 * */
//1.定义指针:
int a=10;
//指针定义的语法:  数据类型 *指针变量名称
 int *p;

// 让指针记录变量b的值
        p=&a;

        cout<<"指针的地址是"<<&a<<endl;

                cout<<"p的地址"<<p<<endl;



//                使用指针
//      可以通过解引用的方式来找到指针指向的内存
//指针前加  *代表解引用 找到指针指向内存中的数据
*p=100;
cout<<"*a="<<&a<<endl;
cout<<"*p="<<p<<endl;


cout<<p<<endl;
    return 0;
}

2.指针所占的内存空间

#include <iostream>

using namespace std;
int main() {

    /*
     *      指针也是一种数据类型  那么指针占用多少内存呢?
     *
     *      在32操作系统下占用4个字节空间  在64位操作系统下占用8个字节
     *              不管是什么数据类型  都是这样
     * */


    int  *p;
    cout<<sizeof p<<endl;//输出8
    double *o;
    cout<<sizeof p<<endl;
    
    cout<<sizeof(float *)<<endl;




    return 0;
}

3.空指针

#include <iostream>

int main() {


    /*      空指针:空指针用于给指针变量进行初始化
     *
     *
     *
     *
     * */

    int *p=NULL;
//    2.空指针是不可以访问的    0-255之间的内存编号  是系统占用的  是不可以访问的

        std::cout<<p<<std::endl;

    
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

4.野指针

#include <iostream>

using namespace std;

int main() {


//    野指针  :指针变量指向非法的内存空间


//1.创建一个指针
int *p=(int *)0x1100;
        cout<<*p<<endl;//在程序中  避免出现指针情况

}

5.const修饰指针

#include <iostream>

using namespace std;

int main() {
/*      const修饰指针的三种情况:
 *                  1.const修饰指针  常量指针
 *                  2.const修饰常量  指针常量
 *                  3.const 修饰指针  又修饰常量
 *
 *
 * */

int a=10;
int b=90;


//1.常量指针
 const int *p=&b;//指针的值向可以修改   但是指针指向的值不可以改

// 2.指针常量
int * const i=&a;//特点:指针的指向不可以改 指向指向的值可以改








}

6.指针和数组

#include <iostream>

using namespace std;

int main() {









//    指针和数组

//        利用指针来访问数组中的元素
int arr[10]={1,2,3,34,};

cout<<arr[0]<<endl;//传统访问


//利用指针来访问
int *p=arr;//arr就是数组的首地址
cout<<"解引用:"<<*p<<endl;


//要想利用指针来访问  下一个元素  只需要 p++
p++;
cout<<"第二个元素为:"<<*p<<endl;

    int *r=arr;

    for (int i = 0; i < 4; ++i) {

        cout<<"指针的元素为"<<*r<<endl;
        r++;

    }

}

7.指针和函数

#include <iostream>

using namespace std;
void swap(int a,int b){
    int temp=a;
    a=b;
    b=temp;

    cout<<a<<b<<endl;
}


void  swap01(int *a,int *b){

    int temp=*a;
    *a=*b;
    *b=temp;
    cout<<"a的值为:"<<*a<<endl;
    cout<<"b的值为:"<<*b<<endl;
}
int main() {
//    指针和函数

//1.值传递
        int a=10;
        int b=12;
    swap(a,b);


//    2.地址传递
    swap01(&a,&b);


}

8.指针配合函数的案例

#include <iostream>

using namespace std;

void Sort(int *arr,int len){

    for (int i = 0; i < len-1; ++i) {
        for (int j = 0; j < len-i-1; ++j) {
//            如果j>j+1 就交换数字
            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){
    for (int i = 0; i < len; ++i) {
        cout<<"排序后的数组:"<<arr[i]<<endl;
    }

}


int main() {


//    封装一个函数  利用冒泡排序  实现对整型数组的升序排序
int arr[10]={23,43,3,21,23,34,56,21};

//数组长度的计算
    int len=sizeof (arr)/sizeof (arr[0]);

//    传递参数
    Sort(arr,len);


//    打印数组
    printArray(arr,len);

}

第七章:结构体

1.结构体基本概念

#include <iostream>

using namespace std;



//1.创建学生数据类型  学生 (姓名,年龄,分数)
struct Student
{
//    自定义的数据类型就是一些类型集合组成的一个类型
// 语法  struct 类型名称  {成员列表}
//    成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
}s3;





int main() {

//结构体基本概念:
//结构体属于用户自定义的数据类型  允许用户存储不同的数据类型


//1.结构体的定义: 语法:struct 结构体名称  {结构体成员变量};

/*          struct结构体名  变量名
 *          struct 结构体名 变量名
 *           struct 结构体名 变量名={成员1值 , 成员2值.....成员n值}
 *
 *
 * */

//示例:



//1.通过类型来创建具体的学生

//方法一:
    struct Student s1;
//给s1来赋值
//访问结构体变量中的属性
s1.name="吴邪";
s1.age=19;
s1.score=90;

cout<<"姓名:"<<s1.name<<"年龄:"<<s1.age<<"成绩:"<<s1.score<<endl;

//方式二:
    struct Student s2={
            "黑瞎子",
            19,
            99
    };
    cout<<"姓名:"<<s2.name<<"年龄:"<<s2.age<<"成绩:"<<s2.score<<endl;


//    方式三:在定义结构体的时候顺便创建结构体变量

    s3.name="张启山";
    s3.age=19;
    s3.score=100;
    cout<<"姓名:"<<s3.name<<"年龄:"<<s3.age<<"成绩"<<s3.score<<endl;

}

2.结构体数组

#include <iostream>
#include "string"
using namespace std;


//结构体数组:将自定义的结构体放入到数组中方便维护
//语法:  struct 结构体名  数组名{元素个数}={{},{},{},.....{}}


//结构体数组:
//        1,创建一个结构体

struct Student{

    string name;
    int age;
    int score;
};





int main() {

//2.创建一个结构体的数组

struct Student stuArray[3]={
        {"吴邪",19,80},
        {"黑瞎子",20,90},
        {"张起灵",25,95}
};

//          3.给结构体数组中的元素赋值
stuArray[2].name="赵子龙";//可以修改里面的数据
stuArray[2].age=30;
stuArray[2].score=85;

//遍历结构体数组
    for (int i = 0; i < 3; ++i) {
        cout<<"姓名:"<<stuArray[i].name<<"——年龄:"<<stuArray[i].age<<"——成绩:"<<stuArray[i].score<<endl;
    }


//4.变量结构体数组





}

3.结构体指针

#include <iostream>
#include "string"
using namespace std;



//结构体指针:

//1.定义学生结构体
struct student{

    string name;//姓名
    int age;//年龄
    int score;//分数

};




int main() {

//创建学生的结构体变量
struct student s={"吴邪",18,90};

//通过指针来指向结构体变量
struct student *p=&s;

//通过指针来访问结构体中变量的数据
        cout<<"姓名:"<<s.name<<endl;
        cout<<"年龄:"<<s.age<<endl;


/*
 * 结构体指针:通过指针访问结构体中的成员
 *          利用操作符  ->  可以通过结构体指针访问结构体属性
 *
 *
 * */



}

4.结构体嵌套结构体

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值