C++函数——函数和复杂类型(数组、结构体、string对象)

函数与数组

    数组作为参数: int sum_arr(int arr[], int n);

    arr是数组名, 但arr实际上并不是数组, 而是一个指针, 指向数组的第一个元素。 但是在编写函数的其余部分时,可以将arr看作是数组。

    当且仅当用于函数头或函数原型中, int *arr和int arr[]的含义才是相同的。 它们都意味着arr是一个int指针。 然而, 数组表示法提醒用户, arr不仅指向int, 还指向int数组的第一个int。

    填充数组或修改数组: 由于接收数组名参数的函数访问的是原始数组(使用指针访问), 而不是其副本, 因此可以通过调用该函数将值赋给数组元素, 或是修改数组元素的值。

    显示数组以及保护数组: 为了确保显示函数不修改原始数组, 可在声明形参时使用关键字const

     二维数组: int sum(int ar2[][4], int size);

#include<iostream>
#include<string>

using namespace std;
int sum_arr(const int arr[], int n);
void fill_arr(int arr[], int n);
void ratio(int arr[], int n, int sum);
void print_arr(const int arr[], int n);

int main()
{
    const int size = 6;
    int student1[size] = {120, 110, 100, 90, 85, 95};
    int totalScore = sum_arr(student1, size);
    cout << "该名学生的总成绩为: " << totalScore << endl;

    int student2[size] = {};
    fill_arr(student2, size);
    totalScore = sum_arr(student2, size);
    cout << "该学生的各科成绩为: " << endl;
    print_arr(student2, size);
    ratio(student2, size, totalScore);
    cout << "各科成绩所占百分比(%): " << endl;
    print_arr(student2, size);

    return 0;
}

//arr是一个指针,指向数组的第一个元素。const int类型,所以不能修改数组内的值。
//n是数组的大小
//在函数体内,可以将arr直接当做数组名来使用,*arr和arr[]的意义相同。
int sum_arr(const int arr[], int n)
{
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        res += arr[i];
    }
    return res;
}

void fill_arr(int arr[], int n)
{
    cout << "请输入该学生的成绩(语数外物化生): " << endl;
    int temp;
    for (int i = 0; i < n; i++)
    {
        cin >> temp;
        arr[i] = temp;
    }
}

void ratio(int arr[], int n, int sum)
{
    for (int i = 0; i < n; i++)
    {
        arr[i] = arr[i] * 100 / sum;
    }
}

void print_arr(const int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

函数与结构体

    使用结构编程时, 最直接的方式是像处理基本类型那样来处理结构, 也就是说, 将结构作为参数传递, 并在需要时将结构用作返回值使用。

    如果结构非常大, 则复制结构将增加内存要求, 降低系统运行的速度, 这种情况下, 应使用指针来访问结构的内容,或按引用进行传递。

#include<iostream>

using namespace std;

struct WorkTime
{
    int hours;
    int mins;
};
WorkTime sum(WorkTime t1, WorkTime t2);
const int Mins_per_hour = 60;
int main()
{
    WorkTime morning = { 2 ,40 };
    WorkTime afternoon = { 6, 40 };
    WorkTime day = sum(morning, afternoon);
    cout << "一天一共工作了: " << day.hours << "小时," << day.mins << "分钟。" << endl;
    return 0;
}

//结构体在函数中可以和基本类型一样使用,作为参数传递或者是作为返回值返回
//结构体较大时,为了避免复制副本,可以使用指针或引用类型
WorkTime sum(WorkTime t1, WorkTime t2)
{
    WorkTime total;
    total.mins = (t1.mins + t2.mins) % Mins_per_hour;
    total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / Mins_per_hour;
    return total;
}

函数与string对象

    与结构相似, 可以将string对象作为完整的实体进行传递。string对象也可以像基本类型那样, 作为参数传递, 并作为返回值使用。

    如果需要多个字符串, 可以声明一个string对象的数组。

#include<iostream>
#include<string>

using namespace std;

void fill_games(string name[], int n);
void print_games(const string name[], int n);

int main()
{
    const int size = 5;
    cout << "请输入" << size << "个你喜欢的游戏的名称: " << endl;
    string gameNames[size] = {};
    fill_games(gameNames, size);
    print_games(gameNames, size);
    return 0;
}

void fill_games(string name[], int n)
{
    for (int i = 0; i < n; i++)
    {
        getline(cin, name[i]);
    }
}

void print_games(const string name[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << ": " << name[i] << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/xiaokunzhang/article/details/80987830