zoukankan      html  css  js  c++  java
  • 数据_数组_容器

    1:数据类型

     

     

     

     1 /*
     2  *  文件名: Charpter2
     3  *  描  述: 复习变量的个别基础知识
     4  *  作  者:刘雷
     5  *  时  间:2019/10/10
     6  *  版  权:version 1.0
     7  */
     8 #include <iostream>
     9 #include <cstdlib>//printf头文件
    10 #include <climits>//INT_MAX等常量
    11 #include <cmath>//数学头文件
    12 #include <iomanip>//控制符头文件
    13 #include <windows.h>//窗口头文件
    14 using namespace std;
    15 #define max_num
    16 int main()
    17 {
    18     
    19     typedef int so;//typedef是别名的意思,把数据类型int取一个别名叫so
    20     so a=12;
    21     size_t a1=10;
    22     unsigned int a2=10;//size_t和unsigned int的作用一样
    23     cout << a << endl;
    24     cout << INT_MAX << endl;
    25     printf("%d",a);
    26 
    27     //float数据类型不适合展示精确数字,容易丢失数据精度
    28     float a3=12345.678910;//保留6位数,所以输出是12345.6,double有效数字位数是16位
    29     cout << a3 << endl;
    30 
    31     double a4=100.0/3.0;
    32     //强制以小数的方式输出
    33     cout << fixed;
    34     //控制显示的精度
    35     cout << setprecision(5);//显示五位小数
    36     cout << a4 << endl;
    37 
    38     //设置小数的宽度setw
    39     cout << setw(8) << "|" << 3.14 << setw(8) << "|" << endl;//       |3.14000       |
    40     
    41     //SetConsoleTitle("示例:打印德玛西亚之力的详细信息");
    42     cout << left << setfill('*') << setw(8) << "a" << setw(8) << "b" << endl;//a*******b*******
    43     cout << right << setfill('*') << setw(8) << "a" << setw(8) << "b" << endl;//*******a******b
    44     return 0;
    45 }

     

     1 /*
     2  *  文件名: Charpter2
     3  *  描  述: 在控制台输出指定多行信息
     4  *  作  者:刘雷
     5  *  时  间:2019/10/13
     6  *  版  权:version 1.0
     7  */
     8 #include <iostream>
     9 #include <windows.h>
    10 using namespace std;
    11 int main()
    12 {
    13     SetConsoleTitle("打印英雄盖伦的详细信息");
    14     double value_attack = 57.88;
    15     double value_attack_distance = 172;
    16     double value_defence = 27.54;
    17     double value_magicResist = 32.10;
    18     double value_life = 616.28;
    19     double value_life_growth = 7.84;
    20     double value_magic = 0.00;
    21     double value_magic_growth = 0.00;
    22     double value_speed = 340;
    23     string orriention = "上单 辅助 打野";
    24     double dianjuan = 450;
    25     double coins = 1000;
    26     cout << "德玛西亚之力*盖伦" << endl;
    27     cout << "伤害:"<< value_attack << "(+4.50)	攻击距离:" << value_attack_distance << endl;
    28     cout << "护甲:"<< value_defence << "(+3.00)	魔抗:" << value_magicResist << "(+1.25)" << endl;
    29     cout << "生命:"<< value_life << "(+84.25)	生命回复:" << value_life_growth << "(+0.50)" << endl;
    30     cout << "法力:"<< value_magic << "(+0.00)		法力回复:" << value_magic_growth << "(+0.00)" << endl;
    31     cout << "移速:"<< value_speed << "		定位 " << endl;
    32     cout << "点券:"<< dianjuan << "		金币 " << coins << endl;
    33 
    34     return 0;
    35 }

     

     

     随机数

     1 /*
     2  *  文件名: Charpter2
     3  *  描  述: 学习随机数
     4  *  作  者:刘雷
     5  *  时  间:2019/10/13
     6  *  版  权:version 1.0
     7  */
     8 #include <iostream>
     9 #include <cstdlib>
    10 #include <ctime>
    11 using namespace std;
    12 int main()
    13 {
    14     /*
    15     stdlib.h头文件中有宏#define RAND_MAX 0x7fff
    16     rand()产生一个0到0x7ffff即0到32767之间的随机数
    17 r   and()/(RAND_MAX+1.0)就等于一个0到1之间的小数了,
    18     因为rand()最大是32767最小是0,再除以32768就是一个
    19     0到1之间的小数(不能等于1),再乘以10就是一个0到10之
    20     间的数了(不等于10).最后再加1,就成了一个求1到10之间
    21     随机数的式子了.*/
    22     int start = 5;
    23     int end =15;
    24     srand(unsigned(time(NULL)));
    25     for(int i=0;i<10;i++){
    26         cout << int(start+(end - start)*rand()/(RAND_MAX+1.0)) << endl;
    27     }
    28 
    29     return 0;
    30 }
     1 /*
     2  *  文件名: Charpter2
     3  *  描  述: 使用循环模拟实现玩家BOSS战
     4  *  作  者:刘雷
     5  *  时  间:2019/10/13
     6  *  版  权:version 1.0
     7  */
     8 #include <iostream>
     9 #include <cstdlib>
    10 #include <ctime>
    11 using namespace std;
    12 int main()
    13 {
    14     int HP1 = 100;//八神庵的血量
    15     int HP2 = 100;//草薙京的血量
    16     int attack1 = 0;//八神庵的攻击值
    17     int attack2 = 0;//草薙京的攻击值
    18     int i = 0;//计数器
    19     srand(unsigned(time(NULL)));
    20     while(true){
    21     i++;
    22     cout << "--------第" << i << "轮战斗开始-------" << endl;
    23     attack1 = 5+10 * rand() / (RAND_MAX + 1);
    24     attack2 = 5+10 * rand() / (RAND_MAX + 1);
    25     HP1 -= attack2;
    26     if(attack2 < 10){
    27         cout << "草薙京使用普攻使八神庵掉了" << attack2 <<"血量" << endl;
    28     }else{
    29         cout << "草薙京使用大招使八神庵掉了" << attack2 <<"血量" << endl;
    30     }
    31     if(HP1 < 0){
    32         cout << "八神庵被击败,草薙京取得胜利" << endl;
    33         break;
    34     }
    35     HP2 -= attack1;
    36     if(attack1 < 10){
    37         cout << "八神庵使用普攻使草薙京掉了" << attack1 <<"血量" << endl;
    38     }else{
    39         cout << "八神庵使用大招使草薙京掉了" << attack1 <<"血量" << endl;
    40     }
    41     if(HP2 < 0)
    42     {
    43           cout << "草薙京被击败,八神庵取得胜利" << endl;
    44           break;
    45     }
    46      cout << "--------第" << i << "轮战斗结束--------" << endl;
    47     }
    48     cout << "--------第" << i << "轮战斗结束--------" << endl;
    49     return 0;
    50 }

    容器

     

     1 /*
     2  *  文件名: Charpter2
     3  *  描  述: 学习vector
     4  *  作  者:刘雷
     5  *  时  间:2019/10/14
     6  *  版  权:version 1.0
     7  */
     8 #include <iostream>
     9 #include <vector>
    10 #include <algorithm>
    11 using namespace std;
    12 int main()
    13 {
    14     vector<double> v = {1.2, 4.1, 44.0, 32.2};//未指定大小
    15     vector<string> v1 = { "a", "an", "the" };//列表初始化
    16     vector<double> v2(4);//指定了大小,但可以扩充,只不过要重新分配内存块,耗时
    17     vector<int> v3(10,1);//十个一
    18     vector<int> v4(v3);//v4=v3
    19 
    20     for(int i = 0; i < v.size(); i++){//遍历
    21         cout << v[i] << " ";
    22     }
    23     cout << endl;
    24     v.push_back(100);//加入100
    25     for(int i = 0; i < v.size(); i++){
    26         cout << v[i] << " ";
    27     }
    28     cout << endl;
    29     v.pop_back();//弹出
    30     v.pop_back();
    31     for(int i = 0; i < v.size(); i++){
    32         cout << v[i] << " ";
    33     }
    34     cout << endl;
    35     vector<double>:: iterator it;//使用迭代器遍历
    36     sort(v.begin(),v.end());//正向排序
    37     for(it = v.begin(); it != v.end(); ++it){
    38         cout << *it << " ";
    39     }
    40     cout << endl;
    41     reverse(v.begin(), v.end());//逆向排序
    42     for(it = v.begin(); it != v.end(); ++it){
    43         cout << *it << " ";
    44     }
    45     cout << endl;
    46     for(int i = 0; i < v1.size(); i++){
    47         cout << v1[i] << " ";
    48     }
    49     cout << endl;
    50     v1.clear();//清空
    51     for(int i = 0; i < v1.size(); i++){
    52         cout << v1[i] << " ";
    53     }
    54     cout << endl;
    55     v.insert(v.begin(),1.0);//pos是插入和删除的地址
    56     v.erase(v.end()-1);
    57     for(int i = 0; i < v.size(); i++){
    58         cout << v[i] << " ";
    59     }
    60     cout << endl;
    61     return 0;
    62 }
  • 相关阅读:
    ajax如何向后台传递数组,在后台该如何接收的问题(项目积累)
    循环读取list 的几种方法?
    jQuery里$(this)和this的区别在哪?
    Hibernate多对多双向关联需要注意的问题(实例说话)
    window.open()用法说明
    struts2 跳转类型 result type=chain、dispatcher、redirect(redirect-action)
    页面中的删除确认(ajax)、输入框中确认信息是否可用(ajax)的jquery代码
    理解ValueStack的基本机制 OGNL表达式
    Struts2中的ModelDriven机制及其运用
    mySQl数据库中不能插入中文的处理办法
  • 原文地址:https://www.cnblogs.com/henuliulei/p/11668401.html
Copyright © 2011-2022 走看看