1。编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然而,如果提供了第二个函数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。
答:
#include<iostream>
using namespace std;
void print(char*, int b=0); //带默认参数时,默认参数放在开始
int main()
{
char x[10] = "abcde";
print(x);
cout << endl;
print(x, 5);
cout << endl;
print(x, 100);
system("pause");
return 0;
}
void print(char* a, int b)
{
static int m = 0; //静态变量,用于储存被调用的次数
m++; //每次调用+1
if (b == 0) //使用默认参数时,只输出一次
{
cout << a << endl;
return;
}
for (int i = 0;i < m;i++)
cout << a << endl;
}
输出:
abcde
abcde
abcde
abcde
abcde
abcde
请按任意键继续. . .
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<string>
using namespace std;
struct CandyBar
{
string name;
double weight;
int heat;
};
void set(CandyBar&, const char* a = "Millennium Munch", const double = 2.85, const int = 350);
void show(const CandyBar&);
int main()
{
CandyBar candy,baby; //声明两个结构
set(candy); //candy使用默认参数
char *m = new char[20];
m = "My Love";
set(baby, m, 3.5, 300); //baby使用自定义参数
show(candy); //显示这两个结构
show(baby);
system("pause");
return 0;
}
void set(CandyBar& m, const char*a, const double b, const int c)
{
m.name = a;
m.weight = b;
m.heat = c;
}
void show(const CandyBar& m)
{
cout << "Name: " << m.name << " , weitght: " << m.weight << " , heat: " << m.heat << endl;
}
输出:
Name: Millennium Munch , weitght: 2.85 , heat: 350
Name: My Love , weitght: 3.5 , heat: 300
请按任意键继续. . .
3.编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,为此可使用表6.4描述的函数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;
string upper(string&);
int main()
{
cout << "Enter a string(q to quit):";
string a;
getline(cin, a);
while (a != "q")
{
cout << upper(a) << endl;
cout << "Next string (q to quit): ";
getline(cin, a);
}
cout << "Bye." << endl;
system("pause");
return 0;
}
string upper(string& a)
{
for (int i = 0;a[i];i++) //如果a[i]是空字符,则a[i]=0,循环结束
{
a[i] = toupper(a[i]); //toupper(a[i])是将小写字母提升成大写,并赋值给a[i]
}
return a;
}
输出:
Enter a string(q to quit):ab
AB
Next string (q to quit): a b
A B
Next string (q to quit): A b
A B
Next string (q to quit): q
Bye.
请按任意键继续. . .
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 couting '\0')
};
// prototypes for set(), show(), and show() go here
int main()
{
string 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 couting '\0') //长度
};
void set(stringy&,const char*);
void show(const stringy&, int b = 1);
void show(const char*, int b = 1);
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing);
//第一个参数是一个结构别名,用于储存第二个参数传递的值(第二个因为是字符串,所以是指针),把第二个设置为第一个结构的str成员
//大概意思就是beany.str new一个新的字符串,然后将testing赋值到这里,然后根据字符串长度要计数
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!");
system("pause");
return 0;
}
void set(stringy& a, const char*b)
{
a.ct = strlen(b) + 1; //长度为b的字符串长度+1
a.str = new char[a.ct]; //new一个出来
strcpy_s(a.str, a.ct, b); //编译器提示strcpy()不安全,因此只能用strcpy_s。这个命令会复制空字符
}
void show(const stringy&a, int b)
{
for (int i = 0;i < b;i++)
cout << a.str << endl;
}
void show(const char* a, int b)
{
for (int i = 0;i < b;i++)
cout << a << endl;
}
5.编写模板函数max5(),它将一个包含5个T类型元素的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将T替换为一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。
答:
#include<iostream>
using namespace std;
template<class T>T max5(T *a)
{
T temp = a[0];
for (int i = 1;i < 5;i++)
if (a[i]>temp)temp = a[i];
return temp;
}
int main()
{
int a[5] = { 1,3,5,4,2 };
double b[5] = { 1.1,5.5,3.3,9.9,4.4 };
int c = max5(a);
double d = max5(b);
cout << "c = " << c << ", d = " << d << endl;
system("pause");
return 0;
}
输出:
c = 5, d = 9.9
请按任意键继续. . .
6.编写模板函数maxn(),它将由一个T类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中最大的元素。在程序对它进行测试,该程序使用一个包含6个int元素的数组和一个包含4个double元素的数组来调用该函数。程序还包含一个具体化,它将char指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由5个字符串指针组成的数组来测试该具体化。
答:
#include<iostream>
using namespace std;
template<typename T>T maxn(T *a, const int b);
template<>char* maxn(char*[], const int);
int main()
{
int a[6] = { 1,3,6,3,2,9 };
double b[4] = { 11.1,55.5,33.3,29.2 };
char *c[5];
c[0] = "abbb";
c[1] = "defff";
c[2] = "hhhhh";
c[3] = "ee";
c[4] = "mmm";
int d = maxn(a,6);
double e = maxn(b,4);
char *f = maxn(c, 5);
cout << "d = " << d << ", e = " << e << ", f = " << f << endl;
system("pause");
return 0;
}
template<typename T>T maxn(T *a, const int b)
{
T temp=a[0];
for (int i= 1;i < b;i++)
if (a[i] > temp)temp = a[i];
return temp;
}
template<>char* maxn(char*a[], const int b)
{
char*c = a[0];
for (int i = 1;i < b;i++)
if (strlen(a[i])>strlen(c))c = a[i];
return c;
}
输出:
d = 9, e = 55.5, f = defff
请按任意键继续. . .
7.修改程序清单8.14,使其使用两个名为SumArray()的模板函数来返回数组元素的总和,而不是显示数组的内容。程序应显示thing的总和以及所有debt的总和。
答:
#include<iostream>
using namespace std;
template<class T>T SumArray(T a[],int n);
template<typename T>T SumArray(T *a[], int n);
struct debts
{
char name[50];
double amount;
};
int main()
{
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 << "SumArray(things, 6) = " << SumArray(things, 6) << endl;
cout << "Sumarray(pd,3) = " << SumArray(pd, 3) << endl;
system("pause");
return 0;
}
template<class T>T SumArray(T a[], int n)
{
T total=0;
for (int i = 0;i < n;i++)
total = total + a[i];
return total;
}
template<typename T>T SumArray(T *a[], int n)
{
T total = 0;
for (int i = 0;i < n;i++)
total = total + *a[i];
return total;
}
输出:
SumArray(things, 6) = 888
Sumarray(pd,3) = 5500
请按任意键继续. . .