讲解C++homework 辅导、conversion c++ 讲解,辅导C++conversion 类、解析C程序代码辅导

- 首页 >> C/C++编程



CMPSC121: Intro to Programming Techniques (Fall 2018)

Homework 03 (20 points)

Due Sunday, September 9 at 11:59pm

Objectives

After this homework assignment, students should be able to:

• Format text output with user-defined spacing

• Output numbers with user-defined settings

• Use conditions to make decisions within the program

Background

The text output of a program should be well-formatted for readability as well as displaying numbers with the

proper notation and precision levels. The default output formatting in C++ is often not desirable, so explicit

instructions can be added to the cout stream. These manipulators and functions are defined in the

<iomanip> library.

• setw(n): set the width (num. characters) of the next item to be at least n

o If the width is less than n, then pad the item with extra spaces

o If the width is greater than n, then make no changes

• left and right: when used in conjunction with setw(), justify the item to one side, forcing the

padding to the other side

• fixed: use the fixed-point notation (the decimal point notation that we are all familiar with) when

displaying the next numerical item

• setprecision(n): when used in conjunction with fixed, set the number of digits after the decimal

point to n

• showpoint: when printing a floating-point number, display the decimal point even if the value is

exactly equal to a whole number

float cost = 10.0;

cout << ":" << cost << ":" << endl;

cout << ":" << setw(7) << right << fixed << setprecision(2) << showpoint

<< cost << ":" << endl;

cout << ":1234567:" << endl;

:10:

: 10.00:

:1234567:

Instructions

Task 1

Write a program named conversion.cpp that:

• Prompts the user to enter 5 temperature values in Fahrenheit (℉)

• Calculates the equivalent Celsius (℃) temperatures for each value entered

• Outputs a labeled three-column display, containing the following info:

o Fahrenheit values in the left column

o Celsius values in the middle column

o A warning message if the given temperature is freezing (less than or equal to 0℃) or boiling

(greater than or equal to 100℃) in the right column

The following formatting rules should be observed:

• Ensure that the columns remain fixed regardless of the values entered by the user (you may assume that

values entered are between -1000℉ and 1000℉)

• Use a precision of 2 decimal places to the right of the decimal point

Refer to the sample output for an example of proper formatting.

Task 2

Write a program named receipt.cpp that:

• Prompts the user for the names of 3 one-word items and their corresponding prices in US dollars

• Makes sure each price is not negative or greater than $999.99 (if invalid input is entered, then output an

error message and terminate the program)

• Makes sure the length of each item name is not 10 or greater (if invalid input is entered, then output an

error message and terminate the program)

• Calculates the total price and displays a well-formatted receipt

Refer to the sample output for an example of proper formatting.

Optional Bonus (+2 points)

Write a program named receiptdollar.cpp that provides an additional functionality to Task 2:

• When printing the prices of each item and the total, format the output so that the dollar sign ($) is

immediately to the left of the price

Sample Output

Task 1 Sample Output

Enter five temperature values in Fahrenheit:

-999.99 32 98.6 212 999.99

Fahrenheit Celsius Warning

-999.99 -573.33 <= Freezing!

32.00 0.00 <= Freezing!

98.60 37.00

212.00 100.00 >= Boiling!

999.99 537.77 >= Boiling!

Task 2 Sample Output

Enter names of 3 one-word items to purchase (each name must be less than

10 letters long):

Gum

Television

This item is 10 or more letters long

Enter names of 3 one-word items to purchase (each name must be less than

10 letters long):

Gum

Diamonds

Oranges

You have purchased 3 items. Enter their prices in US Dollars (must be

less than $1,000):

0.75

4500

You entered an invalid price

Enter names of 3 one-word items to purchase (each name must be less than

10 letters long):

Gum

Flour

Oranges

You have purchased 3 items. Enter their prices in US Dollars (must be

less than $1,000):

0.75

6.00

19.99

+---------+-------+

| RECEIPT |

+---------+-------+

|Gum |$ 0.75|

|Flour |$ 6.00|

|Oranges |$ 19.99|

+---------+-------+

|TOTAL: $ 26.74|

+---------+-------+

Optional Bonus Sample Output

Enter names of 3 one-word items to purchase (each name must be less than

10 letters long):

Candy

Sneakers

Cellphone

You have purchased 3 items. Enter their prices in US Dollars (must be

less than $1,000):

0.99

75.00

949.99

+---------+-------+

| RECEIPT |

+---------+-------+

|Candy | $0.99|

|Sneakers | $75.00|

|Cellphone|$949.99|

+---------+-------+

|TOTAL: $1025.98|

+---------+-------+

Submission

Submit the following file(s) to Canvas before the deadline:

1. conversion.cpp

2. receipt.cpp

3. (optional bonus) receiptdollar.cpp


站长地图