1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然而,如果提供了第二个参数(int 类型),且该参数不为 0,则该函数打印字符的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。
#include <iostream>
using namespace std;
void print_string(const char *str, int n = 0);
int main()
{
const char *str = "This is a test.";
cout << "Only one parameter, print 1 times:" << endl;
print_string(str);
cout << "Having two parameter can print 1 times:" << endl;
print_string(str, 3);
cout << "Bye." << endl;
return 0;
}
void print_string(const char *str, int n)
{
cout << str << endl;
}
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;
struct CandyBar
{
char brand[20];
double weight;
int calorie;
};
void initialize_candybar(CandyBar &temp, const char *str = "Millennium Munch", double w = 2.85, int c = 350);
void show_candybar(const CandyBar &temp);
int main()
{
CandyBar mycandybar;
initialize_candybar(mycandybar);
show_candybar(mycandybar);
return 0;
}
void initialize_candybar(CandyBar &temp, const char *str, double w, int c)
{
strcpy(temp.brand, str);
temp.weight = w;
temp.calorie = c;
}
void show_candybar(const CandyBar &temp)
{
cout << "CandyBar information:" << endl;
cout << "Brand: " << temp.brand << endl;
cout << "Weight: " << temp.weight << endl;
cout << "Calorie: " << temp.calorie << endl;
}
3.编写一个函数,它接受一个指向 string对象的引用作为参数,并将该 string 对象的内容转换为大写,为此可使用表 6.4 描述的函数 toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数,该程序的运行情况如下:
Enter a string (q to quit]: go awayGO AWAY
Next atring [q to quit): good grief!
GOOD GRIEF!
Next atring [g to quit): q
Bye.
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
void print_upper_string(string &str);
int main()
{
string str;
cout << "Enter a string (q to quit): ";
while (getline(cin, str) && str != "q")
{
print_upper_string(str);
cout << str << endl;
cout << "Next string (q to quit): ";
}
cout << "Bye." << endl;
return 0;
}
void print_upper_string(string &str)
{
for (int i = 0; i < str.size(); i++)
{
str[i] = toupper(str[i]);
}
}
4,下面是一个程序框架:
#include ciostream>using mamespace std;
#include ccstring>
struct stringychar * str;int et;
// for strlen(],strcpyl)
// points to a string// length of string inot counting yo']
// prototypes for set [), show(), and show[) go hereint main(l
stringy beany;char tostingl] "Reality isn't what it used to be.";
// first argument is a reference.sat (beany, testing];// 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 merber of beany
show (beany) :
// prints member string once
Bhow(beany,2;// printa member string twice
testing0] m iD';
testingll] =u';
Bhow(testing):// prints testing string once
show(testing,3); // prints testing string thrice
show("Donc!";
return 0;
请提供其中描述的函数和原型,从而完成该程序。注意,应有两个 show()函数,每个都使用默认参数。请尽可能使用 cosnt 参数sct( )使用 new 分配是够的空间来存储指定的字符串。这里使用的技术与设计和实现类时使用的相似。(可能还必须修改头文件的名称,删除 using 编译指令,这取决于所用的编译器。
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
char *str;
int ct;
};
void set(stringy &stry, const char *str);
void show(const stringy &stry, int n = 1);
void show(const char *str, int n = 1);
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing);
show(beany);
show(beany, 2);
testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing, 3);
delete[] beany.str;
show("Done!");
return 0;
}
void set(stringy &stry, const char *str)
{
stry.str = new char[strlen(str) + 1];
strcpy(stry.str, str);
return;
}
void show(const stringy &stry, int n)
{
for (int i = 0; i < n; ++i)
{
cout << stry.str << endl;
}
}
void show(const char *str, int n)
{
for (int i = 0; i < n; ++i)
{
cout << str << endl;
}
}
5.编写模板函数 max5(),它将一个包含个类型元素的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该雨数,将T替换为一个包含5个int 值的数组和一个包含 5个 dowble 值的数组,以测试该函数
#include <iostream>
using namespace std;
template <typename T>
T max5(const T *array);
int main()
{
int test1[5] = {1, 3, 5, 7, 9};
double test2[5] = {10.0, 20.0, 15.0, 12.0, 30.0};
cout << "The max of int array is: " << max5(test1) << endl;
cout << "The max of double array is: " << max5(test2) << endl;
return 0;
}
template <typename T>
T max5(const T *array)
{
T maxv = array[0];
for (int i = 1; i < 5; i++)
{
if (maxv < array[i])
{
maxv = array[i];
}
}
return maxv;
}
6.编写模板函数 maxn(),它将由一个T类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中最大的元素。在程序对它进行测试,该程序使用一个包含 6 个 nt 元素的数组和一个包含 4个 double 元素的数组来调用该函数。程序还包含一个具体化,它将 char 指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由5个字符串指针组成的数组来测试该具体化。
#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
T maxn(T *arr, int n);
template <>
const char *maxn(const char *str[], int n);
int main()
{
int test1[6] = {1, 3, 5, 7, 9, 11};
double test2[4] = {10.0, 20.0, 15.0, 12.0};
const char *test3[5] = {"123", "12345", "123456", "abcdefg", "xio"};
cout << "The max of int array is: " << maxn(test1, 6) << endl;
cout << "The max of double array is: " << maxn(test2, 4) << endl;
cout << "The max of string array is: " << maxn(test3, 5) << endl;
return 0;
}
template <typename T>
T maxn(T *arr, int n)
{
T maxv = arr[0];
for (int i = 0; i < n; i++)
{
if (maxv < arr[i])
{
maxv = arr[i];
}
}
return maxv;
}
template <>
const char *maxn(const char *str[], int n)
{
const char *maxv = *str;
for (int i = 1; i < n; i++)
{
if (strlen(maxv) < strlen(str[i]))
{
maxv = str[i];
}
}
return maxv;
}
7.修改程序清单8.14,使其使用两个名为 SumAray0的模板函数来返回数组元素的总和,而不是显示数组的内z容。程序应显示 thing 的总和以及所有 debt 的总和。
#include <iostream>
using namespace std;
template <typename T>
T SumArray(T arr[], int n);
template <typename T>
T SumArray(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;
}
cout << "Listing Mr. E's total of things:" << endl;
cout << SumArray(things, 6) << endl;
cout << "Listing Mr. E's total of debts:" << endl;
cout << SumArray(pd, 3) << endl;
return 0;
}
template <typename T>
T SumArray(T arr[], int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}
template <typename T>
T SumArray(T *arr[], int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
{
sum += *arr[i];
}
return sum;
}