Menu
CoddyTech

نقل: أوامر المخزون

الدرس 47 من 48 في دورة Swift لمطوري C++ على Coddy.

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 الأعداد الموجبة فقط. تقبل take الأعداد الموجبة فقط بما لا يتجاوز المخزون الحالي على الأكثر. تحافظ عمليات الرفض على كامل الحالة؛ والكميات غير المعروفة تكون صفرًا. تحتفظ عمليات take الناجحة بالعناصر ذات المخزون الصفري. قم بتقديم تقرير بجميع الأسماء المخزنة بترتيب تصاعدي كـ name:quantity تفصل بينها مسافات. يحتوي المدخل على 0 إلى 40 ثلاثية أوامر: add name count أو take name count. الأسماء هي كلمات ASCII بأحرف صغيرة تصل إلى 10 أحرف، والعدادات من -10 إلى 100. قم بإخراج عدد الأوامر الناجحة، متبوعًا بمسافة والتقرير فقط عندما لا يكون فارغًا.

قم بتنفيذ واجهات برمجة التطبيقات (APIs) المطلوبة و solve(_ input: String) -> String. أرجع نص المخرجات بدون سطر جديد في النهاية؛ لا تطبع في الكود القابل للتحرير. يقوم الموجه المغلق بالتحقق بشكل مستقل من APIs على مدخلات صالحة إضافية، ثم يطبع النص المرجع مع سطر جديد واحد بالضبط. تبقى جميع العمليات العددية ضمن حدود Int.

يمتلك مرجع C++ خريطة مرتبة (ordered map) داخل قيمة صنف (class value)؛ تشترك المراجع إلى تلك القيمة في حالتها، بينما سيؤدي نسخ الكائن الذي يمتلك الخريطة إلى إنشاء قيمة مستقلة. احرص على الحفاظ على الرفض دون تعديل وعلى المدخلات ذات المخزون الصفري المتبقية في تنفيذك بلغة 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") }

جميع دروس Swift لمطوري C++

تدرّب بنفسك: Playground لـ Swift