8.2 CandyBar结构包含3个成员,第一个成员存储candy bar的品牌名称;第二个成员存储candy bar的重量(可能有小数);第三个成员存储candy bar的热量(整数)。请编写一个程序,它使用一个这样的函数,即将CandyBar的引用、char指针、double和int作为参数,并用最后3个值设置相应的结构成员。最后3个参数的默认值分别为“Millennium Munch”、2.85和350。另外,该程序还包含一个以CandyBar的引用为参数,并显示结构内容的函数。请尽可能使用const。
#include <iostream>
using namespace std;
struct candy_bar_str{
char *blandname;
double weight;
int heatvalue;
};
void SetCandybarExample(candy_bar_str& canbar, char* name = (char*)"Millennium Munch", double weight = 2.85, int hv = 350)
{
canbar.blandname = name;
canbar.weight = weight;
canbar.heatvalue = hv;
}
void DisplayCandybarExample(const candy_bar_str& canbar)
{
cout<<"canbar.blandname:"<<canbar.blandname<<endl;
cout<<"canbar.weight:"<<canbar.weight<<endl;
cout<<"canbar.heatvalue:"<<canbar.heatvalue<<endl;
}
int main()
{
candy_bar_str canbarImpl;
SetCandybarExample(canbarImpl);
SetCandybarExample(canbarImpl, (char *)"gaigemingzi");
SetCandybarExample(canbarImpl, (char *)"gaigemingzi", 11.11);
SetCandybarExample(canbarImpl, (char *)"gaigemingzi", 11.11, 250);
DisplayCandybarExample(canbarImpl);
return 0;
}
8.3 编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转化成大写,为此可以使用表6.4描述的函数toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数,该程序的运行如下:
#include <iostream>
#include<string>
using namespace std;
void function_example(string &par)
{
for(int i =0; i < par.size(); i++)
{
par[i] = toupper(par[i]);
}
}
int main()
{
string str;
cout<<"Enter a string (q to quit): ";
while(getline(cin, str) && (str != "q"))
{
function_example(str);
cout << str << endl;
cout << "Next string (q to quit): ";
}
cout << "Bye." << endl;
return 0;
}
8.4.下面是一个程序框架:
#include <iostream>
using namespace std;
#include <cstring> // for strlen(), strcpy()
struct stringy {
char * str; // points to a string
int ct; // length of string (not counting '\0')
};
// prototypes for set(), show(), and show() go here
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing); // first argument is a reference,
// allocates space to hold copy of testing,
// sets str member of beany to point to the
// new block, copies testing to new block,
// and sets ct member of beany
show(beany); // prints member string once
show(beany, 2); // prints member string twice
testing[0] = 'D';
testing[1] = 'u';
show(testing); // prints testing string once
show(testing, 3); // prints testing string thrice
show("Done!");
return 0;
}
请提供其中描述的函数和原型,从而完成该程序。注意,应有两个show()函数,每个都使用默认参数。请尽可能使用const参数。set()使用new分配足够的空间来存储指定的字符串。这里使用的技术与设计和实现类时使用的相似。(可能还必须修改头文件的名称,删除using编译指令,这取决于所用的编译器)。
#include <iostream>
using namespace std;
#include <cstring> // for strlen(), strcpy()
struct stringy {
char * str; // points to a string
int ct; // length of string (not counting '\0')
};
// prototypes for set(), show(), and show() go here
void set(stringy &be, char *te);
void show(stringy &be, int n = 1);
void show(string be, int n = 1);
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing); // first argument is a reference,
// allocates space to hold copy of testing,
// sets str member of beany to point to the
// new block, copies testing to new block,
// and sets ct member of beany
show(beany); // prints member string once
show(beany, 2); // prints member string twice
testing[0] = 'D';
testing[1] = 'u';
show(testing); // prints testing string once
show(testing, 3); // prints testing string thrice
show("Done!");
delete beany.str;
return 0;
}
void set(stringy &be, char *te)
{
be.ct = strlen(te);
be.str = new char[be.ct ];
strcpy(be.str, te);
}
void show(stringy &be, int n)
{
for(int i =0; i < n; i++)
{
cout << be.str <<endl;
}
cout<<"-----------------"<<endl;
}
void show(string be, int n)
{
for(int i =0; i < n; i++)
{
cout << be <<endl;
}
cout<<"-----------------"<<endl;
}
8.5 编写模板函数max5(),它将一个包含5个T类型元素的数组作为参数,并返回数组中最大的元素
(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序使用该函数,将T替换为一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。
#include <iostream>
using namespace std;
int SIZE = 5;
template <typename T>
T max5(T ary[])
{
T max = ary[0];
for(int i = 1; i < SIZE; i++)
{
if(ary[i] > max)
{
max = ary[i];
}
}
return max;
}
int main()
{
int iArray[SIZE] = {12, 34, 100, 234, 6};
double dArray[SIZE] = {88.88, 653, 35.05, 2454.454, 566};
cout<<"the max value of iArray[SIZE]:"<<max5(iArray)<<endl;
cout<<"the max value of dArray[SIZE]:"<<max5(dArray)<<endl;
return 0;
}
8.6 编写模板函数maxn(),他将由一个T类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中最大的元素。在程序对它进行测试,该程序使用一个包含6个int元素的数组和一个包含4个都不了元素的数组来调用该函数。程序还包含一个具体化,他将char指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由5个字符串指针组成的数组来测试该具体化。
#include <iostream>
#include <cstring>
using namespace std;
template <typename T> T maxn(T ary[], int n);
template <> char * maxn<char *>(char *arr[], int n);
int main()
{
int iArray[6] = {12, 34, 100, 234, 6, 666};
double dArray[5] = {88.88, 653, 35.05, 2454.454, 566};
cout<<"the max value of iArray:"<<maxn(iArray, 6)<<endl;
cout<<"the max value of dArray:"<<maxn(dArray, 5)<<endl;
char * cArray[5] = {"qitiandasheng", "tianxiataiping", "zhongguowansui", "laomei", "woshishuaige"};
cout<<"the longest length of :"<<maxn(cArray, 5)<<endl;
return 0;
}
template <typename T> T maxn(T ary[], int n)
{
T max = ary[0];
for(int i = 1; i < n; i++)
{
if(ary[i] > max)
{
max = ary[i];
}
}
return max;
}
template <> char * maxn<char *>(char *arr[], int n)
{
char *temp = arr[0];
for(int i = 1; i < n; i++)
{
if(strlen(arr[i]) > strlen(temp))
{
temp = arr[i];
}
}
return temp;
}
8.7 修改程序清单8.14,使其使用两个名为SumArray()的模板函数来返回数组元素的总和,而不是显示数组的内容。程序应显示thing的总和以及所有debt的总和。
/******************程序清单8.14******************/
// tempover.cpp --- template overloading
#include <iostream>
template <typename T> // template A
void ShowArray(T arr[], int n);
template <typename T> // template B
void ShowArray(T * arr[], int n);
template <typename T> // template A
T SumArray(T arr[], int n);
template <typename T> // template B
T SumArray(T * arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main()
{
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];
// set pointers to the amount members of the structures in mr_E
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "Listing Mr. E's counts of things:\n";
// things is an array of int
ShowArray(things, 6); // uses template A
cout << "Listing Mr. E's debts:\n";
// pd is an array of pointers to double
ShowArray(pd, 3); // uses template B (more specialized)
int isum = SumArray(things, 6);
cout<<"the sum of things: "<< isum <<endl;
double dsum = SumArray(pd, 3);
cout<<"the sum of debt: "<< dsum <<endl;
return 0;
}
template <typename T>
void ShowArray(T arr[], int n)
{
using namespace std;
cout << "template A\n";
for (int i = 0; i < n; i++)
cout << arr[i] << ' ';
cout << endl;
}
template <typename T>
void ShowArray(T * arr[], int n)
{
using namespace std;
cout << "template B\n";
for (int i = 0; i < n; i++)
cout << *arr[i] << ' ';
cout << endl;
}
template <typename T>
T SumArray(T arr[], int n)
{
using namespace std;
cout << "template C\n";
T sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
template <typename T>
T SumArray(T * arr[], int n)
{
using namespace std;
cout << "template D\n";
T sum = 0;
for (int i = 0; i < n; i++)
sum += *arr[i];
return sum;
}