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 问题: 得出一张表: ...
随机推荐
- A Tour of Go Exercise: Images
Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...
- 转载Entity Framework 4.1 DbContext使用记之三——如何玩转实体的属性值?
Entity Framework 4.1 DbContext使用记之一——如何查找实体? DbSet.Find函数的使用与实现 Entity Framework 4.1 DbContext使用记之二— ...
- JavaScript一道面试题求y的值是? z 的值是? s的值是?
原文:http://www.zhufengpeixun.cn/JavaScriptmianshiti/2014-04-01/287.html < script type = "text ...
- SqlLite ---.net连接数据库
初识SqlLite ---.net连接数据库 SqlLite以小巧和嵌入式闻名,以前只是听说,现在终于忍不住要尝试下. 先下载ADO.NET2.0 Provider for SQLite,下载完后 ...
- maven快速入门
一.maven maven可以说是管理项目的优秀工具,管理jar包 二.mave安装 1.先安装jdk(本文不详细讲) 2.安装maven ①.maven下载 http://maven.apach ...
- 使用JDK自带jvisualvm监控tomcat
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Postfix上的反垃圾邮件的四个方法
在介绍如何配置Postfix的smtp配置之前有必要首先介绍一下它的背景和特点.Postfix是一个由IBM资助下由WietseVenema 负责开发的自由软件工程的一个产物,其目的是为用户提供除se ...
- cocos2d 场景转换的方法执行顺序
转自:http://shanbei.info/the-cocos2d-scene-conversion-method-execution-order.html 如果你希望在场景转换的过程中使用过渡效果 ...
- Android Studio Error2
ECLIPSE ANDROID PROJECT IMPORT SUMMARY ====================================== Ignored Files: ------- ...
- 安装Intel HAXM为Android 模拟器加速,30秒内启动完成
要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); Android Studio 1.1.0;JDK 1.7.0_75(64位);android-sdk_r24 ...