Menu
CoddyTech

Cuối cùng: Sổ hàng tồn kho

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

challenge icon

Thử thách

Dễ

Triển khai summarize(rows). Mỗi dòng gồm tên mục viết bằng chữ ASCII thường và một số nguyên có dấu, được phân tách bằng một dấu cách. Tính tổng các lượng theo tên; chỉ giữ lại các tổng dương; sắp xếp tên theo thứ tự từ điển; trả về các mục name:total, được phân tách bằng ký tự xuống dòng, hoặc trả về empty nếu không có mục nào. Đầu vào rỗng là hợp lệ.

Giữ nguyên API công khai đã khai báo. Trình điều khiển bị khóa hiển thị sẽ kiểm tra trực tiếp API này, đồng thời in ra solve(lines). Đầu vào đáp ứng hợp đồng đã nêu; mọi phép tính đều có giá trị hữu hạn và nhỏ.

Đặt code của bạn trong Functions.R và Exercise.R. main.R nạp các phần phụ thuộc theo đúng thứ tự và chứa code đầu vào/đầu ra được cung cấp; bạn không thể chỉnh sửa tệp này.

Chuyển toàn bộ tài liệu tham khảo C này sang API công khai R. Tài liệu này triển khai cùng hợp đồng đầu vào hợp lệ.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#include <errno.h>
struct Entry { char *name; long long total; };
/* Read a whole line without imposing an item-name length limit. */
static int read_line(char **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;return 1;
}
static int compare_entries(const void *a,const void *b) {
    return strcmp(((const struct Entry*)a)->name,((const struct Entry*)b)->name);
}
int main(void) {
    struct Entry *entries=NULL;
    size_t count=0;
    int status=0,read_status;
    char *line=NULL;
    while((read_status=read_line(&line))>0) {
        char *separator=strchr(line,' ');
        if(!separator){free(line);status=1;break;}
        *separator='\0';char *end;
        errno=0;long long amount=strtoll(separator+1,&end,10);
        if(errno==ERANGE || end==separator+1 || *end!='\0'){free(line);status=1;break;}
        size_t i=0;
        while(i<count && strcmp(entries[i].name,line)!=0)++i;
        if(i==count) {
            if(count>=SIZE_MAX/sizeof *entries){free(line);status=1;break;}
            struct Entry *grown=realloc(entries,(count+1)*sizeof *entries);
            if(!grown){free(line);status=1;break;}
            entries=grown;entries[count++]=(struct Entry){line,0};
        } else free(line);
        if((amount>0 && entries[i].total>LLONG_MAX-amount) ||
           (amount<0 && entries[i].total<LLONG_MIN-amount)){status=1;break;}
        entries[i].total+=amount;
    }
    if(read_status<0)status=1;
    if(!status) {
        if(count>1)qsort(entries,count,sizeof *entries,compare_entries);
        int found=0;
        for(size_t i=0;i<count;++i)if(entries[i].total>0){
            printf("%s:%lld\n",entries[i].name,entries[i].total);found=1;
        }
        if(!found)puts("empty");
        if(ferror(stdout))status=1;
    }
    for(size_t i=0;i<count;++i)free(entries[i].name);
    free(entries);return status;
}

Tự làm thử

source("Functions.R")
source("Exercise.R")
stopifnot(identical(summarize(c("q 9","q -2","b 1")),"b:1\nq:7"))
lines <- suppressWarnings(readLines(file("stdin"), warn=FALSE))
cat(solve(lines), "\n", sep="")

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

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