1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数,然而提供了第二个参数(int类型)且该参数不为0,则该函数打印的次数将为该函数被调用的次数。
#include <iostream>
using namespace std;
void show(char *ch, int k);
int main()
{
cout << "please enter you show word" << endl;
char ch[50];
cin.getline(ch,50);
cout << "please enter your show cishu:" << endl;
int k;
cin >> k;
show(ch, k);
system("pause");
return 0;
}
void show(char *ch, int k)
{
for (int i = 0; i < k; i++)
{
cout << ch<<" ";
}
}
2.CandyBar结构包含3个成员。第一个成员存储candy bar的品牌名称;第二个成员存储candy bar的重量;第三个成员存储candy bar的热量。请编写一个程序,他使用一个这样的函数,即将CandyBar的引用,char指针,double和int作为参数,并用最后三个值设置相应的结构成员。最后三个参数的默认值分别为“Millennium Munch”,2.85和350.另外该程序还需要包含一个以candyBar的引用为参数,并显示结构内容函数。尽可能使用const。
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string name;
double weight;
int kaluli;
};
void show(CandyBar &i, const char *name = "Millennium Munch", const double weight = 2.85, const int kaluli = 350);
void showset(CandyBar &u);
int main()
{
CandyBar i1, i2, i3;
show(i1);
showset(i1);
show(i2, "ijk", 2.1, 340);
showset(i2);
show(i3, "kji", 3.4, 330);
showset(i3);
system("pause");
return 0;
}
void show(CandyBar &i, const char *name, const double weight, const int kaluli)
{
i.name = name;
i.weight = weight;
i.kaluli = kaluli;
}
void showset(CandyBar &u)
{
cout << "name :" << u.name << endl;
cout << "weigtht:" << u.weight<<endl;
cout << "calorie:" << u.kaluli << endl;
}
3.编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转化为大写,为此可使用表6.4描述的函数toupper()。然后编写一个程序,他通过使用一个循环让您能够用不同的输入来测试这个函数,该程序的运行情况:
#include <iostream>
#include <string>
using namespace std;
void show(string &h);
int main()
{
cout << "please enter your zifuchuan if enter q quit" << endl;
string sh;
getline(cin,sh);
while (1)
{
show(sh);
getline(cin, sh);
if (sh == "q")
{
break;
}
}
system("pause");
return 0;
}
void show(string &h)
{
for (int i = 0; h[i] != '\0'; i++)
{
cout << char(h[i]+'A'-'a');
}
cout << endl;
}
4.请提供其中描述的函数和原型,从而完成该程序。注意,应有两个show()函数,每个都使用默认参数。请请尽可能使用const参数。set()使用new分配足够的控件存储指定的字符串。这里使用的技术与设计和实现类时使用的相似。
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
char *str;
int ct;
};
void set(stringy &st, const char *x);
void show(stringy &st, const int n = 1);
void show(const char *x, const int n = 1);
int main()
{
stringy beany;
char testing[] = " saidugiuashdi aosudghiu ahio";
set(beany ,testing);
show(beany);
show(beany, 2);
testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing, 3);
show("sad");
system("pause");
return 0;
}
void set(stringy &st, const char *x)
{
st.ct = strlen(x) + 1;
st.str = new char[st.ct];
strcpy_s(st.str, st.ct, x);
}
void show(stringy &st, const int n = 1)
{
for (int i = 0; i < n; i++)
{
cout << st.str << endl;
}
cout << endl;
}
void show(const char *x, const int n = 1)
{
for (int i = 0; i < n; i++)
{
cout << x << endl;
}
cout << endl;
}
5.编写模板函数max5(),它将由一个T类型元素组成的数组作为参数,并返回数组中最大的元素。在程序中使用该函数,将T替换为一个包含5个int的值的数组和一个包含5个double的数组以测试该函数。
#include <iostream>
using namespace std;
template <typename T>
void show(T a);
template <typename T>
void show_2(T b);
int main()
{
cout << "mo ren shu ru zhi " << endl;
cout << "int: 10,20,30,40,50" << endl;
cout << "double :1.1,2.2,3.3,4.4,5.5" << endl;
cout << "shi ji xian shi " << endl;
int i[5] = {
10, 20, 30, 40, 50 };
double k[5] = {
1.1, 2.2, 3.3, 4.4, 5.5 };
for (int p = 0; p < 5; p++)
{
show(i[p]);
}
cout <<endl;
for (int p = 0; p < 5; p++)
{
show(k[p]);
}
cout << endl;
system("pause");
return 0;
}
template <typename T>
void show(T a)
{
cout << a << ",";
}
template <typename T>
void show_2(T b)
{
cout << b << ",";
}
6.编写模板函数maxn()它将由一个T类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组的最大元素。在程序对它进行测试,该程序使用一个包含6个int元素的数组和一个包含4个double元素的数组来调用该函数。程序还包含一个具体化,它将char指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由5个字符串指针组成的数组来测试该具体化。
#include <iostream>
using namespace std;
template<typename T>
T maxn(T a[], int k);
template <>char *maxn(char *a[], int n);
int main()
{
cout << "shu ru de shu zu you :" << endl;
int k[6] = {
1, 2, 3, 4, 5, 6};
double p[4] = {
7.1, 8.1, 9.1, 10.1 };
char *q[5] = {
"one", "two", "three", "four", "five" };
for (int i = 0; i < 6; i++)
{
cout << k[i] << " ";
}
cout << endl;
for (int i = 0; i < 4; i++)
{
cout << p[i] << " ";
}
cout << endl;
for (int i = 0; i < 5; i++)
{
cout << q[i] << " ";
}
cout << endl;
int maxint = maxn(k, 6);
cout << "int de zui da zhi " << maxint << endl;
double maxdouble = maxn(p, 4);
cout << "double de zui da zhi " << maxdouble << endl;
char *maxchar = maxn(q, 5);
cout << "char de zui da zhi " << &maxchar << endl;
system("pause");
return 0;
}
template<typename T>
T maxn(T a[], int k)
{
T biao=0;
for (int i = 0; i < k; i++)
{
if (a[i]>biao)
{
biao = a[i];
}
}
return biao;
}
template <>char *maxn(char *a[], int n)
{
int k = 0;
int j = 0;
char *adress;
for (int i = 0; i < n; i++)
{
if (k<strlen(a[i]))
{
k = strlen(a[i]);
j = i;
}
}
adress = a[j];
return adress;
}
7.修改程序清单8.14,使其使用2个名为sumarray()的模板函数来返回数组元素的总和,而不是显示数组的内容。程序应显示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 thing[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 debts:\n";
int sum_1 = sumarray(thing, 6);
cout << "The sum of thing is : " << sum_1 << endl;
double sum_2 = sumarray(pd, 3);
cout << "The sum of the debt is : " << sum_2 << endl;
system("pause");
return 0;
}
template<typename T>
T sumarray(T arr[], int n)
{
cout << "Template A\n";
T sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + arr[i];
}
return sum;
}
template<typename T>
T sumarray(T *arr[], int n)
{
cout << "Template B\n";
T sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + *arr[i];
}
return sum;
}