第三章 函数 课堂笔记

函数的定义与使用:

  调用前先声明函数:
▫若函数定义在调用点之前,则无需另外声明;
▫若函数定义在调用点之后,则需要在调用函数前按如下形式声明函数原型:
   类型标识符  被调用函数名(含类型说明的形参表);
•调用形式   函数名(实参列表)
•嵌套调用 
  ▫在一个函数的函数体中,可以调用另一函数,称为嵌套调用。
•递归调用
  ▫函数直接或间接调用自身。
 
内联函数:
•声明时使用关键字inline。
•编译时在调用处用函数体进行替换,节省了参数传递、控制转移等开销。
•注意:
  ▫内联函数体内不能有循环语句和switch语句。
  ▫内联函数的声明必须出现在内联函数第一次被调用之前
  ▫对内联函数不能进行异常接口声明。 
 
带默认参数值的函数
默认参数值的说明次序
•有默认参数的形参必须在形参列表的最后,也就是说默认参数值的右面不能有无默认值的参数。因为调用时实参与形参的结合是从左向右的顺序。
•例:int add(int x, int y = 5, int z = 6);//正确
int add(int x = 1, int y = 5, int z);//错误
int add(int x = 1, int y, int z = 6);//错误543.3
默认参数值与函数的调用位置
•如果一个函数有原型声明,且原型声明在定义之前,则默认参数值必须在函数原型声明中给出;
而如果只有函数的定义,或函数定义在前,则默认参数值需在函数定义中给出。
 
 
函数重载
C++允许功能相近的函数在相同的作用域内以相同函数名声明,从而形成重载。方便使用,便于记忆。
 形参类型不同int add(int x,int y);float add(float x, floaty);
 形参个数不同int add(int x, int y);int add(int x, int y, int z);
 ▫重载函数的形参必须不同:个数不同或类型不同,编译程序将根据实参和形参的类型及个数的最佳匹配来选择调用哪一个函数
 
 
例题一,二进制转十进制:
#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;
}
 求解分段函数
#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;
}

模拟掷骰子,判断胜负

#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;
}

汉诺塔递归

#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;
}

 

 
posted @ 2020-03-23 11:43  SunCY  阅读(241)  评论(0编辑  收藏  举报