本文格式为Word版,下载可任意编辑面向对象的C程序设计 第六版 课后习题答案第二章 Chapter 2 C++ Basics 1. Solutions to the Programming Projects: 1. Metric - English units Conversion A metric ton is 35,273.92 ounces. Write a C++ program to read the weight of a box of cereal in ounces then output this weight in metric tons, along with the number of boxes to yield a metric ton of cereal. Design: To convert 14 ounces (of cereal) to metric tons, we use the 'ratio of units' to tell us whether to divide or multiply: 1 metric tons 14 ounces * * = 0.000397 metric tons 35,273 ounces The use of units will simplify the determination of whether to divide or to multiply in making a conversion. Notice that ounces/ounce becomes unit-less, so that we are left with metric ton units. The number of ounces will be very, very much larger than the number of metric tons. It is then reasonable to divide the number of ounces by the number of ounces in a metric ton to get the number of metric tons. Now let metricTonsPerBox be the weight of the cereal box in metric tons. Let ouncesPerBox the be the weight of the cereal box in ounces. Then in C++ the formula becomes: const double ouncesPerMetric_ton = 35272.92; metricTonsPerBox = ouncesPerBox / ouncesPerMetricTon; Copyright ? 2022 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 This is metric tons PER BOX, whence the number of BOX(es) PER metric ton should be the reciprocal: boxesPerMetricTon = 1 / metricTonsPerBox; Once this analysis is made, the code proceeds quickly: //Purpose: To convert cereal box weight from ounces to // metric tons to compute number of boxes to make up a // metric ton of cereal. #include using namespace std; const double ouncesPerMetricTon = 35272.92; int main() { double ouncesPerBox, metricTonsPerbox, boxesPerMetricTon; char ans = 'y'; while( 'y' == ans || 'Y' == ans ) { cout > ouncesPerBox; metricTonsPerbox = ouncesPerBox / ouncesPerMetricTon; boxesPerMetricTon = 1 / metricTonsPerbox; cout > ans; } return 0; } A sample run follows: enter the weight in ounces of your favorite cereal: 14 metric tons per box = 0.000396905 boxes to yield a metric ton = 2519.49 Y or y continues, any other characters terminates. y enter the weight in ounces of your favorite cereal: 20 metric tons per box = 0.000567007 boxes to yield a metric ton = 1763.65 Y or y continues, any other characters terminates. n 2. Lethal Dose Certain artificial sweeteners are poisonous at some dosage level. It is desired to know how much soda a dieter can drink without dying. The problem statement gives no information about how to scale the amount of toxicity from the dimensions of the experimental mouse to the dimensions of the dieter. Hence the student must supply this necessary assumption as basis for the calculation. 3 Copyright ? 2022 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 This solution supposes the lethal dose is directly proportional to the weight of the subject, hence This program accepts weight of a lethal dose for a mouse, the weight of the mouse, and the weight of the dieter, and calculates the amount of sweetener that will just kill the dieter, based on the lethal dose for a mouse in the lab. If the student has problems with grams and pounds, a pound is 454 grams. It is interesting that the result probably wanted is a safe number of cans, while all the data can provide is the minimum lethal number! Some students will probably realize this, but my experience is that most will not. I just weighed a can of diet pop and subtracted the weight of an empty can. The result is about 350 grams. The label claims 355 ml, which weighs very nearly 355 grams. To get the lethal number of cans from the number of grams of sweetener, you need the number of grams of sweetener in a can of pop, and the concentration of sweetener, which the problem assumes 0.1% , that is a conversion factor of 0.001. gramsSweetenerPerCan = 350 * 0.001 = 0.35 grams/can cans = lethalDoseDieter / (0.35 grams / can) //Ch2Prob5.cc //Input: lethal dose of sweetener for a lab mouse, weights // of mouse and dieter, and concentration of sweetener in a // soda. //Output: lethal dose of soda in number of cans. //Assumption: lethal dose proportional to weight of subject weightOfDieter lethalDoseDieter = lethalDoseMouse * weightOfMouse 4 Copyright ? 2022 P。