C++自学笔记 非零基础(01)

1.输出HelloWorld

cpp 复制代码
#include "iostream"
using namespace std;
int main(){
    cout<<"你好,世界!Hello,World!"<<endl;
    return 0;
}

2.常量认识 const #define

cpp 复制代码
//常量的定义方式
/**
 * #define 宏常量
 * const 修饰的变量
 * @return
 */

#define Day 7

int main() {
    cout << "Aaa" << endl;
    //Day=7;  //常量一修改 报错
    cout << "一周一共有:" << Day << "天.";

    //const修饰的变量
    const int month = 12;
    //month =13; //const修改的变量 也称作 常量
    return 0;
}

4.数据类型

cpp 复制代码
/*
 * 数据类型
 * 数据类型存在意义:给变量分配一个合适的空间
 * 整型:
 * short 两个字节   -32767 到 -32767
 * int  4个字节
 * long 32位系统4个字节 64位系统8个字节
 * long long 8个字节
 *
 * 浮点型:
 * 单精度 float  4个字节
 * 双精度 double   8个字节
 *
 * 字符型  1个字节
 * 作用:字符型变量用于显示单个字符
 * 语法 char ch='a';
 *
 * 转义字符 \n 换行 \t 水平制表符 \\ 反斜杠
 *
 * 字符串型
 * char 变量名[] ="字符串值";  //c风格
 * string 变量名="字符串值";  需要添加头 #include<string>
 *
 * bool布尔类型 bool
 *      false 假         true 真
 *      本质 1代表真 0代表假
; */

int main() {
    //整型
    short num1 = 10;
    int nu2 = 10;
    long num3 = 10;
    long long num4 = 10;

    //双精度
    float f1 = 3.14f;
    double d1 = 3.14;

    //字符型变量
    char ch = 'a';

    //bool 数据类型
    bool flag = true;
    bool flag2 = false;


    return 0;
}

5.数据输入(cin)

cpp 复制代码
int main() {

    //整型
    int a = 0;
    cout << "请对a进行赋值" << endl;
    cin >> a;
    cout << "a=" << a << endl;


    return 0;
}

6.运算符

cpp 复制代码
int main() {
    //1.算术运算符
    //加减乘除
    int a = 10;
    int b = 3;
    cout << "a-b=" << a - b << endl;
    cout << "a+b=" << a + b << endl;
    cout << "a/b=" << a / b << endl; //整除        整数/整数 = 整数
    cout << "sizeof(a/b)=" << sizeof(4.0 / 2.0) << endl; //整除
    cout << "sizeof(a/b)=" << sizeof(4 / 2) << endl; //整除
    cout << "a%b=" << a % b << endl; //取余

    //赋值运算符     与java相同
    // = += -= &= /= %=
    int aa = 0;
    aa += aa;

    //比较运算符     与java相同
    //   ==    !=     <   >     <=    >=

    //逻辑运算符     与java相同
    // !   &&  ||  优先级  ! > && > ||
    
    return 0;
}

7.程序流程

if else; while; do while; for

cpp 复制代码
int main() {
    //选择结构 if else  与java相同 有嵌套和多个else if
    if (true) {
        cout << "是" << endl;
    } else {
        cout << "否" << endl;
    }

    //三目运算符 与java相同
    int a = 10;
    int b = 20;
    a > b ? a : b;
    cout << "a与b谁大?" << (a > b ? "a大" : "b大") << endl;

    //switch 语句 (整型 字符型 枚举类型)
    //switch case break defalut 四个关键词
    switch (10) {
        case 0:
            cout << 0 << endl;
            break;
        case 10:
            cout << 10 << endl;
            break;
        default:
            cout << "default" << endl;
    }

    //while 循环语句 与java一样
    //    break 也可以退出

    //do while 语句 与java一样
    return 0;
}

8. 水仙花数联系

cpp 复制代码
int main() {
    /*
     * 思路
     * 1.循环所有三位数
     * 2.将每一个三位数每个位分开 ,求3次幂,并判断是否等于该值本身
     */
//    int a=111;
//    int b=a%10;
//    int c=(a/10)%10;
//    int d=(c/100)%10;

    int start = 100;
    int end = 999;
    int num = 0;
    while (start <= end) {
        int b = start % 10;
        int c = (start / 10) % 10;
        int d = (start / 100) % 10;
        if (b * b * b + c * c * c + d * d * d == start) {
            num = num + 1;
            cout << "第" << num << "个" << "水仙花数=" << start << endl;
        }
        start = start + 1;
    }

    cout << "升级版水仙花数" << endl;
    start = 1000;
    end = 9999;
    num = 0;
    while (start <= end) {
        int b = start % 10;
        int c = (start / 10) % 10;
        int d = (start / 100) % 10;
        int e = (start / 1000) % 10;
        if (b * b * b * b + c * c * c * c + d * d * d * d + e * e * e * e == start) {
            num = num + 1;
            cout << "第" << num << "个" << "水仙花数=" << start << endl;
        }
        start = start + 1;
    }
    return 0;
}

9.for循环

cpp 复制代码
/**
 * for循环
 * 作用:满足循环条件,执行循环语句
 * 语法:for(其实表达式;条件表达式;末尾循环体){循环语句}
 * @return
 */
int main() {
    //for循环
    //从数字 0 打印到 9
    for(int i=0;i<=9;i++){
        cout<<i<<endl;
    }

    //输出1到100 这些数字
    //从100个数字中 找到包含7和,7的倍数,改为敲桌子
    for(int i=0;i<=100;i++){
        if(i%7==0){
            cout<<"敲桌子:"<<i<<endl;
        }
        if(i%10==7){
            cout<<"敲桌子:"<<i<<endl;
        }
        if((i/10)%10==7){
            cout<<"敲桌子:"<<i<<endl;
        }
    }
    return 0;
}

10.break continue goto语句

cpp 复制代码
/*
 * break 语句
 * 作用:用于跳出选择结构或者循环结构
 * break使用的时机
 *      出现在switch条件语句中,作用是终止case并跳出switch
 *      出现在循环语句中,作用是跳出当前的循环语句
 *      出现在嵌套循环中,跳出最近的内层循环语句
 *
 * continue
 * 作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环
 *
 * goto语句
 * 作用:可以无条件跳转
 * 建议:不推荐使用
 */
int main() {
    //break
    //  1.出现在switch语句中 与java相同
    //  2.出现在循环嵌套语句中 与java相同
    //  3.出现在嵌套循环中  与java相同

    //continue 与java相同
    //只打印奇数
    for(int i=0;i<100;i++){
        if(i%2==0){
            continue;
        }
        cout<<i<<" ";
    }
    cout<<endl;
    
    //goto
    //可利用 goto 构建循环语句,
    //打死都不可用
    cout<<"goto语句学习"<<endl;
    cout<<"1.XXXXXX"<<endl;
    goto FLAG;
    cout<<"2.XXXXXX"<<endl;
    cout<<"3.XXXXXX"<<endl;
    FLAG:
    cout<<"4.XXXXXX"<<endl;
    cout<<"5.XXXXXX"<<endl;
    return 0;
}

11. 数组

cpp 复制代码
/**
 * 数组
 * 所谓数组,就是一个集合,里面存放了相同类型的数据元素
 * 特点1:数组中的每个数据元素都是相同的数据类型
 * 特典2:数组是由连续的内存位置组成的
 *
 * 一维数组定义方式
 * 1. 数据类型 数组名[数组长度];
 * 2. 数组类型 数组名[数组长度] = {值1,值2,值3...}
 * 3. 数据类型 数组名[]={值1,值2,值3...};
 *
 * 一维数组名称用途:
 * 1.可以统计整个数组在内存中的长度
 * 2.可以获取数组在内存中的首地址
 * 注意:数组名是常量,不能进行赋值操作
 *
 * 二维数组定义方式
 * 1.数据类型 数组名[行数][列数];
 * 2.数据类型 数组名[行数][列数]={{数据1,数据2},{数据3,数据4}};
 * 3.数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4};
 * 4.数据类型 数组名[] [列数] = {数据1,数据2,数据3,s数据4};
 *
 * 二维数组数组名
 * 1.查看二维数组所占内存空间
 * 2.获取二维数组首地址
 * @return
 */
int main() {

    cout<<"一维数组定义方式"<<endl;
    //1. 数据类型 数组名[数组长度];
    // 数组元素坐标从 0 开始。
    int arr[5];
    arr[0]=123;
    cout<<arr[0]<<endl;

    //2. 数组类型 数组名[数组长度] = {值1,值2,值3...}
    int aar2[5]={1,2,3,4,5};
    //打印出的是地址
    cout<<aar2<<endl;

    //3. 数据类型 数组名[]={值1,值2,值3...};
    int aar3[]={1,2,3,4,5};
    cout<<aar3<<endl;

    cout<<endl;
    //1.一维数组名称用途
    cout<<"一维数组名称用途:"<<endl;
    cout<<"aar3 数组长度为"<<sizeof (aar3)<<endl;
    cout<<"aar3 的首地址为:"<<aar3<<endl;
    cout<<"aar3 中每个元素占用内存空间为:"<<sizeof (aar3[0])<<endl;
    cout<<"aar3 中元素个数为:"<<sizeof (aar3)/sizeof (aar3[0])<<endl;
    cout<<"aar3 第二个元素的地址为:"<<&aar3[1]<<endl;

    cout<<endl;
    cout<<"练习题: 找出五只小猪中,最重的小猪"<<endl;
    int xiaoZhuArray[]={300,350,200,150,100};
    int minXiaoZhu=-1;
    for(int i=0;i< size(xiaoZhuArray);i++){
        if(minXiaoZhu<xiaoZhuArray[i]){
            minXiaoZhu=xiaoZhuArray[i];
        }
    }
    cout<<"最重的小猪为:"<<minXiaoZhu<<endl;

    cout<<endl;
    cout<<"冒泡排序"<<endl;
    int arr4[]={4,2,8,0,5,7,1,3,9};
    for(int i=0;i< size(arr4)-1;i++){
        for(int j=0;j< size(arr4)-i-1;j++){
            if(arr4[j]>arr4[j+1]){
                int temp=arr4[j];
                arr4[j]=arr4[j+1];
                arr4[j+1]=temp;
            }
        }
    }
    for(int i=0;i< size(arr4);i++){
        cout<<arr4[i]<<" ";
    }
    cout<<endl;

    cout<<endl;
    cout<<"二维数据"<<endl;
    int arr23[2][3];
    arr23[0][0]=1;
    arr23[0][1]=2;
    arr23[0][2]=3;
    arr23[1][0]=4;
    arr23[1][1]=5;
    arr23[1][2]=6;
    //外层循环打印函数,内层循环打印列数
    for(int i=0;i<2;i++){
        for(int j=0;j<3;j++){
            cout<<arr23[i][j]<<" ";
        }
    }
    cout<<endl;
    // 2.数据类型 数组名[行数][列数]={{数据1,数据2},{数据3,数据4}};
    int arr232[2][3]={{1,2,3},{4,5,6}};

    //3.数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4};
    int arr233[2][3]={1,2,3,4,5,6};

    //4.数据类型 数组名[] [列数] = {数据1,数据2,数据3,s数据4};
    int arr234[][3]={1,2,3,4,5,6};

    //二维数组 查看内存空间大小
    cout<<"二维数组所占用的空间为:"<<sizeof(arr234)<<endl;
    cout<<"二维数组的首地址为:"<<arr234<<endl;
    return 0;
}

12. 函数

头文件

注意:需要导入# include "iostream" using namespace std;

cpp 复制代码
# include "iostream"
using namespace std;
#ifndef _001HELLOWORLD__012HANSHU_H
#define _001HELLOWORLD__012HANSHU_H

#endif //_001HELLOWORLD__012HANSHU_H

void swap(int,int);

源文件

cpp 复制代码
#include "_012HanShu.h"
# include "iostream"
using namespace std;
/**
 * 函数
 *
 * 作用:将一段经常使用的代码封装起来,减少重复的代码
 *
 * 函数的定义:
 *      1.返回值类型
 *      2.函数名
 *      3.参数列表
 *      4.函数体语句
 *      5.return表达式
 *
 * 语法:
 *      返回值类型 函数名(参数列表){
 *          函数体语句;
 *          return表达式;
 *      }
 *
 * 值传递
 *      函数的形参发生改变,并不会影响实参
 *
 * 函数常见4种样式
 *      1.无参无返
 *      2.有参无返
 *      3.无参有返
 *      4.有参有返
 *
 * 函数声明
 * 作用:告诉编译器函数名称及如何调用函数。函数的实际主体可以单独定义
 * 注意:函数的声明可以多次,但是函数的定义只能有一次
 *
 * 函数的分文件编写
 *      1.创建后缀名为 .h 的头文件
 *      2.创建后缀名为 .cpp 的源文件
 *      3.在头文件中写函数的声明
 *      4.在源文件中写函数的定义
 *      5.在源文件中引用头文件  [#include "_012HanShu.h"]
 * @return
 */
 //可声明多次
int add(int a,int b);
int add(int a,int b);

int main() {
    int returnData=add(3,4);
    cout<<"返回值结果为:"<<returnData<<endl;
    cout<<endl;

    cout<<"值传递"<<endl;
    int num1=100;
    int num2=200;
    cout<<"交换前:"<<"num1="<<num1<<"; num2="<<num2<<endl;
    swap(num1,num2);
    cout<<"交换后:"<<"num1="<<num1<<"; num2="<<num2<<endl;
    cout<<endl;
    
    return 0;
}

/**
 * 实现一个加法函数
 * 功能:传入两个整型数据,计算数据相加额结果,并返回
 */
 int add(int a,int b){
    return a+b;
 }

 void swap(int num1,int num2){
    int temp=num1;
    num1=num2;
    num2=temp;
    cout<<"swap函数中"<<"num1="<<num1<<"; num2="<<num2<<endl;
     return;
 }

13.指针

cpp 复制代码
/*
 * 指针
 * 指针的作用:可以通过指针间接访问内存
 *
 * 指针变量的定义和使用
 * 语法:数据类型 * 指针变量;
 *
 * 指针所占的内存空间
 * 32位操作系统 4个字节; 64位操作系统 8个字节;
 *
 * 空指针
 * 空指针:指针变量指向内存中编号为0的空间
 * 用途:初始化指针变量
 * 注意:空指针指向的内存是不可以访问的
 *
 * 野指针
 * 野指针:指针变量指向非法的内存空间
 *
 * const修饰指针
 *      1.const修饰指针 --常量指针
 *          特点:指针的指向可以修改,但是指针指向的值不可以修改
 *      2.const修饰常量 --指针常量
 *      3.const既修饰指针,又修饰常量
 *
 * 指针和数组
 * 利用指针来访问数组中的元素
 *
 * 指针和函数
 * 作用:利用指针作为函数参数,可以修改实参的值
 *
 */
void swap(int *,int * );
int main() {

    //1.定义指针
    int a=10;
    int * p=&a;
    //让指针记录变量a的地址
    p=&a;
    cout<<"a的地址为:"<<&a<<endl;
    cout<<"指针p为:"<<p<<endl;

    //2.使用指针
    //可以通过解引用的方式来找到指针指向的内存
    //指针前加*代表解引用,找到指针指向的内存中的数据
    *p=1000;
    cout<<"a的地址为:"<<a<<endl;
    cout<<"指针p为:"<<*p<<endl;
    cout<<endl;

    cout<<"指针占用空间"<<endl;
    cout<<"指针所占字节数为:"<<sizeof (p)<<endl;
    cout<<endl;

    cout<<"空指针"<<endl;
    //p1的指针地址为0
    int *p1=NULL;
    cout<<"p1指针的地址为:"<<p1<<endl;
    cout<<endl;

    cout<<"野指针"<<endl;
    int *p2=(int*)0x1100;
    cout<<"p2指针的地址为:"<<p2<<endl;
//    cout<<"*p2指针的数据为:"<<*p2<<endl; //报bug
    cout<<endl;

    cout<<"const指针"<<endl;
    //1.常量指针 指针指向的值不可以修改;但是指针的指向可以修改
    const int *p3=&a;
    //*p3 =20;

    //2.指针常量
    //指针的指向不可以更改;但是指针的值可以修改
    int aa=10;
    int * const paa =&aa;

    //3.const既修饰指针,又修饰常量
    //指针的指向和指针的值都不可以修改。
    const int * const paaa=&aa;
    cout<<endl;

    //指针与数组
    cout<<"指针与数组"<<endl;
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
    int * arrp=arr; //首地址给arrp
    cout<<"    "<<"第一个元素为"<<*arrp<<endl;
    cout<<"    "<<"第二个元素为"<<*(arrp+1)<<endl;
    cout<<"    "<<"第三个元素为"<<*(arrp+2)<<endl;
    cout<<"指针遍历数组"<<endl;
    for(int i=0;i< size(arr);i++,arrp++){
        cout<<"    "<<"第"<<i<<"个元素值为:"<<*arrp<<endl;
    }
    cout<<endl;

    cout<<"指针和函数"<<endl;
    int num1=1;
    int num2=2;
    cout<<"交换值前:num1="<<num1<<"; num2="<<num2<<endl;
    swap(&num1,&num2);
    cout<<"交换值后:num1="<<num1<<"; num2="<<num2<<endl;
    return 0;
}

//地址传递
void swap(int * num1,int * num2){
    int temp=*num1;
    *num1=*num2;
    *num2=temp;
}

14.结构体

cpp 复制代码
/*
 * 结构体
 * 结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
 *
 * 语法:struct 结构体名 {结构体成员列表};
 *      struct 结构体名 变量名
 *      struct 结构体名 变量名 ={成员值,成员2值...}
 *      定义结构体时顺便创建变量  [不建议用]
 *
 * struct 创建的时候,可以省略
 *
 * 结构体数组
 * 作用:将自定义的结构体放入到数组中方便维护
 * 语法:struct 结构体名 数组名[元素个数]={{},{},{},{}};
 *
 * 结构体指针
 * 作用:通过指针访问结构体中的成员
 * 利用操作符 -> 可以通过结构体指针访问结构体属性
 *
 * 结构体嵌套结构体
 *
 * 结构体左函数参数
 * 作用:将结构体作为参数向函数中传递
 * 传递方式有两种:
 *      1.值传递
 *      2.地址传递
 *
 * 结构体中const使用场景
 * 作用:用const来防止误操作
 */

//1.创建学生数据类型:学生包括(姓名,年龄,分数)
struct Student{
    string name;
    int age;
    int score; //分数
} s3;//定义结构体时顺便创建变量

//结构体嵌套结构体
struct Teacher {
    string name;
    string age;
    struct Student s1;
    Student s2;
};

void zhiChuanDi(Student);
void diZhiChuanDi(Student * );
void myPrintf(string,Student);
void myPrintf2(string info,const Student * ps);
int main() {
    //2.通过学生类型创建具体学生
    //2.1  struct 结构体名 变量名
    struct Student s1;
    s1.name="李四";
    s1.age=18;
    s1.score=99;
    //2.2 struct 结构体名 变量名 ={成员值,成员2值...}
    struct Student s2={"张三",20,20};

    cout<<"s1.name="<<s1.name<<"; s1.age="<<s1.age<<"; s1.scroe="<<s1.score<<";"<<endl;
    s3.name="王五";
    s3.age=23;
    s3.score=50;
    cout<<"s3.name="<<s3.name<<"; s3.age="<<s3.age<<"; s3.scroe="<<s3.score<<";"<<endl;

    //struct 创建结构体的时候,可以省略
    Student s4;
    cout<<endl;

    cout<<"结构体数组"<<endl;
    struct Student studentArray1[3];
    studentArray1[0].name="张三";
    studentArray1[0].age=18;
    studentArray1[0].score=100;
    studentArray1[1]={"李四",20,98};

    //定义结构体数组时,赋值
    struct Student studentArray2[2]={{"QQ",19,20},{"WW",20,90}};
    cout<<endl;

    cout<<"结构体指针"<<endl;
    Student s5;
    Student * ps5;
    ps5=&s5;
    ps5->name="minyeling";
    //通过结构体指针,访问结构体的属性
    cout<<"ps5->name="<<ps5->name<<endl;
    cout<<endl;

    cout<<"结构体嵌套结构体"<<endl;
    Teacher t;
    t.name="张老师";
    t.age=50;
    t.s1.name="张三";
    t.s1.age=17;
    t.s1.score=68;
    t.s2={"李四",21,99};
    cout<<endl;

    cout<<"结构体做函数参数传递"<<endl;
    Student s6={"王异",20,30};
    zhiChuanDi(s6);
    myPrintf("值传递后 main函数中: ",s6);
    //地址传递
    diZhiChuanDi(&s6);
    myPrintf("地址传递后 main函数: ",s6);
    cout<<endl;

    cout<<"const修饰的结构体"<<endl;
    Student s7={"吕蒙",25,66};
    myPrintf2("const修饰的结构体:",&s7);
    Student s8={"马超",43,65};

    myPrintf2("const修饰的结构体:",&s8);
    return 0;
}

//结构体值传递
void zhiChuanDi(Student s){
    s.name="刘备";
    myPrintf("值传递函数中: ",s);
}

//结构体地址传递
void diZhiChuanDi(Student * ps){
    ps->name="张飞";
    myPrintf("结构体地址传递函数中 ",*ps);
}

//const 修饰地址传递
void myPrintf2(string info,const  Student * ps){
    //ps->name="小马超";//加入const之后,一旦有修改就会报错,可以防止我们误操作
    cout<<info<<" ps->name="<<ps->name<<"; ps->age"<<ps->age<<"; ps->score"<<ps->score<<";"<<endl;
}

void myPrintf(string info,Student s){
    cout<<info<<" s.name="<<s.name<<"; s.age"<<s.age<<"; s.score"<<s.score<<";"<<endl;
}

15

相关推荐
管家罢了3 分钟前
list的模拟实现
开发语言·c++
一只小松许️11 分钟前
C++常引用详解
开发语言·c++
ubuntu180414 分钟前
C0013.Clion中利用C++调用opencv打开摄像头
开发语言·c++·opencv
hello小强1 小时前
golang web笔记-3.响应ResponseWriter
开发语言·笔记·golang
拥有一颗学徒的心1 小时前
设计模式——责任链模式
c++·设计模式·学习方法·责任链模式·设计规范
沥川同学2 小时前
C++番外篇-------排序算法总结
c++·算法·排序算法
人才程序员2 小时前
CSP-J 复赛算法 贪心算法
c语言·c++·算法·贪心算法·noip·竞赛·noi
c_ceiling2 小时前
leetcode_198_打家劫舍
c++·算法·leetcode
笑鸿的学习笔记3 小时前
OpenGL笔记十九之相机系统
笔记·数码相机
凯U编程3 小时前
国庆刷题(day1)
c语言·c++