logo资料库

c++primerplus最全课后习题答案.pdf

第1页 / 共174页
第2页 / 共174页
第3页 / 共174页
第4页 / 共174页
第5页 / 共174页
第6页 / 共174页
第7页 / 共174页
第8页 / 共174页
资料共174页,剩余部分请下载后查看
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition 目录 Chapter 2 ............................................................................................................................................................... 3 PE 2-1 ............................................................................................................................................................ 3 PE 2-2 ............................................................................................................................................................ 3 PE 2-3 ............................................................................................................................................................ 4 PE 2-4 ............................................................................................................................................................ 4 PE 2-5 ............................................................................................................................................................ 5 PE 2-6 ............................................................................................................................................................ 5 Chapter 3 ............................................................................................................................................................... 6 PE 3-1 ............................................................................................................................................................ 6 PE 3-2 ............................................................................................................................................................ 6 PE 3-3 ............................................................................................................................................................ 7 PE 3-4 ............................................................................................................................................................ 7 PE 3-5 ............................................................................................................................................................ 8 PE 3-6 ............................................................................................................................................................ 8 Chapter 4 ............................................................................................................................................................... 9 PE 4-1 ............................................................................................................................................................ 9 PE 4-2 ............................................................................................................................................................ 9 PE 4-3 .......................................................................................................................................................... 10 PE 4-4 .......................................................................................................................................................... 10 PE 4-5 .......................................................................................................................................................... 11 PE 4-6 .......................................................................................................................................................... 11 PE 4-7 .......................................................................................................................................................... 12 PE 4-8 .......................................................................................................................................................... 12 PE 4-9 .......................................................................................................................................................... 13 Chapter 5 ............................................................................................................................................................. 13 PE 5-1 .......................................................................................................................................................... 13 PE 5-2 .......................................................................................................................................................... 14 PE 5-3 .......................................................................................................................................................... 14 PE 5-4 .......................................................................................................................................................... 15 PE 5-5 .......................................................................................................................................................... 16 PE 5-6 .......................................................................................................................................................... 17 PE 5-7 .......................................................................................................................................................... 19 PE 5-8 .......................................................................................................................................................... 19 PE 5-9 .......................................................................................................................................................... 20 Chapter 6 ............................................................................................................................................................. 20 PE 6-1 .......................................................................................................................................................... 20 PE 6-2 .......................................................................................................................................................... 21 PE 6-3 .......................................................................................................................................................... 22 PE 6-4 .......................................................................................................................................................... 22 PE 6-5 .......................................................................................................................................................... 24 PE 6-6 .......................................................................................................................................................... 25 PE 6-7 .......................................................................................................................................................... 27 PE 6-8 .......................................................................................................................................................... 27 PE 6-9 .......................................................................................................................................................... 28 Chapter 7 ............................................................................................................................................................. 29 PE 7-1 .......................................................................................................................................................... 30 PE 7-2 .......................................................................................................................................................... 30 PE 7-3 .......................................................................................................................................................... 32 PE 7-4 .......................................................................................................................................................... 32 PE 7-5 .......................................................................................................................................................... 33 PE 7-6 .......................................................................................................................................................... 34 PE 7-7 .......................................................................................................................................................... 35 SP 1 of 174 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition PE 7-8 .......................................................................................................................................................... 37 PE 7-9 .......................................................................................................................................................... 38 Chapter 8 ............................................................................................................................................................. 39 PE 8-1 .......................................................................................................................................................... 39 PE 8-2 .......................................................................................................................................................... 40 PE 8-3 .......................................................................................................................................................... 41 PE 8-4 .......................................................................................................................................................... 42 PE 8-5 .......................................................................................................................................................... 43 PE 8-6 .......................................................................................................................................................... 43 PE 8-7 .......................................................................................................................................................... 44 Chapter 9 ............................................................................................................................................................. 45 PE 9-1 .......................................................................................................................................................... 45 PE 9-2 .......................................................................................................................................................... 47 PE 9-3 .......................................................................................................................................................... 48 PE 9-4 .......................................................................................................................................................... 48 Chapter 10............................................................................................................................................................ 49 PE 10-1 ........................................................................................................................................................ 49 PE 10-2 ........................................................................................................................................................ 50 PE 10-3 ........................................................................................................................................................ 51 PE 10-4 ........................................................................................................................................................ 53 PE 10-5 ........................................................................................................................................................ 55 PE 10-6 ........................................................................................................................................................ 57 PE 10-7 ........................................................................................................................................................ 59 PE 10-8 ........................................................................................................................................................ 60 Chapter 11............................................................................................................................................................ 62 PE 11-1 ........................................................................................................................................................ 62 PE 11-2 ........................................................................................................................................................ 67 PE 11-3 ........................................................................................................................................................ 72 PE 11-4 ........................................................................................................................................................ 77 PE 11-5 ........................................................................................................................................................ 79 PE 11-6 ........................................................................................................................................................ 81 PE 11-7 ........................................................................................................................................................ 84 Chapter 12............................................................................................................................................................ 86 PE 12-1 ........................................................................................................................................................ 86 PE 12-2 ........................................................................................................................................................ 88 PE 12-3 ........................................................................................................................................................ 92 PE 12-4 ........................................................................................................................................................ 95 PE 12-5 ........................................................................................................................................................ 97 PE 12-6 ...................................................................................................................................................... 101 Chapter 13.......................................................................................................................................................... 106 PE 13-1 ...................................................................................................................................................... 106 PE 13-2 ...................................................................................................................................................... 109 PE 13-3 ...................................................................................................................................................... 113 PE 13-4 ...................................................................................................................................................... 117 Chapter 14.......................................................................................................................................................... 120 PE 14-1 ...................................................................................................................................................... 120 PE 14-2 ...................................................................................................................................................... 123 PE 14-3 ...................................................................................................................................................... 126 PE 14-4 ...................................................................................................................................................... 133 PE 14-5 ...................................................................................................................................................... 135 Chapter 15.......................................................................................................................................................... 141 PE 15-1 ...................................................................................................................................................... 141 PE 15-2 ...................................................................................................................................................... 144 PE 15-3 ...................................................................................................................................................... 146 PE 15-4 ...................................................................................................................................................... 148 Chapter 16.......................................................................................................................................................... 152 SP 2 of 174 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition PE 16-1 ...................................................................................................................................................... 152 PE 16-2 ...................................................................................................................................................... 153 PE 16-3 ...................................................................................................................................................... 153 PE 16-4 ...................................................................................................................................................... 154 PE 16-5 ...................................................................................................................................................... 155 PE 16-6 ...................................................................................................................................................... 156 PE 16-7 ...................................................................................................................................................... 158 PE 16-8 ...................................................................................................................................................... 159 Chapter 17.......................................................................................................................................................... 160 PE 17-1 ...................................................................................................................................................... 160 PE 17-2 ...................................................................................................................................................... 160 PE 17-3 ...................................................................................................................................................... 161 PE 17-4 ...................................................................................................................................................... 162 PE 17-5 ...................................................................................................................................................... 163 PE 17-6 ...................................................................................................................................................... 164 PE 17-7 ...................................................................................................................................................... 173 Chapter 2 PE 2-1 //pe2-1.cpp #include int main() { using namespace std; cout<<"Name:"<<" liangdongxu "< int main(void) { using namespace std; cout << "Enter a distance in furlongs: "; double furlongs; cin >> furlongs; double feet; feet = 220 * furlongs; cout << furlongs << " furlongs = " << feet << " feet\n"; return 0; } SP 3 of 174 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition PE 2-3 // pe2-3.cpp #include using namespace std; void mice(); void run(); int main() { mice(); mice(); run(); run(); return 0; } void mice() { cout << "Three blind mice\n"; } void run() { cout << "See how they run\n"; } PE 2-4 // pe2-4.cpp #include double C_to_F(double); int main() { using namespace std; cout << "Enter a temperature in Celsius: "; double C; cin >> C; double F; F = C_to_F(C); cout << C << " degrees Celsius = " << F << " degrees Fahrenheit\n"; return 0; } double C_to_F(double temp) { return 1.8 * temp + 32.0; } SP 4 of 174 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition PE 2-5 // pe2-5.cpp #include double lightyear_to_astro(double); int main() { using namespace std; cout<<"Enter the number of light years: "; double years, astrounit; cin>>years; astrounit = lightyear_to_astro(years); cout< using namespace std; void maketime(int, int); int main() { double hours, minutes; cout<<"Enter the number of hours: "; cin>>hours; cout<<"Enter the number of minutes: "; cin>>minutes; maketime(hours,minutes); return 0; } void maketime(int hours, int minutes) { cout<<"Time: "<
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition Chapter 3 PE 3-1 // pe3-1.cpp #include const int Inch_Per_Foot = 12; int main(void) { using namespace std; // Note: some environments don't support the backspace character cout << "Please enter your height in inches: ___\b\b\b "; int ht_inch; cin >> ht_inch; int ht_feet = ht_inch / Inch_Per_Foot; int rm_inch = ht_inch % Inch_Per_Foot; cout << "Your height is " << ht_feet << " feet, "; cout << rm_inch << " inch(es).\n"; return 0; } PE 3-2 // pe3-2.cpp #include const int Inch_Per_Foot = 12; const double Meter_Per_Inch = 0.0254; const double Pound_Per_Kilo = 2.2; int main(void) { using namespace std; // Note: some environments don't support the backspace character cout << "Please enter your height in foot and inches: ___\b\b\b "; int ht_inch,ht_foot; cin>>ht_foot>>ht_inch; cout<<"Please enter your weight in pounds:"; double weight; cin>>weight; double bmi = (weight/Pound_Per_Kilo)/(((ht_foot * Inch_Per_Foot+ht_inch)/Meter_Per_Inch)*((ht_foot * Inch_Per_Foot+ht_inch)/Meter_Per_Inch)); cout << "Your height is " <<( ht_foot*Inch_Per_Foot+ht_inch)<<” inches”; cout<<” and ”; cout <<"Your BMI is "<
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition PE 3-3 // pe3-3.cpp #include const double MINS_PER_DEG = 60.0; const double SECS_PER_MIN = 60.0; int main() { using namespace std; int degrees; int minutes; int seconds; double latitude; cout << "Enter a latitude in degrees, minutes, and seconds:\n"; cout << "First, enter the degrees: "; cin >> degrees; cout << "Next, enter the minutes of arc: "; cin >> minutes; cout << "Finally, enter the seconds of arc: "; cin >> seconds; latitude = degrees + (minutes + seconds / SECS_PER_MIN)/MINS_PER_DEG; cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << latitude << " degrees\n"; return 0; } PE 3-4 // pe3-4.cpp #include const int Hours_Per_Day = 24; const int Mins_Per_Hour = 60; const int Seconds_Per_Minute = 60; int main() { using namespace std; int day; int hour; int minute; int second; long seconds; cout << "Enter the number of the seconds:"; cin>>seconds; day = seconds/(Hours_Per_Day * Mins_Per_Hour * Seconds_Per_Minute); hour = (seconds%(Hours_Per_Day * Mins_Per_Hour * Seconds_Per_Minute))/(Mins_Per_Hour*Seconds_Per_Minute); minute = ((seconds%(Hours_Per_Day * Mins_Per_Hour * Seconds_Per_Minute))%(Mins_Per_Hour*Seconds_Per_Minute))/Seconds_Per_Minute; SP 7 of 174 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition second = ((seconds%(Hours_Per_Day * Mins_Per_Hour * Seconds_Per_Minute))%(Mins_Per_Hour*Seconds_Per_Minute))%Seconds_Per_Minute; cout << seconds<< " seconds = " << day << " days, " << hour << " hours," << minute << " minutes"< int main(void) { using namespace std; cout << "How many miles have you driven your car? "; float miles; cin >> miles; cout << "How many gallons of gasoline did the car use? "; float gallons; cin >> gallons; cout << "Your car got " << miles / gallons; cout << " miles per gallon.\n"; return 0; } PE 3-6 // pe3-6.cpp #include const double KM100_TO_MILES = 62.14; const double LITERS_PER_GALLON = 3.875; int main ( void ) { using namespace std; double euro_rating; double us_rating; cout << "Enter fuel consumption in liters per 100 km: "; cin >> euro_rating; // divide by LITER_PER_GALLON to get gallons per 100-km // divide by KM100_TO_MILES to get gallons per mile // invert result to get miles per gallon us_rating = (LITERS_PER_GALLON * KM100_TO_MILES) / euro_rating; cout << euro_rating << " liters per 100 km is "; cout << us_rating << " miles per gallon.\n"; return 0; } SP 8 of 174 September 2, 2004
分享到:
收藏