《C++primerplus》第8章练习题

 1.(简单用一下引用变量,没有采用书中的题目)定义一个替身结构体,存储名字(char[])和力量值(int)。使用结构体引用作为形参写两个函数,一个不加const,使得能对定义的结构体做修改,另一个加上const不变动它的内容。第一个函数设置替身的名字和力量值,第二个函数输出结构的信息。

#include<iostream>
using namespace std;

struct stand
{
    int power;
    char name[15];
};

void show_stand(const stand & temp_show);
void set_stand(stand & temp_set);

int main()
{
    stand new_stand;

    set_stand(new_stand);
    show_stand(new_stand);

    system("pause");
}

void show_stand(const stand & temp_show)
{
    cout << "name:" << temp_show.name << endl;
    cout << "power:" << temp_show.power << endl;
}

void set_stand(stand & temp_set)
{
    cout << "Enter stand's name:";
    cin .get(temp_set.name,15);
    cin.get();
    cout << "Enter stand's power:";
    cin >> temp_set.power;
    cout << "\n";
}

2.编写一个函数,接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,使用toupper函数。通过循环提示输入。

#include<iostream>
#include<cctype>
#include<string>
using namespace std;

void upper_string(string & string_temp);

int main()
{
    string input_string;
    
    cout << "Enter a string (q to quit): ";

    getline(cin,input_string);

    while (input_string != "q")
    {
        upper_string(input_string);
        cout << input_string;
        cout << "\nNext string (q to quit): ";
        getline(cin, input_string);
    }

    cout << "Bye.\n";
    system("pause");
}

void upper_string(string & string_temp)
{
    for (int i = 0; string_temp[i] != '\0'; i++)
    {
        string_temp[i] = toupper(string_temp[i]);
    }
}

3.完成书上的例程,编写一个set函数,接受一个结构体引用和一个字符串,使用new为结构体内的字符指针动态分配空间以存储该字符串。利用函数重载编写两个show函数,都使用默认参数,一个使用上面定义的结构体,输出它存储的字符串,另一个直接输出传入的字符串。

#include<iostream>
#include<cstring>
using namespace std;

struct stringy {
    char * str;
    int ct;
};

void set(stringy & st_ref, string str_set_temp);
void show(const stringy st_show_temp, int times = 1);
void show(const string str_show_temp, int times = 1);

int main()
{
    stringy beany;
    char testing[] = "Reality isn't it used to be.";

    set(beany,testing);
    show(beany);
    show(beany, 2);
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing, 3);
    show("Done!");

    system("pause");
}

void set(stringy & st_ref, string str_set_temp)
{
    st_ref.ct = str_set_temp.length();    //获取字符串的长度
    st_ref.str = new char[st_ref.ct];    //为结构体引用里面的字符数组分配空间
    for (int i = 0; i < st_ref.ct; i++)
    {
        st_ref.str[i] = str_set_temp[i];
    }
}

void show(const stringy st_show_temp, int times = 1)
{
    for (int t = 0; t < times; t++)
    {
        for (int i = 0; i < st_show_temp.ct; i++)
        {
            cout << st_show_temp.str[i];
        }
        cout << "\n";
    }
}

void show(const string str_show_temp, int times = 1)
{
    for (int t = 0; t < times; t++)
    {
        for (int i = 0; i < str_show_temp.length(); i++)
        {
            cout << str_show_temp[i];
        }
        cout << "\n";
    }
}

4.简单使用模板函数。编写模板函数,接受不同类型的数组和其大小,输出其中的最大值。先使用int数组测试,再使用double数组测试。

#include<iostream>
using namespace std;

template <typename T>
T maxn(T * t_ptr, int n);

template <typename T>
void input_num(T * temp, int n);

int main()
{
    int array_size; 

    //先使用int数组测试
    cout << "How many numbers:(int) ";
    cin >> array_size;

    int * int_array = new int [array_size];
    input_num(int_array,array_size);
    cout << "Max number: " << maxn(int_array, array_size) << endl;

    //再使用double数组测试
    cout << "How many numbers:(double) ";
    cin >> array_size;

    double * double_array = new double[array_size];
    input_num(double_array, array_size);
    cout << "Max number: " << maxn(double_array, array_size) << endl;

    delete[]int_array;
    delete[]double_array;
    system("pause");
}

template <typename T>
T maxn(T * t_ptr, int n)
{
    T max = t_ptr[0];
    for (int i = 0; i < n; i++)
    {
        if (t_ptr[i] > max)
            max = t_ptr[i];
        else {};
    }
    return max;
}

template <typename T>
void input_num(T * temp, int n)
{
    cout << "Enter the numbers:" << endl;
    for (int i = 0; i < n; i++)
    {
        cin >> temp[i];
    }
}

*需要注意模板函数每次函数原型和函数的实现之前,都要加上template<typename ...>

猜你喜欢

转载自www.cnblogs.com/banmei-brandy/p/11363842.html