Menu
CoddyTech

移植:库存命令

Coddy 面向 C++ 开发者的 Swift 课程 课程的第 47 课,共 48 课。

challenge icon

挑战

中等

将 C++ 参考代码移植到 Swift。实现 Inventory(),包含 add(_ name: String, count: Int) -> Bool、take(_ name: String, count: Int) -> Bool、quantity(of name: String) -> Int 以及 report() -> String。add 仅接受正数的 count。take 仅接受正数且不超过当前库存的 count。被拒绝的操作会保留所有状态;未知的数量为 0。成功的 take 操作会保留库存为 0 的条目。按名称升序报告所有存储的名称,格式为 name:quantity,用空格分隔。输入包含 0 到 40 个命令三元组:add name count 或 take name count。名称为最多 10 个字母的小写 ASCII 单词,count 范围为 -10 到 100。输出成功的命令数量,仅当 report 非空时,在其后跟一个空格和 report。

实现所需的 API 和 solve(_ input: String) -> String。返回输出文本,末尾不带换行符;不要在可编辑的代码中进行打印。被锁定的测试驱动程序会在其他有效输入上独立检查这些 API,然后打印返回的文本,末尾恰好带有一个换行符。所有数值运算均在 Int 范围内。

C++ 参考内在一个类值中拥有一个有序 map;对该值的引用会共享其状态,而复制拥有 map 的对象则会创建一个独立的值。在你的 Swift 实现中,请确保拒绝时不修改状态,并保留库存为零的条目。

满足上述输入/输出约定的完整 C++ 参考实现。请将此行为移植到所需的 Swift 文件中:

#include <iostream>
#include <string>
#include <map>
#include <sstream>
class Inventory {
    std::map<std::string, int> quantities;
public:
    int quantity(const std::string &name) const {
        auto found = quantities.find(name);
        return found == quantities.end() ? 0 : found->second;
    }
    bool add(const std::string &name, int count) {
        if (count <= 0) return false;
        quantities[name] = quantity(name) + count;
        return true;
    }
    bool take(const std::string &name, int count) {
        int available = quantity(name);
        if (count <= 0 || count > available) return false;
        quantities[name] = available - count;
        return true;
    }
    std::string report() const {
        std::ostringstream output;
        bool first = true;
        for (const auto &[name, value] : quantities) {
            if (!first) output << ' ';
            first = false;
            output << name << ':' << value;
        }
        return output.str();
    }
};
int main() {
    Inventory inventory;
    std::string command, name;
    int count, accepted = 0;
    while (std::cin >> command >> name >> count) {
        bool ok = command == "add" ? inventory.add(name, count) : inventory.take(name, count);
        if (ok) ++accepted;
    }
    std::string report = inventory.report();
    std::cout << accepted;
    if (!report.empty()) std::cout << ' ' << report;
    std::cout << '\n';
}

你的代码应放在 Inventory.swift 和 Functions.swift 中。main.swift 包含提供的输入/输出代码,无法编辑。

亲手试试

import Foundation
func apiChecks(_ input: String) -> Bool {
let x = Inventory(); guard x.quantity(of: "missing") == 0 && !x.take("missing", count: 1) && x.report().isEmpty else { return false }; guard x.add("oak", count: 4) && !x.take("oak", count: 5) && !x.add("oak", count: 0) && x.quantity(of: "oak") == 4 else { return false }; return x.take("oak", count: 4) && x.report() == "oak:0" && !x.take("oak", count: -1) && Inventory().report().isEmpty
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// 公共 API 探测开始
let apiOK = apiChecks(input)
// 公共 API 探测结束
if apiOK { print(solve(input)) } else { print("API mismatch") }

面向 C++ 开发者的 Swift 课程中的所有课

自己练习: Swift 在线编辑器