1、Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
Chapter 2
// pe2-2.cpp
#include
2、
cout << furlongs << " furlongs = "
<< feet << " feet\n";
return 0;
}
// pe2-3.cpp
#include
3、ee blind mice\n";
}
void run()
{
cout << "See how they run\n";
}
// pe2-4.cpp
#include
4、 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
5、/ 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 << "
6、 feet, ";
cout << rm_inch << " inch(es).\n";
return 0;
}
// pe3-3.cpp
#include
7、 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 = de
8、grees + (minutes + seconds / SECS_PER_MIN)/MINS_PER_DEG;
cout << degrees << " degrees, " << minutes << " minutes, "
<< seconds << " seconds = " << latitude << " degrees\n";
return 0;
}
// pe3-5.cpp
#include
9、 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; }
10、
// pe3-6.cpp
#include
11、ating; // 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 "; c
12、out << us_rating << " miles per gallon.\n";
return 0;
}
Chapter 4
// pe4-2.cpp -- storing strings in string objects
#include
13、e(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
14、
#include
15、 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 str
16、uct CandyBar {
char brand[40];
double weight;
int calories;
};
#include
17、Weight: " << snack.weight << endl;
cout << "Calories: " << snack.calories << endl;
return 0;
}
// pe4-7.ccp
#include
18、ce 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 << "Co
19、mpany: " << pie.name << "\n";
cout << "Diameter: " << pie.diameter << " inches\n";
cout << "Weight: " << pie.weight << " ounces\n";
return 0;
}
Chapter 5
// pe5-2.cpp
#include
20、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; } //
21、pe5-4.cpp
// book sales
#include
22、 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];
23、
}
double total = 0.0;
for (month = 0; month < MONTHS; month++)
total += sales[month];
cout << "Total sales: " << total << endl;
return 0;
}
// pe5-6.cpp
#include
24、using namespace std; int n; cout << "How many cars do you wish to catalog?: "; 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) << "
25、\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"; fo
26、r (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
27、 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.
28、cpp
//nested loops
#include
29、 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
30、
#include
31、 if (islower(ch))
ch = toupper(ch);
cout << ch;
}
cin.get(ch);
}
return 0;
}
// pe6-3.cpp
#include
32、 << "c) carnivore p) pianist\n" << "t) tree g) game\n"; char ch; cin >> ch; while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g') { cout << "Please enter a c, p, t, or g: "; cin >> ch; } switch (ch) { case
33、'c' : cout << "A cat is a carnivore.\n"; break; case 'p' : cout << "Radu Lupu is a pianist.\n"; break; case 't' : cout << "A maple is a tree.\n"; break; case 'g' : cout << "Golf is a game.\n";
34、 break;
default : cout << "The program shouldn't get here!\n";
}
return 0;
}
// pe6-5.cpp
// Neutronia taxation
#include
35、 0.15; const double RATE3 = 0.20; int main( ) { using namespace std; double income; double tax; cout << "Enter your annual income in tvarps: "; cin >> income; if (income <= LEV1) tax = 0; else if (income <= LEV2) tax = (income - LE
36、V1) * RATE1; else if (income <= LEV3) tax = RATE1 * (LEV2 - LEV1) + RATE2 * (income - LEV2); else tax = RATE1 * (LEV2 - LEV1) + RATE2 * (LEV3 - LEV2) + RATE3 * (income - LEV3); cout << "You owe Neutronia " << tax << " tvarps in taxes.\n";
37、 return 0;
}
// pe6-7.cpp
#include
38、 { ch = tolower(word[0]); if (isalpha(ch)) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowel++; else consonant++; } else othe
39、r++;
cin >> word;
}
cout << vowel <<" words beginning with vowels\n";
cout << consonant << " words beginning with consonants\n";
cout << other << " others\n";
return 0;
}
// pe6-8.cpp -- counting characters
#include
40、// file I/O suppport
#include
41、ilename, SIZE); inFile.open(filename); // associate inFile with a file if (!inFile.is_open()) // failed to open file { cout << "Could not open the file " << filename << endl; cout << "Program terminating.\n"; exit(EXIT_FAILURE); } int count = 0;
42、 // number of items read inFile >> ch; // get first value while (inFile.good()) // while input good and not at EOF { count++; // one more item read inFile >> ch; // get next value } cout << count << " characters in " << fi
43、lename << endl;
inFile.close(); // finished with the file
return 0;
}
Chapter 7
//pe7-1.cpp -- harmonic mean
#include
44、0 terminates): "; while (cin >> x >> y && x * y != 0) cout << "harmonic mean of " << x << " and " << y << " = " << h_mean(x,y) << "\n"; /* or do the reading and testing in two parts: while (cin >> x && x != 0) { cin >> y; if (y == 0)
45、 break;
...
*/
cout << "Bye\n";
return 0;
}
double h_mean(double x, double y)
{
return 2.0 * x * y / (x + y);
}
// pe7-3.cpp
#include
46、e; }; void showbox(box b); void setbox(box * pb); int main(void) { box carton = {"Bingo Boxer", 2, 3, 5}; // no volume provided setbox(&carton); showbox(carton); return 0; } void showbox(box b) { using namespace std; cout << "Box maker: " << b.maker
47、 << "\nheight: " << b.height
<< "\nlwidth: " << b.width
<< "\nlength: " << b.length
<< "\nvolume: " << b.volume << "\n";
}
void setbox(box * pb)
{
pb->volume = pb->height * pb->width * pb->length;
}
// pe7-4.cpp -- probability of winning
#include 48、stream>
long double probability(unsigned numbers, unsigned picks);
int main()
{
using namespace std;
double total, choices;
double mtotal;
double probability1, probability2;
cout << "Enter total number of game card choices and\n"
"number of picks allowed fo 49、r the field:\n";
while ((cin >> total >> choices) && choices <= total)
{
cout << "Enter total number of game card choices "
"for the mega number:\n";
if (!(cin >> mtotal))
break;
cout << "The chances of getting all " << choices << " 50、picks is one in "
<< (probability1 = probability(total, choices) ) << ".\n";
cout << "The chances of getting the megaspot is one in "
<< (probability2 = probability(mtotal, 1) ) << ".\n";
cout << "You have one chance in ";
cout << probability1 *
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818