8.1
#include<stdafx.h>
#include<iostream>
#include<string>
using namespace std;
void print(string p, int i = 0);//默认参数是在函数声明时指定的,函数定义时不能有默认参数
int main()
{
string st = "c++";
cout << "Test one parameter\n";
print(st);
cout << "Test two parameter\n";
print(st, 3);
return 0;
}
void print(string p,int i)//使用默认参数时的函数声明与不使用默认参数时相同
{
cout << p<<endl;
if (i>1)
{
print(p, --i);
}
}
8.2
#include<stdafx.h>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
struct Candbar
{
char brand[20];
double weight;
int qunantity;
};
void set(Candbar &Can, char *s_p, double s_w, int s_q);
void show(const Candbar & can);
int main()
{
Candbar can;
char brand[20] = "Millennium Munch";
double weight = 2.85;
int qunati = 350;
set(can, brand, weight, qunati);
show(can);
return 0;
}
void set(Candbar &Can,char * s_p, double s_w, int s_q)
{
strcpy_s(Can.brand, s_p);
/*Can.brand=*s_p是错误的,本意是想将s_p所指向的字符串
赋给结构变量的brand成员,但是其为字符数组,数组名称表示其
地址,即Can.brand为其地址,是一个右值可以将其赋给char指针,
这里需要用字符串复制函数才能将其初始化为对应的字符串*/
Can.weight = s_w;
Can.qunantity = s_q;
}
void show(const Candbar & can)
{
cout << "Brand is :" << can.brand << endl
<< "weight is :" << can.weight << endl
<< "qunantity is: " << can.qunantity << endl;
}
8.3
#include<stdafx.h>
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
void s_toupper(string& str);
int main()
{
string str;
cout << "enter a string(q to quit): ";
getline(cin, str);
while (str!="q")
{
s_toupper(str);
cout << str << endl;
cout << "next string(q to quit): ";
getline(cin, str);
}
cout << "Bye";
return 0;
}
void s_toupper(string& str)
{
for (int i = 0; str[i]; i++)
{
str[i] = toupper(str[i]);
}
}
8.4
#include<stdafx.h>
#include<iostream>
#include<cstring>
struct stringy
{
char *str;
int ct;
};
using namespace std;
void set(stringy &stri1, char *testing);
void show(const stringy stri, int n = 1);
void show(const char * str, int n = 1);
int main()
{
stringy beany = {NULL,0};
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);
show("Done!");
return 0;
}
void set(stringy &stri1, char *testing)
{
stri1.ct= strlen(testing);
stri1.str =new char(stri1.ct+1);
strcpy(stri1.str, testing);
}
void show(const stringy stri, int n)
{
for(int i=0;i<n;i++)
{
cout << stri.str[i]<<endl;
}
}
void show(const char * str, int n)
{
for (int i = 0; i <n; i++)
{
cout << str[i]<<endl;
}
}
8.5
#include<stdafx.h>
#include<iostream>
#include<cstring>
using namespace std;
template<typename T>
T max5(T *arry);
int main()
{
int int_a[5] = { 1,4,5,3,7 };
double double_a[5] = { 1.2,2.4,3.5,2.1,4.5 };
int int_max = max5(int_a);
double double_max = max5(double_a);
cout << int_max << endl
<< double_max << endl;
return 0;
}
template<typename T>
T max5(T *arry)
{
T max = arry[0];
for (int i = 1; i < 5; i++)
{
if (max<arry[i])
{
max = arry[i];
}
}
return max;
}
8.6
#include<stdafx.h>
#include<iostream>
#include<cstring>
using namespace std;
template<typename T>
T maxn(T *arry,int n);
int main()
{
int int_a[6] = { 1,4,5,3,7,6 };
double double_a[4] = { 1.2,2.4,3.5,2.1 };
int int_max = maxn(int_a,6);
double double_max = maxn(double_a,4);
cout << int_max << endl
<< double_max << endl;
return 0;
}
template<typename T>
T maxn(T *arry,int n)
{
T max = arry[0];
for (int i = 1; i < n; i++)
{
if (max<arry[i])
{
max = arry[i];
}
}
return max;
}
8.7
#include<stdafx.h>
#include<iostream>
#include<cstring>
struct bebts
{
char name[50];
double amunt;
};
using namespace std;
template<typename T>
int SumArry(T * ar, int n);
template<typename T>
double SumArry(T *arr[], int n);
int main()
{
bebts mr_E[3] = {
{"Ima",2400.0},
{"Ura",1300.0},
{"Iby",1800.0}
};
int thing[6] = { 13,31,103,301,310,130 };
double *p[3];
for (int i = 0; i < 3; i++)
{
p[i] =&mr_E[i].amunt;
}
int t_sum = SumArry(thing, 6);
double mr_sum = SumArry(p, 3);
cout << t_sum << endl << mr_sum << endl;
return 0;
}
template<typename T>
int SumArry(T * ar,int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + ar[i];
}
return sum;
}
template<typename T>
double SumArry(T *arr[], int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
{
sum = sum + *arr[i];
}
return sum;
}