结课项目:库存移植
Coddy 面向 C 开发者的 Ruby 课程的第 47 课,共 48 课。
挑战
简单移植参考程序。库存初始数量为 0;只有当 change(delta) 执行后的库存数量非负时,才接受这次变更,并返回 true 或 false。quantity 用于读取库存数量。Inventory.change(code,delta) 按代码应用变更,仅在变更成功时才创建该代码。report 按代码的字母顺序返回正数库存,格式为 code:quantity 对,若没有则返回 none。输入由多行 code delta 对组成,每行一对。每次变更输出一个布尔值,之后输出报告。将 Stock 和 Inventory 分别放在各自的文件中。
让 solve(input) 返回文本,不要打印。锁定的驱动程序会打印返回的文本,并在末尾添加一个换行符;它还会独立检查必需的公共 API。
你的代码应写入 exercise.rb、stock.rb 和 inventory.rb。main.rb 中包含提供的输入输出代码,不能编辑。
输入范围:数据行最多 1000 行。库存代码仅包含小写 ASCII 字母,每个库存 delta 的范围为 -1000000 到 1000000。对于传感器移植,首行限制的范围为 -1000000 到 1000000;令牌有效性遵循上文的解析器约定。
C 参考程序。使用必需的 Ruby API 匹配其行为。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
static int read_line(char **out,size_t *length_out) {
size_t length=0,capacity=64;
char *line=malloc(capacity);
if(!line)return -1;
int ch;
while((ch=getchar())!=EOF && ch!='\n') {
if(length+1>=capacity) {
if(capacity>SIZE_MAX/2){free(line);return -1;}
capacity*=2;
char *grown=realloc(line,capacity);
if(!grown){free(line);return -1;}
line=grown;
}
line[length++]=(char)ch;
}
if(ch==EOF && length==0){free(line);return ferror(stdin)?-1:0;}
if(length && line[length-1]=='\r')--length;
line[length]='\0';*out=line;
if(length_out)*length_out=length;
return 1;
}
struct Stock {long long quantity;};
static bool stock_change(struct Stock *stock,long long delta) {
long long next=stock->quantity+delta;
if(next<0)return false;
stock->quantity=next;return true;
}
struct Entry {char *code;struct Stock stock;};
struct Inventory {struct Entry entries[1000];size_t count;};
static bool inventory_change(struct Inventory *inventory,char *code,long long delta) {
size_t i=0;
while(i<inventory->count && strcmp(inventory->entries[i].code,code)!=0)++i;
if(i<inventory->count)return stock_change(&inventory->entries[i].stock,delta);
struct Stock stock={0};
if(!stock_change(&stock,delta))return false;
char *copy=malloc(strlen(code)+1);
if(!copy)exit(EXIT_FAILURE);
strcpy(copy,code);
inventory->entries[inventory->count++]=(struct Entry){copy,stock};
return true;
}
static int compare(const void *a,const void *b) {
return strcmp(((const struct Entry*)a)->code,((const struct Entry*)b)->code);
}
static void inventory_report(struct Inventory *inventory) {
qsort(inventory->entries,inventory->count,sizeof inventory->entries[0],compare);
bool first=true;
for(size_t i=0;i<inventory->count;++i) {
struct Entry *entry=&inventory->entries[i];
if(entry->stock.quantity>0) {
printf("%s%s:%lld",first?"":" ",entry->code,entry->stock.quantity);
first=false;
}
}
puts(first?"none":"");
}
int main(void) {
struct Inventory inventory={0};
char *line=NULL;int status;
while((status=read_line(&line,NULL))>0) {
char *code=strtok(line," \t\r\n\f\v");
if(code) {
char *amount=strtok(NULL," \t\r\n\f\v");
if(!amount){free(line);return EXIT_FAILURE;}
long long delta=strtoll(amount,NULL,10);
puts(inventory_change(&inventory,code,delta)?"true":"false");
}
free(line);
}
if(status<0)return EXIT_FAILURE;
inventory_report(&inventory);
for(size_t i=0;i<inventory.count;++i)free(inventory.entries[i].code);
return 0;
}
亲手试试
require_relative 'exercise'
require_relative 'stock'
require_relative 'inventory'
def check(value)
raise 'Public API check failed' unless value
end
s = Stock.new
check(!s.change(-1) && s.quantity == 0 && s.change(4) && !s.change(-5) && s.quantity == 4)
i = Inventory.new
check(!i.change("x", -2) && i.report == "none")
check(i.change("b", 2) && i.change("a", 1) && i.report == "a:1 b:2")
puts solve(STDIN.read)
面向 C 开发者的 Ruby中的所有课
自己练习: Ruby 在线编译器