首页 > > 详细

辅导留学生C/C++Programming 设计、C/C++设计辅导、辅导C/C++编程

Homework 2
1. (50 points) Making Change. Write a class, called Coins, that is a holder for a variety
of coins (similar to a pocket or coin purse). This has no way to split coins or to receive
change back from a transaction, so it must pay with exact change. I wrote a simple
main, put in a file called simple_main.cpp, for you to use to test this first part of the
homework assignment. Note you are also writing the inserter
using namespace std;
#include "Coins.h"
const int CENTS_FOR_CANDYBAR = 482;

int main()
{
/// The first line creates a Coins object called 'pocket.'
Coins pocket( 100, 10, 10, 100 );
cout << "I started with " << pocket << " in my pocket" << endl;
/// This line creates a Coins object called payForCandy and initializes it.
Coins payForCandy =
pocket.extract_exact_change( coins_required_for_cents(CENTS_FOR_CAN
DYBAR) );
cout << "I bought a candy bar for " << CENTS_FOR_CANDYBAR
<< " cents using " << payForCandy << endl;
cout << "I have " << pocket << " left in my pocket" << endl;
return 0;
}


2. (20 points) Write a better main, in a file named coins_main.cpp, that does the
following scenario:
o Create two Coins objects:
pocket: 5 q, 3 d, 6 n, 8 p
piggyBank: 50 q, 50 d, 50 n, 50 p
o Buy a bag of chips that costs 68 cents taking the coins from your pocket.
Display what's left in your pocket.
o Transfer $2.05 from your piggyBank to your pocket. Display how much you
now have in both.
o While vacuuming, you find loose change in your sofa 10q, 10d, 10n, 10p.
Put it in your piggyBank. Display how much you now have in your piggyBank.
Use the format $xx.xx
o To enter the amount found in your sofa, create an object with the number of
coins found, then call deposit_coins.

3. (30 points) Write a third main, in a file called coins_menu.cpp, that presents a (text-
based) menu interface to your program that allows the user to deposit change, extract
change, and to print the balance in a single coins object called myCoins. Be sure you
don't allow someone to extract more money than they have. (Yes, I know this is
probably trivial for most of you, but I want you to get some practice with the C++ while
and switch statements).

Use the debugger while developing your program to help you become more familiar with
the debugger and to help you track down errors. You may use any debugger that
comes with your compiler. If you are using g++, gdb is the normal debugger to use with
it. You must compile with the option -ggdb first. Here is a tutorial on how to use gdb.

What to submit: create a zip, called hw2.zip, of your Coins.h, simple_main.cpp,
coins_main.cpp, and coins_menu.cpp. Write a report, called report.pdf, showing
screenshots of your programs in part 1 and part 2 compile and run. For part 3, repeat
the scenario in Part 2, but do it interactively by selecting the menu options of your menu
interface. Submit these two files to EEE dropbox using the standard names described
in HW1.


 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!