The third chapter function class notes

Defining and using functions:

  Before calling the first function declaration:
▫ If the function is defined before the call point, you do not otherwise stated;
▫ If the function definition after the call point, you need to declare the following form before calling the function Prototype:
   Type identifier called function name (parameter list including a type description);
• Call the form of the function name (argument list)
• nested calls 
  ▫ a function body function can call another function called nested calls.
• recursive call
  ▫ function calls itself directly or indirectly.
 
Inline functions:
• When using the keyword inline statement.
• In the call at compile time be replaced with a function body, saving parameter passing, control transfers and other expenses.
•note:
  ▫ inline functions loop and the body can not have a switch statement.
  ▫ before the declaration within an inline function must appear first inline function is called
  ▫ inline functions can not be an exception interface declaration. 
 
Function with default parameter values
The default parameter values ​​to specify the order
• There are default parameters Finally, the right of that is the default parameter values ​​must not whether the default parameter values ​​in the parameter list of parameters. Because the actual participation of parameter binding when calling from left to right.
• Example: int add (int x, int y = 5, int z = 6); // correct
int add(int x = 1, int y = 5, int z);//错误
int add(int x = 1, int y, int z = 6);//错误543.3
The default parameter values ​​and call the position function
• If there is a function prototype declarations and the prototype declaration before the definition, the default parameter values ​​must be given in a function prototype declaration;
If only the function definition, function definition or the front, the default parameter values ​​need to be given in the function definition.
 
 
Function overloading
C ++ allows functions similar function to the same function name declared in the same scope, thereby forming overloaded. Easy to use, easy to remember.
 Different parameter type int add (int x, int y); float add (float x, floaty);
 The number of different parameter int add (int x, int y); int add (int x, int y, int z);
 Parameter ▫ overloaded functions must be different: different number or different types, the compiler will call the function which is selected according to the best matching parameter arguments and the type and number of
 
 
A example, Binary Decimal:
#include<bits/stdc++.h>
#define endl '\n'
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int N = 1e5+5;
typedef long long ll;
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int base = 1, a,res = 0; cin>>a; while(a){ res += base*(a%2); base*=2; a/=10; } cout<<res<<endl; return 0; }
 Solving piecewise functions
#include<bits/stdc++.h>
#define endl '\n'
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int esp = 1e-10;
typedef long long ll; 
double Sin(double x){
    double res = 0,add = x ; int down = 1;
//    while(  ){
//        down += 2;
//        up *= (x*x);
//        add = up/down;
//        if( down%4==3 ) add = -add;
//        res += add;
//        cout<<" up= "<<up<<" down "<<down<<"  add= "<<add;
//    }cout<<endl;
//    add =  
    do{ 
        res+=add;
        add = -add*(x*x)/(down+1)/(down+2);
        down+=2;
    }while( fabs(add)>esp );
    return res;
} 
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    double r,s,k; 
    cin>>r>>s;
    if( r*r<=s*s ){
        k = sqrt( Sin(r)*Sin(r)+Sin(s)*Sin(s) );
    }
    else{
        k = 0.5*Sin(r*s);
    }
    cout<<k<<endl; 
    return 0;
}

Analog dice, determining the outcome

#include<bits/stdc++.h>
#define endl '\n'
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int N = 1e5+5;
typedef long long ll;
int Do(){
    int a = rand()%6+1,b = rand()%6+1;
    cout<<"player rolled "<<a<<" + "<<b<<" = "<<a+b<<endl;
    return a+b; 
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cout<<"Please enter an unsigned integer:" <<endl;
    unsigned int n; cin>>n;
    srand(n);
    while(1){
        int sum = Do();
        if( sum==7 ||sum==11 ){
            cout<<"player wins"<<endl;
            break;
        }
        else if( sum ==2 ||sum==3||sum==12 ){
            cout<<"player loses"<<endl;
            break;
        }

    }
    return 0;
}

Tower of Hanoi recursive

#include <iostream>
using namespace std; 
void move(char src, char dest) { 
    cout<< src<< " --> " << dest<< endl;
} 
void hanoi(int n, char src, char medium, char dest) {    
    if (n == 1) move(src, dest);
    else {
        hanoi(n -1, src, dest, medium);
        move(src, dest);
        hanoi(n -1, medium, src, dest);
    }
}
int main() {
    int m;
    cout << "Enter the number of diskes: ";
    cin >> m;
    cout << "the steps to moving " << m << " diskes:" << endl;
    hanoi(m,'A','B','C');
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/SunChuangYu/p/12551210.html