C++ primer plus 第七章習題答案

#include
#include<cstring>
using namespace std;
double get_average(int x,int y)//7.1
{
double average;
average = 2.0*x*y / (x + y);
return average;
}
int input_array(int grade[],int n)//7.2 input
{
int array_number = 0;
cout << "Enter the golf grade(!number to stop): ";
for (int i = 0; i < n; i++)
{
if (!(cin >> grade[i]))
{
break;
}
array_number++;
}
return array_number;
}
void show_array(int grade[], int n)//7.2 show
{
cout << "Grade: ";
for (int i = 0; i < n; i++)
{
cout <<grade[i] << " ";
}
cout << endl;
}
double caculate(int grade[], int n)//7.2 caculate
{
double average=0,sum=0;
for (int i = 0; i < n; i++)
{
sum = grade[i] + sum;
}
average = sum / n;
return average;
}
struct box //7.3 box_struct
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show_struct(box box1)//7.3 box_struct_show
{
cout << "Maker: " << box1.maker<<" ";
cout << "Height: " << box1.height << " ";
cout << "Width: " << box1.width << " " << endl;
cout << "Length: " << box1.length << " ";
cout << "volume: " << box1.volume << " "<<endl;

void set_struct(box *box1) //7.3 box_struct_set
{
box1->volume = box1->height * box1->length * box1->width;
}
long double probability(unsigned numbers,unsigned picks) //7.4
{
long double result = 1.0;
long double n;
unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--)
result = result * n / p;
return result;
}
long double factorial(int number)//7.5
{
long double result=0;
if (number <= 1)
{
result = 1;
return result;
}
else result = number * factorial(number - 1);
return result;
}
int Fill_array1(double number[],int n)//7.6
{
int input_count=0;
cout << "Enter the array: ";
for(int i=0;i<n;i++)
{
if (!(cin >> number[i]))
{
break;
}
input_count++;
}
return input_count;
}
void Show_array1(double number[], int n) //7.6
{
cout << "The array you input: ";
for (int i = 0; i < n; i++)
{
cout << number[i] << " ";
}
cout << endl;
}
void Reverse_array1(double number[],int n)//7.6 傳遞數組表示形式
{
double temp;
for (int i = 0; i<n/2; i++)
{
temp = number[i];
number[i] = number[n-1-i];
number[n - 1 - i] = temp;
}
}
void Reverse_array2(double *begin, double *end)//7.6指針表示形式
{
double temp;
for (int i = 0; i<(end - begin + 1) / 2; i++)
{
temp = *(begin + i);
*(begin + i) = *(end - i);
*(end - i) = temp;
}
}
double * Fill_array2(double *begin,double *end)//7.7
{
double temp=0;
double *real_end=begin;
for (int i = 0; i<end-begin+1; i++)//計算數組大小要根據實際輸入而定
{
cout << "Enter value # "<<(i+1)<<": ";
if (!(cin >> temp))
{break;}
else if (temp < 0)
{break;}
*(begin + i) = temp;
real_end++;
}
return real_end;
}
void Show_array2(double *begin, double *end) //7.7
{
for (int i = 0; i < end-begin; i++)
{
cout << "Property #" << (i + 1) << ": $";
cout << *(begin+i) <<endl;
}
}
void revalue(double r, double *begin, double *end)//7.7
{
for (int i = 0; i < end-begin; i++)
*(begin + i) *= r;
}
const int Seasons = 4; //7.8
const char* Snames[Seasons] = { "Spring","Summer","Fall","Winter" };//7.8
struct spend {
double expense[Seasons];
};//7.8b
void fill_a(double expense[], int seasons)//7.8a
{
for (int i = 0; i < seasons; i++)
{
cout << "Enter: " << Snames[i] << " expense: ";
cin >> expense[i];
}
}
void show_a(const double expense[], int seasons)//7.8a
{
double total = 0.0;
cout << "EXPENSES\n";
for (int i = 0; i < seasons; i++)
{
cout << Snames[i] << ": $" << expense[i] << endl;
total += expense[i];
}
cout << "Total Expenses: $" << total << endl;
}
void fill_b(spend * expenses)//7.8b
{
for (int i = 0; i < Seasons; i++)
{
cout << "Enter: " << Snames[i] << " expense: ";
cin >> expenses->expense[i];
}
}
void show_b(const spend *expenses )//7.8b
{
double total = 0.0;
cout << "EXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << expenses->expense[i] << endl;
total += expenses->expense[i];
}
cout << "Total Expenses: $" << total << endl;
}
//7.9
const int SLEN = 30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n) //7.9
{
int actual_number=0;
for(int i=0;i<n;i++)

cout << "Enter student#"<<i+1<<" 's fullname: ";
cin.getline(pa[i].fullname,SLEN);
if(!(strcmp(pa[i].fullname," ")))
{ break;}
actual_number++;
cout << "hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "ooplevel: ";
cin >> pa[i].ooplevel;
cin.get();
}
return actual_number;
}
void display1(student st) //7.9
{
cout << "Student's fullname: " << st.fullname;
cout << " hobby: " << st.hobby;
cout << " ooplevel: " << st.ooplevel<<endl;
}
void display2(const student *ps)//7.9
{
cout << "Student's fullname: " << ps->fullname;
cout << " hobby: " << ps->hobby;
cout << " ooplevel: " << ps->ooplevel<<endl;
}
void display3(const student pa[], int n)//7.9
{
for (int i = 0; i < n; i++)
{
cout << "Student#"<<i+1<<" 's fullname: " << pa[i].fullname;
cout << " hobby: " << pa[i].hobby;
cout << " ooplevel: " << pa[i].ooplevel<<endl;
}
}
double add1(double x, double y) //7.10
{
return x + y + 1;
}
double add2(double x, double y) //7.10
{
return x + y + 2;
}
double add3(double x, double y) //7.10
{
return x + y + 3;
}
double calculate2(double x, double y, double(*pf)(double, double))//7.10
{
return (*pf)(x, y);
}
void main()
{
/*int x = 1, y = 1;
while (x!=0&&y!=0)
{
cout << "Enter the two numbers(0 to end): ";
cin >> x >> y;
cout << "The average is:" << get_average(x,y) << endl;
}*/ //7.1


/*const int Max = 10;
int golf_grade[Max] = { 0 }, grade_number = 0,grade_average=0;
grade_number=input_array(golf_grade,Max);
show_array(golf_grade, grade_number);
grade_average=caculate(golf_grade, grade_number);
cout << "The average: " << grade_average<<endl;
*/ //7.2


/*box user_box = { "Goodball",30,20,10,0};
show_struct(user_box);
set_struct(&user_box);
show_struct(user_box);*/ //7.3 


/*cout << "You have one change in ";
cout << probability(47, 5)*probability(27, 1);
cout << " of winning the lottery;" << endl;*/ //7.4


/*int input_number = 0;
long double result;
do{
cout << "Enter the number: ";
if (!(cin >> input_number))
{
break;
}
result = factorial(input_number);
cout << "The result: " << result<<endl;
} while (1);*/ //7.5


/* const int Max = 20;
double array1[Max];
int array1_number = 0,value= 0;
array1_number=Fill_array1(array1, 20);
Show_array1(array1, array1_number);
Reverse_array1(array1, array1_number);
Show_array1(array1, array1_number);
Reverse_array2(&array1[1], &array1[array1_number-2]);
Show_array1(array1, array1_number);
*/// 7.6


/* const int Max = 5;
double properties[Max] = { 0 };
double * real_end = Fill_array2(properties, &properties[Max - 1]);
Show_array2(properties, real_end);
if (real_end - properties > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input;Please enter a number: ";
}
revalue(factor, properties, real_end);
Show_array2(properties, real_end);
}
cout << "Done.\n";
*/ //7.7


/*double expense[Seasons] = { 0 };
fill_a(expense, Seasons);
show_a(expense, Seasons);*///7.8a


/* spend expenses = { {0} };
fill_b(&expenses);
show_b(&expenses);*/ //7.8b


/* 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";*/ //7.9


    double(*pf[3])(double, double) = { add1,add2,add3 };
double x=0, y=0,value=0;
do {
cout << "Enter the two numbers: ";
if(!(cin>>x>>y))
{break;}
for(int i=0;i<3;i++)
{
value = calculate2(x,y,pf[i]);
cout << "The value is: " << value << endl;
}
} while (1); //7.10 
system("pause");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章