c++ primer plus第八章编程练习

  1. 编写接受字符串地址(那就用引用吧)为参数并打印该字符串的函数,允许第二个整型参数存在,若不为0,则为该函数被调用次数(用递归和默认参数)。
//练习8.1   简单输出字符串
//函数out接受字符串地址并打印字符串
//提供了地址以外的第二个整型参数且该参数不为0,则该参数为函数被调用次数。
#include <iostream>
#include <string>
using namespace std;
void out(const string &s, int n = 0);
int main()
{
    string in;
    cout << "Please enter:";
    getline(cin, in);
    out(in);
    cout << endl;
    out(in, 3);
    cout << "Bye." << endl;
    system("pause");
    return 0;
}
void out(const string &s, int n)
{
    cout << s << endl;
    if (n > 1)
    {
        out(s, --n);
    }
}

在这里插入图片描述

  1. 熟悉的candy bar又来了(第一成员为品牌名称,第二成员为重量,第三成员为热量也就是卡路里)编写程序使用(candy bar结构体的引用、char指针、double和int作为参数)最后3个参数给结构体赋值,设立默认参数为:“Millennium Munch”、2.85和350,另有一个显示函数show_value。
//练习8.2   熟悉的candybar
//使用candy bar结构体的引用、char指针、double和int作为参数的set_value函数
//以结构体引用为地址的显示函数show_value
#include <iostream>
using namespace std;
const int Max = 20;
void set_value(struct CandyBar &can, char *br = "Millennium Munch", double we = 2.85, int cal = 350);
void show_value(const struct CandyBar &can);
struct CandyBar
{
    char brand[Max];
    float weight;
    int calorie;
};
int main()
{
    CandyBar sweet = {"Alpine lollipop", 10.0, 177};
    show_value(sweet);
    char name[Max];
    double wei;
    int calor;
    cout << "Please enter the name:";
    cin.get(name, Max).get();
    cout << "And the weight and calorie:";
    cin >> wei >> calor;
    set_value(sweet, name, wei, calor);
    show_value(sweet);
    system("pause");
    return 0;
}
void set_value(struct CandyBar &can, char *br, double we, int cal)
{
    can.brand[Max] = *br;
    can.weight = we;
    can.calorie = cal;
}
void show_value(const CandyBar &can)
{
    cout << "The " << can.brand << " weight " << can.weight << " grams and contains " << can.calorie << " calories." << endl;
}

在这里插入图片描述

  1. 编写接受string对象的引用为参,将string对象的内容转为大写。
//练习8.3   大写转换函数
//使用string对象
//transfor函数接受string对象的引用,并转换内容为大写。
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void transfor(string &str);
int main()
{
    string st;
    cout << "Enter a string (q to quit):";
    getline(cin, st);
    while (st[0 != 'q'])
    {
        transfor(st);
        cout << st << endl;
        cout << "Next string (q to quit):";
        getline(cin, st);
    }
    cout << "Bye." << endl;
    system("pause");
    return 0;
}
void transfor(string &str)
{
    for (int i = 0; i < str.size(); i++)
    {
        str[i] = toupper(str[i]);
    }
}

在这里插入图片描述
string类对象也可以作为数组一样去调用内部字符
4. 完善程序,补充函数

//练习8.4   编写函数填充程序框架以完善
//使用string对象
//transfor函数接受string对象的引用,并转换内容为大写。
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
    char *str;
    int ct;
};
void set(stringy &st, char *s);
void show(const stringy &str, int n = 1);
void show(const char *r, int n = 1);
int main()
{
    stringy beany;
    char testing[] = "Reality isn't what is used to be.";
    set(beany, testing);
    show(beany);
    show(beany, 2);
    cout << endl;
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing);
    show(testing, 3);
    show("Done!");
    system("pause");
    return 0;
}
void set(stringy &st, char *s)
{
    int len = strlen(s);
    st.str = new char(len);
    strcpy(st.str, s);
    st.ct = len;
}
void show(const stringy &str, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << str.str << endl;
    }
}
void show(const char *r, int n)
{
    for (int j = 0; j < n; j++)
    {
        cout << r << endl;
    }
}

在这里插入图片描述
看了图片就知道程序还是有问题的,如果你编译运行了程序后就会发现,它不像正常程序那样返回值给shell,并且你关闭了我们的小黑窗后,它也显示程序还在运行(至少vscode是这样),各位可以讨论一下,有结论请留言。
5.编辑模板函数max5(),以包含5个T类型元素数组为参数,并返回数组中最大元素

//练习8.5   编写函数max5
#include <iostream>
using namespace std;
template <typename T>
T max5(T *arr);
int main()
{
    int age[5] = {4, 5, 6, 9, 20};
    int oldest;
    double weight[5] = {59.8, 63.0, 70, 80, 49.0};
    double heaviest;
    oldest = max5(age);
    heaviest = max5(weight);
    cout << "The oldest is :" << oldest << " and the heaviest is :" << heaviest << "." << endl;
    system("pause");
    return 0;
}
template <typename T>
T max5(T *arr)
{
    T temp;
    for (int i = 0; i < 5; i++)
    {
        for (int j = i + 1; j < 5; j++)
            temp = (arr[i] > arr[j]) ? arr[i] : arr[j];
    }
    return temp;
}

在这里插入图片描述

  1. 编写函数maxn(),包含两个参数(T类型组成数组和一个表示数组数目的整数),并返回最大元素。
//练习8.6   编写函数maxn
//练习8.5基础上的int型double型只增加参数n
//具体化函数处理char型指针并返回最长的地址
#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
T maxn(T *arr, int n);
template <>
const char *maxn(char *st);
int main()
{
    int age[6] = {2, 5, 9, 10, 24, 18};
    int oldest;
    double weight[4] = {80.0, 69.7, 49, 56.8};
    double heaviest;
    const char *str[5] = {"apple", "baby", "chocolate", "date", "egg"};
    const char *back;
    oldest = maxn(age, 6);
    heaviest = maxn(weight, 4);
    back = maxn(str);
    cout << "The oldest is :" << oldest << " and the heaviest is :" << heaviest << "." << endl;
    system("pause");
    return 0;
}
template <typename T>
T maxn(T *arr, int n)
{
    T temp;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
            temp = (arr[i] > arr[j]) ? arr[i] : arr[j];
    }
    return temp;
}
template <>
const char *maxn(char *st)
{
    const char *temp;
    for (int i = 0; i < 5; i++)
    {
        for (int j = i + 1; j < 5; j++)
        {
            temp = (strlen(st[i]) > strlen(st[j])) ? st[i] : st[j];
        }
    }
    return temp;
}

上面的程序一直有误
7. 改动程序清单8.14使得函数showarray用来显示数组元素总和。

//练习8.7   修改程序清单8.7
//修改函数showarray用来输出函数元素总和
#include <iostream>
using namespace std;
template <typename T>
void showarray(T *arr[], int n);
template <typename T>
void showarray(T arr[], int n);
struct debts
{
    char name[50];
    double amount;
};
int main()
{
    int things[6] = {13, 31, 103, 301, 310, 130};
    debts mr_E[3] = {
        {"Ima Wolfe", 2400.0},
        {"Ura Foxe", 1300.0},
        {"Iby Stout", 1800.0},
    };
    double *pd[3];
    for (int i = 0; i < 3; i++)
        *pd[i] = mr_E[i].amount;
    showarray(things, 6);
    showarray(pd, 3);
    system("pause");
    return 0;
}
template <typename T>
void showarray(T arr[], int n)
{
    T sum = 0.0;
    cout << "Template A" << endl;
    for (int i = 0; i < n; i++)
    {
        sum += arr[i];
    }
    cout << "The sum is:" << sum << "." << endl;
}
template <typename T>
void showarray(T *arr[], int n)
{
    T sum = 0.0;
    cout << "Template B" << endl;
    for (int j = 0; j < n; j++)
        sum += *(arr[j]);
    cout << "The sum is:" << sum << "." << endl;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值