实验一:c++简单程序设计(1)
实验结论
编程练习2-28
switch版源码:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Menu:A(dd) D(elete) S(ort) Q(uit)" << endl;
while (1)
{
char choice;
cin >> choice;
switch (choice)
{
case 'A':
cout << "Data has been added" << endl;
break;
case 'D':
cout << "Data has been deleted" << endl;
break;
case 'S':
cout << "Data has been sorted" << endl;
break;
}
if(choice=='Q')
break;
}
}
if版源码:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Menu:A(dd) D(elete) S(ort) Q(uit)" << endl;
while (1)
{
char choice;
cin >> choice;
if(choice=='A')
cout << "Data has been added" << endl;
if(choice=='D')
cout << "Data has been deleted" << endl;
if(choice=='S')
cout << "Data has been sorted" << endl;
if(choice=='Q')
break;
}
}
2-28运行截图:
编程练习2-29
do_while版源码:
#include <iostream>
using namespace std;
int main(void)
{
int prime = 2;
do
{
int i = 2;
do
{
if (prime % i == 0)
break;
i++;
} while (i <= prime);
if (prime == i)
cout
<< prime << endl;
prime++;
} while (prime <= 100);
cin.get();
}
while版源码
#include <iostream>
using namespace std;
int main(void)
{
int prime=2;
while (prime <= 100)
{
int i=2;
while (i <= prime)
{
if (prime % i == 0)
break;
i++;
}
if (prime == i)
cout << prime << endl;
prime++;
}
cin.get();
}
for版源码:
#include <iostream>
using namespace std;
int main(void)
{
int prime;
for (prime = 2; prime <= 100; prime++)
{
int i;
for (i = 2; i <= prime; i++)
{
if (prime % i == 0)
break;
}
if (prime == i)
cout << prime << endl;
}
cin.get();
}
2-29运行截图:
编程练习2-32
因为程序全程只有while(1)
循环内容 do while与while没有什么差异 故只写了一版
源码:
#include <ctime>
#include <iostream>
#include <random>
#include <windows.h>
using namespace std;
int main(void)
{
cout << "Game Start" << endl;
cout << "Enter\"0\" to exit." << endl;
cout << "Have Fun!" << endl;
Sleep(2000); //暂停2秒
while (1)
{
mt19937 gen(time(NULL)); //用时间作为种子
uniform_int_distribution<> dis(1, 100); //设定随机数的范围为1~100
int num2gess = dis(gen);
int numUgess;
cout << "Please enter the number(1~100) you gess" << endl;
while (1)
{
cin >> numUgess;
if (numUgess == 0)
break;
if (numUgess < num2gess)
cout << "The number you gess is too small! Try again!" << endl;
else if (numUgess == num2gess)
{
cout << "Bingo! Congratulation!" << endl;
break;
}
else
cout << "The number you gess is too large! Try again!" << endl;
}
if (numUgess == 0)
break;
}
}
2-32运行截图:
编程练习2-34
采用顺序无关的写法,并稍加拓展
//原题实现太简单 现将程序改为从m个颜色不同的小球中取n个(n<=m<=20)
#include <iostream>
using namespace std;
long long int fac(long long int num) //阶乘函数
{
long long int r = 1, i;
for (i = 1; i <= num; i++)
{
r *= i;
}
return r;
}
int main(void)
{
long long int Ball, Take;
cout << "Please enter the number of balls and how many balls you want to take." << endl;
cout << "Enter\"0\" to exit" << endl;
while (1)
{
cin >> Ball;
if (Ball == 0)
break;
cin >> Take;
cout << "There are " << fac(Ball) / fac(Take) / fac(Ball - Take) << " possibilities." << endl;
}
}
2-34威力加强版运行截图:
实验总结与体会
- 这四道编程练习题带我们回顾了c++中与c一样的语法知识,并使用了c++中新添加的特性,如:新添加的输入输出流函数与新的头文件。
- 同时我在翻阅往届学长学姐的blog时发现了很多新的知识,万能头文件
#include<bits/stdc++.h>
在这里推荐给大家 - 我在做编程练习2-23时查阅了一些资料发现在c++11标准中新加了
uniform_int_distribution
函数用于生成整数随机数,用法我会贴在下方的引用栏中
cppreference:uniform_int_distribution
CSDN:用时间作为生成随机数的种子
CSDN:如何在c++中暂停特定时间
实验一:c++简单程序设计(1)的更多相关文章
- 实验一 c++简单程序设计
一.实验内容 1.ex 2_28 (1) 用if...else判断 #include<iostream> using namespace std; int main() { char i; ...
- 实验1:c++简单程序设计(1)
//文中有格式错误请无视 //这个编辑器一言难尽 实验目的 1. 掌握c++中类c部分的编程知识: 数据类型,常量,变量,运算符,表达式,分支结构,循环结构 2. 掌握C++中数据输入和输出的基本方法 ...
- 实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...
- 160809209_李梦鑫_C语言程序设计实验3 循环结构程序设计
<C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学 期 2016-2017 第1学期 指导教师 黄俊莲 吉吉老师 实验地点 C05 ...
- 20145206《Java程序设计》实验二Java面向对象程序设计实验报告
20145206<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O. ...
- 20145208 实验三 Java面向对象程序设计
20145208 实验三 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...
- 20162330 实验四 《Android程序设计》 实验报告
2016-2017-2 实验报告目录: 1 2 3 4 5 20162330 实验四 <Android程序设计> 实验报告 课程名称:<程序设计与数据结构> 学生班级:1623 ...
- 20162302 实验四《Android程序设计》实验报告
实 验 报 告 课程:程序设计与数据结构 姓名:杨京典 班级:1623 学号:20162302 实验名称:Android程序设计 实验器材:装有Android Studio的联想拯救者80RQ 实验目 ...
- java实验四《Android程序设计》实验报告
一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:张士洋 学号:20165308 指导教师:娄嘉鹏 实验日期:2018年5月14日 实验时间:13:45 - 15:25 实验序号:08 ...
- 2017-2018-2 20165312 实验四《Android程序设计》实验报告
2017-2018-2 20165312 实验四<Android程序设计>实验报告 一.安装Android Studio并进行Hello world测试和调试程序 安装Android St ...
随机推荐
- linux上磁盘的操作相关命令
1.查看磁盘IO负载 - 看哪些进程在读写磁盘 lsof /dev/sda2 |head
- 为多维数组添加一列以及reshape用法注意
https://blog.csdn.net/orangefly0214/article/details/80934008参考这个了链接 下面是我自己用到的代码,亲测可用 # data = pd.rea ...
- SpringBoot打包不同配置profile
1.application.properties添加变量 spring.profiles.active=@activatedProperties@ 2.pom中添加变量配置 <profiles& ...
- No input file specified. phpStudy nginx报错解决方案
1.首先观察路径是否存在, 2.在vhsos.conf文件中 先科普下: 在Windows系统中,正斜杠 / 表示除法,用来进行整除运算:反斜杠 \ 用来表示目录. 在Unix系统中,/ 表示目录:\ ...
- 移除Excel工作表密码保护小工具含C#源代码
有朋友发了个Excel.xlsx文件给我,让我帮忙看看里面是怎么做出来的.打开审阅后发现,每个Excel工作表都添加了密码保护: 看不到里面的隐藏列和公式等等,感觉很神秘.于是研究了一下Excel文件 ...
- 执行curl 提示curl: (35) SSL connect error
安装acme证书时,执行如下 curl https://get.acme.sh | sh 提示如下报错: curl: (35) SSL connect error curl -v 跟踪时 发现 NSS ...
- Nginx Install 记录
一.安装编译工具及库文件 yum -y install gcc yum -y install gcc-c++ yum -y install zlib; yum -y install pcre-deve ...
- redis 无序集合(set)函数
sAdd 命令/方法/函数 Adds a value to the set value stored at key. If this value is already in the set, FALS ...
- SpringCloud入门之应用程序上下文服务(Spring Cloud Context)详解
构建分布式系统非常复杂且容易出错.Spring Cloud为最常见的分布式系统模式提供了简单易用的编程模型,帮助开发人员构建弹性,可靠和协调的应用程序.Spring Cloud构建于Spring Bo ...
- A Simple Chess (Lucas组合数 + 容斥)
题意:走马步,要求向右向下,不能走进禁止的点.求方案数. 思路:若是n*m比较小的话,那么可以直接DP.但是这道题目不行.不过我们仔细分析可以知道从某个点到某个点是一个组合数,但是数据太大,mod值很 ...