C++ Primer第八章课后编程题

1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数。不过,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让 读者能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示 该函数是如何工作的。
代码:
#include<iostream>
using namespace std;
void show(const char * a, int b=0);
void show(const char * a, int b)
{
    static int uses = 0;
    int lim = ++uses;
    cout << "第" << uses << "次调用.\n";
    if(b==0)
        lim = 1;
    for (int i=0; i<lim; i++)
        cout <<a<<endl;
}
int main()
{
    int i;
    const char * p = "hello world\n";
    cout <<"输入函数调用次数:";
    cin >> i;
    while(i>=0)
    {
        cout << "参数n为:" <<i;
        show(p, i);
        i--;
    }
    cout << "默认n为0:";
    show(p);
    return 0;
}    
运行结果:


2、 CandyBar结构饱含3个成员。第一个成员存储candy bar的品牌名称; 第二个成员存储candy bar的重量(可能有小数);第三个成员存储candy bar 的热量(整数)。请编写一个程序,它使用一个这样的函数,即将CandyBar 的引用、char指针、double和int作为参数,并用最后3个值设置相应的结构 成员。最后3个参数的默认值分别为"Millennium Munch"、2.85和350。另外, 该程序还包含一个以CandyBar的引用为参数,并显示结构内容的函数。 请尽可能使用const .
代码
#include<iostream>
#include<cstring>
using namespace std;
const int MAX=60;
struct CandyBar{
    char brand[MAX];
    double weight;
    int energy;
};
void init(
    CandyBar & cb,
    const char *br = "Millennium Munch",
    double we = 2.85,
    int ca = 350
);
void display(const CandyBar & cb);
int main()
{
    CandyBar candybar;
    cout << "默认的candybar为:"<<endl;
    init(candybar);
    display(candybar);
    char newBrand[MAX];
    double newWeight;
    int newEnergy;
    cout << "请输入新的candybar数据:"<<endl;
    cout << "brand: ";
    cin.get(newBrand, MAX);
    cout << "weight: ";
    cin >> newWeight;
    cout << "Energy: ";
    cin >> newEnergy;
    init(candybar, newBrand, newWeight, newEnergy);
    display(candybar);
    return 0;
}
void init(CandyBar &cb,const char *br, double we, int ca)
{
    strcpy(cb.brand, br);
    cb.weight = we;
    cb.energy = ca;
}
void display(const CandyBar &cb)
{
    cout << "新的candybar为:\n";
    cout << "brand: " << cb.brand<<endl;
    cout << "weight: "<<cb.weight<<endl;
    cout << "energy: "<<cb.energy<<endl;
}
运行结果


3、 编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,为此可使用函数toupper()。.然后编写一个程序,它通过使用一个循环让你能够用不同的输入来测试这个函数,该程序运行情况如下:
enter a string (q to quit) :go away
GO AWAY
next string (q to quit) : good grief !
GOOD GRIEF!
next string (q to quit) : q
bye.
代码:
#include<iostream>
#include<string>
using namespace std;
void wordupper(string &a);
int main()
{
    string str;
    cout << "Enter a string (1 to quit):";
    getline(cin, str);
    int i=0;
    while (str != "q")
    {
        wordupper(str);
        cout << str <<endl;
        cout << "next string (q to quit):";
        getline(cin, str);
    }
    cout << "bey!\n";
    return 0;
}
void wordupper(string &a)
{
    int i;
    while (a[i])
    {
        a[i] = toupper(a[i]);
        i++;
    }
}
运行结果:


5、 编写模板函数max5(),它将一个包含5个T类型元素的数组作为参数, 并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬 编码,而不必通过参数来传递)。在一个程序中使用该函数,将T替换 为一个包含5个int值的数组和一个包含5个dowble值的数组,以测试该 函数。
代码:
#include<iostream>
template<class T>
T max5(T ar[])
{
    int n;
    T max = ar[0];
    for (n=1; n<5; n++)
        if (ar[n] > max)
            max = ar[n];
    return max;
}
const int lim = 5;
int main()
{
    using namespace std;
    double ard[lim] = {-3.1, 8.1, -71.1, 34.4, 2.3};
    int ari[lim] = {2, 3, 10, 9, 7};
    double md;
    int mi;
    md = max5(ard);
    mi = max5(ari);
    cout << "Max double = " << md <<endl;
    cout << "Max int = " << mi <<endl;
    return 0;
}
运行结果:


6、 编写模板函数maxn(),它将由一个T类型元素组成的数组和一个表示数组 元素数目的整数作为参数,并返回数组中最大的元素。在程序对它进行测 试,该程序使用一个包含6个int元素的数组和一个包含4个double元素的数 组来调用该函数。程序还包含一个具体化,它将char指针数组和数组中的 指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符 串,则返回其中第一个字符串的地址。使用由5个字符串指针组成的数组来 测试该具体化。
代码:
#include<iostream>
#include<cstring>
template<class T>
T maxn(T ar[], int n)
{
    int i;
    T max = ar[0];
    for (i=1; i<n; i++)
        if(ar[i] > max)
            max = ar[i];
    return max;
}
template<> const char * maxn(const char * arr[], int n)
{
    const char * max = arr[0];
    int i;
    for (i=1; i<n; i++)
        if(strlen(arr[i]) > strlen(max))
            max = arr[i];
    return max;
}
int main()
{
    using namespace std;
    int ari[6] = {8,4,2,7,10,22};
    double ard[4] = {-3.1, 8.1, -71.1, 34.4};
    const char * char_arr[5] = {
        "hello world!",
        "what your name?",
        "I love you",
        "you are wonderful",
        "hi"
    };
    cout << "The max integer of array is:" << maxn(ari, 6) <<endl;
    cout << "The max double of array is:" << maxn(ard, 4) <<endl;
    cout << "The max string of array is:" << maxn(char_arr, 5) <<endl;
    return 0;
}
运行结果:


7、 修改程序清单8.14,使模板函数返回数组元素的总和, 而不是显示数组的内容.程序thing的总和以及所有debt的总和.
代码:
#include <iostream>
template <typename T>
T ShowArray (T arr[], int n);
template <typename T>
T ShowArray (T * arr[], int n);
struct debts
{
   char name[50];
   double amount;
};
int main(void)
{
   using namespace std;
   int things[6] = {13, 31, 103, 301, 310, 130};
   struct 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;
   cout<<"Listing Mr. E's counts of things:\n";
   cout << ShowArray(things,6) <<endl;
   cout<<"Listing Mr. E's debts:\n";
   cout << ShowArray(pd,3) <<endl;
   return 0;
}
template <typename T>
T ShowArray (T arr[],int n)
{
   using namespace std;
   T total = 0;
   cout<<"template A\n";
   for (int i = 0; i < n; i++)
      total +=arr[i];
   return total;
}
template <typename T>
T ShowArray (T * arr[], int n)
{
   using namespace std;
   T total = 0;
   cout<< "template B\n";
   for (int i = 0; i < n; i++)
      total += *arr[i];
   return total;
}
运行结果:


  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值