【系统性】 循序渐进学C++

这篇具有很好参考价值的文章主要介绍了【系统性】 循序渐进学C++。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

循序渐进学C++

第一阶段:基础

一、环境配置

1.1.第一个程序(基本格式)

#include <iosteam>
using namespace std;

int main(){
	cout<<"hello world"<<endl;
	system("pause");
}

​ 模板

#include <iosteam>
using namespace std;
#include <string.h>
int main(){
	//主要功能代码区
    
    
	system("pause");
}
1.2注释

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

1.3变量

​ 意义:方便管理内存空间

​ 方法:数据类型 变量名 = 数字;

#include <iosteam>
using namespace std;

int main(){
	int a = 10; 
	cout<<"a = "<<a<<endl;
	system("pause");
}
1.4常量

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

#include <iosteam>
using namespace std;
#def Day 7
int main(){
  
	cout<<"一周一共有"<<Day<<endl;
	system("pause");
  
}
1.5关键字

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

1.6变量命名规则

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

二、数据类型

​ 目的:给数据存分配合适的空间大小;避免资源浪费

2.1整形

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

2.2 sizeof

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

int main(){
	cout<<"short类型内存为:"<<sizeof(short)<<endl;
}

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

2.3浮点型

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ float占用四个字节

​ double占用八个字节

​ 科学计数法

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

2.4字符型

​ 注意事项:单引号;单引号里面只能放一个;ASCII值;int(char);A65;a97.

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

2.5转义字符

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

2.6字符串型

​ #include string

​ string a = “你好世界” //C++中的变量

​ char str[] =“Hello world” //C中的

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

2.7布尔数据类型

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

2.8数据的输入

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

#include <iosteam>
using namespace std;
#include <string>

int main(){
  int a =0;
	cout<<"请给a赋值 = "<<endl;
  cin>>a;把输入的值赋值给a
  cout<<"a = "<<a<<endl;
  
  char b ='a';
	cout<<"请给b赋值 = "<<endl;
  cin>>a;把输入的值赋值给a
  cout<<"b = "<<a<<endl;
  
	system("pause");
  
}

三、运算符

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

3.1 算术运算符号

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

#include <iostream>
using namespace std;

int main() {
    int num1, num2;
    cout << "请输入两个整数:" << endl;
    cin >> num1 >> num2;

    // 加法
    int sum = num1 + num2;
    cout << "加法结果:" << sum << endl;

    // 减法
    int difference = num1 - num2;
    cout << "减法结果:" << difference << endl;

    // 乘法
    int product = num1 * num2;
    cout << "乘法结果:" << product << endl;

    // 除法
    if (num2 != 0) {
        double quotient = static_cast<double>(num1) / static_cast<double>(num2);
        cout << "除法结果:" << quotient << endl;
    } else {
        cout << "除数不能为0" << endl;
    }

    return 0;
}

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

3.2赋值运算符

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

#include <iostream>
using namespace std;

int main() {
    int a = 10; // 直接赋值
    cout << "a = " << a << endl;

    a += 5; // 加法赋值
    cout << "a = " << a << endl;

    a -= 3; // 减法赋值
    cout << "a = " << a << endl;

    a *= 2; // 乘法赋值
    cout << "a = " << a << endl;

    a /= 4; // 除法赋值
    cout << "a = " << a << endl;

    a %= 6; // 取模赋值
    cout << "a = " << a << endl;
    
    int v = 10;
    ++v ;
    cout<<"v = "<<v<<endl;
  
  

    return 0;
}


3.3比较运算符

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20;

    // 等于运算符
    if (a == b) {
        cout << "a等于b" << endl;
    } else {
        cout << "a不等于b" << endl;
    }

    // 不等于运算符
    if (a != b) {
        cout << "a不等于b" << endl;
    } else {
        cout << "a等于b" << endl;
    }

    // 大于运算符
    if (a > b) {
        cout << "a大于b" << endl;
    } else {
        cout << "a不大于b" << endl;
    }

    // 小于运算符
    if (a < b) {
        cout << "a小于b" << endl;
    } else {
        cout << "a不小于b" << endl;
    }

    // 大于等于运算符
    if (a >= b) {
        cout << "a大于等于b" << endl;
    } else {
        cout << "a小于b" << endl;
    }

    // 小于等于运算符
    if (a <= b) {
        cout << "a小于等于b" << endl;
    } else {
        cout << "a大于b" << endl;
    }
	system("pause");
    return 0;
}

3.4逻辑运算

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

#include <iostream>
using namespace std;

int main() {
    bool a = true, b = false;

    // 与运算符
    if (a && b) {
        cout << "a和b都为真" << endl;
    } else {
        cout << "a和b不都为真" << endl;
    }

    // 或运算符
    if (a || b) {
        cout << "a和b至少有一个为真" << endl;
    } else {
        cout << "a和b都不为真" << endl;
    }

    // 非运算符
    if (!a) {
        cout << "a为假" << endl;
    } else {
        cout << "a为真" << endl;
    }
	system("pause");
    return 0;
}

四、 程序流程结构

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

4.1顺序结构
#include <iostream>
using namespace std;

int main() {
    int num = 0;

    // if语句
    if (num > 0) {
        cout << "num大于0" << endl;
    } else if (num < 0) {
        cout << "num小于0" << endl;
    } else {
        cout << "num等于0" << endl;
    }

    return 0;
}

if (条件表达式) {
    // 当条件表达式为真时执行的代码块
} else if (条件表达式) {
    // 当第一个条件表达式为假,且当前条件表达式为真时执行的代码块
} else {
    // 当所有条件表达式都为假时执行的代码块
}

别加分号

4.2选择顺序结构

#include <iostream>
using namespace std;
int main() {
    int score = 0;
    cout << "输入分数" << endl;
    cin >> score;
    cout << "您的分数为:" << score << endl;

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

    system("pause");
    return 0;


}

4.2嵌套语句

​ 案例一需求

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ 代码实现

#include <iostream>
using namespace std;

int main() {
    int score;
    cout << "请输入高考考试分数: ";
    cin >> score;

    if (score > 600) {
        cout << "考上一本" << endl;
        if (score > 700) {
            cout << "考入北大" << endl;
        } else if (score > 650) {
            cout << "考入清华" << endl;
        } else if (score > 600) {
            cout << "考入人大" << endl;
        }
    } else if (score > 500) {
        cout << "考上二本" << endl;
    } else if (score > 400) {
        cout << "考上三本" << endl;
    } else {
        cout << "未考上本科" << endl;
    }

    return 0;
}

​ 案例二需求----三只小猪称体重
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ 代码实现:

#include <iostream>
using namespace std;

int main() {
    int weightA, weightB, weightC;
    cout << "请输入小猪A的体重:";
    cin >> weightA;
    cout << "请输入小猪B的体重:";
    cin >> weightB;
    cout << "请输入小猪C的体重:";
    cin >> weightC;

    if (weightA >= weightB && weightA >= weightC) {
        cout << "小猪A最重" << endl;
    } else if (weightB >= weightA && weightB >= weightC) {
        cout << "小猪B最重" << endl;
    } else {
        cout << "小猪C最重" << endl;
    }

    return 0;
}

4.3 三目运算符:

​ 要求

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ 代码实现:

#include <iostream>
using namespace std;

int main() {
    int weightA, weightB, weightC;
    cout << "请输入小猪A的体重:";
    cin >> weightA;
    cout << "请输入小猪B的体重:";
    cin >> weightB;
    cout << "请输入小猪C的体重:";
    cin >> weightC;

    string heaviest = (weightA >= weightB && weightA >= weightC) ? "小猪A" : (weightB >= weightA && weightB >= weightC) ? "小猪B" : "小猪C";
    cout << heaviest << "最重" << endl;

    return 0;
}

​ 注意:?是判断的意思

​ 就是":"就是否则就的意思
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

4.4 switch语句
#include <iostream>
using namespace std;

int main() {
    int weightA, weightB, weightC;
    cout << "请输入小猪A的体重:";
    cin >> weightA;
    cout << "请输入小猪B的体重:";
    cin >> weightB;
    cout << "请输入小猪C的体重:";
    cin >> weightC;

    string heaviest;
    if (weightA >= weightB && weightA >= weightC) {
        heaviest = "小猪A";
    } else if (weightB >= weightA && weightB >= weightC) {
        heaviest = "小猪B";
    } else {
        heaviest = "小猪C";
    }

    switch (heaviest[0]) {
        case '小':
            cout << "最重的小猪是" << heaviest << endl;
            break;
        default:
            cout << "没有找到最重的小猪" << endl;
            break;
    }

    return 0;
}



4.5循环结构

​ 4.5.1结构浏览
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ 4.5.2打印数字

int main(){
    int num = 0;
    cout<<num<<endl;
    
    while(num<10){
        cout<<num<<endl;
        num++;
    }
    
    
    system("puase");
}

​ 4.5.3猜数字案例
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

#include <iostream>
#include <ctime>
#include <cstdlib>

int main() {
    srand(time(0));
    int random_num = rand() % 100 + 1;//rand() % 100生成一个0--99的树
    int guess;

    std::cout << "欢迎来到猜数字游戏!系统已生成一个1到100之间的随机数,请开始猜测:" << std::endl;

    while (true) {
        std::cin >> guess;
        if (guess > random_num) {
            std::cout << "猜大了,请重新输入:" << std::endl;
        } else if (guess < random_num) {
            std::cout << "猜小了,请重新输入:" << std::endl;
        } else {
            std::cout << "恭喜你猜对了!游戏结束。" << std::endl;
            break;
        }
    }

    return 0;
}

​ 4.5.4 do while 实现

#include <iostream>
#include <ctime>
#include <cstdlib>

int main() {
    srand(time(0));
    int random_num = rand() % 100 + 1;
    int guess;
    bool isCorrect = false;

    std::cout << "欢迎来到猜数字游戏!系统已生成一个1到100之间的随机数,请开始猜测:" << std::endl;

    do {
        std::cin >> guess;
        if (guess > random_num) {
            std::cout << "猜大了,请重新输入:" << std::endl;
        } else if (guess < random_num) {
            std::cout << "猜小了,请重新输入:" << std::endl;
        } else {
            std::cout << "恭喜你猜对了!游戏结束。" << std::endl;
            isCorrect = true;
        }
    } while (!isCorrect);

    return 0;
}

​ 4.5.5水仙花数的实现
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

解决办法:
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

#include <iostream>
#include <cmath>

bool is_narcissistic_number(int num) {
    int sum = 0;
    int temp = num;
    int n = std::to_string(num).length();

    while (temp > 0) {
        int digit = temp % 10;
        sum += std::pow(digit, n);
        temp /= 10;
    }

    return sum == num;
}

int main() {
    for (int i = 100; i <= 999; ++i) {
        if (is_narcissistic_number(i)) {
            std::cout << i << " 是水仙花数" << std::endl;
        }
    }

    return 0;
}

4.6 for循环

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

int main(){
    for(int i = 0;i<10;i++){
        cout<<"i = "<<i<<endl;
    }
    system("pause");
}

​ 案例:敲桌子
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ 思路:

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ 代码

#include <iostream>
using namespace std;

bool contains_seven(int num) {
    while (num > 0) {
        if (num % 10 == 7) {
            return true;
        }
        num /= 10;
    }
    return false;
}

bool is_multiple_of_seven(int num) {
    return num % 7 == 0;
}

int main() {
    int num;
    cout << "请输入一个数字: ";
    cin >> num;

    if (contains_seven(num) || is_multiple_of_seven(num)) {
        cout << "敲桌子" << endl;
    } else {
        cout << "不敲桌子" << endl;
    }

    return   0;
}

​ 嵌套循环
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ 代码实现:
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ 乘法口诀表
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

​ 代码实现:

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 9; ++i) {
        for (int j = 1; j <= i; ++j) {
            cout << j << " * " << i << " = " << i * j << "\t";
        }
        cout << endl;
    }

    return 0;
}

​ 结果:
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

4.7 跳转语句

break
【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 10; ++i) {
        if (i == 5) {
            break; // 当i等于5时跳出循环
        }
        cout << i << " ";
    }
    cout << endl;

    for (int i = 0; i < 10; ++i) {
        if (i % 2 == 0) {
            continue; // 当i是偶数时跳过本次循环
        }
        cout << i << " ";
    }
    cout << endl;

    return 0;
}

​ continue代码

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 10; ++i) {
        if (i % 2 == 0) {
            continue; // 当i是偶数时跳过本次循环
        }
        cout << i << " ";
    }
    cout << endl;

    return 0;
}

break和continue的区别:
			**break用于结束整个循环,而continue用于跳过当前迭代继续执行后续迭代**。

在编程中,breakcontinue都是控制流程的关键字,它们影响循环结构的行为。以下是两者的具体区别:

  • 作用范围:当在循环内部遇到break时,会立即退出最内层的循环,并开始执行循环之后的代码。而continue仅跳过当前的迭代,即本次循环剩余未执行的部分,然后继续进行下一次迭代。
  • 使用场景break通常用于在满足某个条件时要完全终止循环的情况。例如,你可能在一个搜索算法中找到目标值后使用break来停止搜索。continue则用于当某次迭代不满足特定条件时,希望略过此次迭代,直接进入下一次迭代的情况。比如,在遍历一个数组时,可能需要忽略某些特定的值。

总结来说,breakcontinue虽然都可以用来控制循环结构,但它们的主要区别在于break用于结束整个循环,而continue用于结束当前迭代并继续执行后续的迭代。

4.8 goto语句

C++中的goto语句是一种跳转语句,用于将程序的执行流程跳转到指定的标签位置。它的语法格式如下:

goto label;
...
label: statement;

其中,label是一个用户自定义的标签,可以是一个标识符或者一个数字。当程序执行到goto语句时,它会跳转到与该标签对应的位置,并从该位置开始执行后面的语句。需要注意的是,使用goto语句可能会导致代码结构混乱,不易于维护和调试,因此在实际编程中应尽量避免使用。

#include <iostream>
using namespace std;

int main() {
    int num = 0;
    start: // 标签
    num++;
    cout << "num: " << num << endl;
    if (num < 5) {
        goto start; // 跳转到start标签
    }
    return 0;
}

五 、数组

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

5.1 数组的定义

数组是一组具有相同数据类型的元素的集合,这些元素在内存中连续存放,并且每个元素都可以通过下标来访问

数组的定义通常包括以下几个要素:

  1. 数据类型:数组中每个元素的类型,如int、float、char等。
  2. 数组名:用于标识数组的变量名。
  3. 数组大小:可以是声明时指定的常量表达式,也可以是后续动态分配的大小。
  4. 初始化:可以在声明时对数组进行初始化,也可以在声明后对数组的元素逐一赋值。
  5. 访问:通过数组名和下标来访问或修改数组中的元素。

此外,在C语言中,数组的下标是从0开始的,所以如果有一个数组a,那么a[0]表示第一个元素,a[n-1]表示最后一个元素,其中n是数组的大小。

总的来说,数组是编程中非常基础且重要的概念,它不仅用于存储数据,还常常用于处理大量数据的情况,如数值计算、数据处理等场景。

5.2 数组的操作

数组的操作主要包括以下几个方面:

  1. 访问数组元素:通过下标来访问数组中的元素,例如a[0]表示第一个元素,a[n-1]表示最后一个元素。
  2. 修改数组元素:通过下标来修改数组中的元素,例如a[0] = 10将第一个元素的值改为10。
  3. 遍历数组元素:使用循环结构来遍历数组中的所有元素,例如使用for循环或while循环。
  4. 数组排序:对数组进行排序,可以使用内置的排序函数或者自定义的排序算法。
  5. 查找数组元素:在数组中查找指定的元素,可以使用线性查找、二分查找等方法。
  6. 动态数组:根据需要动态地增加或减少数组的大小,可以使用C语言中的realloc函数或者C++中的vector容器。
5.3 代码的实现

以下是使用C语言实现数组操作的示例代码:

#include <stdio.h>

int main() {
    // 定义一个整型数组,大小为5
    int a[5] = {1, 2, 3, 4, 5};

    // 访问数组元素
    printf("a[0] = %d", a[0]); // 输出第一个元素
    printf("a[4] = %d", a[4]); // 输出最后一个元素

    // 修改数组元素
    a[0] = 10;
    printf("a[0] = %d", a[0]); // 输出修改后的第一个元素

    // 遍历数组元素
    for (int i = 0; i < 5; i++) {
        printf("a[%d] = %d", i, a[i]);
    }

    // 动态增加数组大小
    int *b = (int *)realloc(a, 10 * sizeof(int));
    if (b != NULL) {
        a = b;
        for (int i = 5; i < 10; i++) {
            a[i] = i + 1;
        }
        printf("New array: ");
        for (int i = 0; i < 10; i++) {
            printf("%d ", a[i]);
        }
        printf("");
    } else {
        printf("Failed to resize the array.");
    }

    return 0;
}

以上代码演示了如何定义、访问、修改、遍历和动态调整数组的大小。其中,realloc函数用于重新分配内存空间,如果分配成功则返回指向新内存空间的指针,否则返回NULL。

5.4 一维数组的作用

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

一维数组是编程中非常基础且重要的概念,它的作用主要有以下几个方面:

  1. 存储数据:一维数组可以用于存储一组相同类型的数据,例如整数、浮点数、字符等。通过下标访问和修改数组元素,可以实现对数据的快速存取和修改。

  2. 处理大量数据:在处理大量数据时,使用一维数组可以有效地减少内存占用和提高程序运行效率。例如,在数值计算、数据处理、图像处理等领域,一维数组被广泛应用。

  3. 实现算法:许多算法需要使用到数组,例如排序算法、查找算法、动态规划等。一维数组作为这些算法的基础数据结构,能够方便地实现各种算法。

  4. 简化代码:使用一维数组可以将复杂的数据结构和操作抽象化,从而简化代码的编写和维护。例如,在字符串处理、矩阵运算等领域,一维数组可以大大简化代码的复杂度。

  5. 统计数组的大小

    以下是使用C语言实现统计数组大小的示例代码:

    #include <stdio.h>
    
    int main() {
        // 定义一个整型数组,大小为5
        int a[5] = {1, 2, 3, 4, 5};
    
        // 计算数组的大小
        int size = sizeof(a) / sizeof(a[0]);
        printf("The size of the array is %d
    ", size); // 输出数组的大小
    
        return 0;
    }
    

    以上代码中,sizeof(a)可以获取整个数组的字节数,而sizeof(a[0])可以获取数组中每个元素的字节数。将两者相除即可得到数组的大小。需要注意的是,这种方法只适用于在定义数组时已知数组大小的情况,如果数组是通过动态分配内存得到的,则需要使用其他方法来获取数组的大小。

  6. 案例一:

    1. 【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

    2. 解析:首先需要遍历数组,找到最大值,然后打印出来。

      代码如下:

      #include <iostream>
      using namespace std;
      
      int main() {
          int arr[5] = {300, 350, 200, 400, 250};
          int max_weight = arr[0];
      
          for (int i = 1; i < 5; i++) {
              if (arr[i] > max_weight) {
                  max_weight = arr[i];
              }
          }
      
          cout << "最重的小猪体重为:" << max_weight << endl;
          return 0;
      }
      
      
      
      

案例二:

【系统性】 循序渐进学C++,c++,开发语言,教程,总结复盘

代码实现:

​ 解析:首先需要声明一个5个元素的数组,然后通过循环将数组元素逆置。

代码如下:

#include <iostream>
using namespace std;

int main() {
    int arr[5] = {1, 3, 2, 5, 4};

    for (int i = 0; i < 5 / 2; i++) {
        int temp = arr[i];
        arr[i] = arr[4 - i];
        arr[4 - i] = temp;
    }

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

总之,一维数组是编程中非常重要的数据结构之一,它不仅能够存储和处理大量数据,还能够实现各种算法和简化代码。文章来源地址https://www.toymoban.com/news/detail-841095.html

第二阶段:核心

第三阶段:提高

到了这里,关于【系统性】 循序渐进学C++的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包