移植:库存命令
Coddy 面向 C 开发者的 Swift 课程的第 47 课,共 48 课。
挑战
中等将 C 语言参考实现移植到 Swift。实现 Inventory(),并提供 add(_ name: String, count: Int) -> Bool、take(_ name: String, count: Int) -> Bool、quantity(of name: String) -> Int 和 report() -> String。只有数量为正数时,add 才会接受。只有数量为正数且不超过当前库存时,take 才会接受。拒绝操作时要保留所有状态;未知物品的数量为 0。成功取出物品后,库存为 0 的条目仍要保留。报告按名称升序列出所有已存储的物品,格式为 name:quantity,各项之间用空格分隔。输入包含 0 到 40 组三元命令:add name count 或 take name count。名称为不超过 10 个字母的小写 ASCII 单词,数量范围为 -10 到 100。输出成功执行的命令数;仅当报告非空时,在其后添加一个空格和报告内容。
实现所需的 API 和 solve(_ input: String) -> String。返回不带末尾换行符的输出文本;不要在可编辑代码中打印内容。锁定的驱动程序会使用其他有效输入独立检查这些 API,然后打印返回的文本,并且恰好添加一个换行符。所有数值运算都不会超出 Int 的范围。
C 语言的 Inventory 会在结构体内存储有界条目,并通过指针更新原始数据。它会对临时数组排序以生成报告,不会改变已存储的状态。请保留所需的 Swift 引用类型 API,并确保拒绝命令后所有状态都保持不变。
完整的 C 语言参考程序。将 main.c 与 inventory.h 放在同一目录中并编译;该头文件提供唯一入口点所使用的实现。此实现已完整提供,可用于本次最终移植。
inventory.h
#ifndef INVENTORY_H
#define INVENTORY_H
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { char name[11]; int quantity; } Item;
typedef struct { Item items[40]; int size; } Inventory;
static int inventory_find(const Inventory *inventory, const char *name) {
for (int i = 0; i < inventory->size; ++i)
if (strcmp(inventory->items[i].name, name) == 0) return i;
return -1;
}
static int inventory_quantity(const Inventory *inventory, const char *name) {
int index = inventory_find(inventory, name);
return index < 0 ? 0 : inventory->items[index].quantity;
}
static bool inventory_add(Inventory *inventory, const char *name, int count) {
if (count <= 0) return false;
int index = inventory_find(inventory, name);
if (index < 0) {
index = inventory->size++;
strcpy(inventory->items[index].name, name);
inventory->items[index].quantity = 0;
}
inventory->items[index].quantity += count;
return true;
}
static bool inventory_take(Inventory *inventory, const char *name, int count) {
int available = inventory_quantity(inventory, name);
if (count <= 0 || count > available) return false;
inventory->items[inventory_find(inventory, name)].quantity = available - count;
return true;
}
static int item_compare(const void *left, const void *right) {
return strcmp(((const Item *)left)->name, ((const Item *)right)->name);
}
static void inventory_report(const Inventory *inventory, char output[1024]) {
Item sorted[40];
memcpy(sorted, inventory->items, (size_t)inventory->size * sizeof(Item));
qsort(sorted, (size_t)inventory->size, sizeof(Item), item_compare);
size_t used = 0;
output[0] = '\0';
for (int i = 0; i < inventory->size; ++i)
used += (size_t)snprintf(output + used, 1024 - used, "%s%s:%d", i ? " " : "", sorted[i].name, sorted[i].quantity);
}
#endif
main.c
#include "inventory.h"
int main(void) {
Inventory inventory = {0};
char command[5], name[11], report[1024];
int count, accepted = 0;
while (scanf("%4s %10s %d", command, name, &count) == 3) {
bool ok = strcmp(command, "add") == 0
? inventory_add(&inventory, name, count)
: inventory_take(&inventory, name, count);
if (ok) ++accepted;
}
inventory_report(&inventory, report);
printf("%d%s%s\n", accepted, report[0] ? " " : "", report);
return 0;
}
在 Inventory.swift 和 Functions.swift 中实现 Swift API。保留提供的 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 在线编辑器