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

c++primer plus第七章编程练习的解答1-8

2014-02-16 20:03 302 查看
第一题

//caculate the harmonic average
//program end until you enter  zero
#include <iostream>
using namespace std;
double h_average(double,double);

int main()
{
double a,b,average;
cout<<"Please enter two numbers:\n";
while(cin>>a>>b&&a!=0&&b!=0)//a*b!=0
{
average=h_average(a,b);
cout<<"Harmonic average:"<<average<<endl;
cout<<"Please enter two numbers:\n";
continue;
}
cout<<"Bye!!";
return 0;//dd
}

double h_average(double a,double b)
{
double average;
average=2*a*b/(a+b);
return average;
}


第二题

//enter at most ten groups of your golf scores
//display,caculate the average,show the average.
#include <iostream>
using namespace std;
int read(double*,int);
double caculate(double*,int);
void show(double*,int,double);
const int Size=10;

int main()
{
double scores[Size];
int count;
cout<<"Enter at most ten groups of your golf scores.(q to quit)\n";
count=read(scores,Size);

double average;
average=caculate(scores,count);

show(scores,count,average);

return 0; //dd

}

int read(double scores[],int n)
{
int i=0;
cout<<"# "<<i+1<<" : ";
for(i;i<n;i++)
{
if(cin>>scores[i])
{
cout<<"# "<<i+2<<" : ";
}
else
break;
}

return i;
}

double  caculate(double scores[],int count)
{
double average=0.0,sum=0.0;
for(int i=0;i<count;i++)
sum+=scores[i];
average=sum/count;

return average;

}

void show(double scores[],int count,double average)
{
cout<<"Here are the scores: ";
for(int i=0;i<count;i++)
cout<<scores[i]<<' ';
cout<<endl;
cout<<"Average: "<<average;

}

第三题

//using a program to test
//two functions ,one that passes structure by value
//the other passes structure by address
#include <iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};

void show1(box);
void show2(box*);
void setbox(box*);

int main()
{
box car =
{
"Mercedes benz",
200,
100,
2,
};
setbox(&car);
show1(car);
cout<<endl;
show2(&car);

return 0;
}

void show1(box car)
{
cout<<"Maker: "<<car.maker<<endl;
cout<<"height: "<<car.height<<endl;
cout<<"width: "<<car.width<<endl;
cout<<"length: "<<car.length<<endl;
cout<<"volume: "<<car.volume<<endl;
}

void show2(box* car)
{
cout<<"Maker: "<<car->maker<<endl;
cout<<"height: "<<car->height<<endl;
cout<<"width: "<<car->width<<endl;
cout<<"length: "<<car->length<<endl;
cout<<"volume: "<<car->volume<<endl;
}

void setbox(box*pb)
{
pb->volume=pb->height*pb->width*pb->length;
}


第四题
// lotto.cpp -- probability of winning
#include <iostream>
// Note: some implementations require double instead of long double
long double probability(unsigned numbers, unsigned picks);
int main()
{
using namespace std;
double total, choices,total2,choices2,p2;
cout << "Enter the total number of choices on the game card and\n"
"the number of picks allowed:\n";
while ((cin >> total >> choices) && choices <= total)
{
cout<<"Enter the total number of the second field number\n"
"and you can choose at most one or not.\n";
while((cin>>total2>>choices2)&&choices2<=1)
{
if(choices2==1){

p2=probability(total2,choices2);
break;
}
else
{
p2=1;
break;
}
}
cout << "You have one chance in ";
cout << probability(total, choices)*p2;
cout << " of winning.\n";
cout << "Next two numbers (q to quit): ";
}
cout << "bye\n";
// cin.get();
// cin.get();
return 0;//dd
}

// the following function calculates the probability of picking picks
// numbers correctly from numbers choices
long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0; // here come some local variables
long double n;
unsigned p;

for (n = numbers, p = picks; p > 0; n--, p--)
result = result * n / p ;
return result;
}

第五题
//using recursive function to caculate factorial
#include <iostream>
using namespace std;
long long factorial(int);

int main()
{
int num;
cout<<"Please enter a integer number(q to quit): ";
while(cin>>num&&num>=0)
{
cout<<num<<"! = "<<factorial(num)<<endl;
cout<<"Please enter a integer number: ";
continue;

}
cout<<"Bye!!";
return 0; //dd

}

long long factorial(int num)
{
if(num==0)
return 1;

else if(num==1)
return 1;

return num*factorial(num-1);
}

第六题
//using three functions
//first one to fill the array ,second one to show the array
//last one to reverse the array
#include <iostream>
using namespace std;
int Fill_array(double*,int);
void Show_array(const double*,int);
void Reverse_array(double*,int);
const int Size=10;

int main()
{
double arr[Size];
int count;
count=Fill_array(arr,Size);

Show_array(arr,count);

Reverse_array(arr,count);

Show_array(arr,count);

return 0;//dd

}

int Fill_array(double* arr,int size)
{
int i=0;
cout<<"Please enter elements: "<<endl;
for(i;i<size;i++)
{
cout<<"# "<<i+1<<": ";
if(cin>>arr[i])
continue;
else
break;
}
return i;
}

void Show_array(const double* arr,int count)
{
cout<<"\nHere are the elements!\n";
for(int i=0;i<count;i++)
cout<<"arr["<<i<<"]= "<<arr[i]<<endl;

}

void Reverse_array(double* arr,int count)
{
double arr2;
for(int i=1;i<count/2;i++)
{
arr2=arr[i];
arr[i]=arr[count-i-1];
arr[count-i-1]=arr2;
}
}

第七题
// arrfun3.cpp -- array functions and const
#include <iostream>
const int Max = 5;

// function prototypes
double* fill_array(double ar[], int limit);
void show_array(const double*, const double*); // don't change data
void revalue(double r, double*, double*);

int main()
{
using namespace std;
double properties[Max];

double* end = fill_array(properties, Max);
show_array(properties, end);
if ((end-properties) > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor)) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, end);
show_array(properties, end);
}
cout << "Done.\n";
// cin.get();
// cin.get();
return 0;//dd
}

double* fill_array(double ar[], int limit)
{
using namespace std;
double temp;
int i;
for (i = 0; i < limit; i++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;
if (!cin) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if (temp < 0) // signal to terminate
break;
ar[i] = temp;
}
return &ar[i];
}

// the following function can use, but not alter,
// the array whose address is ar
void show_array(const double ar[], const double end[])
{
using namespace std;
const double* pt;
int i=0;
for (pt=ar,i; pt!=end; pt++,i++)
{

cout << "Property #" << (i+1) << ": $";
cout << *pt << endl;

}
}

// multiplies each element of ar[] by r
void revalue(double r, double ar[], double end[])
{
for (double* pt=ar; pt !=end; pt++)
*pt *= r;
}

第八题
a

//adapt the 7.15 arrobj.cpp
#include <iostream>
#include <string>
using namespace std;
const int Seasons=4;
const char* Snames[Seasons]={"Spring","Summer","Fall","Winter"};
void Fill(double*);
void Show(double*);

int main()
{
double expenses[Seasons];
Fill(expenses);
Show(expenses);

return 0;//dd
}

void Fill(double ar[])
{
for(int i=0;i<Seasons;i++)
{
cout<<"Enter "<<Snames[i]<<" expenses: ";
cin>>ar[i];
}

}

void Show(double arr[])
{
double total=0.0;
cout<<"\nEXPENSES\n";
for(int i=0;i<Seasons;i++)
{
cout<<Snames[i]<<": $"<<arr[i]<<endl;
total+=arr[i];
}
cout<<"Total Expenses: $"<<total<<endl;
}

第八题
b

//adapt the 7.15 arrobj.cpp
#include <iostream>
#include <string>
using namespace std;
const int Seasons=4;
const char* Snames[Seasons]={"Spring","Summer","Fall","Winter"};
struct Expense
{
double expenses[Seasons];
};

void Fill(Expense*);
void Show(Expense*);

int main()
{
Expense expenses;
Fill(&expenses);
Show(&expenses);

return 0;//dd
}

void Fill(Expense* ar)
{
for(int i=0;i<Seasons;i++)
{
cout<<"Enter "<<Snames[i]<<" expenses: ";
cin>>ar->expenses[i];
}

}

void Show(Expense* arr)
{
double total=0.0;
cout<<"\nEXPENSES\n";
for(int i=0;i<Seasons;i++)
{
cout<<Snames[i]<<": $"<<arr->expenses[i]<<endl;
total+=arr->expenses[i];
}
cout<<"Total Expenses: $"<<total<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息