c++.primer.plus第五版第七章编程练习答案
来源:互联网 发布:网络黄金egd能提现吗 编辑:程序博客网 时间:2024/04/30 06:10
// 1#include <iostream>#include <cmath>using namespace std;double get_harmonic_mean(double n1,double n2);void print_result(double result);bool is_zero(double num);int main(){ double num1 = 0,num2 = 0; double result = 0; while( cin >> num1 >> num2) { // cout<<num1<<" " <<num2<<endl; if(is_zero(num1) || is_zero(num2)) break; else { result = get_harmonic_mean(num1,num2); print_result(result); } } return 0;}bool is_zero(double num){ if(abs(num) <= 1e-15) return true; else return false;}double get_harmonic_mean(double n1,double n2){ return 2.0 * n1 * n2 / (n1 + n2);}void print_result(double result){ cout << "The result is " << result << endl;}// 2#include <iostream>using namespace std;int main(){ double golf_score[10],temp; int i; for(i = 0;i < 10;i++) { cin >> temp; if(!cin) // bad input { cin.clear(); while(cin.get() != '\n') continue; break; } golf_score[i] = temp; } for(int k = 0;k < i;k++) cout << "golf_score-" << k + 1 << " is " << golf_score[k] << endl; double sum = 0,average = 0; for(int j = 0;j < i;j++) sum += golf_score[j]; average = sum / i; cout << "The average is " << average << endl; return 0;}#include <iostream>const int len = 10;using namespace std;int get_golf_score(double golf_score[],int len);void display_golf_score(double golf_score[],int real_len);double get_golf_score_average(double golf_score[],int real_len);int main(){ int real_len; double golf_score[10],average_of_golf_score; real_len = get_golf_score(golf_score,len); display_golf_score(golf_score,real_len); average_of_golf_score = get_golf_score_average(golf_score,real_len); cout << "The average of the golf_score is " << average_of_golf_score << endl; return 0;}int get_golf_score(double golf_score[],int len){ int i; double temp; for(i = 0;i < len;i++) { cin >> temp; if(!cin) // bad input { cin.clear(); while(cin.get() != '\n') continue; break; } golf_score[i] = temp; } return i;}void display_golf_score(double golf_score[],int real_len){ for(int k = 0;k < real_len;k++) cout << "golf_score-" << k + 1 << " is " << golf_score[k] << endl;}double get_golf_score_average(double golf_score[],int real_len){ double sum = 0; for(int j = 0;j < real_len;j++) sum += golf_score[j]; return sum / real_len;}// 3#include <iostream>using namespace std;struct box{ char maker[40]; float height; float width; float length; float volume;};void display_info_of_box(box val);void set_volume_of_box(box * val);void display_and_set_volume(void (*fun1)(box),void (*fun2)(box *),box val1,box * val2);int main(){ box stru1 = {"kane",12.21,12.21,12.21,12.21};// // a// display_info_of_box(stru1);// // b// set_volume_of_box(&stru1);// cout << "The volume of the box is \t\t" << stru1.volume << endl; // c display_and_set_volume(display_info_of_box,set_volume_of_box,stru1,&stru1); cout << "The volume of the box is \t" << stru1.volume << endl; return 0;}void display_info_of_box(box val){ cout << "The information of the box structure is :\n"; cout << "val.maker is \t\t" << val.maker << endl; cout << "val.height is \t\t" << val.height << endl; cout << "val.width is \t\t" << val.width << endl; cout << "val.length is \t\t" << val.length << endl; cout << "val.volume is \t\t" << val.volume << endl;}void set_volume_of_box(box * val){ box * temp = val; temp->volume = temp->height * temp->length * temp->width;}void display_and_set_volume(void (*fun1)(box),void (*fun2)(box*),box val1,box *val2){ fun1(val1); fun2(val2);}// 7.4 lotto.cpp// lotto.cpp -- probability of winning#include <iostream>// Note: soome implementations require double instead of long doublelong double probability(unsigned numbers, unsigned picks);int main(){ using namespace std; double total, choices; cout << "Enter the total number of choices on the game card and\n" "the number of picks allowed:\n"; while((cin >> total >> choices) && choices <= total) // 注意这种用法 { cout << "You have one chance in "; cout << probability(total, choices); // compute the odds cout << " of winning.\n"; cout << "Next two numbers (q to quit): "; } cout << "bye\n"; return 0;}// the following function calculates the probability of picking picks// numbers correctly from numbers choiceslong double probability(unsigned numbers,unsigned picks){ long double result = 1.0; // here come some local variables long double n; unsigned p; for(n = numbers, p = picks; p > 0; n--,p--) result = result * n / p; return result;}// 4 此题有疑问!!!没理解题什么意思!!!#include <iostream>using namespace std;long double probability(unsigned field_number,unsigned special_number,unsigned special_choice,unsigned all_choice);int main(){ int field_number = 47, special_number = 27,special_choice = 1,all_choice = 5; cout << "The result is "; cout << probability(field_number, special_number,special_choice,all_choice) << endl; // compute the odds return 0;}long double probability(unsigned field_number,unsigned special_number,unsigned special_choice,unsigned all_choice){ double special_result = 1.0,end_result = 1.0; for(;special_choice > 0;special_choice--,special_number--) special_result = special_result * special_choice / special_number; cout << "The special_result is " << special_result << endl; int temp = all_choice - special_choice; for(;temp > 0;temp--,--field_number) end_result = end_result * temp / field_number; end_result *= special_result; return end_result;}// 5#include <iostream>using namespace std;long long jchfun(int val);int main(){ int in_num; long long result; cout << "Please input the number of jc: "; while(1) { cin >> in_num; result = jchfun(in_num); cout << "The result is " << result << endl; } return 0;}long long jchfun(int val){ long long result; if(val > 0) result = val * jchfun(val - 1); else if(val == 0) return 1; return result;}// 6#include <iostream>using namespace std;int Fill_array(double *p,int len_max);void Show_array(double *p,int len);void Reverse_array(double *p,int len);int main(){ int len_max,len; double *p = NULL; cout << "Please input the length of the array: "; cin >> len_max; p = new double [len_max]; len = Fill_array(p,len_max); Show_array(p,len); Reverse_array(p,len); Show_array(p,len); delete [] p; return 0;}int Fill_array(double * p,int len_max){ int n; double temp; for(n = 0;n < len_max;n++) { cin >> temp; if(!cin) { cin.clear(); while(cin.get() != '\n') continue; break; } p[n] = temp; } return n;}void Show_array(double *p,int len){ cout << "The numbers of the array is :\n"; for(int i = 0;i < len;i++) cout << p[i] << ' '; cout << endl;}void Reverse_array(double *p,int len){ double temp; for(int i = 0;i < len - 1;i++,len--) { temp = p[i]; p[i] = p[len - 1]; p[len - 1] = temp; }}//-----------------------------------------------------------------------------------------------------------------------// 7.7 arrfun3.cpp// arrfun3.cpp -- array functions and const#include <iostream>const int Max = 5;// function prototypesint fill_array(double ar[],int limit);void show_array(const double ar[], int n); // don't change datavoid revalue(double r,double ar[], int n);int main(){ using namespace std; double properties[Max]; int size = fill_array(properties, Max); show_array(properties,size); cout << "Enter revaluation factor: "; double factor; cin >> factor; revalue(factor,properties,size); show_array(properties,size); cout << "Done.\n"; return 0;}int fill_array(double ar[], int limit){ using namespace std; double temp; int i; for(i = 0;i < limit; i++) { cout << "Enter value #" << (i + 1) << ": "; cin >> temp; if(!cin) // bad input 1-- { cin.clear(); // 2-- while(cin.get() != '\n') // 3-- continue; cout << "Bad input;input process terminated.\n"; break; } else if(temp < 0) break; ar[i] = temp; } return i;}// the following function can use, but not alter,// the array whose address is arvoid show_array(const double ar[], int n){ using namespace std; for(int i = 0; i < n; i++) { cout << "Property #" << (i + 1) << ": $"; cout << ar[i] << endl; }}// multiplies each element of ar[] by rvoid revalue(double r,double ar[], int n){ for(int i = 0; i < n; i++) ar[i] *= r;}//------------------------------------------------------------------------------------------------------------------------------// 7#include <iostream>const int Max = 5;// function prototypesdouble *fill_array(double ar[],int limit);void show_array(const double ar[], double * end); // don't change datavoid revalue(double r,double ar[], double * end);int main(){ using namespace std; double properties[Max]; double *end = fill_array(properties, Max); show_array(properties,end); cout << "Enter revaluation factor: "; double factor; cin >> factor; revalue(factor,properties,end); show_array(properties,end); cout << "Done.\n"; return 0;}double * fill_array(double ar[], int limit){ using namespace std; double temp; int i; for(i = 0;i < limit; i++) { cout << "Enter value #" << (i + 1) << ": "; cin >> temp; if(!cin) // bad input 1-- { cin.clear(); // 2-- while(cin.get() != '\n') // 3-- continue; cout << "Bad input;input process terminated.\n"; break; } else if(temp < 0) break; ar[i] = temp; } return &(ar[i]);}// the following function can use, but not alter,// the array whose address is arvoid show_array(const double ar[], double *end){ using namespace std; int i = 0; for(const double *p = ar;p != end;p++,i++) { cout << "Property #" << (i + 1) << ": $"; cout << *p << endl; }}// multiplies each element of ar[] by rvoid revalue(double r,double ar[], double * end){ for(double *p = ar;p != end;p++) *p *= r;}// 8#include <iostream>#include <cstring>using namespace std;const int SLEN = 30;struct student { char fullname[SLEN]; char hobby[SLEN]; int ooplevel;};int getinfo(student pa[],int n);void display1(student st);void display2(const student *ps);void 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; cout << "Done\n"; return 0;}int getinfo(student pa[],int n){ char fullname_temp[SLEN]; char hobby_temp[SLEN]; int ooplevel_temp,i; for( i = 0;i < n;i++) { cout << i + 1 << ": Please input the fullname:"; cin >> fullname_temp; cin.get(); if(!cin) { cin.clear(); while(cin.get() != '\n') continue; break; } strcpy(pa[i].fullname,fullname_temp); cout << i + 1 << ": Please input the hobby:"; cin >> hobby_temp; cin.get(); if(!cin) { cin.clear(); while(cin.get() != '\n') continue; break; } strcpy(pa[i].hobby,hobby_temp); cout << i + 1 << ": Please input the ooplevel:"; cin >> ooplevel_temp; cin.get(); if(!cin) { cin.clear(); while(cin.get() != '\n') continue; break; } pa[i].ooplevel = ooplevel_temp; } return i;}void display1(student st){ cout << "st.fullname is\t" << st.fullname << endl; cout << "st.hobby is\t" << st.hobby << endl; cout << "st.ooplevel is\t" << st.ooplevel << endl;}void display2(const student *ps){ const student *st = ps; cout << "st.fullname is\t" << st->fullname << endl; cout << "st.hobby is\t" << st->hobby << endl; cout << "st.ooplevel is\t" << st->ooplevel << endl;}void display3(const student pa[],int n){ for(int i = 0;i < n;i++) { cout << "st.fullname is\t" << pa[i].fullname << endl; cout << "st.hobby is\t" << pa[i].hobby << endl; cout << "st.ooplevel is\t" << pa[i].ooplevel << endl; }}// 9 还有个数组指针的形式待写!!!----------------------------------------------------------------======||||||#include <iostream>using namespace std;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 (*p)(double,double));int main(){ double x,y; while(1) { cout << "Please ininput the x,y: "; cin >> x >> y; if(!cin) { cin.clear(); while(cin.get() != '\n') continue; break; } double add_result = calculate(x,y,add); cout << "x + y = " << add_result << endl; double subtract_result = calculate(x,y,subtract); cout << "x - y = " << subtract_result << endl; double multiply_result = calculate(x,y,multiply); cout << "x * y = " << multiply_result << endl; double divide_result = calculate(x,y,divide); cout << "x / y = " << divide_result << endl; } return 0;}double calculate(double x,double y,double (*p)(double,double)){ return p(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);}
0 0
- c++.primer.plus第五版第七章编程练习答案
- C++ Primer Plus第五版 第七章 编程练习答案
- C++.Primer.Plus第五版第五章编程练习答案
- C Primer Plus (第五版) 第五章 编程练习 答案
- C ++ Primer Plus 第六版 第七章编程练习答案
- C primer plus(第五版)编程练习第七章
- c++.primer.plus第五版第六章编程练习答案
- c++.primer.plus第五版第八章编程练习答案
- c++.primer.plus第五版第九章编程练习答案
- c++.primer.plus第五版第十章编程练习答案
- C Primer Plus (第五版) 第六章 编程练习 答案
- C++ Primer Plus第五版 第五章 编程练习答案
- C primer plus 第五章 编程练习个人答案
- C++ Primer Plus第六版 第七章 编程练习答案
- C++ Primer Plus第六版 第五章 编程练习答案
- C++ Primer Plus第五版 第四章 编程练习答案
- C++ Primer Plus第五版 第三章 编程练习答案
- C++ Primer Plus第五版 第二章 编程练习答案
- OSPF协议配置
- HDU-3333-Turing Tree-离散化思想
- PHP调用接口到阿里云OSS同步上传图片
- Java中静态代码块、非静态代码块、构造函数的执行顺序与作用
- iOS7,APNS推送后台不调用didReceive这个方法
- c++.primer.plus第五版第七章编程练习答案
- C# get和set属性
- 基于Android的计步器(Pedometer)的讲解(一)——Fragment页面跳转
- Java将时间加5分钟的方法
- ffmpeg 入门
- 把iPhone的屏幕投影到Mac上
- eclipse使用技巧
- FFmpeg源代码结构图 - 编码
- iOS开发系列--并行开发其实很容易--多线程