分类: C++

  • #C++ Day3 Sep 11 2025

    #include <iostream>

    using namespace std;

    int main()

    // short 2字节     00       2 ^16   [-2 ^ 15 ~2 ^ 15 – 1]  [-32768~32767]

    // int 4字节       0000     2 ^ 16  [-2 ^ 31 ~2 ^ 31 – 1] [-2147483648~2147483647]

    // long 4字节 8字节 0000     2 ^ 16  [-2 ^ 31 ~2 ^ 31 – 1]

    // long long 8字节 00000000  2 ^ 16 [-2 ^ 63 ~2 ^ 63 – 1]

    //[l,r]

    // range:r-l+1

    //make it close to the range can save the space ,so watch out the range

    {

        short a = 32768; // will be get out of the range,and turn to the loop’s start -32768

        int b = 2147483648;

        long c = 1;

        long long d = 1;

        cout << “a=” << a << endl;

        a = 32769;

        cout << “a2=” << a << endl; // show out -32767

        cout << “b=” << b << endl;

        cout << “c=” << c << endl;

        cout << “d=” << d << endl;

        return 0;

    }

    //new reserver symbol sizeof()

    #include <iostream>

    using namespace std;

    int main()

    {

        short a = 1;

        int b = 1;

        long c = 1;

        long long d = 1;

        cout << sizeof(short) << endl;

        cout << sizeof(int) << endl;

        cout << sizeof(long) << endl;

        cout << sizeof(long long) << endl;

        cout << sizeof(a) << endl;

        cout << sizeof(b) << endl;

        cout << sizeof(c) << endl;

        cout << sizeof(d) << endl;

        return 0;

    }

    #include <iostream>

    #include <iomanip>

    #include <cmath>

    using namespace std;

    int main()

    #define eps 1e-7

    // float 0000 4bytes

    /// double 00000000 8bytes

    // if you want to solve the question,must use double ,or you will miss the acuracy

    // to judge the accuration of float, please make it minus in a small range

    {

        float a = 13.1415926f;

        double b = 13.1415926;

        cout << setprecision(10) << a << endl;

        cout << setprecision(10) << b << endl;

        a = 13.1415926f;

        b = 13.141592611111111111;

        cout << setprecision(10) << a << endl;

        cout << setprecision(30) << b << endl; // if you make it out of range ,can still display the wrong number

        cout << sizeof(a) << endl;

        cout << sizeof(b) << endl;

        double c = 1.5e5; // 1.5*10^5

        cout << c << endl;

        double d = 1.5e-5; // 1.5*10^-5

        cout << d << endl;

        double x = 1.0 / 123123111123123123 * 123123111123123123; // when the float is large enough,must will have the error

        cout << x << endl;

        if (fabs(x – 1) < 0.00000001)

        {

            cout << “one two” << endl;

        }

        if (fabs(x – 1) < eps)

        {

            cout << “one two” << endl;

        }

        return 0;

    }

  • 补码

    补码是计算机表示有符号整数的一种方式,核心作用是把 “减法” 变成 “加法”,让计算机只用 “加法器” 就能处理加减运算。下面用最通俗的方式解释:

    一、为什么需要补码?

    计算机里用二进制存数,有符号数的最高位是符号位0 表示正数,1 表示负数)。如果直接用 “符号位 + 原码”(比如 +3 是 0011-3 是 1011),做减法会很麻烦(比如 3-2 要专门设计减法逻辑)。

    补码的巧妙之处:用 “加法” 代替 “减法”。比如 3-2 可以变成 3 + (-2),只要给 -2 一个特殊的 “补码”,让加法的结果等价于减法,计算机就只需 “加法器” 即可。

    二、补码的计算规则

    • 正数的补码:和原码完全相同
      例:+5 的原码是 00000101(8 位),补码也是 00000101
    • 负数的补码:分两步(或用 “模” 理解):
      • 方法 1(步骤法):原码的符号位不变数值位取反,然后加 1
        例:求 -5 的补码:
        ① 原码:10000101(符号位 1,数值位 0000101);
        ② 数值位取反:1111010
        ③ 加 1:1111011
        ④ 最终补码:11111011(符号位保留 1)。
      • 方法 2(模的概念):补码 = 模 – 数的绝对值(更直观)。
        对于 8 位二进制,“模” 是 2⁸ = 256(因为 8 位最多表示 256 个数)。
        所以 -5 的补码 = 256 - 5 = 251,二进制是 11111011(和步骤法结果一致)。

    三、补码如何让 “减法变加法”?

    以 3-2 为例,等价于 3 + (-2)

    • 3 的补码:00000011(正数,补码 = 原码);
    • -2 的补码:用 “模 256 – 2” 得 254,二进制 11111110
    • 两者相加:00000011 + 11111110 = 100000001
    • 因为是 8 位,最高位的 1 会 “溢出”(超出 8 位范围),最终结果取低 8 位 00000001,即 1,和 3-2=1 一致。

    四、特殊情况:-128(8 位补码)

    8 位补码的范围是 -128 ~ 127。其中 -128 比较特殊:它没有 “原码”(因为原码表示范围是 -127 ~ 127),但补码可以表示为 10000000(用 “模 256 – 128 = 128”,二进制 10000000)。

    总结

    补码的核心是用加法代替减法,让计算机硬件更简单。记住:

    • 正数补码 = 原码;
    • 负数补码 = 模 – 绝对值(或 “符号位不变,数值位取反加 1”);
    • 计算时,直接用补码相加,结果自然等价于原数的加减。
  • #C++ Day2 Sep 10 2025

    #include <iostream>

    using namespace std;

    #define X “Vito写算法”

    #define Y “Deep Night”

    int main() {

    cout <<X <<endl <<Y<<endl ;

    //define constant can substitute sth

    }

    #include <iostream>

    using namespace std;

    #define X “Vito写算法”

    #define Y “Deep Night”

    #define Z 1+2

    #define G (1+2)

    const int x = 8;

    int main() {

    cout <<X <<endl <<Y<<endl ;

    cout << Z*Z<<endl; //would be 5,cause 1+2*1+2=5

    cout << G * G<< endl; // would be 9,cause you add the ()

    //x = (x + 1); //if add up the const ,this x can’t be modify

    //c++ is  upper and lower case sensitive 

    cout << x << endl;

    }

    #include <iostream>

    using namespace std;

    int main(){

    //int float;//it is wrong because you can’t use the reserve key as the symbol

    int a = 5;

    if (a == 6) {

    return -1;

    }

    return -2;

    }

    #include <iostream>

    using namespace std;

    int main() {

    int a12_ = 520;

    //int 9b_ = 22; //number can’t be the start of the literal

    int a = 5;

    int A = 8;

    cout << a << endl << A <<endl;

    int appleCount = 0;

    int chuangshijiejing = 0; //pinyin can be normal if you are in the chinese group

    #include <iostream>

    using namespace std;

    int main() {

    int a12_ = 520;

    //int 9b_ = 22; //number can’t be the start of the literal

    int a = 5;

    int A = 8;

    cout << a << endl << A <<endl;

    int appleCount = 0;

    int chuangshijiejing = 0; //pinyin can be normal if you are in the chinese group

    //variety Literal of C++

    /*1.case sensitive

    2.can not be started with number

    3.can not be the system reserve literal

    4.you can use word,number,_ as the variety Literal*/

    }

  • #C++ Day1 Sep 7 2025

    1. Leant how to install the environment 
    2. The dream is to code a game for me
    3. Found the reason of headache:fit the brain 
    4. Not the copy&paste, if you write more, you remember more
    5. Try to answer the question  by yourself, do not to ask other people the first time

    #include <iostream>

    using namespace std;

    int main()

    {

    cout << “Vito Algorithm” << endl <<endl <<endl; //endl is change the line

    return -12;

    }

    #include <iostream>

    //using namespace std;

    int main()

    {

    std::cout << “Vito Algorithm” << std::endl << std::endl << std::endl; //endl is change the line

    // i can use std:: before the cout and endl, so that i don’t need to script namespace

    //the namespace, which is used to divide the different codes that uses the same variety name 

    /*

    multiple notes

    */

    return -12;

    }

    #include <iostream>

    using namespace std;

    int main()

    {

    //std::cout << “Vito Algorithm” << std::endl << std::endl << std::endl; //endl is change the line

    //the definition of the variety:type name =origin value;

    //please give the variaty a origin value, if you didn’t do it, you are making errors when you are cout

    int a = 520;

    int b;

    cout << a << endl;

    cout << b << endl;

    return 0;

    }