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;
}
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;
}
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;
}
相关文章推荐
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 4
- C++ Primer Plus (第6版)编程练习 代码-----第六章
- C++ Primer Plus (第6版)编程练习 代码-----第十章
- c++ primer plus(第6版)中文版 第八章编程练习答案
- C++ Primer Plus (第6版)编程练习 代码-----第七章
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 2
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 6
- C++ Primer Plus (第6版)编程练习 代码-----第五章
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 5
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 3
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 8
- c++ primer plus(第6版)中文版 第二章编程练习答案
- c++ primer plus(文章6版本)中国版 编程练习答案第八章
- 《C++ Primer Plus (第6版)》3.7 编程练习
- C++ Primer Plus(第6版) 第6章 编程练习
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第八章(chapter 8) 1-4
- C++ Primer Plus 第6版 中文版 第5章编程练习
- c++ primer plus(第6版)中文版 第十二章编程练习答案
- c++ primer plus(第6版)中文版 第四章编程练习答案
- C++ primer plus 第八章编程练习