《C++primerplus》第7章练习题
阅读原文时间:2023年07月11日阅读:4

1.用户不断输入两个数,计算调和平均数,直到其中一个数为0。

#include
using namespace std;

double harm_mean(double x, double y)
{
double result;
result = 2 * x*y / (x + y);
return result;
}

int main()
{
double num1, num2;
int flag = 1;

while (flag)  
{  
    cout << "Enter two numbers:\\n";  
    cin >> num1;  
    cin >> num2;  
    if ((num1 == 0) || (num2 == 0))  
        flag = 0;  
    else  
        cout << "Harmonic mean:" << harm\_mean(num1, num2) << endl;

}

cout << "done.\\n";  
system("pause");  

}

2.要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。使用3个数组处理函数分别进行输入、显示和计算平均成绩。

#include
using namespace std;

void score_in(int si[]);
void score_out(int so[]);
void score_mean(int sm[]);

static int counts = 0;

int main()
{
int score[10];

cout << "(enter any letter to stop input)\\nEnter the score:\\n";  
score\_in(score);  
score\_out(score);  
score\_mean(score);

cout << "done.\\n";  
system("pause");  

}

void score_in(int si[])
{
for (int i = 0; i < 10; i++) { if (cin >> si[i])
{
counts++;
}
else
{
cout << "Input stopped." << endl;
break;
}
}
}

void score_out(int so[])
{
cout << "Score:";
for (int i = 0; i < counts; i++)
{
cout << so[i] << " ";
}
cout << "\n";
}

void score_mean(int sm[])
{
double sum = 0;
for (int i = 0; i < counts; i++)
{
sum += sm[i];
}
cout << "Average:" << sum / counts << endl;
}

3.定义一个box结构,存储生产者,长高宽和体积的信息。编写两个函数,第一个按值传递box结构,显示所有成员的值。第二个传递box结构的地址,并将volume成员设置为其它三维长度的体积。将两个函数组成一个简单的程序。

#include
using namespace std;

struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};

void set_box(box *sptr, float x, float y, float z);
void v_box(box *vptr);

int main()
{
box box1;
float l, w, h;

cout << "Enter the box's l,w and h:\\n";  
cin >> l >> w >> h;

set\_box(&box1, l, w, h);  
v\_box(&box1);

cout << "done.\\n";  
system("pause");  

}

void set_box(box *sptr, float x, float y, float z)
{
sptr->height = z;
sptr->length = x;
sptr->width = y;
cout << "Length:" << sptr->length << endl; cout << "Width:" << sptr->width << endl; cout << "Height:" << sptr->height << endl;
}

void v_box(box *vptr)
{
vptr->volume = vptr->height*vptr->length*vptr->width;
cout << "The volume of this box is:" << vptr->volume << endl;
}

4.修改书中7.4的程序,域号码1~47,中奖条件增加一项从27个数中抽中号码。

#include
using namespace std;

long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0;
unsigned n;
unsigned p;

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

return result;  

}

unsigned particular(unsigned pn)
{
return pn;
}

int main()
{
double total, choices;
cout << "Enter the total number of choices on the game card and\n";
cout << "the number of picks allowed:\n";

while ((cin >> total >> choices) && (choices <= total ))  
{  
    cout << "You have one chance in ";  
    cout << probability(total, choices)\*particular(27);  
    cout << " of winning.\\n";  
    cout << "Next two numbers(q to quit):";  
}

cout << "\\ndone.";  
system("pause");  

}

*这题没太看懂什么意思,结果再乘个27不就行了?跟着敲了一边代码。

5.定义一个递归函数算阶乘。

#include
using namespace std;

long long fac(int n)
{
if (n > 0)
return n * fac(n - 1);
else if (n == 0)
return 1;
}

int main()
{
int n;

cout << "Enter a number:\\n";  
cin >> n;  
cout << "Its factorial:" << fac(n) << endl;

cout << "done.\\n";  
system("pause");  

}

6.编写三个处理double数组的函数,分别用于提示输入、显示数组内容和翻转数组中值的顺序。程序先填充数组,显示数组,再翻转数组,再显示数组。

#include
using namespace std;

static int counts = 0; //输入数字计数

void fill_array(double fa[], int asize);
void show_array(double sa[], int ssize);
void reverse_array(double ra[], int rsize);

int main()
{
double input[10];

fill\_array(input, 10);  
show\_array(input, 10);  
reverse\_array(input, 10);  
show\_array(input, 10);

cout << "done.\\n";  
system("pause");  

}

void fill_array(double fa[], int asize)
{
cout << "Enter some numbers:\n"; for (int i = 0; i < asize; i++) { if (cin >> fa[i])
{
counts++;
if (counts == asize)
{
cout << "Input stopped.\n";
break;
}
else {};
}
else
{
cout << "Input stopped.\n";
break;
}
}
cout << counts << " numbers entered.\n";
}

void show_array(double sa[], int ssize)
{
cout << "Input numbers:";
for (int i = 0; i < counts; i++)
{
cout << sa[i] << " ";
}
cout << "\n";
}

void reverse_array(double ra[], int rsize)
{
double t;
//如果要翻转除第一位和最后一位的数字,把i改成1
for (int i = 0; i <= counts / 2; i++)
{
t = ra[i];
ra[i] = ra[counts - i - 1];
ra[counts - i - 1] = t;
}
}

7.修改书中7.7的程序,使用两个指针参数来表示区间。fill_array()函数返回一个指向最后被填充的位置的指针,其它函数可以将它作为第二个参数标识结尾。

#include
using namespace std;

const int Max = 5;

double* fill_array(double ar[], int limit);
void show_array(double *ptl, double *ptr);
double add_array(double *ptl, double *ptr);

int main()
{

double pro\[Max\];

cout << "5 numbers to show\\n";  
show\_array(pro, fill\_array(pro, Max));

cout << "Next 5 numbers to sum\\n";  
cout << "Sum:" << add\_array(pro, fill\_array(pro, Max));

cout << "\\ndone.";  
system("pause");  

}

double* fill_array(double ar[], int limit)
{
double *ptr;
int i = 0;
for (i = 0; i < limit; i++) { cout << "Enter value #" << i + 1 << ":"; cin >> ar[i];
}
ptr = &ar[i];
return ptr;
}

void show_array(double *ptl, double *ptr)
{
for (int i = 0; ptl[i] != *ptr; i++)
{
cout << "Property #" << i + 1 << ":" << ptl[i] << endl;
}
}

double add_array(double *ptl, double *ptr)
{
double sum = 0;
for (int i = 0; ptl[i] != *ptr; i++)
{
sum += ptl[i];
}
return sum;
}

8.修改程序7.15,编写不使用array类两个版本。

#include
//#include
//#include

using namespace std;
/*原程序
const int seasons = 4;
const arraySnames =
{ "Spring","Summer","Fall","Winter" };

void fill(array*pa);
void show(arrayda);

int main()
{
arrayexpenses;

fill(&expenses);  
show(expenses);

cout << "\\ndone.";  
system("pause");  

}

void fill(array*pa)
{
for (int i = 0; i < seasons; i++) { cout << "Enter " << Snames[i] << " expenses:"; cin >> (*pa)[i];
}
}

void show(arrayda)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < seasons; i++)
{
cout << Snames[i] << ":$" << da[i]<<"\t";
total += da[i];
}
cout << "\nTotal Expenses:$" << total << endl;
}
*/

const char Snames[4][10] = {"Spring","Summer","Fall","Winter"};
const int seasons = 4;
void fill_exp(double fe[]);
void show_exp(double se[]);

struct Expenses
{
double expenses[seasons];
};

int main()
{
//double expenses[seasons]; 版本a

Expenses exp;  
fill\_exp(exp.expenses);  
show\_exp(exp.expenses);

//fill\_exp(expenses);    版本a  
//show\_exp(expenses);    版本a

cout << "done.\\n";  
system("pause");  

}

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

void show_exp(double se[])
{
double total = 0;
for (int i = 0; i < seasons; i++)
{
cout << Snames[i] << ":$" << se[i] << "\t";
total += se[i];
}
cout << "\nTotal expenses:" << total << endl;
}

9.把程序补充完整。getinfo函数用于输入学生结构的信息,三种display函数显示学生的信息,但使用三种不同的参数传递方式。

#include
using namespace std;

const int SLEN = 30;

struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};

void getinfo(student pa[], int n);

void display1(student st);

void display2(const student *ps);

void display3(const student pa[], int n);

int main()
{
student students[3];

getinfo(students, 3);

display1(students\[0\]);  
display2(&students\[1\]);  
display3(students, 3);

cout << "done.\\n";  
system("pause");  

}

void getinfo(student pa[], int n)
{
int i = 0;
for (i = 0; i < n; i++) { cout << "Input name of student " << i + 1 << ":\n"; cin.get(pa[i].fullname, SLEN); cin.get(); cout << "Input hobby of student " << i + 1 << ":\n"; cin.get(pa[i].hobby, SLEN); cout << "Input ooplevel of student " << i + 1 << ":\n"; cin >> pa->ooplevel;
cin.get();
}
}

void display1(student st)
{
cout << "Student1's name:" << st.fullname << endl;
}

void display2(const student *ps)
{
cout << "Student2's hobby:" << ps->hobby << endl;
}

void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << pa[i].fullname << " ";
}
}

10.函数指针练习。编写一个calculate()函数,接受三项参数,两个double值和一个函数指针,指针指向计算两数之和(或其它功能)的函数。可尝试使用函数指针数组。

#include
using namespace std;

double add(double x, double y)
{
return x + y;
}

double div(double x, double y)
{
return x / y;
}

double calculate(double x, double y, double (*ptf)(double,double))
{
return ptf(x, y);
}

int main()
{
double x1, x2;
double(*ptf[2])(double, double);
ptf[0] = add;
ptf[1] = div;

cout << "Enter two number:";  
while (cin >> x1 >> x2)  
{  
    cout << "Add:" << calculate(x1, x2, ptf\[0\]) << endl;  
    cout << "Div:" << calculate(x1, x2, ptf\[1\]) << endl;  
    cout << "Enter two number:";  
}

cout << "done.\\n";  
system("pause");  

}