一、实验内容

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. MYISM表并发写请求过多 导致无法被读取解决方案

    MyISAM锁调度是如何实现的呢,这也是一个很关键的问题.例如,当一个进程请求某个MyISAM表的读锁,同时另一个进程也请求同一表的写锁,此时MySQL将会如优先处理进程呢?通过研究表明,写进程将先获 ...

  2. qt学习(二) buttong layout spinbox slider 示例

    开启qt5 creator 新建项目 qt widgets 改写main.cpp #include "mainwindow.h" #include <QApplication ...

  3. mongo学习- mapReduce操作事例

    源数据: { "_id" : 1.0, "name" : "abc", "age" : 43.0, "type ...

  4. wcf服务编程(第3版)文摘

    第1章 wcf基础 什么是wcf: System.ServiceModel.dll 服务 服务的执行边界: proxy 地址:http/https,tcp,ipc,peer newwork,msmq, ...

  5. Register A Callback To Handle SQLITE_BUSY Errors(译)

    http://www.sqlite.org/c3ref/busy_handler.html留着自己看的. Register A Callback To Handle SQLITE_BUSY Error ...

  6. ZOJ3770Ranking System 2017-04-14 12:42 52人阅读 评论(0) 收藏

    Ranking System Time Limit: 2 Seconds      Memory Limit: 65536 KB Few weeks ago, a famous software co ...

  7. npm是干什么的?

    允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用. 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用. 其实npm ...

  8. linux常见命令-查看磁盘空间

    linux查看磁盘使用情况命令 1. 统一每个目录下磁盘的整体情况: df -h 2. 查看指定目录,在命令后直接放目录名,比如查看“usr”目录使用情况:df -h  /usr/ 3. 查看当前目录 ...

  9. [leetcode] 11. Same Tree

    因为我刷题是难度不是按发布日期,所以就有可能遇到这种情况,比如这个... Given two binary trees, write a function to check if they are e ...

  10. task4:结对项目-词频统计

    结对人:周楠 思路:利用TreeMap实现key字典序,然后输出到LinkedList,然后用Comparator,实现字典值从大到小排序,但是key实现值相同的key字典序的想出的实现方法,但是一直 ...