码迷,mamicode.com
首页 > 编程语言 > 详细

C++例题练习(2)

时间:2015-10-28 21:15:18      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:

环境: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>0&&number<=1000);
        if(number == 1) {
            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=2;i<number/2+1;i++) {
        if(number%i == 0) {
            return false;
        } 
    }
    return true;
} 

 2.用类来实现输入和输出时间(时:分:秒)

    要求:将数据成员改为私有;将输入和输出的功能有成员函数实现;在类体内定义成员函数

    代码实现:

 (对于时间输入的合理性,简单的采用断言assert函数来进行处理,不合理,程序直接结束运行)

#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
class Time
{
    public:
        void set_time(Time &time) 
        {
            cout<<"hour(0-23):"<<endl;
            cin>>time.hour;
            assert(time.hour>0&&time.hour<24);
            
            cout<<"minute(0-59):"<<endl;
            cin>>time.minute;
            assert(time.minute>0&&time.minute<60);
            
            cout<<"second(0-59):"<<endl;
            cin>>time.sec;
            assert(time.sec>0&&time.sec<60);
        }
        void show_time(const Time &time)
        {
            cout<<time.hour<<":"<<time.minute<<":"<<time.sec<<endl;
        }
    private:
        int hour;
        int minute;
        int sec;
};
int main()
{
    Time t;
    t.set_time(t);
    t.show_time(t);
    return EXIT_SUCCESS;
}

3.在上题的基础上进行修改:在类体内声明成员函数,而在类外定义成员函数

  代码实现:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cassert>
 4 using namespace std;
 5 class Time
 6 {
 7     public:
 8         void set_time(Time &time);
 9         void show_time(const Time &time);
10     private:
11         int hour;
12         int minute;
13         int sec;
14 };
15 void Time::set_time(Time &time) 
16 {
17     cout<<"hour(0-23):"<<endl;
18     cin>>time.hour;
19     assert(time.hour>0&&time.hour<24);
20             
21     cout<<"minute(0-59):"<<endl;
22     cin>>time.minute;
23     assert(time.minute>0&&time.minute<60);
24             
25     cout<<"second(0-59):"<<endl;
26     cin>>time.sec;
27     assert(time.sec>0&&time.sec<60);
28 }
29 void Time::show_time(const Time &time)
30 {
31      cout<<time.hour<<":"<<time.minute<<":"<<time.sec<<endl;
32 }
33 int main()
34 {
35     Time t;
36     t.set_time(t);
37     t.show_time(t);
38     return EXIT_SUCCESS;
39 }

4.求三个长方柱的体积

  实现代码:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cassert>
 4 using namespace std;
 5 class Box
 6 {
 7     public:
 8         Box(int l,int w,int h);
 9         double volume() 
10         {
11             return length*width*height;
12         }
13     private:
14         double length;
15         double width;
16         double height;
17 };
18 /*
19  *precondition:parameters must be number.
20  *postcondition:null
21 */
22 Box::Box(int l,int w,int h)
23 {
24     length = l;
25     width = w;
26     height = h;
27 }
28 int main()
29 {
30     cout<<"please enter the length, width and height of a rectangle:"<<endl;
31     int l,w,h;
32     cin>>l>>w>>h;
33     assert(l>0&&w>0&&h>0);
34     
35     Box box(l,w,h);
36     cout<<"box volume:"<<box.volume()<<endl;
37     return EXIT_SUCCESS;
38 }

 

C++例题练习(2)

标签:

原文地址:http://www.cnblogs.com/damon-sf/p/4918287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!