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

//这个编辑器一言难尽

实验目的


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:c++简单程序设计(1)的更多相关文章

  1. 实验一 c++简单程序设计

    一.实验内容 1.ex 2_28 (1) 用if...else判断 #include<iostream> using namespace std; int main() { char i; ...

  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. VMware仅主机模式访问外网

    原文转载至:https://blog.csdn.net/eussi/article/details/79054622 保证VMware Network Adapter VMnet1是启用状态  将可以 ...

  2. redis+keepalived安装

    安装redis 我这里装的是一主三从,其中有一个从一直不能切换到主,所以这台机器上不需要配置keepalived,只需要在redis.conf文件配置上加上slaveof 20.200.45.95 6 ...

  3. tensorflow函数学习:队列和线程

    队列函数: FIFOQueue和RandomShuffleQueue 1.FIFOQueue FIFOQueue是先进先出队列,主要针对序列样本.如:使用循环神经网络时,需要处理语音.文字.视频等序列 ...

  4. 用 tornado 做网站 (7)

    转自:http://wiki.jikexueyuan.com/project/start-learning-python/309.html 用 tornado 做网站 (7) 到上一节结束,其实读者已 ...

  5. 如何使用App.config文件,读取字符串?

    如何使用App.config文件,读取字符串? .在项目里添加App.config文件,内容如下: <?xml version="1.0" encoding="ut ...

  6. 基于WMI获取本机真实网卡物理地址和IP地址

    using System; using System.Collections.Generic; using System.Management; using System.Runtime.Intero ...

  7. Python中属性和描述符的简单使用

    Python的描述符和属性是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题苦恼的 ...

  8. TortoiseGit上传项目到GitHub

    1. 简介 gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub. 2. 准备 2.1  安装git:https://git-scm.c ...

  9. angularjs 整合 bootstrap

    第一步 :下载 bootstrap  jquery  ppper.js npm install bootstrap@4.0.0-beta.2 jquery popper.js --save 第二步: ...

  10. leetcode342

    public class Solution { public bool IsPowerOfFour(int num) { ) && ((num & (num - )) == ) ...