一、实验内容

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. 从YouTube改版看“移动优先”——8个移动优先网站设计案例赏析

    2011年,Luke Wroblewski大神提出了移动优先的设计理念.在当时看来这无疑是一个打破行业常规的新型设计原则.而在移动互联网大行其道的今天,谁遵守移动优先的设计理念,设计出最好的移动端网站 ...

  2. Java程序设计8——抽象类、接口与内部类

    1 抽象类 当编写一个类时,常常会为该类定义一些方法,这些方法用以描述该类的行为方式,那么这些方法都有具体的方法体.但在某些情况下,某个父类并不需要实现,因为它只需要当做一个模板,而具体的实现,可以由 ...

  3. http://angular.github.io/router/

    Angular New Router Guide Configuring the Router- This guide shows the many ways to map URLs to compo ...

  4. 从Objective-C到Swift 单例模式

    在Objective-C中经常会用到单例模式.最常见的就是: [UIApplication sharedApplication].delegate 这里的sharedApplication就是一个返回 ...

  5. .NET基础 (13)IFormattable和IformatProvider的使用

    IFormattable和IformatProvider的使用1 如何使用IFormattable接口实现格式化输出2 如何告诉类型格式化输出的方式 IFormattable和IformatProvi ...

  6. 机器学习—集成学习(GBDT)

    一.原理部分: 图片形式~ 二.sklearn实现: 可以看看这个:https://blog.csdn.net/han_xiaoyang/article/details/52663170 1.分类: ...

  7. handsontable-developer guide-load and save

    不过handsontable不能用jquery取对象 var $$ = function(id) { return document.getElementById(id); }, container ...

  8. Java Web系列:JAAS认证和授权基础

    1.认证和授权概述 (1)认证:对用户的身份进行验证. .NET基于的RBS(参考1)的认证和授权相关的核心是2个接口System.Security.Principal.IPrincipal和Syst ...

  9. Object对象方法 cheet sheet

    defineProperty create Object.create(prototype [, propertiesObject ]) prototype:没什么可说的,指定对象的原型 proper ...

  10. html开发基础

    1 Doctype Doctype告诉浏览器使用什么样的html或xhtml规范来解析html文档 有和无的区别 BackCompat:标准兼容模式未开启(或叫怪异模式[Quirks mode].混杂 ...