《C++ Primer Plus》程式設計練習第七章 解析與答案
阿新 • • 發佈:2021-01-04
#include <iostream>
using namespace std;
double average(double x,double y);
int main()
{
cout << "Please Input 2 num; 0 terminated\n";
double x = 0,y = 0;
double result = 0;
while (cin>>x>>y)
{
if(x*y == 0)
cout<< "Done\n";
break;
result = average(x,y);
cout << "result:"<<result<<endl;
}
return 0;
}
double average(double x,double y)
{
return 2.0*x*y/(x+y);
}
#include <iostream>
using namespace std;
int Input(double * ar, int Max);
void Show(const double * ar,int Size);
void Averge(const double * ar,int Size);
const int Max = 10;
int main()
{
double ar[Max];
int Size = Input(ar,Max);
Show(ar,Size);
Averge(ar,Size);
return 0;
}
int Input(double * ar,int Max)
{
int i ;
for (i = 0; i < Max; i++)
{
cout << "Please input:"<<(i+1)<<endl;
cin >> *(ar+i);
if(!cin)
break;
}
cout<<"i = "<<i<<endl;
return i;
}
void Show(const double * ar,int Size)
{
for (int i = 0; i < Size;i++)
{
cout << i << ":"<< *(ar+i)<<" ";
}
cout<<endl;
}
void Averge(const double * ar,int Size)
{
double sum = 0;
for (int i = 0; i < Size;i++)
{
sum += *(ar+i);
}
cout<<"Averge:"<<sum/Size<<endl;
}
3
#include <iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(box a);
void revolume(box * a);
int main()
{
box a = {"Talk is cheap,Show me the Code",20,40,60,0};
box *b = &a;
show(a);
revolume(b);
show(a);
return 0;
}
void show(box a)
{
cout<<"box.marker:"<<a.maker<<endl;
cout<<"box.height:"<<a.height<<endl;
cout<<"box.width:"<<a.width<<endl;
cout<<"box.length:"<<a.length<<endl;
cout<<"box.volume:"<<a.volume<<endl;
}
void revolume(box * a)
{
double volume = (a->height)*(a->length)*(a->width);
a->volume = volume;
}
4
#include <iostream>
using namespace std;
long double probability(unsigned numbers, unsigned picks);
int main()
{
long double pro = 0;
pro = probability(47,5)*probability(27,1);
cout << pro<<endl;
return 0;
}
long double probability(unsigned numbers, unsigned picks)
{
long double results = 1.0;
long double n;
unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--)
results = results * n/p;
return results;
}
5
#include <iostream>
using namespace std;
int function(int num);
int main()
{
int b;
b = function(3);
cout << b<<endl;
return 0;
}
int function(int num)
{
if (num > 0)
{
return num*function(--num);
}
else
return 1;
}
6
#include <iostream>
using namespace std;
int Fill_array(double ar[], int Limit);
void Show_array(double ar[], int Size);
void Reverse_array(double ar[], int Size);
const int Max = 10;
int main()
{
double ar[Max];
int Size = Fill_array(ar,Max);
Show_array(ar,Size);
Reverse_array(ar,Size);
Show_array(ar,Size);
return 0;
}
int Fill_array(double ar[], int Limit)
{
cout << "Input double"<<endl;
int i;
for (i = 0; i < Limit; i++)
{
if(!(cin>>ar[i]))
{
cout<<"Wrong!\n";
break;
}
}
return i;
}
void Show_array(double ar[], int Size)
{
for (int i = 0; i < Size; i++)
{
cout << i<<":"<<ar[i]<<" ";
}
cout <<endl;
}
void Reverse_array(double ar[], int Size)
{
double temp;
int i,j;
for (i = 0,j = Size -1; i < Size,i < j; i++,j--)
{
temp = ar[j];
ar[j] = ar[i];
ar[i] = temp;
}
}
7
#include <iostream>
using namespace std;
double* Fill_array(double * begin, double * end);
void Show_array(double * begin, double * end);
void revalue(double r,double * begin, double * end);
const int Max = 5;
int main()
{
double properties[Max];
double *end = Fill_array(properties,properties+Max);
Show_array(properties,end);
if (end-properties > 0)
{
cout << "Enter r:";
double factor;
while ( !(cin>>factor))
{
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();
return 0;
}
double* Fill_array(double * begin, double * end)
{
double temp;
double *pt;
for (pt = begin;pt !=end;pt++)
{
cout << "Enter value #"<< (pt - begin + 1)<<":";
cin >> temp;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated!";
break;
}
else if (temp < 0)
break;
*pt = temp;
}
return pt;
}
void Show_array(double * begin, double * end)
{
for (double * pt = begin;pt != end;pt++)
{
cout << pt - begin<<":"<<*pt<<" ";
}
cout <<endl;
}
void revalue(double r,double * begin, double * end)
{
for (double * pt = begin;pt != end;pt++)
{
*pt =(*pt) * r;
}
}
#include <iostream>
using namespace std;
typedef double (*fp)(double x, double y);
double add(double x, double y);
double sub(double x, double y);
double calculate(double x, double y,fp fun);
int main()
{
double x=0,y=0;
double (*pf[2])(double,double) = {add,sub};
while (cin>>x>>y)
{
cout<<"add:"<<calculate(x,y,pf[0])<<endl;
cout<<"sub:"<<calculate(x,y,pf[1])<<endl;
}
return 0;
}
double add(double x, double y)
{
return x + y;
}
double sub(double x, double y)
{
return x-y;
}
double calculate(double x, double y,fp fun)
{
return (*fun)(x,y);
}
因為最近事情較多且該書內容淺顯,所以以後只會選擇一些代表性的習題釋出,不好意思。