c++primer plus 6th第八章答案

//1

#include    <iostream>
#include <cstring>
void f(char *s=(char*)"asddsa",int n=0);


int main()
{
using namespace std;
char *s;
s=new char[20];
strcpy(s,"asdsdadsda");
f();
f(s,2);
f();
f((char*)"ad",3);
return 0;
}


void f(char *s,int n)
{
using std::cout;
using std::endl;
static int num=0;
num++;
if(n!=0)
{
for(int i=0;i<num;i++)
cout<<s<<endl;
}
else
cout<<s<<endl;
}

//2

#include    <iostream>
#include <cstring>
struct CandyBar
{
char name[20];
double weight;
int heat;
};


void display(const CandyBar&);


void f(CandyBar&,char*name=(char*)"Millennium Munch",
double weight=2.85,int heat=350);


int main()
{
using namespace std;
CandyBar c{"asd",2.3,1};
display(c);
CandyBar a;
f(a);
display(a);
return 0;
}


void f(CandyBar& candybar,char*name,double weight,int heat)
{
strcpy(candybar.name,name);
candybar.weight=weight;
candybar.heat=heat;
}


void display(const CandyBar &candybar)
{
using std::cout;
using std::endl;
cout<<"name is "<<candybar.name<<endl;
cout<<"weight is "<<candybar.weight<<endl;
cout<<"heat is "<<candybar.heat<<endl;
}

//3

#include    <iostream>
#include <string>
#include <cctype>


const std::string& toUpper(std::string&);


int main()
{
using namespace std;
string s;
cout<<"Enter a string(q to quit):";
while(getline(cin,s)&&s!="q")
{
cout<<toUpper(s)<<endl;
cout<<"Next string(q to quit):";
}
cout<<"Bye!"<<endl;
return 0;
}


const std::string& toUpper(std::string&s)
{
int i;
for(i=0;i<s.length();i++)
s[i]=toupper(s[i]);
return s;
}

//4

#include    <iostream>
#include <cstring>


struct stringy
{
char *str;
int ct;
};


void set(stringy&,char*);
void show(const stringy&,int n=1);
void show(const char*,int n=1);


int main()
{
using namespace std;
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&s,char*a)
{
s.ct=strlen(a);
s.str=new char[s.ct];
strcpy(s.str,a);
}


void show(const stringy&s,int n)
{
using std::cout;
using std::endl;
for(int i=0;i<n;i++)
cout<<s.str<<endl<<s.ct<<endl;
}


void show(const char*s,int n)
{
using std::cout;
using std::endl;
for(int i=0;i<n;i++)
cout<<s<<endl;
}

//5

#include    <iostream>


template<class T>T max5(T a[5])
{
T max=a[0];
for(int i=1;i<5;i++)
if(a[i]>max)
max=a[i];
return max;
}


int main()
{
using namespace std;
int a[]={1,2,3,5,6};
double b[]={1.2,4,2.4,7.6,6.5};
cout<<max5(a)<<endl;
cout<<max5(b)<<endl;
return 0;
}

//6

#include    <iostream>
#include <cstring>




template<typename T> T maxn(T *a,unsigned int n)
{
T max=a[0];
for(int i=1;i<n;i++)
if(a[i]>max)
max=a[i];
return max;
}


template <> char *maxn<char*>(char*a[],unsigned int n)
{
int l=0,num=0;
for(int i=0;i<n;i++)
if(strlen(a[i])>l)
{
l=strlen(a[i]);
num=i;
}
return a[num];
}


int main()
{
using namespace std;


int a[6]={1,3,41,14,1,1};
double b[4]={12.1,3.1,3,4.1};
cout<<maxn(a,6)<<endl;
cout<<maxn(b,4)<<endl;
char *c[5]={(char*)"qweqw",(char*)"asd",(char*)"dasa",(char*)"d3dddd",(char*)"dsa"};
cout<<maxn(c,5)<<endl;
return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章