C++学习第三天

1. 圆的面积和周长

#include <iostream>
using namespace std;

const double pi = 3.14159;

// Function Declarations (Prototypes)
double area(double input_radius);
double circumference(double input_radius);

int main(){
    cout << "Enter the radius of the circle: ";
    double radius;
    cin >> radius;

    //call function "area"
    cout << "Area is: " << area(radius) << endl;

    // call function "circumference"
    cout << "Circumference is: " << circumference(radius) << endl;


    return 0;
}

// Function Definitions (implementations)
double area(double input_radius){
    return pi * input_radius * input_radius;
}

double circumference(double input_radius){ 
    return 2 * pi * input_radius; 
}

2.计算圆柱表面积的函数

#include <iostream>
using namespace std;

const double pi = 3.14;

// function declaration contains two parameters
double surface_area(double radius, double height);

int main(){
    cout << "Enter the radius and height of the cylinder: ";
    double radius; cin >> radius;

    cout << "Enter the height: "; 
    double height; cin >> height;

    cout << "Surface area of the cylinder is: " << surface_area(radius, height) << endl;

    return 0;
}

double surface_area(double radius, double height){
    double area = 2 * pi * radius * (radius + height);
    return area;
}

3. 没有参数和返回值的函数

#include <iostream>
using namespace std;

// function declaration
void say_hello();

int main(){
    say_hello();

    return 0;
}

// function implementation
void say_hello(){
    cout << "Hello, World!" << endl;
}

4. 带默认值的函数参数

/*
如果编写一个函数,在用户没有提供的情况下,将Pi设置为您选择的值呢?

为解决这种问题,一种方式是给函数Area()新增一个表示Pi的参数,并将
其默认值设置为您选择的值。这种默认值可被用户提供的值覆盖。
eg:
    double area(double radius, double Pi = 3.14159);

note:
- 可以给多个参数指定默认值,但这些参数必须位于参数列表的末尾。

*/

5. 递归函数

#include <iostream>
using namespace std;

int get_fib_number(int fib_index){
    if (fib_index < 2)
        return fib_index;
    else //recursion if fib_index >= 2
        return get_fib_number(fib_index - 1) + get_fib_number(fib_index - 2);
}

int main(){
    cout << "Enter 0-based index of desired fibonacci number: ";
    int index = 0;
    cin >> index;

    cout << "Fibonacci number is: " << get_fib_number(index) << endl;


    return 0;
}

6. 函数重载

/*
- 名称和返回类型相同,但参数不同的函数被称为重载函数
- 在应用程序中,如果需要使用不同的参数调用具有特定名称和返回类型的函数,重载函数将很有用
*/
#include <iostream>
using namespace std;

const double pi = 3.14159;

double area(double radius); // for circle
double area(double radius, double height); // overloaded for cylinder


int main(){
    cout << "Enter z for cylinder, c for circle: ";
    char choice = 'z';
    cin >> choice;

    cout << "Enter radius: ";
    double radius = 0;
    cin >> radius;

    if (choice == 'z'){
        cout << "Enter height: ";
        double height = 0;
        cin >> height;

        // invoke overloaded variant of area()
        cout << "Area of cylinder is " << area(radius, height) << endl;
    }
    else
        cout << "Area of circle is " << area(radius) << endl;

    return 0;
}


double area(double radius){
    return pi * radius * radius;
}

double area(double radius, double height){
    return 2 * area(radius) + 2 * pi * radius * height;
}

7. 将数组传递给函数

#include <iostream>
using namespace std;

void display_array(int numbers[], int length);
void display_array(char characters[], int length);

int main(){
    int my_numbers[4] = {1, 2, 3, 4};
    display_array(my_numbers, 4);

    char my_statement[7] = {'H', 'e', 'l', 'l', 'o', '!', '\0'};
    display_array(my_statement, 7);


    return 0;
}

void display_array(int numbers[], int length){
    for (int index = 0; index < length; index++){
        cout << numbers[index] << " ";
    }
    cout << endl;
}

void display_array(char characters[], int length){ // 注意:字符数组不需要指定长度
    for (int index = 0; index < length; index++){
        cout << characters[index] << " ";
    }
    cout << endl;
}

8. 按引用传递参数

#include <iostream>
using namespace std;

const double pi = 3.1415926;

//按引用传递参数是让函数将修改结果提供给调用模块的方式之一

//output parameter result by reference
void area(double radius, double& result){
    result = pi * radius * radius;
}

int main(){
    cout << "Enter the radius of a circle: ";
    double radius;
    cin >> radius;

    double area_fetched = 0;
    area(radius, area_fetched);

    cout << "The area of the circle is: " << area_fetched << endl;


    return 0;
}

9. 内联函数

/*将函数声明为内联的会导致代码急剧膨胀,在声明为内联的函数做了大量
复杂处理时尤其如此。应尽可能少用关键字 inline,仅当函数非常简单,需要降低其开销时(如前面所示),才应使用该关键字。*/

#include <iostream>
using namespace std;

// define inline function
inline long double_number(int number){
    return 2 * number;
}

int main(){
    cout << "Enter an integer: ";
    int input_number = 0;
    cin >> input_number;

    // call inline function
    cout << "Double is: " << double_number(input_number) << endl;


    return 0;
}

10. lambda函数和for_each用法

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

//函数DisplayNums使用STL算法遍历数组的每个元素,并显示其值。
/*
lambda函数的语法如下:
[optional parameters](parameter list) {statements};
其中,optional parameters是可选的捕获列表,parameter list是参数列表,statements是函数体。

for_each用法
for_each (first, last, function);
其中,first和last是迭代器,function是函数或函数对象。
*/
void display_numbers(vector<int>& dynamic_array){
    for_each (dynamic_array.begin(), dynamic_array.end(), \
            [](int element) {cout << element << " ";}); //lambda
    cout << endl;
}

int main(){
    vector<int> my_numbers;
    my_numbers.push_back(10);
    my_numbers.push_back(20);
    my_numbers.push_back(30);

    display_numbers(my_numbers);

    cout << "Sorting them in descending order" << endl;
    sort(my_numbers.begin(), my_numbers.end(), \
        [](int num1, int num2) {return (num2 < num1);});
    
    display_numbers(my_numbers);


    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值