Madam Phoenix is moving to the southwest to open
a ``Fun In The Sun" store selling sunglasses, sunscreen, and other
such items. She's decided to employ you to write an inventory program
for her new store. Here's how it will work.
Each ``activity" your program is to process will appear as a separate
line in the input file. The end of the input is marked by a line
containing an asterisk in column one; no other activity lines will be
so marked. Activity lines begin with a lower-case keyword identifying
the action to be performed. The names of the items in her inventory
are case sensitive, and each contains no more than ten non-blank
characters. All fields in the activity lines are separated by blanks, and
Madam Phoenix guarantees you that there will be no errors in the
input. Here are the various types of activity lines your program is to
process.
- new item-name item-cost item-selling-price
-
This line adds a new item (not previously carried in the store)
to the potential inventory. The item-cost and
item-selling-price
are given as normal dollar amounts, without
the dollar sign. That is, they will contain one or more
decimal
digits, a decimal point, and two more decimal digits. Note
that
this activity line doesn't actually result in a change in the
inventory, but is used in anticipation of adding units of the new
item
to the store's offerings. item-cost is what Madam Phoenix pays
for each unit of the item, and item-selling-price is the
price for which she sells the item. There will be no more than
100 total item-names ever included in the list of items.
item-cost and item-selling-price will never be larger than 100.00.
- delete item-name
-
If an item isn't selling well, Madam Phoenix can remove it from
the inventory by including this line in the program input. All
units of item-name in the inventory are written off as a loss.
- buy item-name quantity
-
When Madam Phoenix buys some units (at the unit-cost, previously indicated) of an item-name to offer for sale, she'll
indicate that with one of these lines in the pregrain input.
quantity indicates the number of units she purchased. The
quantity she purchases will never be larger than 5000 at a time,
but the number of units in the inventory may be as large as
10,000.
- sell item-name quantity
-
When one or more units of an item are sold, that fact is recorded by
placing one of these lines in the input. quantity indicates
the number of units sold (at the item-selling-price previously indicated). Obviously, the quantity sold cannot exceed
the number of items in stock.
- report
-
This line in the input requests a report. This is the only input
line
for which output is expected. Your program will
display
columns, with suitable headings, showing item-name,
the
buying price, the selling price, the number of units in the
inventory,
and the value of the units in the inventory (that is, the
product of the number of units in the inventory and the
buying
price). The lines in the report should be sorted in
alphabetical
order on item name. Following the last item the total value
of
all units in the inventory should be displayed. Then finally, a
line
should appear showing the total profit since the last report
was
issued. Profit is defined as total sales, less the cost of
the
items sold, less the cost of items written off (by the
delete
activity). Print a blank line after every report. The sample
output shown illustrates the desired format for the report.
All numbers in the report must naturally
be exact.
new Shade01 0.50 3.79
new Towel01 1.47 6.98
new Shade02 0.63 4.29
new BluBlock 1.00 4.98
buy BluBlock 100
sell BluBlock 2
buy Towel01 500
buy Shade01 100
buy Shade02 100
sell Towel01 1
sell Towel01 1
sell BluBlock 2
report
delete Shade01
sell BluBlock 5
new Shade03 0.51 1.98
buy Shade03 250
sell Towel01 5
sell Shade03 4
sell Shade02 10
report
*
INVENTORY REPORT
Item Name Buy At Sell At On Hand Value
--------- ------ ------- ------- -----
BluBlock 1.00 4.98 96 96.00
Shade01 0.50 3.79 100 50.00
Shade02 0.63 4.29 100 63.00
Towel01 1.47 6.98 498 732.06
------------------------
Total value of inventory 941.06
Profit since last report 26.94
INVENTORY REPORT
Item Name Buy At Sell At On Hand Value
--------- ------ ------- ------- -----
BluBlock 1.00 4.98 91 91.00
Shade02 0.63 4.29 90 56.70
Shade03 0.51 1.98 246 125.46
Towel01 1.47 6.98 493 724.71
------------------------
Total value of inventory 997.87
Profit since last report 39.93
delete 比較特別,相當於整個貨物遺失,也就是按照成本直接扣除。其餘指令則是增添清單,以及進貨物個數,或者販賣某貨物。
#include <stdio.h>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
struct E {
double buy, sell;
int stock;
int on;
};
int main() {
map<string, E> R;
string cmd;
string name;
double profit = 0;
int x;
while(cin >> cmd) {
if(cmd == "*")
break;
if(cmd == "new") {
E tmp;
cin >> name >> tmp.buy >> tmp.sell;
tmp.on = 1, tmp.stock = 0;
R[name] = tmp;
} else if(cmd == "buy") {
cin >> name >> x;
R[name].stock += x;
} else if(cmd == "sell") {
cin >> name >> x;
R[name].stock -= x;
profit += x*(R[name].sell-R[name].buy);
} else if(cmd == "delete") {
cin >> name;
R[name].on = 0;
profit -= R[name].stock*R[name].buy;
R[name].stock = 0;
} else {
double inventory = 0;
puts(" INVENTORY REPORT");
puts("Item Name Buy At Sell At On Hand Value");
puts("--------- ------ ------- ------- -----");
for(map<string, E>::iterator it = R.begin();
it != R.end(); it++) {
if(it->second.on == 0) continue;
printf("%-12s%8.2lf%13.2lf%13d%13.2lf\n", it->first.c_str(), it->second.buy
, it->second.sell, it->second.stock, (it->second.stock)*(it->second.buy));
inventory += (it->second.stock)*(it->second.buy);
}
puts("------------------------");
printf("Total value of inventory %15.2lf\n", inventory);
printf("Profit since last report %15.2lf\n", profit);
profit = 0;
puts("");
}
}
return 0;
}
文章定位: