• 20240405,数据类型,运算符,程序流程结构


    是我深夜爆炸,不能再去补救C了,真的来不及了,不能再三天打鱼两天晒网了,真的来不及了呜呜呜呜

    我实在是不知道看什么课,那黑马吧……MOOC的北邮的C++正在进行呜呜

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. cout << "hallo world" << endl;
    6. system("pause");
    7. return 0;
    8. }
    1.1  变量

    定义变量:数据类型  变量名称 = 变量初始值【格式】

    1.2  常量  不可修改

    1,#define 宏常量,#define 常量名  常量值
    2,const  修饰的变量  ,const  数据类型 常量名=常量值

    1. #include <iostream>
    2. #define DAY 7
    3. using namespace std;
    4. int main()
    5. {
    6. const int mouth = 31;
    7. cout << "hallo world,一周"<<DAY<<"天,大月"<<mouth << endl;
    8. system("pause");
    9. return 0;
    10. }
    1.3  关键字

    标识符命名规则:非关键字,字母+数字+下划线,首位非数字,大小写
    建议,最好能够见名知意

    二,数据类型

    2.1  整型

    short ==2 【-32768~32767】;int ==4;long==4(分那啥);long long==8;
    sizeof( )

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. short num1 = 32768;
    6. int num2 = 32768;
    7. long num3 = 0;
    8. long long num4 = 0;
    9. cout << num1 << "——"<<sizeof(short)<<endl;
    10. cout << num2 << "——" << sizeof(int) << endl;
    11. cout << num3 << "——" << sizeof(long) << endl;
    12. cout << num4 << "——" << sizeof(long long) << endl;
    13. system("pause");
    14. return 0;
    15. }
    2.2  实型(浮点型

    float 单精度,7有效数字,数值后面加上F表示类型;double,双。。,15-16位
    默认输出6位小数,科学计数法

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. float fnum1 = 3.15344534f;//加后缀自动识别FLOAT,否则DOUBLE
    6. double dnum2 = 4.4335363748456345234232;
    7. float num3 = 3e2;
    8. float num4 = 3e-2;
    9. cout << fnum1 << "——"<<sizeof(float)<<endl;
    10. cout << dnum2 << "——" << sizeof(double) << endl;
    11. cout << num3 << endl;
    12. cout << num4 << endl;
    13. system("pause");
    14. return 0;
    15. }
     2.3  字符型

    CHAR  变量名 =‘单个字符’,大小1字节,ASCII码,a-97,A-67,0-31控制字符,32-126打印字符

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. char a = 'a';
    6. cout << a << "——"<<sizeof(char)<<endl;
    7. cout << a << "——" << (int)a << endl;
    8. system("pause");
    9. return 0;
    10. }
    2.4 转义字符

    水平制表符——对齐、整齐输出,换页和垂直制表感觉和换行差不多

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. cout << "aaa\abbb" << endl;
    6. cout << "aaa\bbb" << endl;
    7. cout << "aaa\f换页bbb" << endl;
    8. cout << "aaa\nbbb" << endl;
    9. cout << "aaa\tbbb" << endl;
    10. cout << "a\tbbb" << endl;
    11. cout << "aaa\vbbb\v垂直制表" << endl;
    12. cout << "aaa\vbbb\v垂直制表" << endl;
    13. cout << "aaa\\bbb" << endl;
    14. cout << "aaa\'bbb" << endl;
    15. cout << "aaa\"bbb" << endl;
    16. cout << "aaa\?bbb" << endl;
    17. return 0;
    18. system("pause");
    19. }
    2.5 字符串型

    1,C风格字符串:CHAR 变量名【】=“字符串值”;CHAR A='A'字符,CHAR A[ ]="A"字符串
    2,C++风格字符串:STRING 变量名=“字符串值”;;包含头文件#include

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. int main()
    5. {
    6. char a[] = "hallo word?";
    7. string b = "ni hao,xiexie";
    8. cout << a << endl;
    9. cout << b << endl;
    10. return 0;
    11. system("pause");
    12. }
    2.6 布尔类型BOOL

    true--1,false--0,sizeof(bool)==1;赋值给数字,除了0都代表真

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. bool flag = true;
    6. cout << flag << endl;
    7. flag = false;
    8. cout << flag << endl;
    9. cout << sizeof(bool) << endl;//1
    10. return 0;
    11. system("pause");
    12. }
    2.7 数据输入
    1. #include <iostream>
    2. #include<string>
    3. using namespace std;
    4. int main()
    5. {
    6. //int
    7. int ant = 23;
    8. cout << ant << endl;
    9. cin >> ant;
    10. cout << ant << endl;
    11. //float
    12. float ff = 5.8900f;
    13. cout << ff << endl;//输出抹零了
    14. cin >> ff;
    15. cout << ff << endl;
    16. //char
    17. char ch = 'a';
    18. cout << ch << endl;
    19. cin >> ch;
    20. cout << ch << endl;
    21. //string
    22. string b= "qunidsefw";
    23. cout << b << endl;
    24. cin >> b;
    25. cout << b << endl;
    26. //bool
    27. bool flag = false;
    28. cout << flag << endl;//除了0,输入啥都是1
    29. cin >> flag;
    30. cout << flag << endl;
    31. return 0;
    32. system("pause");
    33. }

    三,运算符

    3.1 算数运算符

    +,-,*,/,%【小数和小数不能做取余运算】,++A,A++,--A,A--,同C

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. int a = 2;
    6. int b = a++;
    7. int c = ++a;
    8. cout << a << "\t" << b << "\t" << c << endl;
    9. cout << c % a << endl;
    10. cout << a++ * 100 << endl;
    11. cout << ++a * 100 << endl;
    12. return 0;
    13. system("pause");
    14. }
    3.2 赋值运算符

    +=,-=,*=,/=,=,%=

    3.3 比较运算符

    ==,!=,<,>,<=,>=

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. int a = 2;
    6. int b = ++a;
    7. cout << (a>b)<< endl;
    8. cout << (a < b) << endl;
    9. cout << (a != b) << endl;
    10. cout << (a==b)<< endl;
    11. cout << (a <= b) << endl;
    12. cout << (a >= b) << endl;
    13. return 0;
    14. system("pause");
    15. }
    3.4 逻辑运算符

    !非【BOOL里面,不是0都是真】,&&与,||或

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. int a = 2; int b = 10;
    6. cout << !a << endl;
    7. cout << !!a << endl;
    8. a = 2; b = 2;
    9. cout << (a&&b)<< endl;
    10. cout << (a || b) << endl;
    11. a = 0; b = 3;
    12. cout << (a && b) << endl;
    13. cout << (a || b) << endl;
    14. a = 0; b = 0;
    15. cout << (a && b) << endl;
    16. cout << (a || b) << endl;
    17. return 0;
    18. system("pause");
    19. }

    四,程序流程结构

    顺序,选择,循环 
    【C撸了不少了,就不仔细打了】

    4.1 选择结构

    1.0  IF——同C
    2.0 三目运算符:表达式?A:B,如果表达式为真,返回A,假返回B【返回的是变量,可以继续赋值】
    3.0 SWITCH——同C【结构清晰,效率高,只能整型和字符型,BREAK】

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. int a = 9, b = 90;
    6. cout << (a > b ? a : b) << endl;
    7. (a > b ? a : b) = 78;//==78
    8. cout << a << endl;
    9. cout << b << endl;
    10. (a < b ? a : b) = 78;
    11. cout << a << endl;
    12. cout << b << endl;
    13. return 0;
    14. system("pause");
    15. }
    4.2 循环结构

    1.0 WHILE循环
    【RAND()%100,%100表示生成随机数的区间,0~99,0+1~99+1,rand()%100+1
    可以用BREAK退出当前循环
    2.0 DO……WHILE循环

    1. #include <iostream>
    2. #include<ctime>
    3. using namespace std;
    4. int main()
    5. {
    6. srand((unsigned int)time(NULL));
    7. //添加随机数种子,作用:利用当前系统时间生成随机数,防止每次随机数都一样
    8. int num=rand() % 100 + 1;
    9. cout << "请猜数字" << endl;
    10. int val;
    11. cin >> val;
    12. while(num!=val)
    13. {
    14. if (num > val)
    15. {
    16. cout << "猜小了" << endl;
    17. }
    18. else if (num < val)
    19. {
    20. cout << "猜大了" << endl;
    21. }
    22. cin >> val;
    23. }
    24. cout << "恭喜猜对了,数字就是" <<val<< endl;
    25. return 0;
    26. system("pause");
    27. }
    1. #include <iostream>
    2. #include<ctime>
    3. using namespace std;
    4. int main()
    5. {
    6. int a = 0;
    7. do
    8. {
    9. cout << a << endl;
    10. a++;
    11. if (a == 10)break;
    12. } while (a);
    13. a = 0;
    14. while (a)
    15. {
    16. cout << a << endl;
    17. a++;
    18. if (a == 10)
    19. {
    20. break;
    21. }
    22. }
    23. return 0;
    24. system("pause");
    25. }

    3.0 FOR循环
    4.0 嵌套循环——外层执行一次,内层执行一周

    1. #include <iostream>
    2. #include<ctime>
    3. using namespace std;
    4. int main()
    5. {
    6. for (int i = 0; i < 10; i++)
    7. {
    8. for (int i = 0; i < 10; i++)
    9. {
    10. cout << "* ";
    11. }
    12. cout << endl;
    13. }
    14. return 0;
    15. system("pause");
    16. }
    1. #include<iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. int n;
    6. cin >> n;
    7. for (int i = 1; i <= n; i++)
    8. {
    9. for (int j = 1; j <= i; j++)
    10. {
    11. cout << i << "*" << j << "=" << i * j<<"\t";
    12. }
    13. cout << endl;
    14. }
    15. return 0;
    16. system("pause");
    17. }
    4.3 跳转语句
    1.0 break语句,跳出循环,嵌套循环中可以是跳出内循环
    1. #include <iostream>
    2. #include<ctime>
    3. using namespace std;
    4. int main()
    5. {
    6. for (int i = 0; i < 10; i++)
    7. {
    8. for (int i = 0; i < 10; i++)
    9. {
    10. cout << "* ";
    11. if (i == 5)
    12. {
    13. break;
    14. }
    15. }
    16. cout << endl;
    17. }
    18. return 0;
    19. system("pause");
    20. }
    2.0 continue语句,结束本次,继续下一次循环
    1. #include <iostream>
    2. #include<ctime>
    3. using namespace std;
    4. int main()
    5. {
    6. for (int i = 1; i < 100; i++)
    7. {
    8. if (i % 7 != 0 && i / 10 != 7 && i % 10 != 7)
    9. {
    10. continue;
    11. }
    12. cout << i << "\t拍桌子" << endl;
    13. }
    14. return 0;
    15. system("pause");
    16. }
    3.0 goto语句:无条件跳转代码——语法 goto A【标签名常用大写】不建议大量使用

    注意:I==7时,输出【7-不是五的倍数 -下一个】

    1. #include <iostream>
    2. #include<ctime>
    3. using namespace std;
    4. int main()
    5. {
    6. cout << "1ooo" << endl;
    7. cout << "2pppp" << endl;
    8. goto FLAG;
    9. cout << "1ooo" << endl;
    10. cout << "2pppp" << endl;
    11. FLAG:
    12. cout << "23432" << endl;
    13. cout << "2gdfx" << endl;
    14. for (int i = 1; i < 21; i++)
    15. {
    16. cout << i ;
    17. if (i % 5 == 0)
    18. {
    19. goto DA;
    20. }
    21. cout << "不是五的倍数" << endl;
    22. DA:
    23. cout << "下一个" << endl;
    24. }
    25. return 0;
    26. system("pause");
    27. }

  • 相关阅读:
    Linux 下使用 cron 定时任务
    中间件 | Kafka - [安装 & 配置 & 启动]
    Java如何实现定时任务?
    第08章 MyBatisPlus持久化操作(二)
    docker上安装es
    Sqlmap 22.05.22.01
    hadoop入门(九):日志聚集功能配置
    24点游戏开发实例(Qt含源码)
    基于SSM跨境电商网站的设计与实现/海外购物平台的设计
    【CVPR2023】《A2J-Transformer:用于从单个RGB图像估计3D交互手部姿态的锚点到关节变换网络
  • 原文地址:https://blog.csdn.net/qq_51583806/article/details/137375014