C++Primer Plus(第六版)第八章编程练习

第一题:
知识点:

  • 使用默认参数时要通过函数原型,因为编译器通过查看原型来获取函数信息;
  • 使用引用作为函数形参,使用时直接将常规变量实参传入,函数会为该变量创建一个引用而非副本,所以准确的说算是一个伪指针,使用该伪指针直接对原始数据进行使用
#include <iostream>
#include <string>

using namespace std;
void show(const string &str,int i = 0);
static int counting = 0;

int main()
{
    string str = "I'm in chaos";
    show(str);
    cout << endl;
    show(str, 50);
    cout << endl;
    show(str, 15);
    cout << endl;
    show(str);
    return 0;
}

void show(const string &str,int i)
{
    counting++;
    if(i != 0)
    {
        for(int j = 0; j < counting; j++)
            cout << str << endl;
    }
    else
        cout << str << endl;
}

第二题
知识点:

  • 没有需要太注意的,主要就是告诉你大的变量用引用
#include <iostream>

using namespace std;

struct CandyBar
{
    char * name;
    double weight;
    int heat;
};

void setting(CandyBar & a, char *n = "Millennium Munch", double w = 2.85, int h = 350);
void show(const CandyBar & a);

int main()
{
    CandyBar MM,Default;
    setting(MM, "M&M", 2.80, 340);
    setting(Default);
    show(MM);
    show(Default);
    return 0;
}

void setting(CandyBar &a, char *n, double w, int h)
{
    a.name = n;
    a.weight = w;
    a.heat = h;
}

void show(const CandyBar &a)
{
    cout << a.name<< endl;
    cout << a.weight << endl;
    cout << a.heat << endl;
}

第三题:
知识点

  • 用codeblocks好像getline会自动跳过队列前的空字符,不用使用cin.get读取一个换行符
#include <iostream>
#include <cctype>
#include <string>

using namespace std;

void transformer(string &str);

int main()
{
   cout << "Enter a string(q to quit):";
   string a;
   getline(cin,a);
   transformer(a);
   return 0;
}

void transformer(string &str)
{
   while(str != "q")
   {
   int i = str.length();
   for(int j = 0; j < i; j++)
   {
       str[j] = toupper(str[j]);
   }
   cout << str << endl;
   cout << "next string(q to quit):";
   getline(cin,str);
   }
   cout << "byebye";
}

第四题:
知识点

  • 这题没什么难度,但是我想在输出之间加空行的时候,codeblocks说进程终止,我不知道原因,可能是少安了什么编译器吧,结果我只能在函数末尾加空行,不在main里面加了
#include <iostream>
#include <cstring>

using namespace std;

struct stringy
{
   char * str;
   int ct;
};
void set(stringy &stry, char *a);
void show(const stringy &stry, int i = 1);
void show(const char *str, int i = 1);

int main()
{
   stringy beany;
   char testing[] = "Reality isn't want it used to be.";
   set(beany, testing);
   show(beany);
   show(beany, 2);
   testing[0] = 'D';
   testing[1] = 'u';
   show(testing);
   show(testing, 3);
   show("done!");
   return 0;
}
void set(stringy &stry, char *a)
{
   strcpy(stry.str, a);
   stry.ct = strlen(stry.str);
}
void show(const char* str, int i)
{
   for(int j = 0; j < i; j++)
   {
       cout << str << endl;
   }
   cout << endl;
}
void show(const stringy& stry, int i)
{
   for(int j = 0; j < i; j++)
   {
       cout << stry.str << endl;
   }
   cout << endl;
}

第五题
知识点

  • 主要就在于记住模板的定义和声明格式
  • 先template,括号内指出类型名称
  • 之后进行相关函数定义
#include <iostream>

using namespace std;

template <typename T>
T max5(T a[5]);

int main()
{
    int array[5] = {1, 2, 3, 6, 5};
    int max;
    max = max5(array);
    cout << max;
    return 0;
}


template <typename T>
T max5(T a[5])
{
    T temp = a[0];
    for(int i = 0; i < 5; i++)
    {
        if(temp > a[i])
            continue;
        else
            temp = a[i];
    }
    return temp;
}

第六题
知识点

  • 具体化模板的格式,先template<>然后具体到某一类型的函数定义
  • 数组的数组名是地址,而指针存储的是地址,所以两者可以算同类,所以数组的方括号在我看来可以直接拿到前面编程间接访问符‘*’
#include <iostream>
#include <cstring>
using namespace std;

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

int main()
{
    cout.setf(ios_base::showpoint);
    cout << fixed;
    cout.precision(1);

    int array[6] = {1, 2, 3, 6, 5, 9};
    double array2[4] = {1.7, 2.5, 3.0,1.8};
    cout << maxn(array2,4) << endl;
    cout << maxn(array, 6) << endl;
    char *a[5] =
    {
        "First place",
        "Second place",
        "third place",
        "forth place",
        "fifth place"
    };
    char *b = maxn(a,5);

    cout << &b;
    return 0;
}


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

    }
    return temp;
}

template <> char* maxn(char **a, int n)
{
    int temp = 0;
    int location;
    for (int i = 0; i < n; i++)
    {
        if(temp < strlen(a[i]))
        {
            temp = strlen(a[i]);
            location = i;
        }
        else
            continue;
    }
    return a[location];
}

第七题
知识点

  • 没有新知识,就是加强运用模板函数

猜你喜欢

转载自blog.csdn.net/baidu_29452653/article/details/88365901