分类: C++

  • #C++ Day5 October 11 2025


    //5-1-1 math symbols


    #include <iostream>


    using namespace std;



    int main() {


    int a = 1;


    int b = 3;


    cout << a+b << endl;


    cout << a – b << endl;


    cout << a * b << endl;


    cout << a / b << endl; //if int divide int, there will be int too


    cout << a*1.0 / b << endl;//if int multiple float before divide, there will be float



    a = 10000000;


    b = 100000;


    cout << a * b << endl; //output will out the range of int


    cout << (long long)a * b << endl; //we can translate it forcely



    a = -1;


    b = 2;


    cout << a / b << endl;//just cut of the dot number



    a = +1;


    b = -a;


    int c = -(-a);


    cout << a <<‘ ‘ << b <<‘ ‘ << c << endl;//postive & negative



    char A = ‘A’;


    A = A + 1;


    A = A + 24;



    cout << A << endl;



    return 0;


    }




    //5-1-2 mod % symbol


    #include <iostream>


    using namespace std;



    int main(){


    int a = 100;


    int b = 9;


    cout << a % b << endl; //1



    a = 100;


    b = -9;


    cout << a % b << endl; //1



    a = -100;


    b = 9;


    cout << a % b << endl;//the mod symbol %;-100 % 9 is  9*-11 (near 0 direction) not -12 in C/C++ launguage


    //-1





    a = -100;


    b = -9;


    cout << a % b << endl; //-100 -(-9)*11=-1


    //-1



    //1、the symbol of mod is the same as the be devided number


    //first we should take the negative symbol from the  be devided number 


    //second we use it as 100 = -9 * (-11) + 1 


    //third the left +1 is the lease number and add the negative symbol – to the 1


    //fourth the final answer is  -1


    return 0;


    }




    //5 – 1-3 ++ & –symbol


    #include <iostream>


    using namespace std;



    int main() {


    int a = 6; 


    a++; //a=a+1


    cout << a << endl;


    ++a; //a=a+1


    cout << a << endl;


    //the same in the two ways



    int j = 8;


    int x = a++; //first give the value to x, then add 1 to a


    int y = ++j; //first add 1 to the value , then give the value to y (this type is more effective)


    cout << x << endl; 


    cout << y << endl;



    int z = (a++) + (++a);//9+11=20 we must not to write this type code ,because it will make a big problem


    cout << z << endl;



    a–; //a=a-1


    cout << a << endl;


    –a;//a=a-1


    cout << a << endl; 


    }



    //5 – 2 valued symbols


    #include <iostream>


    using namespace std;



    int main() {


    int x = 9;


    int y = 6;


    x = y; // let y to x


    cout << x << endl;



    x += y; //x=x+y   the length is 10 bytes to 7 bytes


    cout << x << endl;



    x -= y; //x=x-y


    cout << x << endl;



    x *= y; //x=x*y


    cout << x << endl;



    x /= y; //x=x/y


    cout << x << endl;



    x %= y; //x=x%y


    cout << x << endl;


    return 0;


    }






  • #C++ Day4 Sep 14 2025

    #include <iostream>

    using namespace std;

    int main() {

    char a = ‘y’; //use the single quotes can be recognized the char type

    char b = ‘z’;

    //char a =”y”;//using double quotes can be recognized as a lot of string

    cout << a << endl;

    cout << int(a) << endl; //cause 1 byte can be 1*2^8=256 ,so that can be include ascii symbol

    //cout << b – a << endl;

    b = 121;

    cout << b << endl;

    cout << sizeof(a) << endl;

    cout << sizeof(char) << endl;

    return 0;

    }

    Day4 October 10 2025

    //4-5 translate words

    #include <iostream>

    using namespace std;

    int main() {

    char a = ‘\a’;

    char n = ‘\n’; //ascii = 7 , which is \a and would make a beep sound

    cout << int(a) << endl;

    cout << “Vito \”Al\”\\gorith\088m Union\tV\0ito” << n; // \n as a key words change the line;\t is tab ;\\can output a \;\0 will cut out;

    //\” can output the ” normally

    return 0;

    }

    /*

    \0xx is a octal(8 position rule)

    \088 would not recognized a real function

    \077 is a ? symbol

    */

    //4-6 string

    #include <iostream>

    #include <string>

    using namespace std;

    int main() {

    char a[] = “维克托算法联盟”; //the end of the string will be a ‘\0’, which made it as the string for the system recognizing 

    //the GBK UTF-8 can change the encode ways 

    //this is the C style string

    cout << sizeof(a) << endl;

    cout << a << endl;

    string b = “夜深人静看算法”;//this is C++ style string 

    cout << b +”vito” << endl; // import the string class can add another string easily 

    return 0;

    }

    //4-7 type bool

    #include <iostream>

    using namespace std;

    // bool is true or flase

    int main() {

    bool flag1 = false;

    bool flag2 = true;

    cout << flag1 << endl << flag2 << endl;

    cout << sizeof(bool) << endl;

    flag1 = !flag1;

    cout << flag1 << endl << flag2 << endl;

    cout << sizeof(flag1) << endl;//bool’s size is 1

    int flag3 = 0;

    cout << sizeof(flag3) << endl; // int=byte, which size is 4

    return 0;

    }

    //4-8 input

    #include <iostream>

    #include <string>

    //i=input

    //o = output

    using namespace std;

    int main() {

    //1.int’s input

    int a = 5;

    cin >> a; // the right used to input into some variables

    cout << “a turned to :” << a << endl;

    //2.float’s input

    double b = 5;

    cin >> b; // the right used to input into some variables

    cout << “b turned to :” << b << endl;

    //3.char’s input

    char c = 5;

    cin >> c; // the right used to input into some variables

    cout << “c turned to :” << c << endl;

    //4.string’s input

    string d = “”;

    cin >> d; // the right used to input into some variables

    cout << “d turned to :” << d << endl;

    //5.bool’s input

    bool e = false;

    cin >> e; // the right used to input into some variables

    cout << “e turned to :” << e << endl;

    return 0;

    //all the input must be legal

    }

  • #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;

    }