//编程练习1 (默认参数)
#include <iostream>
int print(char * ,int n = 0);
using namespace std;
int count = 0;
int main()
{
print("xi");
print("xi",3);
print("mm",3);
return 0;
}
int print(char * str ,int n)
{
count++;
if(n==0)
{
cout << str << endl;
}
else
{
cout << "there have two parameter.\n";
for (int i=0;i < count; i++)
{
cout << str << endl;
}
}
}
//编程练习2 (默认参数、引用)
#include <iostream>
struct CandyBar
{
char * name;
double weight;
int calorie;
} ;
using namespace std;
const CandyBar & fillnum( CandyBar & re , char * ar= "Millennium Munch" , double w =2.85, int c= 350);
void show (const CandyBar & re);
int main()
{
CandyBar DZ;
fillnum(DZ,"xiang",3.45);
show(DZ);
fillnum(DZ,"QIU",2.22,2);
show(DZ);
return 0;
}
const CandyBar & fillnum ( CandyBar & re , char ar[] , double w, int c)
{
re.name = ar;//数组的填充
re.weight = w;
re.calorie = c;
return re;
}
void show (const CandyBar & re)
{
cout << "show : \n";
cout << "name:" << re.name << endl;
cout << "weight:" <<re.weight << endl;
cout << "calorie:" << re.calorie <<endl;
}
//编程练习3
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void turn( string & str );
int main()
{
cout << "Enter a string (q to quit): ";
string str;
getline (cin,str);
while (cin && (str != "q"))
{
turn(str);
cout << "Next string (q to quit): ";
getline (cin,str);
}
cout << "Bye.\n";
return 0;
}
void turn(string & str )
{
for (int i = 0; i < str.length() ; i++)
{
str[i] = toupper(str[i]);//如果在这里新建一个变量来保存程序会出错,原因待研究!
} //用循环可以输出新变量的内容
cout << str << endl;
}
//编程练习4
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
char *str;
int ct;
};
void set(stringy & stry ,const char *ar);//这里要使用引用
void show(const stringy &str,int n=1);
void show(const char *,int n=1);
int main()
{
stringy beany;
char testing[] = "Reality isn't what is used to be.";
set(beany,testing);
show(beany);
show(beany,2);
testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing,3);
show("Done!");
delete [] beany.str;
return 0;
}
void set(stringy & stry ,const char *ar)
{
char *ps = new char[strlen(ar)+1];
stry.str = ps;
strcpy(stry.str,ar);
stry.ct = strlen(stry.str);
}
void show(const stringy &str,int n)
{
if(n==1)
{
cout << str.str << endl;
}
else
{
cout << "print " << n << "times.\n";
while(n!=0)
{
cout << str.str<< endl;
n--;
}
}
}
void show(const char * ar,int n)
{
if(n==1)
{
cout << ar << endl;
}
else
{
cout << "print " << n << "times.\n";
while(n!=0)
{
cout << ar<< endl;
n--;
}
}
}
//编程练习5(函数模板)
#include <iostream>
const int Size = 5;
using namespace std;
template <class T>
T max5( T * ar);
int main()
{
int num[Size] = {1,2,3,4,5};
double num1[Size] = {2.3,4.5,3.4,34.3,23.4};
cout << "max int: " << max5(num) << endl;
cout << "max double:" << max5(num1) << endl;
return 0;
}
template <class T>
T max5( T * ar)
{
T max = ar[0];
int i = 1;
while(i<Size)
{
if (max<=ar[i])
{
max = ar[i];
}
i++;
}
return max;
}
//编程练习6(具体化)
#include <iostream>
#include <cstring>
using namespace std;
template <class T>
T maxn( T * ar,int n);
template <> const char* maxn(const char * str[],int n); //具体化的函数名跟模板函数的一样
int main()
{
int num[6] = {1,2,3,4,5,6};
double num1[4] = {2.3,4.5,3.4,34.3};
cout << "max int: " << maxn(num,6) << endl;
cout << "max double:" << maxn(num1,4) << endl;
const char * str[5] = {"pemhasf","qsdfs","ro","is","grl"};
cout << "max char length address:" << maxn(str,5) << endl;
return 0;
}
template <class T>
T maxn( T * ar,int n)
{
T max = ar[0];
int i = 1;
while(i<n)
{
if (max<=ar[i])
{
max = ar[i];
}
i++;
}
return max;
}
template<> const char* maxn(const char *str[],int n)
{
const char * max = str[0];
int i = 1;
while(i<n)
{
if (strlen(max)< strlen(str[i]))
{
max = str[i];
}
i++;
}
return max;
}
// 编程练习7(匹配函数)
#include <iostream>
template <typename T> // template A
void SumArray(T arr[], int n);
template <typename T> // template B
void SumArray(T * arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main()
{
using namespace std;
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];
// set pointers to the amount members of the structures in mr_E
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "Listing Mr. E's counts of things:\n";
// things is an array of int
SumArray(things, 6); // uses template A
cout << "Listing Mr. E's debts:\n";
// pd is an array of pointers to double
SumArray(pd, 3); // uses template B (more specialized)
// cin.get();
return 0;
}
template <typename T>
void SumArray(T arr[], int n)
{
using namespace std;
T sum = 0;
cout << "template A's sum :";
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
cout << sum <<endl;
}
template <typename T>
void SumArray(T * arr[], int n)
{
using namespace std;
T sum = 0;
cout << "template B's sum :";
for (int i = 0; i < n; i++)
sum += *arr[i] ;
cout << sum <<endl;
}