实验内容:

2-28

实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入。A表示增加,D表示删除,

S表示排序,Q表示退出。输入为A、D、S时分别提示“数据已经增加、删除、排序。”,输入Q时程序结束。

以下分别是我用两种语句写出的简单菜单程序:

  • if...else语句
  • #include<iostream>
    #include<cstdlib>
    using namespace std;
    int main()
    {
    char option;
    while()
    {
    cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Selet one:";
    cin>>option;
    if(option=='A')
    {
    cout<<"data has added"<<endl;
    continue;
    }
    else if(option=='D')
    {
    cout<<"data has deleted"<<endl;
    continue;
    }
    else if(option=='S')
    {
    cout<<"data has sorted"<<endl;
    continue;
    }
    else if(option=='Q')
    break;
    else
    {
    cout<<"There is a mistake in the input,please re-enter."<<endl;
    continue;
    }
    }
    }
  • switch语句
  • #include<iostream>
    #include<cstdlib>
    using namespace std;
    int main()
    {
    char option;
    while()
    {
    cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Selet one:";
    cin>>option;
    switch(option)
    {
    case'A':cout<<"data has added "<<endl;break;
    case'D':cout<<"data has deleted"<<endl;break;
    case'S':cout<<"data has sorted"<<endl;break;
    case'Q':exit();break;
    default:cout<<"There is a mistake in the input,please re-enter."<<endl;
    } }
    return ;
    }

2-29

用穷举法找出1~100间的质数并显示出来。

这个程序我用了while和for循环两种实现方法。算法设计:用一个数除以2到sqrt这个数,其中只要有一个能被整除,那这个数就不是素数,反之是素数。具体代码如下:

  • while循环
  • #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
    int i,j,k,sign;
    i=;
    while(i<=)
    {
    sign=;
    k=sqrt(i);
    j=;
    while(j<=k)
    {
    if(i%j==)
    {
    sign=;
    break;
    }
    j++;
    }
    if(sign)
    cout<<i<<endl;
    i++;
    }
    }
  • for循环
  • #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
    int i,j,k,a;
    for(i=;i<=;i++)
    {
    a=;
    k=sqrt(i);
    for(j=;j<=k;j++)
    {
    if(i%j==)
    {
    a=;
    break;
    }
    }
    if(a)
    cout<<i<<endl;
    }
    }

    运行结果如下:

发现这些素数的排列不美观,于是我用setw()函数美化了一下这些数的排列方式,代码及运行结果如下:

#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
int i,j,k,sign,m=;
for(i=;i<=;i++)
{
sign=;
k=sqrt(i);
for(j=;j<=k;j++)
{
if(i%j==)
{
sign=;
break;
}
}
if(sign)
{
for(i=;i<=;i++)
{
m++;
if(m%!=)
cout<<setw()<<i;
if(m%==)
cout<<setw()<<i<<endl;
}
}
}
}

这个实验题我只用了两种循环语句,do...while和while语句相似,就不拿来占篇幅了。关于这些循环语句不做过多陈述。

2-32

在程序中定义一个整型变量,赋以1~100的值,要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。

这个题目我才开始做的时候是自己在程序里定义了用户要猜的数,这样程序的重复利用率就比较低,先放源码:

  • # include<iostream>
    using namespace std;
    int main()
    {
    int guessnumber=;
    int m=;
    while(m!=guessnumber)
    {
    cout<<"please guess the number(1~100):";
    cin>>m;
    if(m>guessnumber)
    cout<<"You guessed a big number"<<endl;
    else if(m<guessnumber)
    cout<<"You guessed a small number"<<endl;
    else
    cout<<"You has guessed the number!"<<endl;
    }
    }

    考虑到程序的实用价值,进行了一次小升级:

  • # include<iostream>
    using namespace std;
    int main()
    {
    int guessnumber=;
    int m=;
    while(m!=guessnumber)
    {
    cout<<"please guess the number(1~100):";
    cin>>m;
    if(m>guessnumber)
    {
    int i;
    i=m-guessnumber;
    if(i>=)
    cout<<"Your guessnumber is too big"<<endl;
    else
    cout<<"Your guessnumber is very close,but it's still a little big"<<endl; }
    else if(m<guessnumber)
    {
    int j;
    j=guessnumber-m;
    if(j>=)
    cout<<"Your guessnumber is too small"<<endl;
    else
    cout<<"Your guessnumber is very close,but it's still a little small"<<endl;
    }
    else
    cout<<"You has guessed the number!"<<endl;
    }
    }

    两个程序运行结果如下:

改变不大,但是从甲方考虑,显然2更适合。

在第三次升级的时候,考虑到程序的实用性,我加入了随机数,并允许在用户猜对数字后重开(清屏),給用户更多选择,下面是升级的代码及运行结果:

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main() {
srand(time());
int m= rand() % + ;//将随机数控制在1-100之间
int guessnumber;//用户输入的数字
char choice;//作出选择,是否继续
cout<< "Hello,master.My name is Xiao U."<<endl
<<"I had a problems,can you help me?The method is simple."<<endl
<<"Here's a bunch of numbers(0~100),but there's only one right,you should find the right number."<<endl
<<"Starting right now"<<endl;
do{
cout<< "Please enter a number: ";
cin>> guessnumber;
if( guessnumber >m)
{
cout<<"You guessed too high!,it's a pity";
}
if( guessnumber <m)
{
cout<<"You guessed too low!,it's a pity!";
}
if( guessnumber ==m)
{
cout<< "You get it!You have helped me solve the problem. HaHa,The number is: " <<m<<endl;
cout<< "Do you want to play again?(Y/N):";
cin>>choice;
if(choice== 'Y')
{
system("cls");//清空屏幕
cout<<"welcome back,master!"<<endl;
continue;
}
else if(choice== 'N')
{
cout<< "bye,master.";
break;
}
else
{
cout<< "You entered a wrong character! program will exit!";
break;
}
}
}while();
system("pause");
return EXIT_SUCCESS;//结束程序
}

2-34

口袋中有红黄蓝白黑5种颜色的球若干个。没词葱口袋中取出3个颜色不同的球,问有多少种取法。

代码如下:

#include<iostream>
using namespace std;
int fac(int x)
{
int i,j;
j=;
for(i=;i<=x;i++)
j*=i;
return j;
}
int main()
{
int al;
int a,b,c,d,e;
cout<<"Please enter the number of each colour ball" <<endl;
cout<<"red,yellow,blue,white,black:";
while(cin>> a>> b>> c>> d>> e)
{
al=fac()/fac()/fac()*a*b*c*d*e;
cout<<"There are"<<al<<"methods to choose three kinds of the balls";
system("pause");
}
return ;
}

运行结果:

这一题我还写了一个拓展版,将5个球拓展到自定义的情况,但是不符合题目要求,就不放代码了~

实验反思:

  • 2-28这是一个很简单的c++基础编程题,但是关于这一题还是有一些话说,作为一个伪小白,平时会阅读一些别人写的程序,关于if语句,个人很反对把复杂的难以理解的代码语句放到if的条件里面,这会严重影响到代码阅读,尽管很多时候在语法和逻辑上没有问题。if...else语句还要注意的一个问题就是如何用break和continue控制程序流程,这里不多说。关于switch语句,它的目的,学过c/c++的人应该都知道,就是为了实现深层嵌套的if...else逻辑,switch语句我觉得比较重要的就是定义变量方面的问题,switch结构只能在最后一个标号(这个标号可以是case或default)中定义变量。除此以外,没有特别需要说明的地方。
  • 2-32关于最后升级版的程序,我查了关于srand()函数和清屏函数的相关内容,这个升级给用户提供了选择继续还是再开的选择,而且在程序实用性上也得到了加强,相比于前两个,显然这个价值更高。但是程序现在仍然存在一个bug,就是清屏后产生的随机数没有改变,还是和第一次一样,这样就达不到对这串代码的预期。由于这是课堂实验作业,时间有限,暂时先提交,在日后我会抓紧修改,如果有同学有好的想法,欢迎交流。还有就是,我想在这个程序内加一个计数器,记录重开的次数,有兴趣欢迎交流~

c++简单程序设计 实验一的更多相关文章

  1. 160809209_李梦鑫_C语言程序设计实验3 循环结构程序设计

    <C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学    期 2016-2017 第1学期 指导教师 黄俊莲 吉吉老师 实验地点 C05 ...

  2. 20145206《Java程序设计》实验二Java面向对象程序设计实验报告

    20145206<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O. ...

  3. 实验四 Android程序设计 实验报告

    实验四 Android程序设计 实验报告 目录 代码托管地址 Android程序设计-1 Android程序设计-2 Android程序设计-3 Android程序设计-4 Android程序设计-5 ...

  4. 20155326 实验四 Android程序设计实验报告

    20155326 实验四 Android程序设计实验报告 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局管理器的使用: 3. ...

  5. Java程序设计实验 实验五

    课程:Java程序设计实验   班级:1353  姓名:符余佳源  学号:20135321 成绩:                           指导教师:娄嘉鹏      实验日期:2015. ...

  6. 20155323 第四次实验 Android程序设计实验报告

    20155323 第四次实验 Android程序设计实验报告 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局管理器的使用: ...

  7. 实验四 Android程序设计 实验报告 20162305李昱兴

    实验四 Android程序设计 实验报告 20162305李昱兴 一.Android Studio的安装测试 1.有关该软件 Android Studio,是基于Itellij IDEA的一款流行的I ...

  8. C语言程序设计实验报告三

    C程序设计实验报告 姓 名:张美盛 实验地点:家 实验时间:2020年3月29日 实验项目:4.3.1 If语句的应用 4.3.2 switch-case的应用 4.3.3 switch-case嵌套 ...

  9. C语言程序设计实验报告四

    C程序设计实验报告 姓 名:赖瑾 实验地点:家 实验时间:2020年4月9日 实验项目:5.3.1练习2 求数列的前n项和 5.3.2练习2 求水仙花数 5.3.4 十进制转换 5.3.5练习1 百马 ...

随机推荐

  1. RxPermissions Usage

    refs:https://github.com/tbruyelle/RxPermissions https://www.jianshu.com/p/c3546e5cd2ffhttps://www.ji ...

  2. xlrd模块;xlwt模块使用,smtp发送邮件

    先安装 pip3 install xlwt pip3 install xlrd import xlwt, xlrd from xlrd.book import Book from xlrd.sheet ...

  3. idea报错:Invalid bound statement (not found)

    在配置MyBatis接口映射的Mapper.xml时,提示Invalid bound statement (not found)异常,就算是接口和xml名字相同,路径相同也无法找到,在网上找到了几种解 ...

  4. Mapreduce中maptask过程详解

    一.Maptask并行度与决定机制 1.一个job任务的map阶段的并行度默认是由该任务的大小决定的: 2.一个split切分分配一个maprask来并行处理: 3.默认情况下,split切分的大小等 ...

  5. 教你正确打开async/await关键字的使用

    这段时间在项目开发中看到了一些async/await的使用,在aspnet core的host组件源码中也看到了许多的async/await代码.在开发时,正确的使用了async/await是可以提高 ...

  6. (三)ajax请求不同源之websocket跨域

    WebSocket是一种通信协议,使用ws://(非加密)和wss://(加密)作为协议前缀.该协议不实行同源政策,只要服务器支持,就可以通过它进行跨源通信. 一.WebSocket目标 在一个单独的 ...

  7. BZOJ.2780.[SPOJ8093]Sevenk Love Oimaster(广义后缀自动机)

    题目链接 \(Description\) 给定n个模式串,多次询问一个串在多少个模式串中出现过.(字符集为26个小写字母) \(Solution\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...

  8. 【安全性测试】Android测试中的一点小发现

    在执行某个项目中的APP测试发现的两个问题,自然也是提供参考,作为经验记录下来. 一.通过apk的xml文件获取到某项目APP的账号和密码 使用eclipsel或者drozer,获得apk的xml文件 ...

  9. Eclipse导出自己的项目(仅供自己保留方式war包)

    War: Jar包:

  10. oracle统计数据时,涉及两个表的数据

    SELECT t1.*,a.num FROM (SELECT SUM(t.total_profit) total_profit, SUM(t.main_business_income) main_bu ...