首页 > > 详细

辅导CMPSC121 Structs辅导留学生C/C++

C++,Struct。

Objectives

After this lab assignment, students should be able to:

  • Declare and initialize C++ structs
  • Implement designs using structs

Instructions

In this lab, we will create an abstraction of a vending machine in C++, using structs to describe each item in the machine, and a vector to maintain a list of all the items in the machine.
Use the following struct declaration for vending machine items:

1
2
3
4
5
6
struct item #123;
string name; // name of the item
int code; // numerical code entered to select the item
float price; // price per unit of the item
int stock; // number of items available in the machine
#125;;

 

Use the following vector declaration for the vending machine:

1
2
int main() #123;
vectorlt;itemgt; vendingMachine;

 

Complete the three following function headers shown below:

1
2
float totalValue(vectorlt;itemgt; vendingMachine)
// return the total value of all items in the vending machine

 

1
2
3
4
void stock(string name, int count, vectorlt;itemgt; amp;vendingMachine)
// add "count" items to the vending machine with the given "name"
// if the item to be added was not already in the machine, prompt the user for its code and price (in dollars)
// if the code is already being used by the machine, prompt the user for a different code

1
2
3
4
5
6
7
void purchase(float balance, int code, vectorlt;itemgt; amp;vendingMachine)
// make a purchase from the vending machine
// "balance" is the amount of money (in dollars) inserted
// "code" is the item to purchase
// if "code" is not mapped to an item, display error message
// if the item is more expensive than "balance", display error message
// if purchase is successful, display change to be returned in dollars and update the stock of that item.

Testing the Functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() #123;
vectorlt;itemgt; vendingMachine;
stock("Doublemint Gum", 1, vendingMachine);
cout lt;lt; "-------------------" lt;lt; endl;
purchase(.50, 100, vendingMachine);
cout lt;lt; "-------------------" lt;lt; endl;
stock("Chocolate Chip Cookies", 10, vendingMachine);
cout lt;lt; "-------------------" lt;lt; endl;
stock("Chocolate Chip Cookies", 5, vendingMachine);
cout lt;lt; "-------------------" lt;lt; endl;
purchase(.75, 100, vendingMachine);
cout lt;lt; "-------------------" lt;lt; endl;
purchase(1.00, 100, vendingMachine);
cout lt;lt; "This vending machine now has $" totalValue(vendingMachine) lt;lt; fixed lt;lt; setprecision(2) lt;lt; " worth of items" lt;lt; endl;
#125;

Sample Output

The item you are adding, Doublemint Gum, was not in the machine. Please enter the following information for this item Code: 100 Price: $0.75 You have added 1 units of Doublemint Gum. Now there are 1 units in the machine ------------------ You have selected to purchase Doublemint Gum, which costs $0.75 Your balance is $0.50. Not enough money! ------------------ The item you are adding, Chocolate Chip Cookies, was not in the machine. Please enter the following information for this item Code: 100 That code is already being used by Doublemint Gum. Enter a new code: 101 Price: $2.00 You have added 10 units of Chocolate Chip Cookies. Now there are 10 units in the machine ------------------ You have added 5 units of Chocolate Chip Cookies. Now there are 15 units in the machine ------------------ You have selected to purchase Doublemint Gum, which costs $0.75 Thank you for purchasing! Your change is: $0.00 ------------------ Item not found! ------------------ This vending machine now has $30.00 worth of items Press any key to continue ... 

Submission

Call your program VendingMachine.cpp. This file must contain all of the following items:

  • Completed function definitions:
    • totalValue()
    • purchase()
    • stock()
  • Declaration of struct item
  • main() function with same code found in the “Testing the Functions” section above
  • A sample output of your program, like shown in the “Sample Output” section above (the output does not have to match exactly)

Submit VendingMachine.cpp to Canvas before the deadline.

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

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