注释:msvc编译。
第一题
注意点:使用全局变量对函数调用次数进行统计
//8.1
#include<iostream>
using namespace std;
void Show_str(const char a[], int i = 0);
int Count = 0;//全局变量,全局储存可用
int main()
{
const char a[30] = "Hello World!";
Show_str(a);
Show_str(a, 1);
Show_str(a, 1);
Show_str(a, 1);
return 0;
}
void Show_str(const char a[], int i)
{
if (i != 0)
{
Count++;
for (int m = 0; m < Count; m++)
{
cout << a << endl;
}
}
else
cout << a << endl;
}
第二题
注意点:引用结构参数,设置默认值指针,复制char字符串。
//8.2
#include<iostream>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
struct CandyBar
{
char name[40];
double weight;
int heat;
};
void enter_stru(CandyBar&, const char* n= "Millennium Much", double m= 2.85, int a = 350);
void Show_stru(const CandyBar&);
int main()
{
CandyBar andy{};
enter_stru(andy);
CandyBar funny{};
enter_stru(funny, "Billennium Faker", 2.999, 2100);
Show_stru(andy);
Show_stru(funny);
return 0;
}
void enter_stru(CandyBar& a, const char* b, double c, int d)
{
strcpy(a.name,b);
a.weight = c;
a.heat = d;
}
void Show_stru(const CandyBar& a)
{
std::cout << "name:\t" << a.name << endl
<< "weight: \t" << a.weight << endl
<< "heat:\t" << a.heat << endl;
}
第三题
注意点:string字符串的比较符号。
//8.3
#include<iostream>
#include<string>
using namespace std;
void a_up(string& );
int main()
{
string a;
cout << "Enter a string (q to quit):\n";
getline(cin, a);
while (a != "q")
{
a_up(a);
cout << "Next string (q to quit):\n";
getline(cin, a);
}
return 0;
}
void a_up(string& a)
{
int m = size(a);
for (int i = 0; i < m; i++)
{
a[i]=toupper(a[i]);
cout << a[i];
}
cout << endl;
}
第四题
注意点:创立在结构中的char指针需要额外定义分配空间,用new或malloc。
//8.4
#include<iostream>
using namespace std;
#include<cstring>
struct stringy
{
char* str; //结构中的char指针
int ct;
};
void set(stringy&, char[]);
void show(const stringy&, int n = 1);
void show(const char *, 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);
show("done!");
return 0;
}
void set(stringy&a, char b[])
{
int n=strlen(b);
a.str= new char[n+1];
a.str = b;
}
void show(const stringy& a, int n)
{
for (int i = 0; i < n; i++)
cout << a.str << endl;
}
void show(const char*m, int n)
{
for (int i = 0; i < n; i++)
cout << m << endl;
}
第五题
//8.5
#include<iostream>
using namespace std;
template<typename T>
T max5(T a[],int n=5);
int main()
{
int a[5] = { 1,2,4,5,7 };
double b[5] = { 1.2,3.3,45.6,78.0,123.4 };
int a1 = max5(a);
double a2 = max5(b);
cout << a1 << "\t" << a2 << endl;
return 0;
}
template<typename T>
T max5(T a[], int n)
{
T m;
for (int i = 0; i < n - 1; i++)
m = a[i] > a[i + 1] ? a[i] : a[i + 1];
return m;
}
第六题
//8.6
#include<iostream>
using namespace std;
template<typename T>
T maxn(T a[], int n);
template<>const char* maxn<const char*>(const char*a[], int n);
int main()
{
int ai[6]{ 1,2,3,4,5,4 };
double ad[4]{ 1.2,3.4,4.3,80.1 };
const char* ach[5]{ "first","two","twice","third","am" };
int a1 = maxn(ai, 6);
double a2 = maxn(ad, 4);
const char* ap = maxn(ach, 5);
cout << a1 << endl << a2 << endl << ap << endl;
return 0;
}
template<typename T>
T maxn(T a[], int n)
{
T m=0;
for (int i = 0; i < n - 1; i++)
m = a[i] > a[i + 1] ? a[i] : a[i + 1];
return m;
}
template<> const char* maxn<const char*>(const char* a[], int n)
{
int ap = 0;
for (int i = 0; i < n - 1; i++)
ap=strlen(a[ap]) < strlen(a[i + 1]) ?i+1:ap;
return a[ap];
}
第七题
注意点:创立两个重载函数模板。
//8.7
#include <iostream>
using namespace std;
template<typename T>
T SumArray(T arr[], int n);
template<typename T>T SumArray (T* a[], int n);
struct debts
{
char name[20];
double amount;
};
int main()
{
using namespace std;
int things[6] = { 13,31,103,301,310,130 };
struct debts mr_E[3] =
{
{"IF",2400.0},
{"UF",1300.0},
{"IS",1800.0}
};
double* pd[3];
for (int i = -0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << SumArray(things, 6) << endl << SumArray(pd, 3);
return 0;
}
template<typename T>
T SumArray(T arr[], int n)
{
T sum=0;
for (int i = 0; i < n; i++)
sum +=arr[i];
return sum;
}
template<typename T>
T SumArray(T* a[], int n)
{
int sum=0;
for (int i = 0; i < n; i++)
sum += *a[i];
return sum;
}