הסבה: פקודות מלאי
שיעור 47 מתוך 48 בקורס Swift למפתחי ++C של Coddy.
אתגר
בינוניהעבירו את מימוש הייחוס ב־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. הפלט הוא מספר הפקודות שהצליחו, ואחריו רווח ודוח רק אם הוא אינו ריק.
ממשו את ממשקי ה־API הנדרשים ואת solve(_ input: String) -> String. החזירו את טקסט הפלט בלי ירידת שורה בסוף; אל תדפיסו מתוך הקוד שניתן לעריכה. מנהל התוכנית הנעול בודק באופן עצמאי את ממשקי ה־API על קלטים תקינים נוספים, ואז מדפיס את הטקסט שהוחזר עם ירידת שורה אחת בדיוק. כל הפעולות המספריות נשארות בגבולות של Int.
מימוש הייחוס ב־C++ מחזיק מפה ממוינת בתוך ערך של מחלקה; הפניות לערך הזה חולקות את מצבו, ואילו העתקה של האובייקט שמחזיק את המפה תיצור ערך עצמאי. שמרו במימוש 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
4פונקציות ומערכים
פונקציות ותוויותInout וגישה בלעדיתטאפלים ותוצאות מרובותלמערכים יש סמנטיקה של ערךסיכום: צעדים עוליםתרגלו בעצמכם: Swift אונליין