题解 | #自动售货系统#
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int balance = 0;
class Goods {
private:
string name;
int price;
int num;
public:
Goods(string name, int price, int num) : name(name), price(price), num(num) {};
void setnum(const int newnum) {
num = newnum;
}
void view() {
cout << name << " " << price << " " << num << endl;
}
bool isNull() {
if (num == 0)return true;
else return false;
}
int showprice() {
return price;
}
};
void init(const string& str, Goods* good, map<int, int>& money) {
string token, s;
vector<string>tokens;
istringstream tokenStream(str);
int i = 0;
while (getline(tokenStream, token, ' ')) {
istringstream ss(token);
while (getline(ss, s, '-')) {
int k = stoi(s);
if (i <= 5)good[i].setnum(k);
else if (i == 6)money[1] = k;
else if (i == 7)money[2] = k;
else if (i == 8)money[5] = k;
else if (i == 9)money[10] = k;
else break;
i++;
}
}
cout << "S001:Initialization is successful" << endl;
}
void query(const string& str, Goods* good, map<int, int>& money) {
if (str[0] == '0') {
for (int i = 0; i < 6; i++) {
good[i].view();
}
}
if (str[0] == '1') {
for (const auto& pair : money) {
cout << pair.first << "yuan coin number= " << pair.second << endl;
}
}
else cout<<"E010:Parameter error"<<endl;
}
void change(Goods* good, map<int, int>& money) {
int one = 0, two = 0, five = 0, ten = 0;
if (balance == 0)cout << "E009:Work failure" << endl;
else {
while (balance) {
if (balance >= 10 && money[10]) {
ten++;
money[10]--;
balance -= 10;
} else if (balance >= 5 && money[5]) {
five++;
money[5]--;
balance -= 5;
} else if (balance >= 2 && money[2]) {
two++;
money[2]--;
balance -= 2;
} else if (balance >= 1 && money[1]) {
one++;
money[1]--;
balance -= 1;
} else break;
}
cout << "1 yuan coin number=" << one << endl;
cout << "2 yuan coin number=" << two << endl;
cout << "5 yuan coin number=" << five << endl;
cout << "10 yuan coin number=" << ten << endl;
}
}
void pay(const string& str, Goods* good, map<int, int>& money) {
int k = stoi(str);
if (k != 5 && k != 1 && k != 2 && k != 10) {
cout << "E002:Denomination error" << endl;
return;
}
if (k == 5 || k == 10) {
if (money[1] + money[2] * 2 < k) {
cout << "E003:Change is not enough, pay fail" << endl;
return;
}
}
if (good[0].isNull() && good[1].isNull() && good[2].isNull() &&
good[3].isNull() && good[4].isNull() && good[5].isNull()) {
cout << "E005:All the goods sold out" << endl;
return;
}
money[k]++;
balance += k;
cout << "S002:Pay success,balance=" << balance << endl;
}
void buy(const string& str, Goods* good, map<int, int>& money) {
int k = stoi(string(1, str[1]));
if (str != "A1" && str != "A2" && str != "A3" && str != "A4" && str != "A5" &&
str != "A6")cout << "E006:Goods does not exist" << endl;
else if (good[k - 1].isNull())cout << "E007:The goods sold out" << endl;
else if (balance < good[k - 1].showprice())cout << "E008:Lack of balance" <<
endl;
else {
balance -= good[k - 1].showprice();
cout << "S003:Buy success,balance=" << balance << endl;
}
}
int main() {
string str;
map<int, int>money;
Goods good[6] = {
Goods("A1", 2, 0),
Goods("A2", 3, 0),
Goods("A3", 4, 0),
Goods("A4", 5, 0),
Goods("A5", 8, 0),
Goods("A6", 6, 0)
};
money = {
{1, 0},
{2, 0},
{5, 0},
{10, 0}
};
while (getline(cin, str, ';')) {
if (str[0] == 'r')init(str.substr(2), good, money);
if (str[0] == 'q')query(str.substr(2), good, money);
if (str[0] == 'c')change(good, money);
if (str[0] == 'b')buy(str.substr(2), good, money);
if (str[0] == 'p')pay(str.substr(2), good, money);
}
}

