好文档 - 专业文书写作范文服务资料分享网站

用C++解决问题第十版Chapter 5 Functions for All Subtasks

天下 分享 时间: 加入收藏 我要投稿 点赞

Savitch

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

Chapter 5

//Postcondition:

//Prompt given to the user for input in the format FF II, //where FF is integer number of feet and II is a double //number of inches. feet and inches are returned as entered //by the user.

void convert (int feet, double inches, double& meters ); //Preconditions:

//REQUIRED CONSTANTS: INCHES_PER_FOOT, METERS_PER_FOOT //inches < 12, feet within range of values for int type //Postconditions:

//meters assigned 0.3048 * (feet + inches/12) //observe that the centimeter requirement is met by

//the value of the first two decimal places of the converted //feet, inches input.

void output( int feet, double inches, double meters ); //input: the formal argument for meters fits into a double //output:

//\//\

//where meters is displayed as a number with two decimal //places

int main() {

int feet;

double inches, meters; char ans;

14

Copyright ? 2018 Pearson Education, Inc.

Savitch

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

Chapter 5

do {

input ( feet, inches );

convert ( feet, inches, meters ); output ( feet, inches, meters );

cout << \ << endl; cin >> ans;

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

return 0; }

void input ( int& feet, double& inches ) {

cout << \cin >> feet;

cout << \cin >> inches; }

const double METERS_PER_FOOT = 0.3048; const double INCHES_PER_FOOT = 12.0;

void convert (int feet, double inches, double& meters ) {

meters = METERS_PER_FOOT * (feet + inches/INCHES_PER_FOOT); }

14

Copyright ? 2018 Pearson Education, Inc.

Savitch

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

Chapter 5

void output( int feet, double inches, double meters ) {

//inches, meters displayed as a number with two decimal //places

cout.setf( ios::showpoint ); cout.setf( ios::fixed ); cout.precision(2);

cout << \ << inches << endl

<< \ << meters << endl; } /*

A typical run follows: 06:59:16:~/AW$ a.out

Enter feet as an integer: 5 Enter inches as a double: 7 the value of feet, inches5,7.00

converted to meters, centimeters is 1.70 Y or y continues, any other character quits y

Enter feet as an integer: 245 Enter inches as a double: 0 the value of feet, inches245,0.00

converted to meters, centimeters is 74.68 Y or y continues, any other character quits q

06:59:49:~/AW$

14

Copyright ? 2018 Pearson Education, Inc.

Savitch

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

Chapter 5

*/

Practice Program 3: Conversion metric to English Conversion of meters back to feet and inches

//Task: Convert meters with centimeters (just the decimal //part of meters)to feet/inches //

//Input: a length in feet and inches, with possible decimal //part of inches

//Output: A length measured in feet with any decimal fraction //converted to inches by multiplying by 12. Fractions of an //inch are represented by 2 decimal places. //

//Required: functions for input, computation, and output. //Include a loop to repeat the calculation at the user's //option. //

//Remark: The computation is a simple conversion from meters //to feet, inches, where inches has a decimal part. //Output is restricted to 2 decimal places

//Comment: Please see Problem 4 for discussion of 'meters and //centimeters' #include using namespace std;

void input ( double & meters ); //Precondition: function is called //Postcondition:

//Prompt given to the user for input of a number of meters as

14

Copyright ? 2018 Pearson Education, Inc.

Savitch

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

Chapter 5

//a double. input of a double for meters has been accepted

void convert (int& feet, double& inches, double meters ); //Preconditions:

// REQUIRED CONSTANTS: INCHES_PER_FOOT, METERS_PER_FOOT //Postconditions:

//feet is assigned the integer part of meters (after //conversion to feet units) inches is assigned the

//fractional part of feet ( after conversion to inch units

void output( int feet, double inches, double meters ); //input: the formal argument for meters fits into a double //output:

//\//\// \

//where meters is displayed as a number with two decimal //places

int main() {

int feet;

double inches, meters; char ans; do {

input ( meters );

convert ( feet, inches, meters ); output ( feet, inches, meters );

cout << \

14

Copyright ? 2018 Pearson Education, Inc.

用C++解决问题第十版Chapter 5 Functions for All Subtasks

SavitchProblemSolvingw/C++,10eInstructor’sResourceGuideChapter5//Postcondition://PromptgiventotheuserforinputintheformatFFII,//whereFFisint
推荐度:
点击下载文档文档为doc格式
9upfl60rfu3bj0w6iip07zlrl1bk8m0130e
领取福利

微信扫码领取福利

微信扫码分享