1、
Chapter 2
// pe2-2.cpp
#include
2、<< feet << " feet\n";
return 0;
}
// pe2-3.cpp
#include
3、ee how they run\n";
}
// pe2-4.cpp
#include
4、<< " degrees Fahrenheit\n";
return 0;
}
double C_to_F(double temp)
{
return 1.8 * temp + 32.0;
}
Chapter 3
// pe3-1.cpp
#include
5、e 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";
6、 return 0;
}
// pe3-3.cpp
#include
7、d 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;
8、 cout << degrees << " degrees, " << minutes << " minutes, "
<< seconds << " seconds = " << latitude << " degrees\n";
return 0;
}
// pe3-5.cpp
#include
9、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
10、00_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
11、 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;
12、
}
Chapter 4
// pe4-2.cpp -- storing strings in string objects
#include
13、 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、sing 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, ",
15、"); 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
16、 calories;
};
#include
17、lories << endl;
return 0;
}
// pe4-7.ccp
#include
18、 "; 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 << "
19、 inches\n";
cout << "Weight: " << pie.weight << " ounces\n";
return 0;
}
Chapter 5
// pe5-2.cpp
#include
20、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
21、st 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];
22、 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++)
23、
total += sales[month];
cout << "Total sales: " << total << endl;
return 0;
}
// pe5-6.cpp
#include
24、 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);
25、 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"; d
26、elete [] pc;
return 0;
}
// pe5-7.cpp -- count words using C-style string
#include
27、 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
28、namespace std int rows; 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
29、 already has correct value for next loop
for ( ; col <= rows; col++)
cout << '*';
cout << endl;
}
return 0;
}
Chapter 6
// pe6-1.cpp
#include
30、 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); }
31、 return 0;
}
// pe6-3.cpp
#include
32、 (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g') { cout << "Please enter a c, p, t, or g: "; cin >> ch; } switch (ch) { case 'c' : cout << "A cat is a carnivore.\n"; break; case 'p' : cout << "Radu Lupu is a pianist.\n
33、"; break; case 't' : cout << "A maple is a tree.\n"; break; case 'g' : cout << "Golf is a game.\n"; break; default : cout << "The program shouldn't get here!\n"; } return 0; } // pe6-5.cpp
34、
// Neutronia taxation
#include
35、t << "Enter your annual income in tvarps: "; cin >> income; if (income <= LEV1) tax = 0; else if (income <= LEV2) tax = (income - LEV1) * RATE1; else if (income <= LEV3) tax = RATE1 * (LEV2 - LEV1) + RATE2 * (income - LEV2); else tax = R
36、ATE1 * (LEV2 - LEV1) + RATE2 * (LEV3 - LEV2)
+ RATE3 * (income - LEV3);
cout << "You owe Neutronia " << tax << " tvarps in taxes.\n";
return 0;
}
// pe6-7.cpp
#include
37、ar ch; int vowel = 0; int consonant = 0; int other = 0; cout << "Enter words (q to quit):\n"; cin >> word; while ( word != "q") { ch = tolower(word[0]); if (isalpha(ch)) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
38、 || ch == 'u') vowel++; else consonant++; } else other++; cin >> word; } cout << vowel <<" words beginning with vowels\n"; cout << consonant << " words beginning with con
39、sonants\n";
cout << other << " others\n";
return 0;
}
// pe6-8.cpp -- counting characters
#include
40、har filename[SIZE]; char ch; ifstream inFile; // object for handling file input cout << "Enter name of data file: "; cin.getline(filename, SIZE); inFile.open(filename); // associate inFile with a file if (!inFile.is_open()) // failed to open file {
41、 cout << "Could not open the file " << filename << endl; cout << "Program terminating.\n"; exit(EXIT_FAILURE); } int count = 0; // number of items read inFile >> ch; // get first value while (inFile.good()) // while input good and not at E
42、OF { count++; // one more item read inFile >> ch; // get next value } cout << count << " characters in " << filename << endl; inFile.close(); // finished with the file return 0; } Chapter 7 //pe7-1.cpp -- harmonic mean
43、include
44、< h_mean(x,y) << "\n"; /* or do the reading and testing in two parts: while (cin >> x && x != 0) { cin >> y; if (y == 0) break; ... */ cout << "Bye\n"; return 0; } double h_mean(double x, double y) { return 2.0 * x * y / (x
45、 y);
}
// pe7-3.cpp
#include
46、 setbox(&carton); showbox(carton); return 0; } void showbox(box b) { using namespace std; cout << "Box maker: " << b.maker << "\nheight: " << b.height << "\nlwidth: " << b.width << "\nlength: " << b.length << "\nvolume: " << b.vol
47、ume << "\n";
}
void setbox(box * pb)
{
pb->volume = pb->height * pb->width * pb->length;
}
// pe7-4.cpp -- probability of winning
#include
48、 double mtotal; double probability1, probability2; cout << "Enter total number of game card choices and\n" "number of picks allowed for the field:\n"; while ((cin >> total >> choices) && choices <= total) { cout << "Enter total number of game card choices
49、" "for the mega number:\n"; if (!(cin >> mtotal)) break; cout << "The chances of getting all " << choices << " picks is one in " << (probability1 = probability(total, choices) ) << ".\n"; cout << "The chances of getting the megas
50、pot is one in " << (probability2 = probability(mtotal, 1) ) << ".\n"; cout << "You have one chance in "; cout << probability1 * probability2; // compute the probability cout << " of winning.\n"; cout << "Next set of numbers (q to quit): ";






