一、实验内容

1、ex 2_28

(1) 用if...else判断

 #include<iostream>
using namespace std;
int main()
{
char i;
cout<<"Menu: A(dd) D(elect) S(ort) Q(uit), Select one:";
while(cin>>i)
{
if(i=='A')
cout<<"Data has been added."<<endl;
else if(i=='D')
cout<<"Data has been delected."<<endl;
else if(i=='S')
cout<<"Data has been sorted."<<endl;
else if(i=='Q') exit();
else
cout<<"Have no choice,please select another one."<<endl;
cout<<"Menu: A(dd) D(elect) S(ort) Q(uit), Select one:";
}
return ;
}

运行结果如下:

(2)用switch语句

 #include<iostream>
using namespace std;
int main()
{
char i;
cout<<"Menu: A(dd) D(elect) S(ort) Q(uit), Select one:";
while(cin>>i)
{
switch(i)
{
case 'A':cout<<"Data has been added."<<endl;break;
case 'D':cout<<"Data has been delected."<<endl;break;
case 'S':cout<<"Data has been sorted."<<endl;break;
case 'Q':exit();
default:cout<<"have no choice,please select another one."<<endl;
}
cout<<"Menu: A(dd) D(elect) S(ort) Q(uit), Select one:";
}
return ;
}

结果同上。

2、ex 2_29

(1)while循环

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i=,j,k=;
while(i<=)
{
j=;
while(j<=i)
{
if(i%j==) break;
j++;
}
if(i==j)
{
cout<<setw()<<i;
k++;
}
i++;
if(k%==) cout<<endl;
}
return ;
}

结果如下:

(2)do...while循环

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i=,j,k=;
do
{
j=;
do
{
if(i%j==) break;
j++;
}while(j<=i);
if(i==j)
{
cout<<setw()<<i;
k++;
}
i++;
if(k%==) cout<<endl;
}while(i<=);
return ;
}

(3)for循环

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j,k=;
for(i=;i<=;i++)
{
for(j=;j<=i;j++)
if(i%j==) break;
if(i==j)
{
cout<<setw()<<i;
k++;
}
if(k%==) cout<<endl;
}
return ;
}

3、ex 2_32

(1)while循环

 #include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time());
int m=rand()%+,n; cout<<"Please write down the number you guess:";
while(cin>>n)
{
if(m<n)
cout<<"What you guess is bigger than the number."<<endl;
if(m>n)
cout<<"What you guess is smaller than the number."<<endl;
if(m==n)
{cout<<"Congratulation!"<<endl;break;}
cout<<"Please write down the number you guess:";
}
return ;
}

(2)do...while循环

 #include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time());
int m=rand()%+,n; cout<<"Please write down the number you guess:";
do
{
cin>>n;
if(m<n)
cout<<"What you guess is bigger than the number."<<endl;
if(m>n)
cout<<"What you guess is smaller than the number."<<endl;
if(m==n)
{cout<<"Congratulation!"<<endl;break;}
cout<<"Please write down the number you guess:";
}while(m!=n);
return ;
}

效果如下:

 4、ex 2_34

 #include<iostream>
#include<iomanip>
using namespace std;
enum Balls{red,yellow,blue,white,black};
void Print(int a)
{
switch(a)
{
case :cout<<setw()<<"red";break;
case :cout<<setw()<<"yellow";break;
case :cout<<setw()<<"blue";break;
case :cout<<setw()<<"white";break;
case :cout<<setw()<<"black";break;
}
}
int main()
{
int n=,i,j,k;
for(i=red;i<=blue;i++)
{
for(j=i+;j<=white;j++)
{ for(k=j+;k<=black;k++)
{
Print(i);Print(j);Print(k);
cout<<endl;
n++;
}
}
}
cout<<"There are "<<n<<" ways in all."<<endl;
return ;
}

效果如下:

二、实验反思

1、注意"A"与'A'的区别,"A"表示字符串,'A'表示字符,比较的是ASCII码。

2、三种循环各具特点,用前应思考其作用,明显看出29题用for循环简洁。

3、生成随机数时,注意头文件cstdlib,ctime;设置域宽时,注意头文件iomanip。

4、使用枚举类型,返回球颜色时,我使用一个了函数将数字与颜色对应。

三、实验小评

https://www.cnblogs.com/fifi1224/p/10555549.html

https://www.cnblogs.com/yinyinzuinihai/p/10556280.html#4213197

https://www.cnblogs.com/sora5934/p/10562100.html

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

  1. 实验1:c++简单程序设计(1)

    //文中有格式错误请无视 //这个编辑器一言难尽 实验目的 1. 掌握c++中类c部分的编程知识: 数据类型,常量,变量,运算符,表达式,分支结构,循环结构 2. 掌握C++中数据输入和输出的基本方法 ...

  2. 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...

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

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

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

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

  5. 20145208 实验三 Java面向对象程序设计

    20145208 实验三 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...

  6. 20162330 实验四 《Android程序设计》 实验报告

    2016-2017-2 实验报告目录: 1 2 3 4 5 20162330 实验四 <Android程序设计> 实验报告 课程名称:<程序设计与数据结构> 学生班级:1623 ...

  7. 20162302 实验四《Android程序设计》实验报告

    实 验 报 告 课程:程序设计与数据结构 姓名:杨京典 班级:1623 学号:20162302 实验名称:Android程序设计 实验器材:装有Android Studio的联想拯救者80RQ 实验目 ...

  8. java实验四《Android程序设计》实验报告

    一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:张士洋 学号:20165308 指导教师:娄嘉鹏 实验日期:2018年5月14日 实验时间:13:45 - 15:25 实验序号:08 ...

  9. 2017-2018-2 20165312 实验四《Android程序设计》实验报告

    2017-2018-2 20165312 实验四<Android程序设计>实验报告 一.安装Android Studio并进行Hello world测试和调试程序 安装Android St ...

随机推荐

  1. Linux automake命令

    1)automake 使用教程 http://loftor.com/archives/automake.html 2)configure.in Makefile.am解析 http://blog.cs ...

  2. [Training Video - 5] [Groovy Script Test Step - Collections, Exceptions] Array and ArrayList

    Array: def x = new String[5] x[0] = "India" x[1] = "USA" x[2] = "Korea" ...

  3. jfinal框架教程

    jfinal框架教程 下面通过一个小例子了解jfinal的结构和特点 1.建数据库(我用的是oracle数据库,其他的相对也差不多) -- Create table create table CLAS ...

  4. HTML5 本地存储+layer弹层组件制作记事本

    什么是 HTML5 Web 存储? 使用HTML5可以在本地存储用户的浏览数据. 早些时候,本地存储使用的是 cookie.但是Web 存储需要更加的安全与快速. 这些数据不会被保存在服务器上,但是这 ...

  5. IntelliJ IDEA 2017版开发SpringBoot之fastJsonHttpMessageConvert使用

    继承WebMvcConfigurerAdapter,改写成自己的json转换工具的写法 1.建立实体类 package com.fastjson; import com.alibaba.fastjso ...

  6. Redis是可以安装成windows服务-开机自启 win7 64位

    其实Redis是可以安装成windows服务的,开机自启动,命令如下: redis-server --service-install redis.windows.conf 安装完之后,就可看到Redi ...

  7. IKAnalyzer兼容Lucene 5.4.0版本抛出异常?

    ava.lang.AbstractMethodError: org.apache.lucene.analysis.Analyzer.createComponents(Ljava/lang/String ...

  8. solrconfig.xml配置详解

    solrconfig.xml配置文件主要定义了SOLR的一些处理规则,包括索引数据的存放位置,更新,删除,查询的一些规则配置. 可以在tomcat的安装路径下找到这个文件C:\Program File ...

  9. C#实现AStar寻路算法

    AStar寻路算法是一种在一个静态路网中寻找最短路径的算法,也是在游戏开发中最常用到的寻路算法之一:最近刚好需要用到寻路算法,因此把自己的实现过程记录下来. 先直接上可视化之后的效果图,图中黑色方格代 ...

  10. ASP.NET添加Mysql数据源

    在ASP.NET的数据源控件上添加mysql数据库连接,首先需要在windows系统下添加mysql的数据源才能在vs中添加数据源 1.在控制面板下打开系统与安全-->打开管理工具-->点 ...