• 实验一:C++简单程序设计


    这次的实验主要是四个问题,在解决问题的基础上要求使用不同的循环结构和分支结构。在复习C语言的同时了解C++不同于C语言的地方。

    2-28 /*问题描述:实现一个简单的菜单程序,运行时现实“Menu: A(dd)  D(elete)  S(ort)  Q(uit) , Select one:”提示用户输入。A表示增加,D表示删除,S表示排序,Q表示退出,输入A、D、S时分别提示“数据已经增加、删除、排序。”,输入为Q时程序结束。*/

    # if选择结构源码:

     1 #include<iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int judge=1;
     6     while(judge!=0)
     7     {
     8     
     9         cout<<"Menu:A(dd) D(elete) S(ort) Q(uit), Select one:"<<endl;
    10         char s;
    11         cin>>s;
    12         
    13         if(s!='A'&&s!='D'&&s!='S'&&s!='Q')
    14         cout<<"Wrong directive, please try agine"<<endl;
    15         
    16         else if(s=='A')
    17         cout<<"Date has added"<<endl;
    18         
    19         else if(s=='D')
    20         cout<<"Date has deleted"<<endl;
    21         
    22         else if(s=='S')
    23         cout<<"Date has sorted"<<endl;
    24         
    25         else if(s=='Q')
    26         judge=0;
    27     }
    28     return 0;
    29 }

    # switch选择结构源码:

     1 #include<iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int judge=1;
     6     while (judge != 0)
     7     {
     8 
     9         cout << "Menu:A(dd) D(elete) S(ort) Q(uit), Select one:" << endl;
    10         char s;
    11         cin >> s;
    12         switch (s)
    13         {
    14 
    15         case 'A':
    16         {
    17             cout << "Date has added" << endl; break; }
    18 
    19         case 'D':
    20         {
    21             cout << "Date has deleted" << endl; break; }
    22 
    23         case 'S':
    24         {
    25             cout << "Date has sorted" << endl; break; }
    26 
    27         case 'Q':
    28         {
    29             judge = 0; break; }
    30 
    31         default:cout << "Wrong directive, please try agine" << endl;
    32         }
    33     }
    34     return 0;
    35 }

    运行结果截图:

    2-29 /*问题描述:用穷举法找出1--100间的质数并显示出来。分别使用while,do...while,for循环结构语句实现*/

    # while循环结构:

     1 #include<iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int i=2,k=0;
     6     while(i<=100)
     7     {
     8         int j=2;
     9         while(j<i)
    10         {
    11             if(i%j==0)
    12             break;
    13             else
    14             j++;
    15         }
    16          if(i<=j)
    17          {
    18              cout<<i<<"   ";
    19              k++;
    20              if(k%6==0)
    21              cout<<endl;
    22          }
    23          i++;
    24     }
    25     return 0;
    26 }

    #do...white循环结构:

     1 #include<iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int i=2,k=0;
     6     do
     7     {
     8         int j=2;
     9         do
    10         {
    11             if(i%j==0)
    12             break;
    13             else
    14             j++;
    15         }while(j<=i);
    16         if(i==j)
    17         {
    18             cout<<i<<"   ";
    19             k++;
    20             if(k%6==0)
    21             cout<<endl;
    22             
    23         }
    24         i++;
    25         
    26     }while(i<=100);
    27     return 0;
    28 } 

    #for循环结构:

     1 #include<iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int i=2,j,k=0;
     6     for(i;i<=100;i++)
     7     {
     8         for(j=2;j<=i;j++)
     9             if(i%j==0)
    10             break;
    11             
    12         if(i==j)
    13         {
    14             cout<<i<<"   ";
    15             k++;
    16             if(k%6==0)
    17             cout<<endl;
    18         } 
    19     }
    20     return 0;
    21 }

    运行结果截图:

    2-32 /*问题描述:在一个程序中定义一个整形变量,赋予1--100的值。要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。分别使用while,do...while语句实现循环*/

    #while循环结构:

     1 /*程序每次生成的数字和当前的时间有关,
     2 而时间是变化的,所以每次的数字也不同*/ 
     3 #include<cstdlib>
     4 #include<iostream>
     5 #include<ctime>/*引用time*/ 
     6 using namespace std;
     7 int main()
     8 {
     9     srand ( time(0));/*使用当前时间作为随机生成器的种子*/ 
    10     int random = rand();
    11     int a=1+rand()%100;/*可随机取得0-100之间的任意数*/ 
    12     int gu_num;
    13     cout<<"please input you guess number(between 1-100):"<<endl;
    14     cin>>gu_num;
    15     if(a==gu_num)
    16     cout<<"Conguratulations,you get it right"<<endl;
    17     else
    18     while(a!=gu_num)
    19     {
    20         if(a>gu_num)
    21         cout<<"Bigger than you guessed, please guess again"<<endl;
    22         else
    23         cout<<"Lower than you guessed, please guess again"<<endl;
    24         cin>>gu_num;
    25     }
    26     cout<<"Conguratulations,you get it right"<<endl;
    27     return 0;
    28 }

    #do...while循环结构:

     1 #include<iostream>
     2 #include<cstdlib>
     3 #include<ctime>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     srand ( time(0));
     9     int random = rand();
    10     int a=1+rand()%100; 
    11     int gu_num;
    12     cout<<"please input you guess number(between 1-100):";
    13     cin>>gu_num;
    14     
    15     if(a==gu_num)
    16     cout<<"Conguratulations,you get it right"<<endl;
    17     
    18     else
    19     do
    20     {
    21         if(a>gu_num)
    22         cout<<"Bigger than you guessed, please guess again"<<endl;
    23         
    24         else
    25         cout<<"Lower than you guessed, please guess again"<<endl;
    26         cin>>gu_num;
    27     }while(a!=gu_num);
    28     
    29     cout<<"Conguratulations,you get it right"<<endl;
    30     return 0;
    31 }

    运行结果截图:

    2-34/*问题描述:口袋中有红黄蓝白黑5种颜色的球若干个。每次从口袋中取出3个不同颜色的球,问有多少种取法?*/

    分析问题是组合数型问题,采用组合数公式Cnm=n!/m!/(n-m)!来计算方法总数,为了提高代码重用性,这里引入新的变量来表示总数与取数。

    代码展示:

     1 /*分析问题本质是一个组合数,可以采用简单的
     2 组合数计算公式  n!/(m!*(n-m)!) ,如此,可以
     3 定义一个阶乘函数*/ 
     4 #include<iostream>
     5 using namespace std;
     6 
     7 long factorial(long );//阶乘函数声明 
     8  
     9 int main()
    10 {
    11     int m,n;
    12     cout<<"请输入不同球的种类数与每次取出的球数:"; 
    13     cin>>n>>m;
    14     cout<<factorial(n)/(factorial(m)*factorial(n-m))<<endl;
    15     return 0;
    16 }
    17 
    18 long factorial(long x)//阶乘函数定义 
    19 {
    20     int num=1;
    21     for(int i=1;i<=x;i++)
    22     num*=i;
    23     return num;
    24 }

    运行结果截图:

    实验小结:

    1、通过这四个实验,较好的复习了C语言的基础的选择,循环结构。同时掌握了C++基本的输入,输出的方式。

    2、自主学习所得:本次实验收获较大的是学会了引入随机数,并且在过程中了解了伪随机数及如何避免重复得到同一个随机数。对比书本上提供的种子方法,我更倾向于引入time()。

         此处粘贴关于time()用法的链接 /*https://en.cppreference.com/w/cpp/numeric/random/srand*/。

    作者:爱因斯坦PLUS。

  • 相关阅读:
    [论文笔记]CVPR2017_Joint Detection and Identification Feature Learning for Person Search
    [论文笔记]Objects as Points
    [论文笔记]ICCV2017_SVDNet for Pedestrian Retrieval
    [论文笔记]ICPR2016_Person Re-Identification Using CNN Features Learned from Combination of Attributes
    VMware ESXI6.0服务器安装系列:RAID设置
    LVM基础详细说明及动态扩容lvm逻辑卷的操作记录
    调用对象 “ha-datastoresystem”的“HostDatastoreSystem.QueryVmfsDatastoreCreateOptions” 失败。
    动态扩容lvm逻辑卷的操作记录
    Kubernetes之Flannel介绍
    Linux服务器同步网络时间
  • 原文地址:https://www.cnblogs.com/shenqidetao/p/10539780.html
Copyright © 2020-2023  润新知