Create the equivalent of a four-function calculator

Assignment Help Basic Computer Science
Reference no: EM131076056

Amazing as it may seem, the old British pounds-shillings-pence money notation (£9.19.11-see Exercise 10 in Chapter 4, "Structures") isn't the whole story. A penny was further divided into halfpennies and farthings, with a farthing being worth 1/4 of a penny. There was a halfpenny coin, a farthing coin, and a halffarthing coin. Fortunately all this can be expressed numerically in eighths of a penny:
1/8 penny is a halffarthing
1/4 penny is a farthing
3/8 penny is a farthing and a half
1/2 penny is a halfpenny (pronounced ha'penny)
5/8 penny is a halfpenny plus a halffarthing
3/4 penny is a halfpenny plus a farthing
7/8 penny is a halfpenny plus a farthing and a half

Let's assume we want to add to the sterling class the ability to handle such fractional pennies. The I/O format can be something like £1.1.1-1/4 or £9.19.11-7/8, where the hyphen separates the fraction from the pennies. Derive a new class called sterfrac from sterling. It should be able to perform the four arithmetic operations on sterling quantities that include eighths of a penny. Its only member data is an int indicating the number of eighths; you can call it eighths. You'll need to overload many of the functions in sterling to handle the eighths. The user should be able to type any fraction in lowest terms, and the display should also show fractions in lowest terms. It's not necessary to use the full-scale fraction class (see Exercise 11 in Chapter 6), but you could try that for extra credit..

Exercise 10 in Chapter 4

Create a structure called sterling that stores money amounts in the old-style British system discussed in Exercises 8 and 11 in Chapter 3, "Loops and Decisions." The members could be called pounds, shillings, and pence, all of type int. The program should ask the user to enter a money amount in new-style decimal pounds (type double), convert it to the old-style system, store it in a variable of type struct sterling, and then
display this amount in pounds-shillings-pence format.

Exercises 8

Write a program that repeatedly asks the user to enter two money amounts expressed in old-style British currency: pounds, shillings, and pence. (See Exercises 10 and 12 in Chapter 2, "C++ Programming Basics.") The program should then add the two amounts and display the answer, again in pounds, shillings, and pence. Use a do loop that asks the user whether the program should be terminated. Typical interaction might be
Enter first amount: £5.10.6
Enter second amount: £3.2.6
Total is £8.13.0
Do you wish to continue (y/n)?
To add the two amounts, you'll need to carry 1 shilling when the pence value is greater than 11, and carry 1 pound when there are more than 19 shillings.

Exercises 10

In the heyday of the British empire, Great Britain used a monetary system based on pounds, shillings, and pence. There were 20 shillings to a pound, and 12 pence to a shilling. The notation for this old system used the pound sign, £, and two decimal points, so that, for example, £5.2.8 meant 5 pounds, 2 shillings, and 8 pence. (Penceis the plural of penny.) The new monetary system, introduced in the 1950s, consists of only pounds and pence, with 100 pence to a pound (like U.S. dollars and cents). We'll call this new system decimal pounds. Thus £5.2.8 in the old notation is £5.13 in decimal pounds (actually £5.1333333). Write a program to convert the old pounds-shillings-pence format to decimal pounds. An example of the user's interaction with the program would be
Enter pounds: 7
Enter shillings: 17
Enter pence: 9
Decimal pounds = £7.89
In most compilers you can use the decimal number 156 (hex character constant ‘\x9c') to represent the pound sign (£). In some compilers, you can put the pound sign into your program directly by pasting it from the Windows Character Map accessory.

Exercise 12

Write the inverse of Exercise 10, so that the user enters an amount in Great Britain's new decimal-pounds notation (pounds and pence), and the program converts it to the old pounds-shillings-pence notation. An example of interaction with the program might be
Enter decimal pounds: 3.51
Equivalent in old notation = £3.10.2.
Make use of the fact that if you assign a floating-point value (say 12.34) to an integer variable, the decimal fraction (0.34) is lost; the integer value is simply 12. Use a cast to avoid a compiler warning. You can use statements like
float decpounds; // input from user (new-style pounds)
int pounds; // old-style (integer) pounds
float decfrac; // decimal fraction (smaller than 1.0)
pounds = static_cast(decpounds); // remove decimal fraction
decfrac = decpounds - pounds; // regain decimal fraction
You can then multiply decfrac by 20 to find shillings. A similar operation obtains pence.

Exercise 11

Create a three-function calculator for old-style English currency, where money amounts are specified in pounds, shillings, and pence. (See Exercises 10 and 12 in Chapter 2.) The calculator should allow the user to add or subtract two money amounts, or to multiply a money amount by a floating-point number. (It doesn't make sense to multiply two money amounts; there is no such thing as square money. We'll ignore division. Use the general style of the ordinary four-function calculator in Exercise 4 in this chapter.)

Exercise 4

Create the equivalent of a four-function calculator. The program should ask the user to enter a number, an operator, and another number. (Use floating point.) It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select the operation. Finally, display the result.
When it finishes the calculation, the program should ask whether the user wants to do another calculation. The response can be ‘y' or ‘n'. Some sample interaction with the program might look like this:
Enter first number, operator, second number: 10 / 3
Answer = 3.333333
Do another (y/n)? y
Enter first number, operator, second number: 12 + 100
Answer = 112
Do another (y/n)? n

Reference no: EM131076056

Questions Cloud

Program that manipulates a database of product records : Quantity: one int = 8 bytes The program should allow the user to add a record, ?nd a record that matches a product name, and change the price and quantity of a product by a given amount. P19
Qualitatively analyze fedex and ups : Familiarize with the format of 10-Q and 10-K. Analyze the Management Discussion and Analysis, auditor's report, financial statements and notes to financial statements.
Systems development life cycle : What is a systems development life cycle and What are some of the predictive models associated with the systems development life cycle? APA Format with 2 minimum references.
Privacy of medical and financial records : How would you allow people to maintain the privacy of their medical and financial records, while making it possible for companies to advertise products that are interesting and useful to consumers? Where would you draw the dividing line between wh..
Create the equivalent of a four-function calculator : Create a three-function calculator for old-style English currency, where money amounts are specified in pounds, shillings, and pence.
Consumer of goods or services : Whether you work in the IT field or you use a company's technology solution as a consumer of its goods or services, it is likely that you have experienced the result of an enterprise application integration (EAI) project.
Briefly discuss two limitations of specific factors model : Briefly discuss two limitations of the Specific Factors Model? In what ways does the Hecksher-Ohlin Model complement tire Specific Factors Model
What is the value of one wheat futures contract : A wheat futures contract is quoted in cents per bushel with a contract unit of 5,000 bushels. If the contract is quoted at a settle price of 529, then What is the value of one wheat futures contract?
Focus on a strategy : For this assignment, you will be asked to to focus on a strategy: The College of IST wants to increase its enrollment by a total of 20% within the next three years. It wants you to provide ideas for each of Porter's four strategies.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Identifies the cost of computer

identifies the cost of computer components to configure a computer system (including all peripheral devices where needed) for use in one of the following four situations:

  Input devices

Compare how the gestures data is generated and represented for interpretation in each of the following input devices. In your comparison, consider the data formats (radio waves, electrical signal, sound, etc.), device drivers, operating systems suppo..

  Cores on computer systems

Assignment : Cores on Computer Systems:  Differentiate between multiprocessor systems and many-core systems in terms of power efficiency, cost benefit analysis, instructions processing efficiency, and packaging form factors.

  Prepare an annual budget in an excel spreadsheet

Prepare working solutions in Excel that will manage the annual budget

  Write a research paper in relation to a software design

Research paper in relation to a Software Design related topic

  Describe the forest, domain, ou, and trust configuration

Describe the forest, domain, OU, and trust configuration for Bluesky. Include a chart or diagram of the current configuration. Currently Bluesky has a single domain and default OU structure.

  Construct a truth table for the boolean expression

Construct a truth table for the Boolean expressions ABC + A'B'C' ABC + AB'C' + A'B'C' A(BC' + B'C)

  Evaluate the cost of materials

Evaluate the cost of materials

  The marie simulator

Depending on how comfortable you are with using the MARIE simulator after reading

  What is the main advantage of using master pages

What is the main advantage of using master pages. Explain the purpose and advantage of using styles.

  Describe the three fundamental models of distributed systems

Explain the two approaches to packet delivery by the network layer in Distributed Systems. Describe the three fundamental models of Distributed Systems

  Distinguish between caching and buffering

Distinguish between caching and buffering The failure model defines the ways in which failure may occur in order to provide an understanding of the effects of failure. Give one type of failure with a brief description of the failure

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd