题解 | #自动售货系统#
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
oop
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
class RendMach {
public:
void init(const string& cmd) {
stringstream ss(cmd);
char r;
char sbl;
ss >> r >> m_product["A1"][1] >> sbl >> m_product["A2"][1] >> sbl
>> m_product["A3"][1] >> sbl >> m_product["A4"][1] >> sbl
>> m_product["A5"][1] >> sbl >> m_product["A6"][1]
>> m_moneyCnt[1] >> sbl >> m_moneyCnt[2] >> sbl
>> m_moneyCnt[5] >> sbl >> m_moneyCnt[10];
cout << "S001:Initialization is successful" << endl;
}
void coinIn(const string& cmd) {
stringstream ss(cmd);
char p;
int page;
ss >> p >> page;
if (page != 1 && page != 2 && page != 5 && page != 10) {
cout << "E002:Denomination error" << endl;
return;
}
if (m_moneyCnt[1] + m_moneyCnt[2] * 2 < page) {
cout << "E003:Change is not enough, pay fail" << endl;
return;
}
bool flag = false;
for (auto pdct : m_product) {
if (pdct.second[1] > 0) flag = true;
}
if (!flag) {
cout << "E005:All the goods sold out" << endl;
return;
}
++m_moneyCnt[page];
m_inBalance += page;
cout << "S002:Pay success,balance=" << m_inBalance << endl;
return;
}
void buy(const string& cmd) {
stringstream ss(cmd);
char b;
string name;
ss >> b >> name;
if (m_product[name].empty()) {
cout << "E006:Goods does not exist" << endl;
return;
}
if (m_product[name][1] == 0) {
cout << "E007:The goods sold out" << endl;
return;
}
if (m_inBalance < m_product[name][0]) {
cout << "E008:Lack of balance" << endl;
return;
}
--m_product[name][1];
m_inBalance -= m_product[name][0];
cout << "S003:Buy success,balance=" << m_inBalance << endl;
return;
}
void coinOut() {
if (m_inBalance == 0) {
cout << "E009:Work failure" << endl;
return;
}
vector<int> outCoin(4, 0);
while (m_inBalance >= 10) {
if (m_moneyCnt[10] == 0) break;
--m_moneyCnt[10];
++outCoin[3];
m_inBalance -= 10;
}
while (m_inBalance >= 5) {
if (m_moneyCnt[5] == 0) break;
--m_moneyCnt[5];
++outCoin[2];
m_inBalance -= 5;
}
while (m_inBalance >= 2) {
if (m_moneyCnt[2] == 0) break;
--m_moneyCnt[2];
++outCoin[1];
m_inBalance -= 2;
}
while (m_inBalance >= 1) {
if (m_moneyCnt[1] == 0) break;
--m_moneyCnt[1];
++outCoin[0];
m_inBalance -= 1;
}
cout << "1 yuan coin number=" << outCoin[0] << endl;
cout << "2 yuan coin number=" << outCoin[1] << endl;
cout << "5 yuan coin number=" << outCoin[2] << endl;
cout << "10 yuan coin number=" << outCoin[3] << endl;
}
void query(const string& cmd) {
if (cmd.size() != 3 || cmd[1] != ' ' || (cmd[2] != '0' && cmd[2] != '1')) {
cout << "E010:Parameter error" << endl;
return;
}
else if (cmd[2] == '0') {
vector<pair<string, vector<int>>> info;
for (auto p : m_product) {
info.push_back(p);
}
sort(info.begin(), info.end(), [](const pair<string, vector<int>>& a, const pair<string, vector<int>>& b) {
if (a.second[1] > b.second[1]) {
return true;
}
else if (a.second[1] == b.second[1] && a.first < b.first) {
return true;
}
else return false;
});
for (auto res : info) {
cout << res.first << ' ' << res.second[0] << ' ' << res.second[1] << endl;
}
}
else if (cmd[2] == '1') {
for (auto p : m_moneyCnt) {
cout << p.first << " yuan coin number=" << p.second << endl;
}
}
}
private:
int m_inBalance = 0;
unordered_map<string, vector<int>> m_product = {
{"A1", {2, 0}},
{"A2", {3, 0}},
{"A3", {4, 0}},
{"A4", {5, 0}},
{"A5", {8, 0}},
{"A6", {6, 0}}
};
unordered_map<int, int> m_moneyCnt = {{1, 0}, {2, 0}, {5, 0}, {10, 0}};
};
int main() {
string str;
RendMach render;
while(getline(cin, str, ';')) {
if (str[0] == 'r') {
render.init(str);
}
else if (str[0] == 'p') {
render.coinIn(str);
}
else if (str[0] == 'b') {
render.buy(str);
}
else if (str[0] == 'c') {
render.coinOut();
}
else if (str[0] == 'q') {
render.query(str);
}
}
}
格力公司福利 425人发布