环境: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)的更多相关文章

  1. BIT 树状数组 详解 及 例题

    (一)树状数组的概念 如果给定一个数组,要你求里面所有数的和,一般都会想到累加.但是当那个数组很大的时候,累加就显得太耗时了,时间复杂度为O(n),并且采用累加的方法还有一个局限,那就是,当修改掉数组 ...

  2. STL模板中的map的使用与例题

    最近的计分赛,记得自己的都只是过了两题.遇到了两次map,自己在寒假看了一点的map,只知道在字符串匹配的时候可以用的到.但是自己对map的使用还是不够熟练使用,这回在第一次和第二次的计分赛中都遇到可 ...

  3. C语言经典例题100

    C语言经典例题100 来源 http://www.fishc.com 适合初学者 ----------------------------------------------------------- ...

  4. 图的全局最小割的Stoer-Wagner算法及例题

    Stoer-Wagner算法基本思想:如果能求出图中某两个顶点之间的最小割,更新答案后合并这两个顶点继续求最小割,到最后就得到答案. 算法步骤: --------------------------- ...

  5. lca入门———树上倍增法(博文内含例题)

    倍增求LCA: father[i][j]表示节点i往上跳2^j次后的节点 可以转移为 father[i][j]=father[father[i][j-1]][j-1] 整体思路: 先比较两个点的深度, ...

  6. acm常见算法及例题

    转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题  初期:一.基本算法:     (1)枚举. (poj17 ...

  7. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

  8. C语言中的经典例题用javascript怎么解?(一)

    C语言中的经典例题用javascript怎么解?(一) 一.1+2+3+……+100=?        <script type="text/javascript">  ...

  9. 数据库留言板例题:session和cookie区别

    session和cookie区别: <?php session_start(); //session_start();必须写在所有的php代码前边 ?> <!DOCTYPE html ...

  10. mysql连接查询经典小例题

    mysql连接查询: Mysql连接查询支持多表连接 对同一张表可以重复连接多次(别名在多次连接同一张表时很重要) 例题1: 下面有2张表 teams表 比赛结果表:result 问题: 得出一张表: ...

随机推荐

  1. 【C语言】-循环结构-for语句

    for语句: for (表达式1;表达式2;表达式3) { 循环语句 } 表达式1 给循环变量赋初值 表达式2 为循环条件 表达式3 用来修改循环变量的值,称为循环步长. for语句的执行流程: 例: ...

  2. hdu 5533 Dancing Stars on Me

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5533 Dancing Stars on Me Time Limit: 2000/1000 MS (Ja ...

  3. C++学习笔记(十四):模板

    模板就是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数,从而实现了真正的代码可重用性.模版可以分为两类,一个是函数模版,另外一个是类模版.Java中对应的技术称为泛型. 函数模板 ...

  4. AdapterView及其子类之二:使用ListActivity及ArrayAdapter创建列表

    见归档项目ListActivityDemo.zip. 基本步骤如下: 1.创建一个TextView,用于指定每一个ListView的格式 <?xml version="1.0" ...

  5. 编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish 和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。

    package animal; public class Animal { //成员属性 private String kind; public String getKind() { return k ...

  6. Objective-C中的Block(闭包) (轉載)

    来源: 伯乐在线 - 青玉伏案 链接:http://ios.jobbole.com/83229/ 学习OC有接触到一个新词Block(个人感觉又是一个牛气冲天的词),但不是新的概念,不是新的东西.学过 ...

  7. 教你50招提升ASP.NET性能(九):显式的使用using语句减少内存泄露

    (15)Reduce memory leaks dramatically with the “using” statement 招数15: 显式的使用using语句减少内存泄露 If a type i ...

  8. NSDateFormatter 问题

    NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init]; NSString *inputDateStr = @&quo ...

  9. 【49】了解new_handler的行为

    1.使用operator new无法获取内存时,对于旧式编译器,会返回一个null指针.对于新式编译器,会抛出一个异常. 2.考虑下面的需求,当operator new 无法获取内存时,程序员期望获得 ...

  10. http://www.360doc.com/content/10/1012/09/3722251_60285817.shtml

    http://www.360doc.com/content/10/1012/09/3722251_60285817.shtml http://www.docin.com/p-163063250.htm ...