/* Ch5 exercise 2 to exercise 6 */#include"std_lib_facilities.h"doublectok(double c){/* c is celus temperature, it must bigger than -273.15 */if(c <-273.15)error("celus temperature must bigger than -273.15");return c +273.15;}/* 5-5 */doublektoc(double k){/* k is thermodynamic temperature, it must no less than 0.0 */if(k <0.0)error("thermodynamic temperature must bigger than -273.15");return k -273.15;}intmain()try{double c =0.0;
cin >> c;/*5-3
if (c < -273.15)
error("celus must bigger than -273.15");
*/double k =ctok(c);
cout << k <<" K\n";return0;}catch(runtime_error& e){
cerr <<"runtime error: "<< e.what()<<'\n';return1;}catch(...){
cerr <<"Some exceptions occured!\n";return2;}
5.7
/* quadratic equations of one unknown
design a function of quadratic formula
*/#include"std_lib_facilities.h"//我们还需要通过估计的方法,判断程序的结果是否合理,先设置一个允许误差 epsilon,判断所得的根是否超过误差。staticdouble epsilon{1.0e-7};//close enough to zerodoublecheck(double x,double a,double b,double c){// check x against epsilon;// in case of a poor solution print a message to alert the userdouble e = a * x * x + b * x + c;if(e ==0.0)return x;elseif(e > epsilon)
cout <<"poor root; off by "<< e <<'\n';// positive and larger than epsilonelseif(e <-epsilon)
cout <<"poor root; off by "<< e <<'\n';// positive and larger than epsilonreturn x;}voidquadratic_formula(double a,double b,double c){//solve the quadratic equation of one unknownif(a ==0){if(b ==0.0&& c !=0.0)throwruntime_error("This is not a equation");elseif(b ==0.0&& c ==0.0)
cout <<"There are infinity roots\n";elseif(b !=0)
cout <<"root = "<<check(-c / b, a, b, c)<<'\n';}else{double delta = b * b -4* a * c;if(delta <0)throwruntime_error("no real root");elseif(delta ==0)
cout <<"root = "<<check(-b /(2* a), a, b, c)<<'\n';else{double x1{0.0}, x2{0.0};
x1 =(-b -sqrt(delta))/(2* a);
x2 =(-b +sqrt(delta))/(2* a);
cout <<"root1 = "<<check(x1, a, b, c)<<"\troot2 = "<<check(x2, a, b, c)<<'\n';}}}intmain()try{
cout <<"Solve root(s) of quadratic equations of one unknown: ax^2 + bx + c = 0\n";
cout <<"Enter a, b, and c\n";double a, b, c;
a = b = c =0.0;while(cin >> a >> b >> c){quadratic_formula(a, b, c);
cout <<"try again (enter a, b, c)\n";}return0;}catch(runtime_error& e){
cerr <<"runtime_error: "<< e.what()<<'\n';return1;}
5.8 和 5.9
/* exercise 5_8 and 5_9 */#include"std_lib_facilities.h"classover_flow{};// 专门报告溢出错误的类voidsum_n(vector<int>v,int n){if(n > v.size())throwruntime_error("the number of values you want to sum is more than the number of integers you entered");bool is_neg =false;int sum =0;for(int i =0; i < n; i++){
sum += v[i];//test if over flowif(sum <0){if(!is_neg && v[i]>0)throw over_flow{};
is_neg =true;}else{if(is_neg && v[i]<0)throw over_flow{};
is_neg =false;}}
cout <<"The sum of the first "<< n <<" numbers (";for(int i =0; i < n; i++)
cout << v[i]<<' ';
cout <<"\b) is "<< sum <<".\n";}intmain()try{
cout <<"Please enter the number of values you want to sum:\n";int n =0;
cin >> n;
cout <<"Please enter some integers (press '|' to stop):\n";
vector<int>v;int num;while(cin >> num)
v.push_back(num);sum_n(v, n);return0;}catch(runtime_error& e){
cerr <<"runtime error: "<< e.what()<<'\n';return1;}catch(over_flow){
cerr <<"Oops! The sum is over flow!\n";return2;}
5.10
/* exercise 5_8 and 5_9 */#include"std_lib_facilities.h"classover_flow{};// 专门报告溢出错误的类voidsum_and_diff_n(vector<double>v,int n){if(n > v.size())throwruntime_error("the number of values you want to sum is more than the number of integers you entered");
vector<double>diff;double sum =0;for(int i =0; i < n; i++){
sum += v[i];if(i >0)
diff.push_back(v[i -1]- v[i]);}
cout <<"The sum of the first "<< n <<" numbers (";for(int i =0; i < n; i++)
cout << v[i]<<' ';
cout <<"\b) is "<< sum <<".\n";
cout <<"The diff of "<< n <<" neighbor numbers: ";for(int i =0; i < n -1; i++)
cout << diff[i]<<' ';
cout <<'\n';}intmain()try{
cout <<"Please enter the number of values you want to sum:\n";int n =0;
cin >> n;
cout <<"Please enter some integers (press '|' to stop):\n";
vector<double>v;double x;while(cin >> x)
v.push_back(x);sum_and_diff_n(v, n);return0;}catch(runtime_error& e){
cerr <<"runtime error: "<< e.what()<<'\n';return1;}catch(over_flow){
cerr <<"Oops! The sum is over flow!\n";return2;}
5.11
/*斐波那契数列*/#include"std_lib_facilities.h"intmain(){int temp =0;int fib =1, prev_fib =0;//fib是当前斐波那契数,prev_fib是fib的前一个数while(1){
cout << fib <<" ";
temp = prev_fib + fib;if(temp <=0)break;
prev_fib = fib;
fib = temp;}
cout <<"\nthe largest Fibonacci number that fits in an int is "<< fib <<'\n';return0;}
5.12 和 5.13
/* exercise 5-12 and 5-13 "bull and cow" game */#include"std_lib_facilities.h"intmain()try{
cout <<"At the beginning of this game, please enter an integer no lower than zero:\n";int seed{0};do{
cin >> seed;if(!cin)error("Oops! You input wrong, you should enter integer");if(seed >=0)break;
cout <<"You should enter an ingeger no lower than zero:\n";}while(1);srand(seed);
vector<int>random(4);unsignedint guess;unsignedint i =0;unsignedint bulls =0;unsignedint cows =0;
string choice{"go"};
cout <<"Input g(go) to play game, or input q(quit) to quit\n";
cin >> choice;if(!cin)error("Oops! You input wrong!");while(choice[0]=='g'){for(i =0; i < random.size(); i++)
random[i]=randint(9);
cout <<"Please input four integers between 0 to 9\n";do{
bulls =0;
cows =0;for(i =0; i < random.size(); i++){
cin >> guess;if(!cin)error("Oops! You input wrong, you should enter integer");for(unsigned j =0; j < random.size(); j++)if(guess == random[j]){if(i == j)
bulls++;else
cows++;}}if(bulls == random.size()){
cout <<"Congratulations 4 bulls!\n";break;}elseif(bulls < random.size()){
cout << bulls <<" bulls "<< cows <<" cows......\n";
cout <<"Input g(go) to try again, or input q(quit) to quit\n";
cin >> choice;if(!cin)error("Oops! You input wrong!");}}while(choice[0]=='g');
cout <<"Input g(go) to play next round, or input q(quit) to quit\n";
cin >> choice;if(!cin)error("Oops! You input wrong!");}return0;}catch(runtime_error& e){
cerr <<"runtime error: "<< e.what()<<'\n';}
第五章 习题答案5.2 到 5.65.75.8 和 5.95.105.115.12 和 5.135.2 到 5.6/* Ch5 exercise 2 to exercise 6 */#include"std_lib_facilities.h"double ctok(double c){ /* c is celus temperature, it must bigger than -273.15 */ if (c < -273.15) error("celus temperature