题解 | #自动售货系统#

自动售货系统

https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//思路:用类来实现。
//需要哪些数据,用什么类型存储,除此之外,还要方便方法对数据进行操控
//1、商品:要保证顺序,用二维数组,6*3的vector来存储:名称、价值、数量
//2、钱币:要保证顺序,用二维数组,4*2的vector来存储:面额、数量
//3、投币余额:int balance
//(可选)存钱盒1元和2元的总额:int sum12,直接计算也行,就不单独封装方法了。
//(可选)商品是否为空,一个标志bool
//需要哪些方法。
//1、命令初步处理,处理过的命令判断,调用哪个函数
//2、商品和钱币初始化
//3、投币:需要记录存钱盒1元和2元的总额
//4、退币:遵循原则
//5、购买商品
//6、查询

//分功能编写,结构相对清晰。如有帮助,可以点赞。
bool Compare(const vector<int>& a, const vector<int>& b) {
    return a[2] > b[2];
}
class SellSystrm {
  private:
    vector<vector<int>> goods = {{1, 2, 0}, {2, 3, 0}, {3, 4, 0}, {4, 5, 0}, {5, 8, 0}, {6, 6, 0}};
    vector<vector<int>> currency = {{1, 0}, {2, 0}, {5, 0}, {10, 0}};
    int balance = 0;
    int sum12 = 0;
  public:
    void Processing();
    void Initialize(const vector<int>& a, const vector<int>& b);
    void Push(int money);
    void Pop();
    void Buy(int item);
    void Inquire();
};
int main() {
    SellSystrm ss;
    ss.Processing();
}
void SellSystrm::Processing() {
    char ch;
    while (cin >> ch) {
        if (ch == 'r') {
            vector<int> a(6), b(4);
            for (int i = 0; i < 6; i++) {
                (cin >> a[i]).get();
            }
            for (int i = 0; i < 4; i++) {
                (cin >> b[i]).get();
            }
            Initialize(a, b);
        } else if (ch == 'p') {
            int money;
            (cin >> money).get();
            Push(money);
        } else if (ch == 'c') {
            cin.get();
            Pop();
        } else if (ch == 'b') {
            int item;
            (cin >> ch >> item).get();
            Buy(item);
        } else if (ch == 'q') {
            Inquire();
        }
    }
}
void SellSystrm::Initialize(const vector<int>& a, const vector<int>& b) {
    for (int i = 0; i < 6; i++) {
        goods[i][2] = a[i];
    }
    for (int i = 0; i < 4; i++) {
        currency[i][1] = b[i];
    }
    cout << "S001:Initialization is successful" << endl;
}
void SellSystrm::Push(int money) {
    auto it = find_if(currency.begin(),
    currency.end(), [money](const vector<int>& v) {
        return money == v[0];
    });
    if (it == currency.end()) {
        cout << "E002:Denomination error" << endl;
    } else {
        int sum = currency[0][0] * currency[0][1] + currency[1][0] * currency[1][1];
        if (money != 1 && money != 2 && sum < money) {
            cout << "E003:Change is not enough, pay fail" << endl;
        } else {
            bool flag = true;
            for (auto& i : goods) {
                if (i[2]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                cout << "E005:All the goods sold out" << endl;
            } else {
                balance += money;
                (*it)[1]++;
                cout << "S002:Pay success,balance=" << balance << endl;
            }
        }
    }
}
void SellSystrm::Pop() {
    if (balance == 0) {
        cout << "E009:Work failure" << endl;
    } else {
        vector<vector<int>> res = {{1, 0}, {2, 0}, {5, 0}, {10, 0}};
        for (int i = 3; i >= 0; i--) {
            res[i][1] = min(currency[i][1], balance / currency[i][0]);
            currency[i][1] -= res[i][1];
            balance -= res[i][0] * res[i][1];
        }
        balance = 0;
        for (int i = 0; i < 4; i++) {
            cout << res[i][0] << " yuan coin number=" << res[i][1] << endl;
        }
    }
}
void SellSystrm::Buy(int item) {
    bool flag = true;
    int index = 0;
    for (int i = 0; i < 6; i++) {
        if (item == goods[i][0]) {
            flag = false;
            index = i;
            break;
        }
    }
    if (flag) {
        cout << "E006:Goods does not exist" << endl;
    } else {
        if (goods[index][2] == 0) {
            cout << "E007:The goods sold out" << endl;
        } else {
            if (balance < goods[index][1]) {
                cout << "E008:Lack of balance" << endl;
            } else {
                goods[index][2]--;
                balance -= goods[index][1];
                cout << "S003:Buy success,balance=" << balance << endl;
            }
        }
    }
}
void SellSystrm::Inquire() {
    if (cin.get() != ' ') {
        cout << "E010:Parameter error" << endl;
    } else {
        int type;
        (cin >> type).get();
        if (type == 0) {
            vector<vector<int>> temp(goods);
            stable_sort(temp.begin(), temp.end(), Compare);
            for (int i = 0; i < 6; i++) {
                cout << "A" << goods[i][0] << " " << goods[i][1] << " " << goods[i][2] << endl;
            }
        } else if (type == 1) {
            for (int i = 0; i < 4; i++) {
                cout << currency[i][0] << " yuan coin number=" << currency[i][1] << endl;
            }
        } else {
            cout << "E010:Parameter error" << endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务