C++例题练习(2)
环境:Dev-C++( Version:5.6.1)
1.循环输入一个1-1000的整数,判断是否为素数(输入1时程序结束)
素数:只能被1和自身整除.
实现代码:
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
/*
*function name:isPrimeNumber.
*precondition:parameter value is more than zero and less than or equal one thousand.
*postcondition:return true when parameter value is prime number.
*/
bool isPrimeNumber(const int &number); int main()
{
while(true)
{
int number;
cout<<"please input a integer number(0-1000):"<<endl;
cin>>number;
//condition
assert(number>&&number<=);
if(number == ) {
break;
} bool flag;
flag = isPrimeNumber(number);
if(flag) {
cout<<"yes, the number is prime number."<<endl;
} else {
cout<<"no, the number is not prime number."<<endl;
}
}
return EXIT_SUCCESS;
}
bool isPrimeNumber(const int &number)
{
for(int i=;i<number/+;i++) {
if(number%i == ) {
return false;
}
}
return true;
}
2.用类来实现输入和输出时间(时:分:秒)
要求:将数据成员改为私有;将输入和输出的功能有成员函数实现;在类体内定义成员函数
代码实现:
(对于时间输入的合理性,简单的采用断言assert函数来进行处理,不合理,程序直接结束运行)
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
class Time
{
public:
void set_time()
{
cout<<"hour(0-23):"<<endl;
cin>>hour;
assert(hour>&&hour<); cout<<"minute(0-59):"<<endl;
cin>>minute;
assert(minute>&&minute<); cout<<"second(0-59):"<<endl;
cin>>sec;
assert(sec>&&sec<);
}
void show_time()
{
cout<<"time:"<<endl;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
private:
int hour;
int minute;
int sec;
};
int main()
{
Time t;
t.set_time();
t.show_time();
return EXIT_SUCCESS;
}
3.在上题的基础上进行修改:在类体内声明成员函数,而在类外定义成员函数
代码实现:
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
class Time
{
public:
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;
};
void Time::set_time()
{
cout<<"hour(0-23):"<<endl;
cin>>hour;
assert(hour>&&hour<); cout<<"minute(0-59):"<<endl;
cin>>minute;
assert(minute>&&minute<); cout<<"second(0-59):"<<endl;
cin>>sec;
assert(sec>&&sec<);
}
void Time::show_time()
{
cout<<"time:"<<endl;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main()
{
Time t;
t.set_time();
t.show_time();
return EXIT_SUCCESS;
}
4.求三个长方柱的体积
由成员函数完成如下功能:
1>由键盘分别输入3个长方柱的长,宽,高;
2>计算长方柱的体积;
3>输出3个长方柱的体积;
实现代码:
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
class Box
{
public:
void set_box();
void volume()
{
cout<<"volume :"<<length*width*height<<endl;
}
private:
double length;
double width;
double height;
};
/*
*precondition:null
*postcondition:set instance data
*/
void Box::set_box()
{
cout<<"please enter the length, width and height of a rectangle:"<<endl;
cin>>length>>width>>height;
assert(length>&&width>&&height>);
}
int main()
{
Box box1;
box1.set_box();
box1.volume(); Box box2;
box2.set_box();
box2.volume(); Box box3;
box3.set_box();
box3.volume();
return EXIT_SUCCESS;
}
C++例题练习(2)的更多相关文章
- BIT 树状数组 详解 及 例题
(一)树状数组的概念 如果给定一个数组,要你求里面所有数的和,一般都会想到累加.但是当那个数组很大的时候,累加就显得太耗时了,时间复杂度为O(n),并且采用累加的方法还有一个局限,那就是,当修改掉数组 ...
- STL模板中的map的使用与例题
最近的计分赛,记得自己的都只是过了两题.遇到了两次map,自己在寒假看了一点的map,只知道在字符串匹配的时候可以用的到.但是自己对map的使用还是不够熟练使用,这回在第一次和第二次的计分赛中都遇到可 ...
- C语言经典例题100
C语言经典例题100 来源 http://www.fishc.com 适合初学者 ----------------------------------------------------------- ...
- 图的全局最小割的Stoer-Wagner算法及例题
Stoer-Wagner算法基本思想:如果能求出图中某两个顶点之间的最小割,更新答案后合并这两个顶点继续求最小割,到最后就得到答案. 算法步骤: --------------------------- ...
- lca入门———树上倍增法(博文内含例题)
倍增求LCA: father[i][j]表示节点i往上跳2^j次后的节点 可以转移为 father[i][j]=father[father[i][j-1]][j-1] 整体思路: 先比较两个点的深度, ...
- acm常见算法及例题
转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法: (1)枚举. (poj17 ...
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...
- C语言中的经典例题用javascript怎么解?(一)
C语言中的经典例题用javascript怎么解?(一) 一.1+2+3+……+100=? <script type="text/javascript"> ...
- 数据库留言板例题:session和cookie区别
session和cookie区别: <?php session_start(); //session_start();必须写在所有的php代码前边 ?> <!DOCTYPE html ...
- mysql连接查询经典小例题
mysql连接查询: Mysql连接查询支持多表连接 对同一张表可以重复连接多次(别名在多次连接同一张表时很重要) 例题1: 下面有2张表 teams表 比赛结果表:result 问题: 得出一张表: ...
随机推荐
- iOS7自定义statusbar和navigationbar的若干问题
当然有许多问题是这篇文章中没有提到的,按照文章的方法进行设置,你可能会遇到以下问题: 1.navigationbar的背景图片自定义以后,statusbar虽然和navigationbar共用了背景图 ...
- Umbraco Forms 使Rendering Forms scripts 在不同的template中
具体请参考 https://our.umbraco.org/documentation/products/umbracoforms/developer/Rendering-Scripts/ 转载 ht ...
- Remastersys打包你自己的ubuntu成iso文件,保存原来的所有配置
你是不是辛辛苦苦地配好了ubuntu结果不久又重装,然后又重新配置很久呢? 笔者好不容易配置好了torch,但是换硬盘,于是就想到了将ubuntu打包成iso文件,下次直接安装,然后配置好的东西都搬过 ...
- [BZOJ]2132: 圈地计划 最小割
圈地计划 Description 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解,这块土地是一 ...
- 给postfix设置黑名单
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- linux 内核之旅
http://www.kerneltravel.net/?p=534 http://mp.weixin.qq.com/mp/homepage?__biz=MzI3NzA5MzUxNA==&hi ...
- mysql之sql语句细节问题汇总
一.mysql count distinct null 使用注意事项 1 用一个例子来讲解一个问题,现在又一个库表hello,表内容如下: id name 1 Null 2 ...
- Memcached source code analysis -- Analysis of change of state--reference
This article mainly introduces the process of Memcached, libevent structure of the main thread and w ...
- 如何查找局域网的外网ip
方法一:一个简单的方法 用你电脑打开www.ip138.com 就可以看到自己的公网IP地址 方法二:如果一定要从路由器里面看 那就打开路由的配置页面 一般在系统状态里面 会有个WAN口IP 那就是你 ...
- [COCOS2DX]官网helloworld在VS2012中的部署
VS2012.JDK.Eclipse(和adt插件)之类的基本安装这里直接略过. 以下为对cocos2dx 3.5版本在VS2012中部署的摸索: 开源项目“愤怒的小鸟”原来设置: .;..\Clas ...