• C++习题2


    赋值运算符<逻辑运算符<关系运算符<算术运算符

    指针调用数组元素 

    1. 指针调用数组元素
    2. int a[3][4] = {{1,4,7,10},{2,5,8,11},{3,6,9,12}};
    3. int* pa[3] = { a[0],a[1],a[2] };
    4. int(*pb)[4]; pb = a;
    5. int** q = pa;
    6. cout << *(a[2] + 3)<
    7. cout << *(*(pa + 2) + 3)<
    8. cout << pb[2][3]<
    9. cout << *(pb + 2)[3] << endl;//错误
    10. cout << *(*(pb+2)+3) << endl;
    11. cout << q[2][3]<
    12. if(x%2)中x的性质
    13. int x = -5;//表示奇数
    14. if (x % 2) {
    15. x = 1;
    16. }
    17. cout << x;
    18. cin.getline()的用法
    19. char a[5];
    20. cin.getline(a, 5);//geiline最多读取4个元素,末尾补\0
    21. cout << a;
    22. 函数参数
    23. 函数不一定有形参,可以为void
    24. 如果有形参,可以是常量,变量,也可以是表达式
    25. 数组名作函数参数
    26. void sz(char a[]);
    27. char a[] = "abcd";
    28. / sz(a);
    29. #1:实参是常量
    30. res = my_min(1, 2)
    31. #2:实参是变量
    32. a = 1
    33. b = 2
    34. res = my_min(a, b)
    35. #3:实参是表达式
    36. res = my_min(10 * 2, 10 * my_min(3, 4))
    37. #4:实参可以是常量、变量、表达式的任意组合
    38. a = 2
    39. my_min(1, a, 10 * my_min(3, 4))
    1. int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
    2. int* p = a + 3;
    3. cout << p[5];
    4. 结果为9
    5. p的地址为a[3]的地址,p[5]即(p+5)为a[8]的地址
    6. 故结果为9
    7. int x = 0, s = 0;
    8. while (!x != 0)
    9. {
    10. s += ++x;
    11. }
    12. cout << s;
    13. s 的值为1 条件循环的意思是x==0

    7f676c17ed9d4abb88f24d2e6eaebc00.png  

    #不能参与构成标识符   标识符只能是数字,字母,下划线组成,开头不能是数字

    1. char str[] = "mess\15\t\\efghi\n";
    2. cout << sizeof(str);

     结果为14  mess 4个字节  efghi 5个字节  \15 \t  \\ \n各一字节 注意末尾的\0

    不主动设置大小,数组会自动在末尾补\0 

    1. //要输出Anhui HFUT
    2. char str1[80] = "Anhui province";
    3. char str2[80] = "my HFUT";
    4. //strcpy(str1+6, str2+3);
    5. strcpy(str1 + 5, str2 + 2);
    6. cout << str1;

     strcpy(str1 + 5, str2 + 2)

    str1 + 5表示str1[5]的地址  str2+2表示str2[2]的地址

    将str2[2]及以后的内容复制到str1[5]及其以后

    也可以用注释的第二种方法

    d4309ad7c7f641bbb33f5eda9df4a1ef.png

    声明函数指针:   数据类型(*函数指针名)(形参表)

    指向一个函数,调用函数

    利用定义的指针p,给如下定义的结构类型动态分配(用new运算符)20个struct 对象并使用 

    1. struct student {
    2. char name[5];
    3. int ID;
    4. float grade;
    5. };
    6. struct student* p;
    7. int main(void) {
    8. p = new struct student [2];
    9. if (p == NULL)
    10. {
    11. exit(1);
    12. }
    13. int i = 0;
    14. for (i = 0; i < 2; i++)
    15. {
    16. cin >> p[i].grade >> p[i].ID;
    17. cout << endl;
    18. cin.getline(p[i].name, 5);
    19. }
    20. cout << p[1].name<1].grade<1].ID;
    21. delete[]p;
    22. p = NULL;
    23. return 0;
    24. }

    int a = 1, b = 0, c;
        c = a - 1 && ++b;
        cout << a << "," << b << "," << c;

    结果是1,0,0 a的值不会变 当a-1为假,条件判断立即结束,++b不再执行

    ifstream inFile;

    inFile.open("s.txt",ios::in)以只读的方式打开文件s.txt

     

    算法的特征:有穷性、确定性、输入性、输出性、可行性

    cout << 0x13 && 0x17;  结果是19,也就是16进制的0x13

    e95bd02e8b624cea8a69c7069260d2c5.jpg

    cin.get只读一个字符

     

     488342590e744fa9ac53870f4b932c85.jpg

     p=new student[20]

     

     

     

     

  • 相关阅读:
    Commons Collections1
    Windows cmd窗口常用命令
    adb调试系统app
    基于verilog的CRC校验(汇总)
    JDBC-Statement+ResultSet介绍
    商城多语言改造方案
    hiveSql冷门但好用函数 --持续更新
    息肉分割(Polyp Segmentation)方向常用数据集汇总
    自定义md-loader来简单高效的维护组件文档
    Vim基本配置&快捷键&常用命令
  • 原文地址:https://blog.csdn.net/qq_35660654/article/details/128109210