C + + Example exercises (2)

Source: Internet
Author: User

Environment: dev-c++ (version:5.6.1)

1. Loop through a 1-1000 integer to determine if it is a prime number (input 1 o'clock end of program)

Primes: divisible by only 1 and itself.

Implementation code:

#include <iostream>#include<cstdlib>#include<cassert>using namespacestd;/**function name:isprimenumber. *precondition:parameter value is more than zero and less than or equal one thousand. *p Ostcondition:return True when parameter value is prime number.*/BOOLIsprimenumber (Const int&Number );intMain () { while(true)    {        intNumber ; cout<<"Please input a integer number (0-1000):"<<Endl; CIN>>Number ; //conditionASSERT (number>0&&number<= +); if(Number = =1) {             Break; }                BOOLFlag; Flag=isprimenumber (number); if(flag) {cout<<"Yes, the number is prime number."<<Endl; } Else{cout<<"No, the number is not prime number."<<Endl; }    }    returnexit_success;}BOOLIsprimenumber (Const int&Number ) {     for(intI=2; i<number/2+1; i++) {        if(number%i = =0) {            return false; }     }    return true;} 

2. Use class to achieve input and output time (time: minutes: seconds)

Requirements: Change data members to private, implement input and output functions with member functions, define member functions in the class body

Code implementation:

(For the rationality of time input, it is simple to use assert assert function to deal with, unreasonable, the program ends the operation directly)

#include <iostream>#include<cstdlib>#include<cassert>using namespacestd;classtime{ Public:        voidSet_time (Time &Time ) {cout<<"Hour (0-23):"<<Endl; CIN>>Time.hour; ASSERT (Time.hour>0&&time.hour< -); cout<<"minute (0-59):"<<Endl; CIN>>Time.minute; ASSERT (Time.minute>0&&time.minute< -); cout<<"second (0-59):"<<Endl; CIN>>time.sec; ASSERT (Time.sec>0&&time.sec< -); }        voidShow_time (ConstTime &Time ) {cout<<time.hour<<":"<<time.minute<<":"<<time.sec<<Endl; }    Private:        inthour; intminute; intsec;};intMain () {time t;    T.set_time (t);    T.show_time (t); returnexit_success;}

3. Modify on the basis of the above topic: Declare member functions in a class and define member functions outside of the class

Code implementation:

1#include <iostream>2#include <cstdlib>3#include <cassert>4 using namespacestd;5 class Time6 {7      Public:8         voidSet_time (Time &Time );9         voidShow_time (ConstTime &Time );Ten     Private: One         inthour; A         intminute; -         intsec; - }; the voidTime::set_time (Time &Time ) - { -cout<<"Hour (0-23):"<<Endl; -Cin>>Time.hour; +ASSERT (time.hour>0&&time.hour< -); -              +cout<<"minute (0-59):"<<Endl; ACin>>Time.minute; atASSERT (time.minute>0&&time.minute< -); -              -cout<<"second (0-59):"<<Endl; -Cin>>time.sec; -ASSERT (time.sec>0&&time.sec< -); - } in voidTime::show_time (ConstTime &Time ) - { tocout<<time.hour<<":"<<time.minute<<":"<<time.sec<<Endl; + } - intMain () the { * Time t; $ t.set_time (t);Panax Notoginseng t.show_time (t); -     returnexit_success; the}

4. Find the volume of three long Fang

Implementation code:

1#include <iostream>2#include <cstdlib>3#include <cassert>4 using namespacestd;5 classBox6 {7      Public:8Box (intLintWinth);9         DoubleVolume ()Ten         { One             returnlength*width*height; A         } -     Private: -         Doublelength; the         Doublewidth; -         Doubleheight; - }; - /* + *precondition:parameters must be number. - *postcondition:null + */ ABox::box (intLintWinth) at { -Length =l; -width =W; -Height =h; - } - intMain () in { -cout<<"Please enter the length, width and height of a rectangle:"<<Endl; to     intl,w,h; +Cin>>l>>w>>h; -ASSERT (l>0&&w>0&&h>0); the      * box Box (l,w,h); $cout<<"Box Volume:"<<box.volume () <<endl;Panax Notoginseng     returnexit_success; -}

C + + Example exercises (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.