找回密码
 立即注册
查看: 251|回复: 0

Unreal虚幻_C++学习

[复制链接]
发表于 2023-3-9 08:04 | 显示全部楼层 |阅读模式
我使用到是 Clion 代码编辑器

下载地址:https://www.jetbrains.com/products.html#type=ide


汉化教程:

Clion --> Preferences --> Plugins --> 搜索 Chinese --> install




C++入门练习代码示例:
  1. //导入 iostream库
  2. #include <iostream>
  3. //入口函数
  4. int main() {
  5. //    导入 std 命名空间
  6.     using namespace std;
  7.     //单独导入 cout endl cin
  8. //    using std::cout;
  9. //    using std::endl;
  10. //    using std::cin;
  11. //    输出语句 cout 属于std 命名空间
  12.     cout << "Hello World!!!";
  13.     //输出 换行  \n换行
  14.     cout << endl;
  15.     cout << "我是你爸爸呀?\n";
  16.     //练习根据输入的年龄 来计算多少个月
  17.     //创建一个int 变量 名字为 age
  18.     int age;
  19.     cin >> age; //输入 age年龄
  20.     int month = age * 12; //计算月数 *表示乘号
  21.     cout << "在地球上生存了"<<month<<"个月\n";
  22.     //此 输入是为了防止 执行到return 是程序结束
  23.     cin >> age;
  24.         //返回一个 整形
  25.     return 0;
  26. }
复制代码
变量命名规则





整型(基本类型)

字符类型

代码示例:
  1. //
  2. //  main.cpp
  3. //  004-整型
  4. //
  5. //  Created by linjie on 2020/12/29.
  6. //
  7. #include <iostream>
  8. using namespace std;
  9. int main(int argc, const char * argv[]) {
  10.     // insert code here...
  11.     short s = 3.1;
  12.     int i = 1;
  13.     long l = 111;
  14.    
  15.     //加了unsigned 整型类型 不能赋值负值 且 正值的存储量扩大两倍
  16.     unsigned short e = -3;
  17.     int b = 10000000000000000;
  18.     unsigned int f = 40000000000000000;
  19.    
  20.     cout << "----------数值类型--------" << endl;
  21.     cout << "型" << s << endl;
  22.     cout << "整型" << i << endl;
  23.     cout << "长整型" << l << endl;
  24.    
  25.     cout << "----------最大最小值--------" << endl;
  26.     cout << INT_MAX << endl; //int 存储最大值
  27.     cout << INT_MIN << endl; //int 存储最小值
  28.     cout << SHRT_MAX << endl; //Short 存储的最大值
  29.     cout << SHRT_MIN << endl;//Short 存储的最小值
  30.    
  31.     cout << "----------unsigned--------" << endl;
  32.     cout << e << endl;
  33.     cout << b << endl;
  34.     cout << f << endl;
  35.    
  36.    
  37.     //字符类型
  38.     //ASC 码表 http://ascii.911cha.com/
  39.     char aa = ' '; //空格符号 也属于一个字符
  40.     char bb = 'a';
  41.     char cc = 97; // 97 对应 ASC码表的 字符 a
  42.     char dd = '\\n';
  43.     int ee = 'a'; //字符可以 给int类型 赋值
  44.    
  45.     cout << "----------char字符也属于整型--------" << endl;
  46.     cout << "aa =>" << aa << endl;
  47.     cout << "bb =>" << bb << endl;
  48.     cout << "cc =>" << cc << endl;
  49.     cout << "dd =>" << dd << endl;
  50.     cout << "ee =>" << ee << endl;
  51.    
  52.     cin.get();
  53.    
  54.     return 0;
  55. }
复制代码
输出结果:


Bool布尔类型
  1. //
  2. //  main.cpp
  3. //  bool类型
  4. //
  5. //  Created by linjie on 2021/1/6.
  6. //
  7. #include <iostream>
  8. using namespace::std;
  9. int main(int argc, const char * argv[]) {
  10.    
  11.     //bool布尔类型
  12.     bool a = true; //1
  13.     bool b = false; //0
  14.    
  15.     //为bool 类型 赋值 非0即1 1表示true
  16.     bool c = 100;
  17.     bool d = 2;
  18.    
  19.     cout << "---------bool类型---------" << endl;
  20.     cout << "a =>" << a << endl;
  21.     cout << "b =>" << b << endl;
  22.     cout << "c =>" << c << endl;
  23.     cout << "d =>" << d << endl;
  24.    
  25.     cin.get();
  26.    
  27.     return 0;
  28. }
复制代码
输出结果:


const常量

代码示例:
  1. //
  2. //  main.cpp
  3. //  const-常量
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. using namespace::std;
  9. int main(int argc, const char * argv[]) {
  10.    
  11.     //变量是可变的
  12.     int a = 100;
  13.     a = 90;
  14.    
  15.     //常量 不可变的量
  16.     const int b = 11;
  17.     //b = 10; //该行报错 常量不可变 只能赋值一次
  18.    
  19.     return 0;
  20. }
复制代码
浮点类型



代码示例:
  1. //
  2. //  main.cpp
  3. //  浮点类型
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <climits> //引入函数库 才能输出__FLT_MAX__ 和 __FLT_MIN__
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.    
  12.     //单精度 浮点
  13.     float a = 1.1;
  14.     //双精度浮点
  15.     double b = 2.222;
  16.     //长双精度浮点
  17.     long double c = 2.2222;
  18.    
  19.     cout<< "浮点存储最大值" << __FLT_MAX__ << endl;
  20.     cout<< "浮点存储最小值" << __FLT_MIN__ << endl;
  21.    
  22. }
复制代码
输出结果:


简单练习:
  1. //
  2. //  main.cpp
  3. //  浮点类型
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <climits> //引入函数库 才能输出__FLT_MAX__ 和 __FLT_MIN__
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.    
  12.     //1.让用户输入自己的身高(米),然后把它转换成厘米输出出来。
  13.     float height;
  14.     cout << "请输入您的身高(米)=>";
  15.     cin >> height;
  16.     cout << endl << "您身高为 =>" << height * 100 << "厘米。" << endl;
  17.     //2.编写一个程序,让用户输入秒,然后把它转换成 多少天 多少小时 分钟 秒
  18.     float min;
  19.     cout << "请输入多少秒(自动转换)" ;
  20.     cin >> min;
  21.     cout << min / 60 / 24 << "天" << min / 60 << "小时" << min << "分钟" << min * 60 << "秒" << endl;
  22.     //3.要求用户输入一个班级的 男生和女生的数量,并输出女生的比例(百分比)
  23.     float Ncount;
  24.     cout << "请输入男生的数量 ==>";
  25.     cin >> Ncount;
  26.     float Wcount;
  27.     cout << "请输入女生的数量 ==>";
  28.     cin >> Wcount;
  29.     cout << "女生比例为 ==>  " << Wcount/(Ncount + Wcount) * 100 << "%" << endl;
  30.     //输入一个 四位数 获取 个 十 百 千分位的数值
  31.     int shu;
  32.     cout << "请输入一个四位数 ==> ";
  33.     cin >> shu;
  34.    
  35.     int ge = shu % 10;
  36.     int shi = (shu % 100) /10;
  37.     int bai = (shu % 1000) /100;
  38.     int qian = shu / 1000;
  39.    
  40.     cout << "个位=>" << ge << "   十位=>" << shi << "   百位=>" << bai << "   千位=>" << qian;
  41.    
  42.    
  43. }
复制代码
输出结果:


数组
  1. //
  2. //  main.cpp
  3. //  数组
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <climits> //引入函数库 才能输出__FLT_MAX__ 和 __FLT_MIN__
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.    
  12.     //语法: 变量类型 数组名字[长度] = {数组的值};
  13.     //第一种数组 赋值方式
  14.     int arr[4] = {1,2,3,4};
  15.     //第二种赋值方式 赋值的 个数等于或小于声明的数组长度
  16.     int arr2[4] = {1,2,3};
  17.     //第三种赋值方式 不声明数组长度 自动根据你 赋值的个数自动确定数组长度
  18.     int arr3[] = {1,2,3,4,5,6,7,8};
  19.    
  20.     //C++ 11 数组语法
  21.     int arr4[]{4,5,6,1};
  22.    
  23.     cout << arr4[-1] << endl; //访问一个不存在的下标的值 输出1
  24.     cout << arr4[5] << endl; //访问一个不存在的下标的值 输出2
  25.     cout << arr4[0] << endl; //访问一个 存在的下标的值 输出4
  26.     arr4 [0] = 100; //修改数组下标为0 的值
  27.     cout << arr4[0] << endl; //输出 100
  28. }
复制代码
输出结果:


字符串
  1. //
  2. //  main.cpp
  3. //  string字符串
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <climits> //引入函数库 才能输出__FLT_MAX__ 和 __FLT_MIN__
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.    
  12.     //语法:  string 字符串名字 = "值";
  13.     string str = "阿威";
  14.    
  15.     //字符串拼接
  16.     string str1 = "黑狗";
  17.     string str2 = "牛逼";
  18.     string str3 = str1 + str2;
  19.     cout << str3 << endl; //黑狗牛逼
  20.     //字符串的长度大小 size()
  21.     cout << str3.size() << endl;
  22.    
  23.     //输入getline不建议和cin进行混用
  24.     cout << "请输入任意信息=>";
  25.     getline(cin, str3);
  26.     cout << str3 << endl;
  27.    
  28.     //字符串赋值
  29.     string str4 ;
  30.     string str5 = "队长啊星";
  31.     str4 = str5;
  32.     cout << str4 << endl; //队长啊星
  33.     cout << str5 << endl; //队长啊星
  34.    
  35. }
复制代码
输出结果:


结构体
  1. //
  2. //  main.cpp
  3. //  struct结构体
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <climits> //引入函数库 才能输出__FLT_MAX__ 和 __FLT_MIN__
  9. using namespace::std;
  10. //定义结构体 在外部定义 每个函数都能访问
  11. struct Test{
  12.     string type1;
  13.     string type2;
  14.     string type3;
  15. };
  16. int main(int argc, const char * argv[]) {
  17.    
  18.     //结构体 申明赋值
  19.     Test t1{"怪物","英雄","物品"};
  20.     //调用方法  结构体赋值申明的名字.结构体内定义的变量名  比如:t1.type1
  21.     cout << t1.type1 << "   " << t1.type2 << "   " << t1.type3 << endl;
  22.    
  23.     //结构体数组
  24.     Test test[]{{"怪物","英雄","物品"},{"怪物2","英雄2","物品2"},{"怪物3","英雄3","物品3"}};
  25.     //访问 结构体数组内的值
  26.     cout << test[1].type1 << "   " << test[1].type2 << "   " << test[1].type3 << endl;
  27. }
复制代码
输出结果:


枚举类型
  1. //
  2. //  main.cpp
  3. //  enum枚举类型
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <climits> //引入函数库 才能输出__FLT_MAX__ 和 __FLT_MIN__
  9. using namespace::std;
  10. //定义枚举 在外部定义 每个函数都能访问
  11. enum HeroType{
  12.   ShuChu,  //0
  13.   FaShi,   //1
  14.   ShangDan, //2
  15.   DaYe,     //3
  16.   FuZhu     //4
  17. };
  18. int main(int argc, const char * argv[]) {
  19.     //调用枚举类型
  20.     //枚举类型 枚举变量名字 = 枚举内定义的值的名字;
  21.     HeroType heroType = ShuChu;
  22.     cout << heroType << endl; //0
  23.     heroType = FaShi;
  24.     cout << heroType << endl; //1
  25.     int aa = FaShi + 2;
  26.     cout << aa << endl; //3
  27.     heroType = HeroType(4);
  28.     cout << heroType << endl; //4
  29.     cout << ShangDan << endl; //2
  30. }
复制代码
输出结果:


指针
  1. //
  2. //  main.cpp
  3. //  指针
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <climits> //引入函数库 才能输出__FLT_MAX__ 和 __FLT_MIN__
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.     int a = 1;
  12.     int b = 2.5;
  13.    
  14.     //& 取一个变量的内存地址
  15.     //* 取一个内存地址 存储的值
  16.     cout << "获取a变量内存地址=> " << &a <<endl;
  17.     cout << "获取a变量的值==> " << *(&a) << endl;
  18.     cout << "获取b变量内存地址=> " << &b <<endl;
  19.     cout << "获取b变量的值==> " << *(&b) << endl;
  20.    
  21.     //指针定义
  22.     //语法: 变量类型* 变量名 = &值;
  23.     int* aa = &a;
  24.     int* bb = &b;
  25.     //输出地址
  26.     cout << aa << endl;
  27.     cout << bb << endl;
  28.     //输出值
  29.     cout << *aa << endl;
  30.     cout << *bb << endl;
  31.    
  32.     int* cc = &a;
  33.     int* dd = &a;
  34.     //因为 cc dd 都指向同一个地址 那就变量a的地址 所以改变 a cc dd任意一个变量的值 三个值都会跟着改变
  35.     *cc = 100;
  36.     cout << a << "   " << *cc << "   " << *dd << endl;  // 100 100 100
  37.     *dd = 200;
  38.     cout << a << "   " << *cc << "   " << *dd << endl; // 200 200 200
  39.     a = 11;
  40.     cout << a << "   " << *cc << "   " << *dd << endl;// 11 11 11
  41.    
  42.     //空指针的申明 三种方式
  43.     int* ee = 0;
  44.     int* ff = NULL;
  45.     int* gg = nullptr; //C++11 的语法 nullptr专门为指针而生 空指针
  46.    
  47.     //void 声明一个 可以接收任何类型的指针
  48.     void * hh;
  49.     hh = &a;
  50.     //hh = &b;
  51.     cout << *((int*)hh) << endl;  // (int*)强制转换为int类型的指针数据
  52.    
  53.     //new int 申请声明一个 新的内存
  54.     int * ii = new int;
  55.     *ii = 100;
  56.     cout << *ii << endl;
  57.     delete ii;  //delete 删除指针
  58.    
  59.    
  60. }
复制代码
输出结果:


指针和数组的关系
  1. //
  2. //  main.cpp
  3. //  指针和数组的关系
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <climits> //引入函数库 才能输出__FLT_MAX__ 和 __FLT_MIN__
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.    
  12.     //其实数组本身 就是一个指针
  13.     int arr[]{1,2,3,4,5,6,7,8,9,10};
  14.     cout << arr << endl; //数组本身就是一个指针 输出数组 就是输出其内存地址
  15.     cout << *arr << endl; //*号 取一个内存地址里的值 输出1 数组默认是指针
  16.     cout << *arr + 1 << endl; //*arr默认取第一个值 *arr+1表示取第二个值 依次类推 *arr+2就是取第三个值
  17.    
  18.     //使用new 创建数组 new 表示开辟一个新的内存地址 所以不用了 要用delete删除
  19.     int * pp = new int[20];
  20.     //取值 赋值方式一
  21.     pp[0] = 88;
  22.     cout << pp[0] << endl;
  23.     //取值 赋值方式二 (指针方式)
  24.     *(pp + 1) = 99;
  25.     cout << *(pp + 1) << endl;
  26.    
  27.     //删除 不用的 开辟的内存地址
  28.     delete[] pp;
  29. }
复制代码
输出结果:


C++11 数组创建方式(推荐)
  1. //
  2. //  main.cpp
  3. //  C++11 数组创建方式
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <climits> //引入函数库 才能输出__FLT_MAX__ 和 __FLT_MIN__
  9. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  10. using namespace::std;
  11. int main(int argc, const char * argv[]) {
  12.    
  13.     //C++11 数组创建方式
  14.     array<int,6> arr1 = {1,2,3,4,5,6};
  15.     array<int, 6> arr2;
  16.     arr2 = arr1; //可以直接 数组 = 数组
  17.     cout << arr1[0] << "  " << arr2[0] << endl;
  18.     cout << arr2[5] << "  " << arr2[5] << endl;
  19.     cout << arr1[6] << "  " << arr2[6] << endl; //数组这里声明最大长度为6 但是这里访问第七个值 也就是下标6 输出1 访问下标7 输出2 依次类推
  20. }
复制代码
输出结果:


for循环
  1. //
  2. //  main.cpp
  3. //  for 循环
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.    
  12.    
  13.     array<int, 10> arr = {1,2,3,4,5,6,7,8,9,10};
  14.     //for循环
  15.     //语法:
  16. //    for (初始化变量; 循环条件判断; 变量自增长) {
  17. //        方法体
  18. //    }
  19.     //循环输出数组里的值
  20.     for (int i = 0; i < arr.size(); i++) {
  21.         cout << arr[i] << endl;
  22.     }
  23.    
  24. }
复制代码
输出结果:


组合赋值运算符 和 关系运算符
  1. //
  2. //  main.cpp
  3. //  组合赋值运算符 和 关系运算符
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. #include <string>
  10. using namespace::std;
  11. int main(int argc, const char * argv[]) {
  12.    
  13.     //  组合赋值运算符
  14.     // + - * / % (加 减 乘 除 取余)
  15.     // += -= *= /= %= (加等 减等 乘等 除等 取余等)
  16.     int a = 11;
  17.     int b = 22;
  18.     a += b; // a = a + b;  33
  19.     cout << a << endl;
  20.     a -= b; // a = a - b;  11
  21.     cout << a << endl;
  22.     a *= b; // a = a * b;  242
  23.     cout << a << endl;
  24.     a /= b; // a = a / b;  11
  25.     cout << a << endl;
  26.     a %= b; // a = a % b;  11
  27.     cout << a << endl;
  28.    
  29.     //逻辑运算符
  30.     // > >= < <= == != (大于 大于或等于 小于 小于或等于 等于 不等于)
  31.     int c = 11;
  32.     int d = 22;
  33.     int e = 11;
  34.     cout << (c > d) << endl; //输出0 0表示false 错误 满足就返回true 不满足返回false
  35.     cout << (c < d) << endl; //输出1 1表示true 正确 满足就返回true 不满足返回false
  36.     cout << (c >= e) << endl; //输出1 1表示true 正确 >= 大于或等于只要成立一个 满足就返回true 不满足返回false
  37.     cout << (c <= e) << endl;//输出1 1表示true 正确 <= 小于或等于只要成立一个 满足就返回true 不满足返回false
  38.     cout << (c == e) << endl;//输出1 1表示true 正确 == 等于满足 满足就返回true 不满足返回false
  39.     cout << (c != e) << endl; //输出0 0表示fasle 错误 != 不等于 不满足返回false 满足返回true
  40.    
  41.     //字符串比较
  42.     char ch[] = "牛逼";
  43.     char ch2[] = "牛逼";
  44.     cout << (ch == ch2) << endl; //返回0 不相等 因为两个数组 比较的是内存地址 内存地址不相同所以返回0
  45.     cout << strcmp(ch, ch2) << endl; //这个比较特殊 返回0 这里的0表示相等 1表示不想等  strcmp()比较两个char字符类型的数据是否相等
  46.    
  47.     string str = "队长阿威";
  48.     string str2 = "队长阿威";
  49.     cout << (str == str2) << endl; //返回1 表示相等
  50.    
  51.    
  52.    
  53.    
  54.    
  55. }
复制代码
输出结果:


while循环
  1. //
  2. //  main.cpp
  3. //  while 循环
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. #include <string>
  10. using namespace::std;
  11. int main(int argc, const char * argv[]) {
  12.    
  13. //for循环与while循环比较
  14. //    for (初始化; 条件判断; 自增) {
  15. //        方法体
  16. //    }
  17. //语法:
  18. //    初始化
  19. //    while(条件判断){
  20. //        方法体
  21. //        自增
  22. //    }
  23.    
  24.     int i = 0;
  25.     while(i < 10){
  26.         cout << "循环执行了" << i+1 << "次" << endl;
  27.         i++;
  28.     }
  29. }
复制代码
输出结果:


宏和类型别名
  1. //
  2. //  main.cpp
  3. //  宏-类型别名
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. #include <string>
  10. using namespace::std;
  11. //宏定义
  12. //#define 别名 代码段
  13. #define Start int main
  14. //类型别名
  15. //typedef 数据类型 别名;
  16. typedef string XingString;
  17. //宏调用
  18. Start(int argc, const char * argv[]) {
  19.     XingString str = "类型别名";
  20.     cout << str << endl;
  21. }
复制代码
输出结果:


do while循环和for循环第二种方式
  1. //
  2. //  main.cpp
  3. //  do wile循环和for循环第二种方式
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. #include <string>
  10. using namespace::std;
  11. int main(int argc, const char * argv[]) {
  12. //     初始化
  13. //    do {
  14. //        循环体
  15. //        自增
  16. //    } while (循环条件);
  17.    
  18.     int i = 0;
  19.     do {
  20.         cout << "第" << i+1 << "次循环" << endl;
  21.         i++;
  22.     } while (i < 10);
  23.    
  24.     //for循环第二种方式输出 数组
  25. //    for(数据类型 别名 : 数组名称){
  26. //        方法体
  27. //    }
  28.     array<int, 5> arr = {1,2,3,4,5};
  29.     for(int temp : arr){
  30.         cout << temp << endl;
  31.     }
  32.    
  33. }
复制代码
输出结果:


二维数组和循环嵌套
  1. //
  2. //  main.cpp
  3. //  二维数组和循环嵌套
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.    
  12.     //int arr[行][列] = { {},{},{},{},{} };
  13.     //创建一个 5行5列的二维数组
  14.     int arr[5][5] ={
  15.         {1,2,3,4,5},
  16.         {1,2,3,4,5},
  17.         {1,2,3,4,5},
  18.         {1,2,3,4,5},
  19.         {1,2,3,4,5}
  20.     };
  21.    
  22.     //循环嵌套
  23.     for (int i =0; i < 5; i++) { //外层循环执行 一次
  24.         for (int j =0; j < 5; j++) { //内层循环 执行完 次
  25.             cout << i << " " << j << endl;
  26.         }
  27.     }
  28. //    0 0
  29. //    0 1
  30. //    0 2
  31. //    0 3
  32. //    0 4
  33. //    1 0
  34. //    1 1
  35. //    1 2
  36. //    1 3
  37. //    1 4
  38. //    2 0
  39. //    2 1
  40. //    2 2
  41. //    2 3
  42. //    2 4
  43. //    3 0
  44. //    3 1
  45. //    3 2
  46. //    3 3
  47. //    3 4
  48. //    4 0
  49. //    4 1
  50. //    4 2
  51. //    4 3
  52. //    4 4
  53.    
  54.     //先输出 行 在输出 列
  55.     for (int i =0; i < 5 ; i++) { //外层循环执行 一次
  56.         for (int j =0; j < 5; j++) { //内层循环 执行完 次
  57.             cout << arr[i][j] << " ";
  58.         }
  59.         cout << endl;
  60.     }
  61. //    1 2 3 4 5
  62. //    1 2 3 4 5
  63. //    1 2 3 4 5
  64. //    1 2 3 4 5
  65. //    1 2 3 4 5
  66.    
  67.     //先输出 列 在输出 行
  68.     for (int i =0; i < 5 ; i++) { //外层循环执行 一次
  69.         for (int j =0; j < 5; j++) { //内层循环 执行完 次
  70.             cout << arr[j][i] << " ";
  71.         }
  72.         cout << endl;
  73.     }
  74. //    1 1 1 1 1
  75. //    2 2 2 2 2
  76. //    3 3 3 3 3
  77. //    4 4 4 4 4
  78. //    5 5 5 5 5
  79.    
  80. }
复制代码
if else判断语句
  1. //
  2. //  main.cpp
  3. //  if else判断语句
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.    
  12.     int hp = 0;
  13.     //语法
  14. //    if(判断条件){
  15. //        条件成立 执行这里的代码
  16. //    }else{
  17. //        否则不成立 执行这里的代码
  18. //    }
  19.     if(hp == 0){
  20.         cout << "英雄已经阵亡" << endl;
  21.     }else{
  22.         cout << "英雄生命值很健康" << endl;
  23.     }
  24.    
  25.    
  26.     int age = 17;
  27. //    if(判断条件){
  28. //        条件成立 执行这里的代码 然后跳出判断
  29. //    }else if(判断条件){
  30. //        成立 执行这里的代码 然后跳出判断
  31. //    }else{
  32. //        否则以上条件都不满足 执行这里的代码 然后跳出判断
  33. //    }
  34.     if(age < 18){
  35.         cout << "您未满18岁" << endl;
  36.     }else if(age >= 18){
  37.         cout << "您已成年可以尽情游玩" << endl;
  38.     }else{
  39.         cout << "否则执行我" << endl;
  40.     }
  41.    
  42.     //if判断语句第二种写法 不需要方法体 默认紧挨到一行 为方法体代码
  43.     if(age == 17)
  44.         cout << "您今年17岁了" << endl;
  45.    
  46. }
复制代码
输出结果:


逻辑运算符
  1. //
  2. //  main.cpp
  3. //  逻辑运算符
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.     int a = 1;
  12.     int b = 11;
  13.    
  14.     //逻辑运算符 &&(与) ||(或) !(非)
  15.    
  16. //&&(与) 必须两边条件 都满足才为true 否则其中一边不满足 则为false
  17.     bool result = a > 0 && a==1;
  18.     bool result2 = a > 0 && b==1;
  19.     cout << result << endl; //1 true
  20.     cout << result2 << endl; //0 false
  21.    
  22. //||(或) 两个条件 其中一边满足就为true 除非两个条件不满足 才为false
  23.     bool result3 = a > 0 || a==1;
  24.     bool result4 = a > 0 || b==1;
  25.     cout << result3 << endl; //1 true
  26.     cout << result4 << endl; //1 true
  27.    
  28. //!取反
  29.     cout << !true << endl; //0 false
  30.     cout << !false << endl; //1 true
  31. }
复制代码
输出结果:


三元表达式
  1. //
  2. //  main.cpp
  3. //  逻辑运算符
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11.   //三元表达式
  12.   //语法:  判断条件?成立取这里的值:不成立取这里的值
  13.     int a = true ? 100 : 1;
  14.     cout << a << endl; //100
  15.    
  16.     //相当于 =>
  17.     if(true){
  18.         a = 100;
  19.     }else{
  20.         a = 1;
  21.     }
  22.     cout << a << endl; //100
  23.    
  24.    
  25.    
  26. }
复制代码
输出结果:


switch判断
  1. //
  2. //  main.cpp
  3. //  switch判断
  4. //
  5. //  Created by linjie on 2021/1/7.
  6. //
  7. #include <iostream>
  8. #include <array> //使用 C++11 数组创建方式 需要引入该类库
  9. using namespace::std;
  10. int main(int argc, const char * argv[]) {
  11. //语法:
  12. //    switch (判断的变量) {
  13. //        case 判断条件:
  14. //                该条件成立 执行这里的代码
  15. //            break; //执行完后跳出switch
  16. //        case 判断条件:
  17. //                该条件成立 执行这里的代码
  18. //            break;//执行完后跳出switch
  19. //        case 判断条件:
  20. //                该条件成立 执行这里的代码
  21. //            break;//执行完后跳出switch
  22. //        default:
  23. //                上面全部条件不成立 执行这里
  24. //            break;//执行完后跳出switch
  25. //    }
  26.    
  27.     int money = 100;
  28.     switch (money) {
  29.         case 100:
  30.             cout << "恭喜你成为至尊VIP1" << endl;
  31.             break;
  32.         case 1000:
  33.             cout << "恭喜你成为至尊VIP2" << endl;
  34.             break;
  35.         case 10000:
  36.             cout << "恭喜你成为至尊VIPMAX" << endl;
  37.             break;
  38.         default:
  39.             cout << "恭喜你一分没冲" << endl;
  40.             break;
  41.     }
  42. }
复制代码
输出结果:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-29 07:27 , Processed in 0.178360 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表