Menu
CoddyTech

Bài cuối: Chuyển chương trình quản lý kho

Bài học 47 trên 48 trong khóa học Ruby dành cho lập trình viên C của Coddy.

challenge icon

Thử thách

Dễ

Chuyển chương trình tham chiếu sang Ruby. Tồn kho bắt đầu từ 0; change(delta) chỉ chấp nhận thay đổi nếu lượng tồn kho sau đó không âm, rồi trả về true hoặc false. quantity đọc lượng tồn kho. Inventory.change(code,delta) áp dụng thay đổi cho từng mã, chỉ tạo mã khi thao tác thành công. report trả về các lượng tồn kho dương theo thứ tự mã chữ cái, dưới dạng các cặp code:quantity, hoặc none. Đầu vào gồm các cặp code delta, mỗi dòng một cặp. Đầu ra gồm một giá trị boolean cho mỗi lần thay đổi, sau đó là báo cáo. Đặt Stock và Inventory trong các tệp riêng.

Trả về văn bản từ solve(input) mà không in ra. Trình điều khiển bị khóa sẽ in văn bản được trả về cùng một ký tự xuống dòng ở cuối và kiểm tra riêng các API công khai bắt buộc.

Đặt code của bạn trong exercise.rb, stock.rb, inventory.rb. main.rb chứa code đầu vào/đầu ra được cung cấp và không thể chỉnh sửa.

Giới hạn đầu vào: tối đa 1000 dòng dữ liệu. Mã tồn kho chỉ chứa chữ cái ASCII viết thường, và mỗi delta tồn kho nằm trong khoảng từ -1000000 đến 1000000. Đối với bản chuyển cổng cảm biến, giới hạn ở dòng đầu tiên nằm trong khoảng từ -1000000 đến 1000000; tính hợp lệ của token tuân theo hợp đồng của trình phân tích cú pháp ở trên.

Chương trình tham chiếu C. Hãy khớp hành vi của chương trình này bằng các API Ruby bắt buộc.

#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;
}

Tự làm thử

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)

Tất cả bài học trong Ruby dành cho lập trình viên C

Tự luyện tập: Trình biên dịch Ruby online