/***********************************
2017年10月20日15:40:57
Athor:xiyuan255
Course:C++
Contain:review7.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第七章的练习题(选取部分)
【 P250 】
*************************************/
// review7.cpp -- C++ Primer plus 第七章练习题
#include <iostream>
#include <string> // exsampleNo8 field
#include <array> // exsampleNo8 field
#include <vector> // exsampleNo8 field
#include <fstream>
using namespace std;
void exampleNo1(void);
void exampleNo2(void);
void exampleNo3(void);
void exampleNo4(void);
void exampleNo5(void);
void exampleNo6(void);
void exampleNo7(void);
void exampleNo8(void);
void exampleNo9(void);
void exampleNo10(void);
int main(void)
{
//exampleNo1(); // example 1 test
//exampleNo2(); // example 2 test
//exampleNo3(); // example 3 test
//exampleNo4(); // example 4 test
//exampleNo5(); // example 5 test
//exampleNo6(); // example 6 test
//exampleNo7(); // example 7 test
//exampleNo8(); // example 8 test
//exampleNo9(); // example 9 test
exampleNo10(); // example 10 test
return 0;
}
double sum_average(double, double);
void exampleNo1(void)
{
cout << "请输入两个数(其中一个为0则程序结束): \n";
double number1, number2;
while (cin >> number1 >> number2 && number1 && number2) {
cout << "调和平均值为:" << sum_average(number1, number2) << endl;
cout << "继续请输入两个数:";
}
cout << "Done.\n";
}
double sum_average(double x, double y)
{
return (2 * x * y / (x + y));
}
/**
输出结果:
请输入两个数(其中一个为0则程序结束):
1 2
调和平均值为:1.33333
继续请输入两个数:45 69
调和平均值为:54.4737
继续请输入两个数:0 23
Done.
*/
const int SIZE = 10;
int input_func(double ar[], int n);
void show_func(const double * ar, int n);
double average(double [], int n);
void exampleNo2(void)
{
double score[SIZE];
int inCnt = input_func(score, SIZE);
show_func(score, inCnt);
cout << "average value = " << average(score, inCnt) << endl;
}
int input_func(double ar[], int n)
{
int count;
cout << "- 高尔夫的成绩(最多输入" << SIZE << "个) -\n";
for (count = 0; count < n; count++) {
cout << "请输入第" << (count + 1) << "次的成绩:";
if (!(cin >> ar[count]))
break;
}
return count;
}
void show_func(const double * ar, int n)
{
cout << "第1个到第" << n << "个的成绩是:";
for (int i = 0; i < n; i++) {
cout << ar[i] << " ";
}
}
double average(double ar[], int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++) {
sum += ar[i];
}
return (sum / n);
}
/**
输出结果:
- 高尔夫的成绩(最多输入10个) -
请输入第1次的成绩:78
请输入第2次的成绩:86
请输入第3次的成绩:91
请输入第4次的成绩:88
请输入第5次的成绩:82
请输入第6次的成绩:86
请输入第7次的成绩:q
第1个到第6个的成绩是:78 86 91 88 82 86 average value = 85.1667
*/
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show_sound(box soun);
void modifty_sound(box * pSoun);
void exampleNo3(void)
{
box sound = { "China", 50.0f, 45.0f, 40.0f, 0.0f };
show_sound(sound);
modifty_sound(&sound);
cout << "After modifty: \n";
show_sound(sound);
}
void show_sound(box soun)
{
cout << "Maker: " << soun.maker << endl;
cout << "Height: " << soun.height << endl;
cout << "Width: " << soun.width << endl;
cout << "Length: " << soun.length << endl;
cout << "Volume: " << soun.volume << endl;
}
void modifty_sound(box * pSoun)
{
pSoun->volume = pSoun->height * pSoun->length * pSoun->width;
}
/**
输出结果:
Maker: China
Height: 50
Width: 45
Length: 40
Volume: 0
After modifty:
Maker: China
Height: 50
Width: 45
Length: 40
Volume: 90000
*/
// Note: some implementations require double instead of long double
long double probability(unsigned number, unsigned picks);
void exampleNo4(void)
{
using namespace std;
unsigned total, choices;
cout << "请输入域号码:\n";
cout << "Enter the total number of choice on the game card and\n"
<< "the number of picks allowed:";
cin >> total >> choices;
long double win1 = probability(total, choices);
cout << "请输入特选号码:\n";
cout << "Next two numbers: ";
cin >> total >> choices;
cout << "You have one chance in ";
cout << win1 * probability(total, choices); // compute the odds
cout << " of winning.\n";
cout << "Bye.\n";
}
// the following calculates the probability of picking picks
// number correctly from numbers choices
long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0; // here come some local variable
long double n;
unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--) {
result = result * n / p;
}
return result;
}
/**
输出结果:
请输入域号码:
Enter the total number of choice on the game card and
the number of picks allowed:47 5
请输入特选号码:
Next two numbers: 27 1
You have one chance in 4.14164e+007 of winning.
Bye.
*/
long long factorial(long long n);
void exampleNo5(void)
{
cout << "请输入一个被求阶乘的数:";
long long number;
while (cin >> number) {
cout << number << "! = " << factorial(number) << endl;
cout << "输入下一个被求阶乘的数:";
}
}
long long factorial(long long n)
{
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
/**
输出结果:
请输入一个被求阶乘的数:0
0! = 1
输入下一个被求阶乘的数:1
1! = 1
输入下一个被求阶乘的数:2
2! = 2
输入下一个被求阶乘的数:3
3! = 6
输入下一个被求阶乘的数:4
4! = 24
输入下一个被求阶乘的数:5
5! = 120
输入下一个被求阶乘的数:q
*/
int Fill_array(double arr[], int len);
void Show_array(const double * arr, int len);
void Reverse_array(double [], int len);
void exampleNo6(void)
{
const int SIZE = 10;
double data[SIZE];
int count = Fill_array(data, SIZE);
Show_array(data, count);
Reverse_array(data, count);
cout << "data数组的内容全部反转后:\n";
Show_array(data, count);
Reverse_array(&data[1], count-2);
cout << "data除首尾两项外反转后:\n";
Show_array(data, count);
}
int Fill_array(double arr[], int len)
{
int i;
cout << "输入非数字则结束输入:\n";
for (i = 0; i < len; i++) {
cout << "输入第" << i + 1 << "个数:";
if (!(cin >> arr[i]))
break;
}
return i;
}
void Show_array(const double * arr, int len)
{
cout << "arr: ";
for (int i = 0; i < len; i++) {
cout << *(arr + i) << " ";
}
cout <<endl;
}
void Reverse_array(double arr[], int len)
{
double temp = 0;
for (int i = 0; i < len/2; i++) {
temp = arr[i];
arr[i] = arr[len-1-i];
arr[len-1-i] = temp;
}
}
/**
输出结果:
输入非数字则结束输入:
输入第1个数:1.0
输入第2个数:2.0
输入第3个数:3.0
输入第4个数:4.0
输入第5个数:5.0
输入第6个数:q
arr: 1 2 3 4 5
data数组的内容全部反转后:
arr: 5 4 3 2 1
data除首尾两项外反转后:
arr: 5 2 3 4 1
*/
double * fill_array(double * pHead, double * pTail);
void show_array(double * pHead, double * pTail);
void revalue(double r, double * pHead, double * pTail);
void exampleNo7(void)
{
const int Max = 5;
double properties[Max];
double* point = fill_array(properties, properties+Max);
show_array(properties, point);
if (point > properties) {
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, point);
show_array(properties, point);
}
cout << "Done.\n";
}
double * fill_array(double * pHead, double * pTail)
{
double temp;
double * pt;
int i;
for (i = 0,pt = pHead; pt < pTail; pt++, 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) // singal to terminate
break;
*pt = temp;
}
return pt;
}
// the following function can use, but not alter
// the array whose address is ar
void show_array(double * pHead, double * pTail)
{
int i;
for (i = 0; pHead < pTail; i++, pHead++) {
cout << "Property #" << (i + 1) << ": $";
cout << *pHead << endl;
}
}
// multiplies each element of ar[] by r
void revalue(double r, double * pHead, double * pTail)
{
while (pHead < pTail) {
(*pHead) *= r;
pHead++;
}
}
/**
输出结果:
Enter value #1: 100000
Enter value #2: 80000
Enter value #3: 222000
Enter value #4: 240000
Enter value #5: 118000
Property #1: $100000
Property #2: $80000
Property #3: $222000
Property #4: $240000
Property #5: $118000
Enter revaluation factor: 0.8
Property #1: $80000
Property #2: $64000
Property #3: $177600
Property #4: $192000
Property #5: $94400
Done.
*/
#if 0 // 例题8 自己要求的两种方法array和vactor
// constant data
const int Seasons = 4;
// 方法一:
#if 0
const char * Snames[Seasons] =
{ "Spring", "Summer", "Fall", "Winter" };
#endif
// 方法二:
const string Snames[Seasons] =
{ "Spring", "Summer", "Fall", "Winter" };
// 方法一:
#if 0
// function to modify array object
void fill(double (*)[Seasons]);
// function that uses array object without modifying it
void show(double [Seasons]);
#endif
// 方法二:
// function to modify array object
void fill(std::vector<double> (*pa));
// function that uses array object without modifying it
void show(std::vector<double> da);
void exampleNo8(void)
{
// 方法一:
//double expenses[Seasons];
// 方法二:
std::vector<double> expenses(Seasons);
fill(&expenses);
show(expenses);
}
void fill(std::vector<double> (*pa))
{
using namespace std;
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}
void show(std::vector<double> da)
{
using namespace std;
double total = 0.0;
cout << "EXPENSES\n";
for (int i = 0; i < Seasons; i++) {
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses: $" << total << endl;
}
#endif
// constant data
const int Seasons = 4;
const char * Snames[Seasons] =
{ "Spring", "Summer", "Fall", "Winter" };
// 方法一:
#if 0
// function to modify array object
void fill(double (*)[Seasons]);
// function that uses array object without modifying it
void show(double [Seasons]);
#endif
// 方法二:
struct expen_s {
double expens[Seasons];
};
// function to modify array object
void fill(expen_s *pa);
// function that uses array object without modifying it
void show(expen_s da);
void exampleNo8(void)
{
// 方法一:
//double expenses[Seasons];
// 方法二:
expen_s expenses;
fill(&expenses);
show(expenses);
}
void fill(expen_s * pa)
{
using namespace std;
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i] << " expenses: ";
//cin >> (*pa)[i];
cin >> (*pa).expens[i];
}
}
void show(expen_s da)
{
using namespace std;
double total = 0.0;
cout << "EXPENSES\n";
for (int i = 0; i < Seasons; i++) {
cout << Snames[i] << ": $" << da.expens[i] << endl;
//total += da[i];
total += da.expens[i];
}
cout << "Total Expenses: $" << total << endl;
}
/**
输出结果:
Enter Spring expenses: 212
Enter Summer expenses: 256
Enter Fall expenses: 208
Enter Winter expenses: 244
EXPENSES
Spring: $212
Summer: $256
Fall: $208
Winter: $244
Total Expenses: $920
*/
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN]; //嗜好
int ooplevel; // 水平等级
};
// getinfo() has two arguments: a poniter to the first element of
// an array of student structures and an int representing the
// number of elements of the array. The function solicits and
// stors data about students. It terminates input upon filling
// the array or upon encountering a blank line for the student
// name. The function returns the actual number of array elements
// filled.
int getinfo(student pa[], int n);
// display1() takes a student structure as an argument
// and displays its contents
void display1(student st);
// display2() takes the address of student structure as an
// argument and displays the structure's contents
void display2(const student * ps);
// display3() takes the address of the first element of an array
// of student structures and the number of array elements as
// arguments and displays the contents of the structures
void display3(const student pa[], int n);
void exampleNo9(void)
{
cout << "Enter class size: ";
int class_size;
cin >> class_size;
while (cin.get() != '\n')
continue;
student * ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);
for (int i = 0; i < entered; i++) {
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete [] ptr_stu;
cout << "Done\n";
}
int getinfo(student pa[], int n)
{
int i;
for (i = 0; i < n; i++) {
cout << "Enter #" << i + 1 << endl;
cout << "fullname: ";
cin.getline(pa[i].fullname, SLEN); // 获取一行数据
if ( 0 == strlen(pa[i].fullname)) break; //判断是否为空白行
cout << "hobby: ";
if (!(cin >> pa[i].hobby)) break;
cout << "ooplevel: ";
if (!(cin >> pa[i].ooplevel)) break;
cin.get();
}
return i;
}
void display1(student st)
{
//cout << "- display1 -\n";
cout << "fullname: " << st.fullname
<< ", hobby: " << st.hobby
<< ", ooplevel: " << st.ooplevel
<< endl;
}
void display2(const student * ps)
{
//cout << "- display2 -\n";
cout << "fullname = " << ps->fullname
<< ", hobby = " << ps->hobby
<< ", ooplevel = " << ps->ooplevel
<< endl;
}
void display3(const student pa[], int n)
{
//cout << "- display3 -\n";
for (int i = 0; i< n; i++) {
cout << "fullname-- " << pa[i].fullname
<< ", hobby-- " << pa[i].hobby
<< ", ooplevel-- " << pa[i].ooplevel
<< endl;
}
}
/**
输出结果:
Enter class size: 10
Enter #1
fullname: zhangsan
hobby: kandianshi
ooplevel: 3
Enter #2
fullname: lishi
hobby: wandiannao
ooplevel: 8
Enter #3
fullname: wangwu
hobby: dalangqiu
ooplevel: 2
Enter #4
fullname: zhaoliu
hobby: tingyinyue
ooplevel: 5
Enter #5
fullname:
fullname: zhangsan, hobby: kandianshi, ooplevel: 3
fullname = zhangsan, hobby = kandianshi, ooplevel = 3
fullname: lishi, hobby: wandiannao, ooplevel: 8
fullname = lishi, hobby = wandiannao, ooplevel = 8
fullname: wangwu, hobby: dalangqiu, ooplevel: 2
fullname = wangwu, hobby = dalangqiu, ooplevel = 2
fullname: zhaoliu, hobby: tingyinyue, ooplevel: 5
fullname = zhaoliu, hobby = tingyinyue, ooplevel = 5
fullname-- zhangsan, hobby-- kandianshi, ooplevel-- 3
fullname-- lishi, hobby-- wandiannao, ooplevel-- 8
fullname-- wangwu, hobby-- dalangqiu, ooplevel-- 2
fullname-- zhaoliu, hobby-- tingyinyue, ooplevel-- 5
Done
*/
double add(double x, double y);
double subtract(double x, double y);
double multiply(double x, double y);
double divide(double x, double y);
double calculate(double x, double y, double (*pa)(double x, double y));
void exampleNo10(void)
{
double (*pf[4])(double x, double y) = { add, subtract, multiply, divide };
double (*(*pd)[4])(double x, double y) = &pf;
double result = 0.0;
double num1, num2;
cout << "Enter two number:";
while (cin >> num1 >> num2) {
for (int i = 0; i < 4; i++) {
result = calculate(num1, num2, (*pd)[i] );
if (0 == i)
cout << num1 << " + " << num2 << " = " << result << endl;
else if (1 == i)
cout << num1 << " - " << num2 << " = " << result << endl;
else if (2 == i)
cout << num1 << " * " << num2 << " = " << result << endl;
else if (3 == i)
cout << num1 << " / " << num2 << " = " << result << endl;
}
cout << "Next two number:";
}
}
double calculate(double x, double y, double (*pa)(double x, double y))
{
return ( (*pa)(x, y) );
}
double add(double x, double y)
{
return x + y;
}
double subtract(double x, double y)
{
return x - y;
}
double multiply(double x, double y)
{
return x * y;
}
double divide(double x, double y)
{
return x / y;
}
/**
输出结果:
Enter two number:3.0 4.0
3 + 4 = 7
3 - 4 = -1
3 * 4 = 12
3 / 4 = 0.75
Next two number:6.0 8.0
6 + 8 = 14
6 - 8 = -2
6 * 8 = 48
6 / 8 = 0.75
Next two number:q
*/