Chapter 23. - Under the function (f) - Comprehensive example

Two examples

One, determine prime numbers

// IsPrimeNumber.cpp: the contains This File The 'main' function Program Execution Begins and ends there..
 //
 
#include " pch.h " 
#include <the iostream>
 the using  namespace STD; 

#include <the cmath> BOOL IsPrimeNumber ( int NUM ); int main () 
{ 
    COUT << " - is a prime number is determined whether - " << endl; 
     int NUM = 0 ; 
    COUT << " Please enter a positive integer: " << endl;
    cin >>



NUM;
     the while (NUM> 0 ) {
         IF (IsPrimeNumber (NUM)) { 
            COUT << << NUM " is a prime number " << endl; 
        } 
        the else { 
            COUT << << NUM " is not a prime number " << endl; 
        } 
        COUT << endl << " Please enter a positive integer: " << endl; 
        CIN >> NUM;
         IF (NUM == 0 ) {
             BREAK ;
        }
    }

    return  0 ; 
} 

BOOL IsPrimeNumber ( int NUM) {
     IF ( . 1 == NUM || 2 == NUM) {
         return  to true ; 
    } 

    int S = static_cast < int > (sqrt (NUM));           // square root 
    for ( int = I 2 ; I <= S; I ++) { // from 2 traversing until the root 
        IF ((NUM% I) == 0 ) {         // if divisible, is not a prime number 
            return  to false ; 
        } 
    } 
    return true;
}

 

Add a condition to enter 0 to exit the loop.

 

Second, split the string

 

Guess you like

Origin www.cnblogs.com/smart-zihan/p/11344839.html