//文中有格式错误请无视

//这个编辑器一言难尽

实验目的


1. 掌握c++中类c部分的编程知识: 数据类型,常量,变量,运算符,表达式,分支结构,循环结构

2. 掌握C++中数据输入和输出的基本方法

3. 熟练使用c++程序开发环境,掌握c++程序编写、编译、运行、调试的方法

实验准备


1. 简单的c++程序结构 学习/复习教材「2.1.3 C++程序实例」

2. c++中数据输入输出的基本方法 学习/复习教材2.3节,学习C++中I/O流、预定义的插入符<<和提取符>>的基本用法。

3. if语句、switch语句、while语句、do…while语句的用法 学习/复习教材2.4节,通过示例理解背后简单算法及c++分支语句、循环语句的用法

4. 自定义数据类型: typedef,枚举类型用法 学习/复习教材2.5节,结合示例理解枚举类型和int型在类型转换时的注意事项

实验内容


Part2: 编程练习

教材第2章习题2-28 简单的菜单程序

教材第2章习题2-29 穷举法求质数

教材第2章习题2-32 猜数

教材第2章习题2-34排列组合

实验结论


2-28简单的菜单程序(if-else

Code:

 #include<iostream>
using namespace std;
int main()
{
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit)";
cout<<"Select one:";
char k;
while(cin>>k)
{
if(k=='A')
cout<<"Data has added.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
else if(k=='D')
cout<<"Data has deleted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
else if(k=='S')
cout<<"Data has sorted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
else if(k=='Q')
break;
else
cout<<"No such choice,please select again.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
}
return ;
}

Screenshot:

(switch

Code:

 #include<iostream>
using namespace std;
int main()
{
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit)";
cout<<"Select one:";
char k;
while(cin>>k&&k!='Q')
{
switch(k)
{
case 'A':
cout<<"Data has added.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
continue;
case 'D':
cout<<"Data has deleted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
continue;
case 'S':
cout<<"Data has sorted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
continue;
default:cout<<"No such choice,please select again.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
}
}
return ;
}

Screenshop:

2-29 穷举法求质数(while

Code:

 #include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
cout<<"1~100间的质数为:"<<endl;
int x=;
int a,b,c=;
while(x<=)
{
a=sqrt(x);
if(x!=)
{
b=;
while(x%b!=&&b<=a)
b++;
if(b>a)
{
c++;
cout<<setw()<<x;
if(c%==)
cout<<"\n";
}
}
x++;
}
return ;
}

Screenshop:

(for

Code:

 #include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
cout<<"1~100间的质数为:"<<endl;
int x;
int a,b,c=;
for(x=;x<=;x++)
{
a=sqrt(x);
if(x==)
continue;
else
{
for(b=;b<=a;b++)
if(x%b==)
break;
if(b>a)
{
c++;
cout<<setw()<<x;
if(c%==)
cout<<"\n";
}
}
}
return ;
}

Screenshop:

(do-while

Code:

 #include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
cout<<"1~100间的质数为:"<<endl;
int x=;
int a,b,c=;
do
{
x++;
a=sqrt(x);
if(x!=)
{
b=;
do
{
b++;
}while(x%b!=&&b<=a);
if(b>a)
{
c++;
cout<<setw()<<x;
if(c%==)
cout<<"\n";
}
}
}while(x<);
return ;
}
 

Screenshop:

2-32 猜数(while

Code:

 #include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int x;
int a;
srand(time(NULL));
x=+rand()%(-+);
cout<<"Your guess number is(1~100): ";
cin>>a;
while(a!=x)
{
if(a<x)
{
cout<<"Bigger than the number."<<endl;
cout<<"Your guess number is(1~100): ";
cin>>a;
}
else
{
cout<<"Lower than the number. "<<endl;
cout<<"Your guess number is(1~100): ";
cin>>a;
}
}
cout<<"Congretulations.You guessed it.~";
return ;
}

Screenshop:

(do-while

Code:

 #include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int x;
int a;
srand(time(NULL));
x=+rand()%(-+);
cout<<"Your guess number is(1~100): ";
do
{
cin>>a;
if(a<x)
{
cout<<"Bigger than the number."<<endl;
cout<<"Your guess number is(1~100): ";
}
else if(a>x)
{
cout<<"Lower than the number. "<<endl;
cout<<"Your guess number is(1~100): ";
}
}while(a!=x);
cout<<"Congretulations.You guessed it.~";
return ;
}

Screenshop:

//暴力循环

2-34排列组合(排列

Code:

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x[],k=,i;
for(x[]=;x[]<=;x[]++)
{
for(x[]=;x[]<=;x[]++)
{
if(x[]==x[])
continue;
else
{
for(x[]=;x[]<=;x[]++)
{
if(x[]==x[]||x[]==x[])
continue;
else
{
k++;
for(i=;i<=;i++)
{
if(i!=)
{
switch(x[i])
{
case :
cout<<"red ";
continue;
case :
cout<<"yellow ";
continue;
case :
cout<<"blue ";
continue;
case :
cout<<"white ";
continue;
case :
cout<<"black ";
}
}
else
{
switch(x[i])
{
case :
cout<<"red\n";
continue;
case :
cout<<"yellow\n";
continue;
case :
cout<<"blue\n";
continue;
case :
cout<<"white\n";
continue;
case :
cout<<"black\n";
}
}
}
}
}
}
}
}
cout<<"Total: "<<k;
return ;
}

Screenshop:

(组合

Code:

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x[],k=,i;
for(x[]=;x[]<=;x[]++)
for(x[]=x[]+;x[]<=;x[]++)
for(x[]=x[]+;x[]<=;x[]++)
{
k++;
for(i=;i<=;i++)
{
if(i!=)
{
switch(x[i])
{
case :
cout<<"red ";
continue;
case :
cout<<"yellow ";
continue;
case :
cout<<"blue ";
continue;
case :
cout<<"white ";
continue;
case :
cout<<"black ";
}
}
else
{
switch(x[i])
{
case :
cout<<"red\n";
continue;
case :
cout<<"yellow\n";
continue;
case :
cout<<"blue\n";
continue;
case :
cout<<"white\n";
continue;
case :
cout<<"black\n";
}
}
}
}
cout<<"Total: "<<k;
return ;
}

Screenshop:

实验总结


这次试验主要是类C的部分,CPP的特性我还没有完全体会到(从I/O流已经可以看出些特点的程度)。

在进行多次判断时还是用switch更方便。

多数时候for循环较为好用。

写程序时尽量避免嵌套过多层循环,严重拖慢编译和运行速度。

srand(time(NULL));

x=1+rand()%(100-1+1);

以此来取一定范围内的随机数。

枚举数据类型不同于整型或字符型。

最新文章

  1. Jenkins 2.x版本的安装步骤(Windows)
  2. jQuery最佳实践(不断更新中...)
  3. oracle ebs request一直pending
  4. JS初学之-效果没出来怎么办?-alert函数测试
  5. android开发中遇到的bug
  6. 源代码jar包中中文注释乱码
  7. delphi xe5 android 开发数据访问server端(二)
  8. 写一个段落python代码推理list深浅
  9. windows 守护进程
  10. JavaScript ----------------- 原型式继承
  11. Android_Dialog cancle 和dismiss 区别
  12. 【原创】UVAOJ水题10025解题报告
  13. 要重定向 IO 流,Process 对象必须将 UseShellExecute 属性设置为 False。
  14. U3D 控件
  15. WinHTTrack Website Copier使用说明
  16. C++版 - 剑指offer 面试题22:栈的压入、弹出序列 题解
  17. python三大框架之一flask应用
  18. 接口测试 mock server 工具moco
  19. 【Android】第18章 位置服务和手机定位&mdash;本章示例主界面
  20. 用fritzing绘制arduino硬件连线图

热门文章

  1. 【BZOJ】1013 [JSOI2008]球形空间产生器sphere(高斯消元)
  2. 使用 Nmon 监控 Linux 的系统性能
  3. php基础和数据库
  4. Mysql-两表的连接,copy表,select的各种用法
  5. 红帽配置Centos仓库[红帽Redhat7替换Centos7网络源]
  6. PHP大小写是否敏感问题的汇总
  7. 完整版openlayer的例子及中文注释(完整中文版)
  8. Java工具类实现校验公民身份证的有效性
  9. .NET Framework 官方下载地址
  10. oracle 数据字典