这篇文章上次修改于 1036 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

C++ 第一天

day1.cpp

#include <iostream>;
#include <ctime>;
#include "requests.h";
#include "func.h"

using namespace requests;
using namespace std;


// 结构体
struct Student
{
    string name;
    int age;
};

struct cnet
{
    string url;
    int timeout;
};

struct people
{
    string name;
    int age;
};

struct country
{
    string name;
    people p;
};



int main() {
    // ☆数据类型
    // 整数
    /*short a = 32768;
    int b = 32768;
    long c = 32768;
    long long d = 32768;*/

    // 小数
    /*float f1 = 3e2;  
    float f2 = 3e-2;  
    float f3 = 3.1412345f;
    cout << f3;
    double e = 32768;*/

    // 字符
    /*char g = '1';
    cout << g << endl;
    cout << (int)g << endl;
    cout << a << endl;
    cout << sizeof(a) << endl;

    cout << b << endl;
    cout << sizeof(b) << endl;
    
    cout << c << endl;
    cout << sizeof(c) << endl;
    
    cout << d << endl;
    cout << sizeof(d) << endl;*/

    // 字符串
    /*char str0[] = "hello doone";
    string str1 = "hello world";
    cout << str0 << endl;
    cout << str1 << endl;*/

    // 布尔
    /*bool a = TRUE;
    bool b = FALSE;
    cout << a << endl;
    cout << b << endl;
    bool a = 1;
    cin >> a;
    cout << a << endl;*/

    // 输入
    /*string a = "";
    cout << "input:";
    cin >> a;
    cout << a << endl;*/

    // 运算
    /*int num1 = 9;
    int num2 = 2;

    cout << num1 + num2 << endl;
    cout << num1 - num2 << endl;
    cout << num1 * num2 << endl;
    cout << num1 / num2 << endl;
    cout << num1 % num2 << endl;*/


    // ☆流程结构
    // if 
    /*int a = 0;
    int b = 0;
    cout << "请输入第一个数:";
    cin >> a;
    cout << "请输入第二个数:";
    cin >> b;
    if (a > b) {
        cout << "第一个数大于第二个数";
    }
    else if (a == b){
        cout << "第一个数等于第二个数";
    }    
    else
    {
        cout << "第一个数小于第二个数";
    }*/

    // 三目运算符
    /*int a = 0;
    int b = 2;
    int c = 3;

    cout << (a ? b : c) << endl;*/

    // switch
    /*int a = 0;
    switch (a)
    {
    case 1:
        cout << "星期一" << endl;
        break;
    case 2:
        cout << "星期二" << endl;
        break;
    case 3:
        cout << "星期三" << endl;
        break;
    case 4:
        cout << "星期四" << endl;
        break;
    case 5:
        cout << "星期五" << endl;
        break;
    case 6:
        cout << "星期六" << endl;
        break;
    case 7:
        cout << "星期七" << endl; 
        break;
    default: 
        cout << "ERROR!" << endl;
        break;
    }*/

    // while
    /*int a = 1;
    while (a <= 10)
    {
        cout << a << endl;
        a++;
    }*/

    // 随机数
    /*srand((unsigned int)time(NULL));
    int a = rand() % 100;
    cout << a << endl;*/

    // do-while
    /*int a = 1;
    do
    {
        cout << a << endl;
        a++;
    } 
    while (a <= 10);*/

    // for
    /*int a = 1;
    for (; a <= 1; a++)
    {
        cout << a << endl;
    }
    cout << a << endl;*/

    // exp:乘法口诀表
    /*for (int i = 1; i <= 9; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            cout << j << "*" << i << "=" << i * j << " ";
        }
        cout << endl;
    }*/

    // goto 
    /*cout << 0 << endl;
    cout << 1 << endl;
    cout << 2 << endl;
    goto FLAG;

    cout << 3 << endl;

    FLAG:
    cout << 4 << endl;
    cout << 5 << endl;*/

    // 数组
    /*int arr1[] = { 0, 1, 2, 3, 4, 5, 6 };
    cout << arr1[0] << endl;
    cout << arr1[1] << endl;
    cout << arr1[2] << endl;
    cout << arr1[3] << endl;
    cout << arr1[4] << endl;
    cout << arr1[5] << endl;
    cout << arr1[6] << endl;
    cout << endl;
    cout << sizeof(arr1) << endl;
    cout << sizeof(arr1[0]) << endl;
    cout << (long)&arr1[0] << endl;*/

    // exp:寻找最大的数
    /*int arr1[] = { 0, 100, 2, 3, 4, 5, 600, 1000 };
    cout << sizeof(arr1) << endl;
    cout << sizeof(arr1[0]) << endl;

    int r = find_max(arr1, sizeof(arr1) / sizeof(arr1[0]));
    cout << "最大值为" << r << endl;*/
    
    // exp:逆置
    /*int arr1[] = { 0, 100, 2, 3, 4, 5, 600, 1000 };
    reverse(arr1, sizeof(arr1) / sizeof(arr1[0]));
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
        cout << arr1[i] << endl;
    }*/

    // exp:冒泡排序
    /*int arr1[] = { 6, 5, 4, 3, 2, 1 };
    sort(arr1, sizeof(arr1) / sizeof(arr1[0]));
    for (int i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++) {
        cout << arr1[i] << endl;
    }*/

    // 二维数组
    /*long long arr2[2][3] = {
        {648648, 2, 3},
        {4, 5, 6}
    };

    cout << sizeof(arr2) << endl;
    cout << sizeof(arr2[0]) << endl;
    cout << sizeof(arr2[0][0]) << endl;

    cout << sizeof(arr2) / sizeof(arr2[0]) << endl;
    cout << sizeof(arr2[0]) / sizeof(arr2[0][0]) << endl;

    cout << &arr2 << endl;
    cout << arr2 << endl;
    cout << arr2[0] << endl;
    cout << arr2[0][0] << endl;
    cout << arr2 << endl;
    cout << (long long) & arr2[0][0] << endl;
    cin >> arr2[0][0];*/

    // 指针
    /*int a = 648;
    int* b = &a;
    int* c = NULL;

    cout << &a << endl;
    cout << b << endl;
    cout << &b << endl;
    cout << sizeof(b) << endl;
    cout << sizeof(&b) << endl;*/

    // 常量指针 可改指向但是不可以改内存中的值
    /*int a = 1;
    const int* b = &a;
    int c = 2;
    b = &c;*/

    // 指针常量 指向不可改但是可以改内存中的值
    /*int a = 1;
    int* const b = &a;
    *b = 2;*/

    // 修饰指针
    /*int a = 1;
    const int* const b = &a;*/

    // 结构体
    /*Student doone;
    Student jack = {"123", 23};
    doone.name = "王大鹏";
    doone.age = 22;
    cout << doone.name << endl;
    cout << doone.age << endl;
    cout << jack.name << endl;
    cout << jack.age << endl;*/

    // 结构体数组
    /*Student arr[] = {
        {"Doone", 22},
        {"Jack", 23},
    };
    cout << arr[0].name << endl;
    cout << arr[0].age << endl;
    cout << arr[1].name << endl;
    cout << arr[1].age << endl;*/

    // 结构体指针
    /*cnet a = { "halee.cc", 5 };
    cnet* b = &a;
    cout << a.url << " " << a.timeout << endl;
    cout << b->url << " " << b->timeout << endl;*/

    // 结构体嵌套
    country cn;
    cn.name = "China";
    cn.p.name = "Doone";
    cn.p.age = 22;
    country* cn1 = &cn;
    cout << cn.name << " " << cn.p.name << " " << cn.p.age << endl;
    cout << cn1->name << " " << cn1->p.name << " " << cn1->p.age << endl;
    cout << (*cn1).name << " " << (*cn1).p.name << " " << (*cn1).p.age << endl;

    // requests
    /*Response res = Get("https://halee.cc");
    cout << res.status << endl;
    cout << res.GetText() << endl;*/
    
    system("pause");
    return 0;
}

func.h

#include <iostream>;

// 头文件写函数声明
int find_max(int* a, int len);

void reverse(int* a, int len);

void sort(int* a, int len);

func.cpp

#include "func.h"

// 源文件写函数定义

// 寻找最大的数  int* a也可以写成int a[]
int find_max(int* a, int len) {
    int res = 0;
    for (int i = 0; i < len; i++) {
        if (a[i] > res) {
            res = a[i];
        }
    }
    return res;
}
// 倒置
void reverse(int* a, int len) {
    int start = 0;
    int end = len - 1;
    while (start < end)
    {
        int temp = a[start];
        a[start] = a[end];
        a[end] = temp;
        start++;
        end--;
    }
}

// 从小到大排列
void sort(int* a, int len) {
    for (int j = 0; j < len - 1; j++) {
        for (int i = 0; i < len - 1 - j; i++) {
            if (a[i] > a[i + 1]) {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
            }
        }
    }
}

C++第二天

day2.cpp


#include <iostream>
#include <ctime>
#include<windows.h>
#include "func.h"
using namespace std;

struct student
{
    string sname;
    int age = NULL;
};

struct teacher {
    string tname;
    struct student sarr[5];
};

struct hero {
    string name;
    int age;
    string sex;
};

// 结构体赋值
void allocatespace(struct teacher* tarr, int t_len, int s_len) {
    string nameseed = "abcdefg";
    for (int i = 0; i < t_len; i++) {
        tarr[i].tname = "teacher_";
        tarr[i].tname += nameseed[i];
        for (int j = 0; j < s_len; j++) {
            tarr[i].sarr[j].sname = "student_";
            tarr[i].sarr[j].sname += nameseed[j];
            tarr[i].sarr[j].age = rand() % 61 + 40;
        }
    }
}

void printinfo(struct teacher* tarr, int t_len, int s_len) {
    for (int i = 0; i < t_len; i++) {
        cout << "老师:" << tarr[i].tname << endl;
        for (int j = 0; j < s_len; j++) {
            cout << "\t姓名:";
            cout << tarr[i].sarr[j].sname << " \t年龄:" << tarr[i].sarr[j].age << endl;
        }
    }
}

//冒泡排序
void bubblesort(struct hero* harr, int len) {
    for (int i = 0; i < len - 1; i++) {
        for (int j = 0; j < len - i - 1; j++) {
            if (harr[j].age > harr[j + 1].age) {
                hero temp = harr[j];
                harr[j] = harr[j + 1];
                harr[j + 1] = temp;
            }
        }
    }
}

void printhero(struct hero* harr, int len) {
    for (int i = 0; i < len; i++) {
        cout << "姓名:" << harr[i].name << "\t年龄:" << harr[i].age << " 性别:" << harr[i].sex << endl;
    }
}

int main()
{
    srand((unsigned int)time(NULL));
    // 结构体案例1
    /*teacher tarr[3];
    allocatespace(tarr, sizeof(tarr) / sizeof(tarr[0]), 5);
    printinfo(tarr, sizeof(tarr) / sizeof(tarr[0]), 5);*/

    // 结构体案例2
    /*hero harr[] = {
        {"a", rand() % 61 + 18, "男"},
        {"b", rand() % 61 + 18, "女"},
        {"c", rand() % 61 + 18, "女"},
        {"d", rand() % 61 + 18, "男"},
        {"e", rand() % 61 + 18, "男"},
    };
    printhero(harr, sizeof(harr) / sizeof(harr[0]));
    bubblesort(harr, sizeof(harr) / sizeof(harr[0]));
    cout << endl;
    printhero(harr, sizeof(harr) / sizeof(harr[0]));*/

    // nvidia-smi,这里会报错
    for (;;) {
        system("nvidia-smi");
        Sleep(1000);
        system("cls");
    }
    // 引用
    /*int a = 10;
    int& b = a;
    b = 20;
    cout << a << endl;
    cout << b << endl;*/

    // 常量引用
    /*const int& a = 10;
    cout << a << endl;*/

    system("pause");
    return 20230201;
}