题目
程序
8.1
#include <iostream>
using namespace std;
void print(const char * str,int n=0);
int count=1;
int main()
{
char p[]="**";
print(p);
print(p,3);
print(p,8);
print(p);
return 0;
}
void print(const char * str,int n)
{
if(n==0)
{
cout<<str<<endl;
}
else
{
for(int i=0;i<count;i++)
{
cout<<str;
}
cout<<endl;
}
count++;
}
8.2
#include <iostream>
#include <cstring>
using namespace std;
const int SIZE=40;
struct CandyBar
{
char name[SIZE];
double weight;
int calories;
};
void set(CandyBar &candy,char *name="Millennium Munch",double weight=2.85,int calories=350);
void show(const CandyBar &candy);
int main()
{
CandyBar candy1,candy2;
char name[SIZE];
double weight;
int calories;
set(candy1);
show(candy1);
cout<<"Name: ";
cin.getline(name,SIZE);
cout<<"Weight: ";
cin>>weight;
cout<<"Calories: ";
cin>>calories;
set(candy2,name,weight,calories);
show(candy2);
return 0;
}
void set(CandyBar &candy,char *name,double weight,int calories)
{
strcpy(candy.name,name);
candy.weight=weight;
candy.calories=calories;
}
void show(const CandyBar &candy)
{
cout<<"Name: "<<candy.name<<endl;
cout<<"Weight: "<<candy.weight<<endl;
cout<<"Calories: "<<candy.calories<<endl;
}
8.3
#include <iostream>
#include <string>
using namespace std;
void toup(string &letter);
int main()
{
string letter;
cout<<"Enter a string(q to quit): ";
getline(cin,letter);
while(letter!="q")
{
toup(letter);
cout<<"Next string(q to quit): ";
getline(cin,letter);
}
cout<<"Bye."<<endl;
return 0;
}
void toup(string &letter)
{
for(int i=0;i<letter.size();i++)
{
letter[i]=toupper(letter[i]);
}
cout<<letter<<endl;
}
8.4
#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{
char * str;
int ct;
};
void set(stringy & beany,char *str);
void show(const stringy & beany,int n=1);
void show(const char *str,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 & beany,char *str)
{
beany.ct=strlen(str);
beany.str=new char[beany.ct+1];
strcpy(beany.str,str);
}
void show(const stringy & beany,int n)
{
for(int i=0;i<n;i++)
{
cout<<beany.str<<endl;
}
cout<<endl;
}
void show(const char *str,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<strlen(str);j++)
{
cout<<str[j];
}
cout<<endl;
}
cout<<endl;
}
8.5
#include <iostream>
using namespace std;
template <class T>
T max5(T num[]);
int main()
{
int num1[5]={4,2,3,4,5};
cout<<max5(num1)<<endl;
double num2[5]={3.2,4.5,5.3,6.4,7.3};
cout<<max5(num2)<<endl;
return 0;
}
template <class T>
T max5(T num[])
{
const int n=5;
T max=num[0];
for(int i=1;i<n;i++)
{
// if(max<num[i])
// {
// max=num[i];
// }
max=max>num[i]? max: num[i];
}
return max;
}
8.6
#include <iostream>
#include <cstring>
using namespace std;
template <class T>
T maxn(T num[],int n);
template <> char *maxn(char *str[],int n);
int main()
{
int num1[6]={4,2,3,4,5,9};
cout<<maxn(num1,6)<<endl;
double num2[4]={4.5,5.3,6.4,7.3};
cout<<maxn(num2,4)<<endl;
char * str[5]={"dog","duck","box","computer","egg"};
cout<<maxn(str,5)<<endl;
return 0;
}
template <class T>
T maxn(T num[],int n)
{
T max=num[0];
for(int i=1;i<n;i++)
{
// if(max<num[i])
// {
// max=num[i];
// }
max=max>num[i]? max: num[i];
}
return max;
}
template <> char *maxn(char *str[],int n)
{
char *max=str[0];
int l=strlen(str[0]);
for(int i=1;i<n;i++)
{
if(l<strlen(str[i]))
{
max=str[i];
l=strlen(str[i]);
}
}
return max;
}
8.7
#include <iostream>
template <typename T> // template A
T SumArray(T arr[], int n);
template <typename T> // template B
T 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: "<<SumArray(things,6)<<endl;
cout << "Listing Mr. E's debts: "<<SumArray(pd,3)<<endl;
// cin.get();
return 0;
}
template <typename T>
T SumArray(T arr[], int n)
{
using namespace std;
cout << "template A\n";
T sum=arr[0];
for (int i = 1; i < n; i++)
sum+=arr[i];
return sum;
}
template <typename T>
T SumArray(T * arr[], int n)
{
using namespace std;
cout << "template B\n";
T sum=* arr[0];
for (int i = 1; i < n; i++)
sum+=*arr[i];
return sum;
}