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

  Write program that prompts user to pick either seat or price

Write a program that prompts users to pick either a seat or a price. Mark sold seats by changing the price to 0. When a user specifies a seat, make sure it is available.

  Stereotype entity classes

Research and explain the three stereotype entity classes: Boundary Class, ControlClass, and Entity Classes. Show the graphical notations and provide exemplary diagrams. Finally, describe their roles in system design.

  Why a computer would have difficulty answering your question

Fully state and justify any choices, assumptions or claims that you make using the suggested Learning Resources for this Week and/or your own research.

  Discuss the different program errors that likely to occure

discuss the different program errors that are likely to occur and how they should be handled within scripts

  Operate an apache-based linux web server

29. Asume you operate an Apache-based Linux Web server that hosts your company's e-commerce site. Assume further that there is a worm called "WorminatorX," which exploits a (fictional) buffer overflow bug in the Apache Web server package that can res..

  Understanding of natural monopolies

Based on your understanding of "natural monopolies," do you think the cable industry today should be considered a natural monopoly? Why or why not? (Based on your reading, research and analysis in the textbook, and other relevant sources)

  Efforts to increase consumer awareness of products

Use of digital media within organizations has transformed how organizations communicate with prospective and current customers.

  Would that be a live acquisition, or a postmortem one

Consider a local area network, attached to the Internet, with email capabilities provided to LAN users. What are some of the capabilities already built in to these systems that might aid the investigator? How might you put those to use? Would that be..

  A candle maker produces jar candles

1. A candle maker produces jar candles that have a label weight of 20.4 oz. Assume that the distribution of the weights of these candles is N (21.37,0.16). Let X denote the weight of a candle selected at random from the production line.(a)  Let X bar..

  Do the results match the expected linear behavior

The first program should acquire (say) 40,001 write locks on a file. These locks are placed on alternating bytes of the file; that is, locks are placed on bytes 0, 2, 4, 6, and so on through to (say) byte 80,000. Having acquired these locks, the p..

  Identify three different types of analysis models

Identify three different types of analysis models (analytical, simulation-based, and surrogate models) that can be used to represent the weight-holding capacity of the table as a function of the geometrical variables.

  What is the present worth of the savings

what is the present worth of the savings (in $/acre-ft) to the utility between the old and the new contracts? Let the interest rate equal 6% per year.

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