C++ Primer Plus 第六版 第七章编程题答案

  • 格式:doc
  • 大小:128.00 KB
  • 文档页数:15

//7.13.10#include<iostream>double add(double x,double y);double subtraction(double x,double y);double calculate(double x,double y ,double (*ps)(double a ,double b));int main(){using namespace std;cout << "Please enter two number: \n";double x,y;cin >> x >>y ;if(cin){double result_add,result_subtraction;result_add = calculate(x,y,add);result_subtraction = calculate(x,y,subtraction);cout << "The sum of "<< x << " and "<< y << "is " << result_add<<endl;cout << "The subtraction of "<< x << " and "<< "is "<< result_subtraction << endl;cout << "Enter a number: ";cin >> x;cout << "Enter another number: ";cin >> y;}return 0;}double add(double x,double y){return x+y;}double subtraction(double x,double y){return x-y;}double calculate(double x,double y,double (*ps)(double,double)){double a;a = (*ps)(x,y);return a;}/*//7.13.9#include<iostream>using namespace std ;const int SLEN = 30 ;struct student {char fullname[SLEN];char hobby[SLEN];int ooplevel;};// getinfo() has two arguments: a pointer 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// stores data about students. It terminates input upon filling // the array or upon encoutering a blank line for the student// name. The function returns the actual number of array elements // filled.int getinfo(student pa[], int n);// dispay1() takes a student structure as an argument// and displays its contentsvoid display1(student st);// display2() takes the address of student structure as an// argument and displays the structure's contentsvoid 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 structuresvoid display3(const student pa[], int n);int main(){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 ;return 0 ;}int getinfo(student pa[],int n){int i = 0 ;while ( i < n){cout << "Enter your name: ";cin.get( pa[i].fullname,SLEN) ;char stop;stop =cin.get();if (stop!= '\0')break;cout << "Enter your hobby: ";cin.getline(pa[i].hobby ,SLEN);cout << "Enter the ooplevel: ";cin >> pa[i].ooplevel ;cin.get();i++;}return i ;}void display1(student st){using namespace std ;cout << st.fullname << endl<< st.hobby <<endl<< st.ooplevel << endl ;}void display2(const student * ps){using namespace std ;cout << ps->fullname << endl<< ps->hobby << endl<< ps->ooplevel << endl ;}void display3(const student pa[],int n){using namespace std ;int i = 0 ;while (i < n){cout << pa[i].fullname << endl<< pa[i].hobby << endl<< pa[i].ooplevel << endl ;i++;}}*//*//7.13.8(b)#include<iostream>struct expense{double ex[4];};const int Seasons = 4;const char arr[4][10] ={"Spring","Summer","Fall","Winter"};void fill(expense * ps);void show(const expense * ps);int main(){using namespace std ;expense esp;fill(&esp);show(&esp);return 0 ;}void fill(expense * ps){using namespace std ;for (int i = 0 ;i < Seasons; i++){cout << "Enter " << arr[i] <<" expenses: ";cin >> ps->ex[i];}}void show(const expense * ps){using namespace std ;double total = 0.0;cout << "\nEXPENSES\n";for (int i = 0 ;i< Seasons;i++){cout << arr[i] << ": $" << ps->ex[i] << endl ;total += ps->ex[i];}cout << "Total Expenses: $" << total << endl ;}*//*//7.13.8 ?#include<iostream>const int Seasons = 4 ;const char arr[4][10] ={"Spring","Summer","Fall","Winter"};void fill(double arr[]);void show(const double arr[]);int main(){using namespace std ;double expenses[4];fill(expenses);show(expenses);return 0 ;}void fill(double ar[]){using namespace std ;for (int i = 0 ;i < Seasons; i++){cout << "Enter " << arr[i] <<" expenses: ";cin >> ar[i];}}void show(const double ar[]){using namespace std ;double total = 0.0;cout << "\nEXPENSES\n";for (int i = 0 ;i< Seasons;i++){cout << arr[i] << ": $" << ar[i] << endl ;total += ar[i];}cout << "Total Expenses: $" << total << endl ; }*//*//7.13.7#include<iostream>const int Max = 5;double * fill_array(double arr[], int limit);void show_array(double arr[],double * ps);void revalue(double r , double arr[], double * ps);int main(){using namespace std ;double properties[Max];double * ps ;ps = fill_array(properties, Max);show_array(properties,ps);if (ps != properties){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,ps);show_array(properties,ps);}cout << "Done.\n";cin.get();cin.get();return 0;}double * fill_array(double arr[], int limit){using namespace std ;double temp ;int i ;for (i = 0 ;i< limit; i++){cout << "Enter value #" << (i+1) << ": ";cin >> temp ;if (!cin){cin.clear();while (cin.get() != '\n')continue ;cout << "Bad input; input process terminated.\n";break;}else if (temp < 0)break;arr[i] = temp ;}return &arr[i-1];}void show_array(double arr[],double * ps){using namespace std ;for (int i = 0; ps != &arr[i];i++){cout << "Property #"<< (i+1) << ": &";cout << arr[i] << endl ;}}void revalue(double r ,double arr[], double * ps) {for (int i = 0 ;ps != &arr[i]; i++)arr[i] *= r ;}*//*//7.13.6#include<iostream>const int Max = 100 ;int Fill_array(double arr[],int length);void Show_array(const double arr[],int length); void Reverse_array(double arr[],int length);int main(){using namespace std ;double arr[Max];int Size;Size = Fill_array(arr,Max);Show_array(arr,Size);cout << endl ;Reverse_array(arr,Size);Show_array(arr,Size);return 0 ;}int Fill_array(double arr[],int Max){using namespace std ;int i = 0 ;cout << "Please enter number!!!\n";for (i;i < Max;i++){cout << "Enter number to the arr[" << i << "]: ";cin >> arr[i];if (!cin){cin.clear();while (cin.get() != '\n')continue;cout << "Not a number!!! the process terminated";break;}}return i ;}void Show_array(const double arr[],int length){using namespace std ;cout << endl;int i = 0 ;while (i < length){cout << arr[i] << " ";i++;}}void Reverse_array(double arr[],int length){int j ;j = length/2 ;int k = 1;while (k <=j){double arr1 ;arr1 = arr[k];arr[k]= arr[length-1-k];arr[length-1-k]=arr1;k++;}}*//*//7.13.5#include<iostream>long double function(unsigned number);int main(){using namespace std ;long double sj;unsigned number ;cout << "Enter a number: ";cin >> number;sj = function(number);cout << number << "! = " << sj << endl ;return 0 ;}long double function(unsigned number){long double sj ;if (number == 0)sj = 1;elsesj = number * function(number -1 );return sj ;}*//*//7.13.4#include<iostream>long double probability(unsigned numbers, unsigned picks);int main(){using namespace std ;double ch[4];cout << "ch[4]数簓组哩?中D,?ch[0]:total1,ch[1]:choices1;ch[2]:total2,ch[3]:choices2\n";for (int i = 0; i<4;i++){cout << "the " << i+1 << " numbers: ";cin >> ch[i];if (!cin){cin.clear();while(cin.get() != '\n')continue;cout << "bad input";break;}else if (ch[i] < 0)break;}long double p1;p1 = probability(ch[0],ch[1]);long double p2;p2 = probability(ch[2],ch[3]);long double p3 ;p3 = p1 * p2 ;cout << "you have one chance in " << p3 << " of win.\n";return 0;}long double probability(unsigned numbers, unsigned picks){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;}*//*//7.13.3#include<iostream>struct box{char maker[40];float height;float width ;float length ;float volume ;};void display(box b1);void function(box * ps);int main(){box b1 ={"Jack H",3.2,2.3,6.5};function(&b1);display(b1);return 0;}void display(box b1){using namespace std;cout <<"Maker: " << b1.maker <<endl;cout << "Height: " << b1.height << endl;cout <<"Width: " << b1.width << endl ;cout << "Length: " << b1.length << endl ;cout << "Volume: " << b1.volume << endl ;}void function(box * ps){ps->volume = ps->height * ps->length * ps ->width ; }*//*//7.13.2#include<iostream>const int Max = 10;int input(double golf[] , int max);void show( const double golf[], int length);double mean(const double golf[],int length);int main(){double golf[Max];int size;size =input(golf ,Max);//std::cout<<size<<std::endl;show(golf,size);double mean_golf ;mean_golf = mean(golf,size);std::cout << std::endl ;std::cout << mean_golf <<std::endl;return 0;}int input(double golf[], int max){using namespace std;int i = 0;double gol_f;for (i;i<max;i++){cout << "Enter your points: ";cin >> gol_f ;if (!cin){cin.clear();while (cin.get() != '\n')continue;cout << "Bad input; input process terminated.\n";break;}else if (gol_f < 0)break;golf[i] = gol_f;}return i;}void show(const double golf[],int length){using namespace std;for (int i = 0 ;i <length ;i++){cout << golf[i] << " ";}}double mean(const double golf[],int length) {int i = 0;double sum =0.0 ;while (i<length){sum = sum + golf[i] ;i++;}//std::cout << sum << std::endl;double mean_g ;mean_g = sum / length;//std::cout << mean_g<<std::endl;return mean_g ;}*//*//7.13.1#include<iostream>double mean(double a,double b);int main(){using namespace std;double a;double b;cin >> a >> b;while(a!=0&&b!=0){double c;c=mean(a,b);cout << "the mean of "<<a <<" and "<< b << " is: "<< c <<endl;cin >> a;cin >> b;}return 0 ;}double mean(double a,double b){double mean_c;mean_c= 2.0*a*b/(a+b);return mean_c;}*/。