代写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:
struct item {
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
};
—|—
Use the following vector declaration for the vending machine:
int main() {
vector
—|—
Complete the three following function headers shown below:
float totalValue(vector
// return the total value of all items in the vending machine
—|—
void stock(string name, int count, vector
// 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
—|—
void purchase(float balance, int code, vector
// 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
int main() {
vector<item> vendingMachine;
stock("Doublemint Gum", 1, vendingMachine);
cout << "-------------------" << endl;
purchase(.50, 100, vendingMachine);
cout << "-------------------" << endl;
stock("Chocolate Chip Cookies", 10, vendingMachine);
cout << "-------------------" << endl;
stock("Chocolate Chip Cookies", 5, vendingMachine);
cout << "-------------------" << endl;
purchase(.75, 100, vendingMachine);
cout << "-------------------" << endl;
purchase(1.00, 100, vendingMachine);
cout << "This vending machine now has $" totalValue(vendingMachine) << fixed << setprecision(2) << " worth of items" << endl;
}
—|—
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:
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.