logo资料库

c++ primer plus 第五版课后习题答案.pdf

第1页 / 共73页
第2页 / 共73页
第3页 / 共73页
第4页 / 共73页
第5页 / 共73页
第6页 / 共73页
第7页 / 共73页
第8页 / 共73页
资料共73页,剩余部分请下载后查看
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Solutions for Programming Exercises in C++ Primer Plus, 5th Edition Chapter 2 // pe2-2.cpp #include 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; } // 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"; } // pe2-4.cpp #include double C_to_F(double); int main() SP 1 of 73 September 2, 2004
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Solutions for Programming Exercises in C++ Primer Plus, 5th Edition { 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; } Chapter 3 // 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; } // 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"; SP 2 of 73 September 2, 2004
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Solutions for Programming Exercises in C++ Primer Plus, 5th Edition 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; } // pe3-5.cpp #include 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; } // 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 3 of 73 September 2, 2004
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Solutions for Programming Exercises in C++ Primer Plus, 5th Edition Chapter 4 // pe4-2.cpp -- storing strings in string objects #include #include int main() { using namespace std; string name; string dessert; cout << "Enter your name:\n"; getline(cin, name); // reads through newline cout << "Enter your favorite dessert:\n"; getline(cin, dessert); cout << "I have some delicious " << dessert; cout << " for you, " << name << ".\n"; return 0; } // pe4-3.cpp -- storing strings in char arrays #include #include const int SIZE = 20; int main() { using namespace std; char firstName[SIZE]; char lastName[SIZE]; char fullName[2*SIZE + 1]; cout << "Enter your first name: "; cin >> firstName; cout << "Enter your last name: "; cin >> lastName; strncpy(fullName,lastName,SIZE); strcat(fullName, ", "); strncat(fullName, firstName, SIZE); fullName[SIZE - 1] = '\0'; cout << "Here's the information in a single string: " << fullName << endl; return 0; } // pe4-5.cpp // a candybar structure struct CandyBar { char brand[40]; double weight; int calories; }; #include SP 4 of 73 September 2, 2004
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Solutions for Programming Exercises in C++ Primer Plus, 5th Edition int main() { using namespace std; //introduces namespace std CandyBar snack = { "Mocha Munch", 2.3, 350 }; cout << "Brand name: " << snack.brand << endl; cout << "Weight: " << snack.weight << endl; cout << "Calories: " << snack.calories << endl; return 0; } // pe4-7.ccp #include const int Slen = 70; struct pizza { char name[Slen]; float diameter; float weight; }; int main(void) { using namespace std; pizza pie; cout << "What is the name of the pizza company? "; cin.getline(pie.name, Slen); cout << "What is the diameter of the pizza in inches? "; cin >> pie.diameter; cout << "How much does the pizza weigh in ounces? "; cin >> pie.weight; cout << "Company: " << pie.name << "\n"; cout << "Diameter: " << pie.diameter << " inches\n"; cout << "Weight: " << pie.weight << " ounces\n"; return 0; } Chapter 5 // pe5-2.cpp #include int main(void) { using namespace std; double sum = 0.0; SP 5 of 73 September 2, 2004
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Solutions for Programming Exercises in C++ Primer Plus, 5th Edition double in; cout << "Enter a number (0 to terminate) : "; cin >> in; while (in != 0) { sum += in; cout << "Running total = " << sum << "\n"; cout << "Enter next number (0 to terminate) : "; cin >> in; } cout << "Bye!\n"; return 0; } // pe5-4.cpp // book sales #include const int MONTHS = 12; const char * months[MONTHS] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int main() { using namespace std; //introduces namespace std int sales[MONTHS]; int month; cout << "Enter the monthly sales for \"C++ for Fools\":\n"; for (month = 0; month < MONTHS; month++) { cout << "Sales for " << months[month] << ": "; cin >> sales[month]; } double total = 0.0; for (month = 0; month < MONTHS; month++) total += sales[month]; cout << "Total sales: " << total << endl; return 0; } // pe5-6.cpp #include struct car { char name[20]; int year;}; int main(void) { using namespace std; int n; cout << "How many cars do you wish to catalog?: "; SP 6 of 73 September 2, 2004
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Solutions for Programming Exercises in C++ Primer Plus, 5th Edition cin >> n; while(cin.get() != '\n') // get rid of rest of line ; car * pc = new car [n]; int i; for (i = 0; i < n; i++) { cout << "Car #" << (i + 1) << ":\n"; cout << "Please enter the make: "; cin.getline(pc[i].name,20); cout << "Please enter the year made: "; cin >> pc[i].year; while(cin.get() != '\n') // get rid of rest of line ; } cout << "Here is your collection:\n"; for (i = 0; i < n; i++) cout << pc[i].year << " " << pc[i].name << "\n"; delete [] pc; return 0; } // pe5-7.cpp -- count words using C-style string #include #include // prototype for strcmp() const int STR_LIM = 50; int main() { using namespace std; char word[STR_LIM]; int count = 0; cout << "Enter words (to stop, type the word done):\n"; while (cin >> word && strcmp("done", word)) ++count; cout << "You entered a total of " << count << " words.\n"; return 0; } // pe5-9.cpp //nested loops #include int main() { using namespace std; //introduces namespace std int rows; SP 7 of 73 September 2, 2004
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Solutions for Programming Exercises in C++ Primer Plus, 5th Edition int row; int col; int periods; cout << "Enter number of rows: "; cin >> rows; for (row = 1; row <= rows; row++) { periods = rows - row; for (col = 1; col <= periods; col++) cout << '.'; // col already has correct value for next loop for ( ; col <= rows; col++) cout << '*'; cout << endl; } return 0; } Chapter 6 // pe6-1.cpp #include #include int main( ) { using namespace std; //introduces namespace std char ch; cin.get(ch); while(ch != '@') { if (!isdigit(ch)) { if (isupper(ch)) ch = tolower(ch); else if (islower(ch)) ch = toupper(ch); cout << ch; } cin.get(ch); } return 0; } // pe6-3.cpp #include int main(void) { SP 8 of 73 September 2, 2004
分享到:
收藏