IRM - Chapter - 6e - 02

更新时间:2024-05-25 03:26:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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.

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

1

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

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.

2

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

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;

This is metric tons PER BOX, whence the number of BOX(es) PER metric ton should be the reciprocal:

boxesPerMetricTon = 1 / metricTonsPerBox;

3

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

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;

4

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

int main() {

double ouncesPerBox, metricTonsPerbox,

boxesPerMetricTon; char ans = 'y';

while( 'y' == ans || 'Y' == ans ) {

cout << “enter the weight in ounces of your”

<< “favorite cereal:” <

cin >> ouncesPerBox; metricTonsPerbox = ouncesPerBox / ouncesPerMetricTon;

5

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

boxesPerMetricTon = 1 / metricTonsPerbox;

cout << \box = \

<< metricTonsPerbox << endl;

cout << \metric ton = \

<< boxesPerMetricTon << endl;

cout << \any other character ”

<< “terminates.\endl;

cin >> ans; } return 0;

6

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

}

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

7

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

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.

8

Copyright ? 2007 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

weightOfDieter lethalDoseDieter = lethalDoseMouse * weightOfMouse

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

9

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

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)

10

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

//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 // Concentration of sweetener in the soda is 1/10 percent #include using namespace std;

11

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

const double concentration = .001; // 1/10 of 1 percent const double canWeight = 350; const double gramsSweetnerPerCan = canWeight ??concentration; //units of grams/can int main() {

double lethalDoseMouse, lethalDoseDieter, weightMouse,

weightDieter; //units: grams double cans; char ans; do

12

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

{

cout << \the mouse in grams\

<< endl;

cin >> weightMouse; cout << \dose for the mouse in“

<< ”grams \ cin >> lethalDoseMouse; cout << \weight of the dieter in”

<<“ grams \ cin >> weightDieter; lethalDoseDieter = lethalDoseMouse

??weightDieter/weightMouse;

13

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

cout << \parameters:\\nmouse weight: \

<< weightMouse << \ << \mouse: \

<< lethalDoseMouse << \ << \weightDieter

<< \ << \grams of sweetener is: \

<< lethalDoseDieter << endl;

cans = lethalDoseDieter / gramsSweetnerPerCan;

14

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

cout << \cans of pop: \

<< cans << endl; cout << \any other character quits\

<< endl; cin >> ans;

} while ( 'y' == ans || 'Y' == ans ); return 0; }

A typical run follows: 17:23:09:~/AW$ a.out

Enter the weight of the mouse in grams 15

15

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

Enter the lethal dose for the mouse in grams 100

Enter the desired weight of the dieter, in grams 45400

For these parameters: mouse weight: 15 grams

lethal dose for the mouse: 100 grams

Dieter weight: 45400 grams The lethal dose in grams of sweetener is: 302667

Lethal number of cans of pop: 864762

Y or y continues, any other character quits y

16

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

Enter the weight of the mouse in grams 30

Enter the lethal dose for the mouse in grams 100

Enter the desired weight of the dieter, in grams 45400

For these parameters: mouse weight: 30 grams

lethal dose for the mouse: 100 grams

Dieter weight: 45400 grams The lethal dose in grams of sweetener is: 151333

Lethal number of cans of pop: 432381

17

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

Y or y continues, any other character quits q

17:23:56:~/AW$

3. Pay Increase

The workers have won a 7.6% pay increase, effective 6 months retroactively. This program is to accept the previous annual salary, then outputs the retroactive pay due the employee, the new annual salary, and the new monthly salary. Allow user to repeat as desired. The appropriate formulae are:

const double INCREASE = 0.076;

18

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

newSalary = salary * (1 + INCREASE);

monthly = salary / 12; retroactive = (salary – oldSalary)/2; The code follows:

//Ch2Prob3.cc

//Given 6 mos retroactive 7.6% pay increase, //input salary

//Output new annual and monthly salaries, retroactive pay #include using namespace std;

const double INCREASE = 0.076;

19

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

int main() {

double oldSalary, salary, monthly, retroactive; char ans;

cout << \salary.\

<< \salary, monthly ”

<< “salary, and retroactive pay.\

cin >> oldSalary;//old annual salary salary =

oldSalary*(1+INCREASE);//new annual salary

monthly = salary/12;

20

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

retroactive = (salary – oldSalary)/2;

cout << \salary << endl;

cout << \<< monthly << endl;

cout << \due: \

<< retroactive << endl; return 0; }

17:50:12:~/AW$ a.out

Enter current annual salary. 100000

I'll return new annual salary, monthly salary, and retroactive pay.

new annual salary 107600

21

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

new monthly salary 8966.67 retroactive salary due: 3800

4. Retroactive Salary

// File: Ch2.4.cpp

// Modify program from Problem #3 so that it calculates retroactive // salary for a worker for a number of months entered by the user.

//Given a 7.6% pay increase, //input salary

//input number of months to compute retroactive salary

//Output new annual and monthly salaries, retroactive pay

#include

const double INCREASE = 0.076;

22

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

int main() {

using std::cout; using std::cin; using std::endl;

double oldSalary, salary,

monthly, oldMonthly, retroactive; int numberOfMonths; // number of months to pay retroactive increase char ans;

cout << \salary and a number of months\\n\ << \compute retroactive pay.\\n\

<< \salary, monthly \

<< \pay.\

cin >> oldSalary;//old annual salary

cin >> numberOfMonths;

23

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

salary = oldSalary *

(1+INCREASE); //new annual salary oldMonthly = oldSalary/12; monthly = salary/12;

retroactive = (monthly -

oldMonthly) * numberOfMonths; // retroactive = (salary - oldSalary)/2; // six months retroactive pay increase.

cout << \salary << endl;

cout << \<< monthly << endl;

cout << \\

<< retroactive << endl;

return 0; } /*

Typical run

24

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

Enter current annual salary and a number of months

for which you wish to compute retroactive pay.

I'll return new annual salary, monthly salary, and retroactive pay. 12000 9

new annual salary 12912 new monthly salary 1076

retroactive salary due: 684 Press any key to continue

*/

5. No solution provided.

6. No solution provided.

7. Payroll

25

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

This problem involves payroll and uses the selection construct. A possible restatement: An hourly employee's regular payRate is $16.78/hour for hoursWorked <= 40 hours. If hoursWorked > 40 hours, then (hoursWorked -40) is paid at an overtime premium rate of 1.5 * payRate. FICA (social security) tax is 6% and Federal income tax is 14%. Union dues of $10/week are withheld. If there are 3 or more covered dependents, $15 more is withheld for dependent health insurance.

a) Write a program that, on a weekly basis, accepts hours worked then outputs gross pay,

26

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

each withholding amount, and net (take-home) pay.

b) Add 'repeat at user discretion' feature.

I was unpleasantly surprised to find that with early GNU g++ , you cannot use a leading 0 (such as an SSN 034 56 7891) in a sequence of integer inputs. The gnu iostreams library took the integer to be zero and went directly to the next input! You either have to either use an array of char, or 9 char variables to avoid this restriction.

Otherwise, the code is fairly straight forward.

//file Ch2Prob7.cc

27

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

//pay roll problem:

//Inputs: hoursWorked, number of dependents

//Outputs: gross pay, each deduction, net pay //

//This is the 'repeat at user discretion' version //Outline:

//In a real payroll program, each of these values would be //stored in a file after the payroll calculation was printed //to a report. //

//regular payRate = $10.78/hour for hoursWorked <= 40 //hours. //If hoursWorked > 40 hours,

28

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

// overtimePay = (hoursWorked - 40) * 1.5 * PAY_RATE.

//FICA (social security) tax rate is 6%

//Federal income tax rate is 14%. //Union dues = $10/week . //If number of dependents >= 3 // $15 more is withheld for dependent health insurance. //

#include using namespace std;

const double PAY_RATE = 16.78; const double SS_TAX_RATE = 0.06; const double FedIRS_RATE = 0.14; const double STATE_TAX_RATE = 0.05;

29

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

const double UNION_DUES = 10.0; const double OVERTIME_FACTOR = 1.5; const double HEALTH_INSURANCE = 15.0;

int main() {

double hoursWorked, grossPay, overTime, fica,

incomeTax, stateTax, union_dues, netPay; int numberDependents, employeeNumber; char ans;

//set the output to two places, and force .00 for cents

30

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(2); // compute payroll do {

cout << \(digits only,”

<< “ no spaces or dashes) \\n”;

cin >> employeeNumber ; cout << “Please the enter hours worked and number “

<< “of employees.” << endl;

cin >> hoursWorked ; cin >> numberDependents;

31

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

cout << endl;

if (hoursWorked <= 40 ) grossPay = hoursWorked * PAY_RATE;

else {

overTime =

(hoursWorked - 40) * PAY_RATE * OVERTIME_FACTOR;

grossPay = 40 * PAY_RATE + overTime;

}

fica = grossPay * SS_TAX_RATE;

32

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

incomeTax = grossPay * FedIRS_RATE;

stateTax = grossPay * STATE_TAX_RATE;

netPay =

grossPay - fica - incomeTax

- UNION_DUES - stateTax;

if ( numberDependents >= 3 ) netPay = netPay - HEALTH_INSURANCE;

//now print report for this employee:

cout << \

33

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

<< employeeNumber << endl;

cout << \hoursWorked << endl;

cout << \<< PAY_RATE << endl;

if (hoursWorked > 40 ) {

cout << \worked: \

<< hoursWorked - 40 << endl;

cout << \premium: \

<< OVERTIME_FACTOR << endl;

34

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

}

cout << \grossPay << endl;

cout << \<< fica << endl;

cout << \withheld: \

<< incomeTax << endl; cout << \\

if (numberDependents >= 3 ) cout << \Premium withheld: \

<< HEALTH_INSURANCE << endl;

35

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

cout << \Union Dues withheld: \

<< UNION_DUES << endl; cout << \<< endl << endl;

cout << \another employee?”

<< “ Y/y repeats, any other ends\

cin >> ans;

} while( 'y' == ans || 'Y' == ans ); return 0; }

//A typical run:

36

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

14:26:48:~/AW $ a.out

Enter employee SSN (digits only, no spaces or dashes) 234567890

Please the enter hours worked and number of employees. 10 1

Employee number: 234567890 hours worked: 10.00 regular pay rate: 16.78 gross pay: 167.80

FICA tax withheld: 10.07 Federal Income Tax withheld: 23.49

State Tax withheld: 8.39 Flabbergaster's Union Dues withheld: 10.00

37

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

Net Pay: 115.85

Compute pay for another employee? Y/y repeats, any other ends y

Enter employee SSN (digits only, no spaces or dashes) 987654321

Please the enter hours worked and number of employees. 10 3

Employee number: 987654321 hours worked: 10.00 regular pay rate: 16.78 gross pay: 167.80

FICA tax withheld: 10.07 Federal Income Tax withheld: 23.49

38

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

State Tax withheld: 8.39 Health Insurance Premium withheld: 35.00

Flabbergaster's Union Dues withheld: 10.00 Net Pay: 80.85

Compute pay for another employee? Y/y repeats, any other ends y

Enter employee SSN (digits only, no spaces or dashes) 123456789

Please the enter hours worked and number of employees. 45 3

Employee number: 123456789 hours worked: 45.00

39

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Savitch

Problem Solving w/ C++, 6e Instructor’s Resource Guide

Chapter 2

regular pay rate: 16.78 overtime hours worked: 5.00 with overtime premium: 1.50 gross pay: 797.05

FICA tax withheld: 47.82 Federal Income Tax withheld: 111.59

State Tax withheld: 39.85 Health Insurance Premium withheld: 35.00

Flabbergaster's Union Dues withheld: 10.00 Net Pay: 552.79

Compute pay for another employee? Y/y repeats, any other ends n

14:28:12:~/AW $

40

Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

本文来源:https://www.bwwdw.com/article/dhb7.html

Top