您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus (第6版)编程练习 代码-----第八章

2015-04-21 16:18 417 查看
1.太笨了,真的没有看懂这个练习题

8.2

#include "stdafx.h"
#include<iostream>
#include <string>
#include<cctype>
using namespace std;
struct CandyBar
{
char *name;
float weight;
int cal;

};
void display(CandyBar &cb,char *name="Millennium Munch",float weight=2.85,int cal=350);
int _tmain(int argc, _TCHAR* argv[])
{

CandyBar bar,bar1;
display(bar,"sugarsugar", 20.3,800);
display(bar1);

system("pause");
return 0;
}
void display(CandyBar &cb,char *name,float weight,int cal)
{
cb.name=name;
cb.weight=weight;
cb.cal=cal;
cout<<"name:"<<cb.name<<endl;
cout<<"weight:"<<cb.weight<<endl;
cout<<"calories:"<<cb.cal<<endl;
}


</pre><pre name="code" class="cpp">//8.3
void change(string &str);
int _tmain(int argc, _TCHAR* argv[])
{
string in;
cout<<"Enter a string (q to quit): ";
getline(cin,in);
while(in!="q\0")
{
change(in);
cout<<in<<endl<<"Next string(q to quit): ";
getline(cin,in);
}
cout<<"Bye.";
system("pause");
return 0;
}
void change(string &str)
{
int n=str.length();
for(int i=0;i<n;i++)
{
str[i]=toupper(str[i]);
}

}


8.4

struct stringy{
char * str;
int ct;
};
void set(stringy &s,char ch[]);
void show(const stringy &s ,int n=1);
void show(const char ch[] ,int n=1);
int _tmain(int argc, _TCHAR* argv[])
{
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!");
system("pause");
return 0;
}

void set(stringy &s,char ch[])
{
s.str=ch;
s.ct=strlen(s.str);
}
void show(const stringy &s ,int n)
{
for(int i=0;i<n;i++)
{
cout<<s.str<<endl;
cout<<s.ct<<endl<<endl;

}

}
void show(const char ch[] ,int n)
{
for(int i=0;i<n;i++)
{

cout<<ch<<endl<<endl;

}

}

8.5

template<typename T>
void max5(T s[]);

int _tmain(int argc, _TCHAR* argv[])
{
int t1[5]={1,2,4,5,3};
double t2[5]={2.01,1.0567,3.05678,4.02345,5.12340};
max5(t1);
max5(t2);
system("pause");
return 0;
}
template<typename T>
void max5(T s[])
{

T a=s[0];
for(int i=0;i<5;i++)
{
cout<<i+1<<": "<<s[i]<<endl;

if(a<s[i])
a=s[i];

}
cout<<"The max is :"<<a<<endl<<endl<<endl;

}

8.6  

template<typename T>
void maxn(T s[],int n);

template<>void maxn<char*>(char *st[],int n);
int _tmain(int argc, _TCHAR* argv[])
{
int t1[6]={1,2,4,5,3,0};
double t2[4]={2.01,1.0567,3.05678,4.02345};
maxn(t1,6);
maxn(t2,4);
char *s1[5]={"abc","defgh","kl","qwer578ddahg","applepie"};
maxn(s1,5);
system("pause");
return 0;
}
template<typename T>
void maxn(T s[],int n)
{

T a=s[0];
for(int i=0;i<n;i++)
{
cout<<i+1<<": "<<s[i]<<endl;

if(a<s[i])
a=s[i];

}
cout<<"The max is :"<<a<<endl<<endl<<endl;

}
template<>void maxn<char*>(char *st[],int n)
{
int a=strlen(st[0]);
int max=0;

for(int i=0;i<n;i++)
{
cout<<i+1<<": "<<st[i]<<endl;
int b=strlen(st[i]);
if(a<b)
{ a=b;
max=i;
}

}
cout<<"The max is :"<<st[max]<<" has "<<a<<" character."<<endl<<endl<<endl;

}

8.7
//例子中的函数没有改动
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[50];
double amount;

};
int main()
{
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];

for(int i=0;i<3;i++)
pd[i]=&mr_E[i].amount;

cout<<"Listing Mr.E's count of things:\n";

ShowArray(things,6);
SumArray(things,6);
cout<<"Listing Mr.E's debts:\n";
ShowArray(pd,3);
SumArray(pd,3);
system("pause");
return 0;

}

template <typename T >
void ShowArray(T arr[],int n)
{
cout<<"template A \n";

for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<endl;
}

template <typename T >
void ShowArray(T *arr[],int n)
{
cout<<"template B \n";

for(int i=0;i<n;i++)
cout<<*arr[i]<<" ";
cout<<endl;
}

template <typename T >
void SumArray(T arr[],int n)
{
cout<<"The sum of things is :";
int sum=0;
for(int i=0;i<n;i++)
sum=sum+arr[i];
cout<<sum<<endl;
}

template <typename T >
void SumArray(T *arr[],int n)
{
cout<<"The sum of debt is :";
double sum=0.0;
for(int i=0;i<n;i++)
sum=sum+*arr[i];
cout<<sum<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息