环境: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. light oj 1116 - Ekka Dokka

    1116 - Ekka Dokka   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Ekka ...

  2. JS 实现地区,省份,城市,县区4级联动

    刚学JS,实战一下,做一个省份信息4级联动. 但是网上查了都是3级的,没有4级,现在做一个的是带地区的4级联动,最简单的办法是修改别人的代码,最开始找到的事类似于下边这个的: 省市县关联菜单 < ...

  3. How To Use Logstash and Kibana To Centralize Logs On CentOS 6

    原文链接:https://www.digitalocean.com/community/tutorials/how-to-use-logstash-and-kibana-to-centralize-l ...

  4. Android设计模式系列--观察者模式

    观察者模式,是一种非常常见的设计模式,在很多系统中随处可见,尤其是涉及到数据状态发生变化需要通知的情况下.本文以AbstractCursor为例子,展开分析.观察者模式,Observer Patter ...

  5. SpringMVC 参考文档

    原文地址:http://blog.csdn.net/lufeng20/article/details/7598801

  6. 有关AES加密的问题

    遇到一个项目,需要用AES加密密码,android的已经写好了,java源码: private static final String AES_OPTIONS = "AES/ECB/PKCS ...

  7. Oracle12c功能增强 新特性之管理功能的增强

    1.    数据文件在线重命名和迁移 不想先前的版本号.在Oracle12cR1中,数据文件的迁移或重命名不再要求一系列的步骤,比如:将表空间至于仅仅读模式,然后数据文件逻辑等操作.在12cR1中.数 ...

  8. Design Pattern Explained 读书笔记二——设计模式序言

    设计模式的由来: 20 世纪 90 年代初,一些聪明的开发者偶然接触到 Alexander(Christopher Alexander 的建筑师) 有关模式的工作.他们非常想知道,在建筑学成立的理论, ...

  9. 实例源码--Android高德地图实例源码

      下载源码 技术要点: 1.高德地图 API的使用 2.定位 ,查询路线,公交查询等地图相关技术 3.源码带有非常详 细的中文注释 ...... 详细介绍:  1. 高德地图API的使用 本套实例采 ...

  10. C#_MVC_ajax for form

    在上一篇介绍MVC中的Ajax实现方法的时候,曾经提到了除了使用Ajax HTML Helper方式来实现之外,Jquery也是实现Ajax的另外一种方案. 通过get方法实现AJax请求 View ...