编程练习1
#include <iostream>
using namespace std;
void p(char ch[]);
void p(char ch[], int i);
const int ChSize = 40;
int main()
{
char ch[ChSize];
cout << "please enter a string: ";
cin.getline(ch, ChSize);
p(ch);
p(ch, 5);
system("pause");
return 0;
}
void p(char ch[])
{
cout <<"show this string only once: "<<endl;
cout << ch <<endl;
}
void p(char ch[], int i)
{
if (i!=0)
{
cout <<"show this string " <<i<<" time(s): "<<endl;
for (int n=0;n<=i;n++)
cout << ch << endl;
}
}
编程练习2
#include <iostream>
using namespace std;
struct CandyBar{
char name[40];
double weight;
int kcal;
};
void set(CandyBar &cb,const char name[] = "Millennium Munch",const double weight=2.85,const int kcal=350);
void show(const CandyBar &cb);
int main(){
CandyBar cb,cb2;
set(cb, "candy12345",123.2, 1700);
set(cb2);
show(cb);
show(cb2);
system("pause");
return 0;
}
void set(CandyBar &cb,const char name[],const double weight,const int kcal){
strcpy(cb.name, name);
cb.weight = weight;
cb.kcal = kcal;
}
void show(const CandyBar &cb){
using namespace std;
std::cout << "name: "<< cb.name << ", weight: "<<cb.weight<<", kcal: "<<cb.kcal<<std::endl;
}
编程练习3
#include <iostream>
#include <string>
using namespace std;
string toupper(string str);
int main()
{
string str;
while(true)
{
cout<< "Enter a string (q to quite): ";
getline(cin,str);
if(str=="q")
break;
cout << toupper(str) <<endl;
}
system("pause");
return 0;
}
string toupper(string str)
{
for (int i=0;i<str.length();i++)
{
str[i] = toupper(str[i]);
}
return str;
}
编程练习4
#include <iostream>
#include <string>
using namespace std;
struct stringy {
char * str;
int ct;
};
void set(stringy &sty, const char * st);
void show(const stringy &sty, int i=1);
void show(const string &str, int i=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!");
system("pause");
return 0;
}
void set(stringy &sty, const char * st)
{
char * cs = new char[strlen(st)+1];
strcpy(cs, st);
sty.str = cs;;
sty.ct = strlen(st);
}
void show(const stringy &sty, int i)
{
for (i; i>0;i--)
{
cout << "string is: " << sty.str << endl;
cout << "string length is: " << sty.ct << endl;
}
}
void show(const string &str, int i)
{
for (i; i>0;i--)
{
cout << "string is: " << str << endl;
}
}
编程练习5
#include <iostream>
using namespace std;
template <class T>
T max5(T *t);
int main()
{
int intArray[5] = {1,4,8,12,3};
double doubleArray[5] = {3.2, 5.67, 9.0, 12.5, 3.19};
cout << max5(intArray) << endl;
cout << max5(doubleArray) << endl;
system("pause");
return 0;
}
template <class T>
T max5(T *t)
{
T temp = *t;
for (int i=1;i<5;i++)
{
t++;
if (temp<*t)
temp=*t;
}
return temp;
}
编程练习6
#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
T maxn(T arr[], int n);
template <> char * maxn<char *>(char *arr[], int n);
int main()
{
int intarr[6] = {1, 2, 3, 4, 5, 6};
double doublearr[4] = {1.2, 3.4, 4.5, 5.6};
char *charr[5]={"wang","li","zhang","zhao","huang"};
cout << " the largest integer is : " << maxn(intarr, 6) << endl;
cout << " the largest double is : " << maxn(doublearr, 6) << endl;
cout << " the longest string is : " << maxn(charr, 5) << endl;
system("pause");
return 0;
}
template <typename T>
T maxn(T arr[], int n)
{
T temp = arr[0];
for (int i = 1; i < n; i++)
{
if (temp < arr[i])
temp = arr[i];
}
return temp;
}
template <> char * maxn<char *>(char *arr[], int n)
{
char *pa=arr[0];
int temp = strlen(pa);
for (int i = 1; i < n; i++)
{
if(temp < strlen(arr[i]))
pa = arr[i];
else
pa = pa;
}
return pa;
}
编程练习7
#include <iostream>
using namespace std;
template <typename T>
void ShowArray(T arr[], int n);
template <typename T>
void ShowArray(T * arr[], int n);
template <typename T>
void SumArray(T arr[], int n);
template <typename T>
void SumArray(T * arr[], int n);
struct debts
{
char name[30];
double amount;
};
int main()
{
int things[6] = {13, 31, 103, 301, 310, 130};
struct debts mr_E[3] =
{
{"Ima Wolfe", 2400},
{"Ura Foxe", 1300},
{"Iby Stout", 1800}
};
double *pd[3];
for (int i=0; i<3;i++)
pd[i] = &mr_E[i].amount;
cout << "Listing Mr. E's counts of things: "<<endl;
ShowArray(things, 6);
SumArray(things, 6);
cout <<"Listing Mr. E's debts: "<<endl;
ShowArray(pd, 3);
SumArray(pd, 3);
system("pause");
return 0;
}
template <typename T>
void ShowArray(T arr[], int n)
{
cout << "template A"<<endl;
for (int i=0;i<n;i++)
cout << arr[i]<<" ";
cout<<endl;
}
template <typename T>
void ShowArray(T * arr[], int n)
{
cout << "template B"<<endl;
for (int i=0;i<n;i++)
cout << *arr[i]<<" ";
cout<<endl;
}
template <typename T>
void SumArray(T arr[], int n)
{
cout << "template C"<<endl;
T temp = 0;
for (int i=0;i<n;i++)
temp = temp+arr[i];
cout<<"sum of array is " << temp <<endl;
}
template <typename T>
void SumArray(T * arr[], int n)
{
cout << "template D"<<endl;
T temp = 0;
for (int i=0;i<n;i++)
temp = temp+ *arr[i];
cout<<"sum of array is " << temp <<endl;
}