Menu
CoddyTech

结课项目:传感器移植

Coddy 面向 C 开发者的 Ruby 课程的第 48 课,共 48 课。

challenge icon

挑战

简单

移植参考程序。Above(limit)#accept?(n) 会接受严格大于 limit 的 n。Sensor(policy)#feed(token) 会解析由可选符号和一个或多个 ASCII 数字组成的内容,不允许有空格或其他字符;并且只接受 -1000000 到 1000000 之间的整数。无效 token 会使 invalid 加 1;被 policy 接受的有效 token 会使 accepted 加 1,并加到 total 中。被拒绝的有效 token 不会改变任何计数。summary 会返回以空格分隔的 accepted、invalid 和 total。输入的第一行是 limit;之后的每一行都是一个 token,行尾会被移除。三个计数器的初始值都是 0。自定义 policy 对象也必须能够正常工作。

让 solve(input) 返回文本,不要打印。锁定的驱动程序会在返回的文本后打印一个换行符,并独立检查必需的公共 API。

你的代码需要写在 exercise.rb、above.rb 和 sensor.rb 中。main.rb 包含提供的输入/输出代码,不能修改。

输入范围:数据行最多 1000 行。库存代码只包含小写 ASCII 字母,每个库存增量都在 -1000000 到 1000000 之间。对于 sensor 移植,第一行的 limit 在 -1000000 到 1000000 之间;token 是否有效遵循上述解析器约定。

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 Above {int limit;};
struct Policy {bool (*accepts)(const void *,int);const void *context;};
static bool above_accepts(const void *context,int value) {
    const struct Above *above=context;
    return value>above->limit;
}
struct Sensor {struct Policy policy;long long accepted,invalid,total;};
static bool parse_token(const char *token,size_t length,int *out) {
    bool negative=false;
    size_t i=0;
    if(i<length && (token[i]=='+' || token[i]=='-'))negative=token[i++]=='-';
    if(i==length)return false;
    int value=0;
    for(;i<length;++i) {
        if(token[i]<'0' || token[i]>'9')return false;
        int digit=token[i]-'0';
        if(value>(1000000-digit)/10)return false;
        value=value*10+digit;
    }
    *out=negative?-value:value;return true;
}
static void sensor_feed(struct Sensor *sensor,const char *token,size_t length) {
    int value;
    if(!parse_token(token,length,&value)){++sensor->invalid;return;}
    if(sensor->policy.accepts(sensor->policy.context,value)) {
        ++sensor->accepted;sensor->total+=value;
    }
}
int main(void) {
    char *line=NULL;
    if(read_line(&line,NULL)!=1)return EXIT_FAILURE;
    struct Above above={(int)strtol(line,NULL,10)};free(line);
    struct Sensor sensor={{above_accepts,&above},0,0,0};
    int status;size_t length;
    while((status=read_line(&line,&length))>0){sensor_feed(&sensor,line,length);free(line);}
    if(status<0)return EXIT_FAILURE;
    printf("%lld %lld %lld\n",sensor.accepted,sensor.invalid,sensor.total);
    return 0;
}

亲手试试

require_relative 'exercise'
require_relative 'above'
require_relative 'sensor'
def check(value)
  raise 'Public API check failed' unless value
end
check(Above.new(4).accept?(5) && !Above.new(4).accept?(4))
policy = Object.new
def policy.accept?(n); n.even?; end
s = Sensor.new(policy)
["2", "-4", "3", "x", "4\n", "1000001"].each { |t| s.feed(t) }
check(s.summary == "2 3 -2" && s.summary == "2 3 -2")

puts solve(STDIN.read)

面向 C 开发者的 Ruby中的所有课

自己练习: Ruby 在线编译器