制作一个可以随机显示符合指定正则表达式的文本的应用程序 [英] Make an application that displays text at random that conforms to the specified regex

查看:102
本文介绍了制作一个可以随机显示符合指定正则表达式的文本的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,因此本着Code-Golf的精神,我在这里尝试新的东西:代码保龄球.

OK, so in the spirit of Code-Golf, I'm trying out something new here: Code-Bowling.

在高尔夫中,您尝试获得最低的分数(最小的应用程序,最优雅的应用程序等).在保龄球中,您尝试获得最高分.因此,如果您遵循此规则,那么进行代码保龄球挑战的目标是使最大,最混搭,最难维护的代码片段仍然能够满足挑战的要求.但是,仅出于此目的而延长源代码是没有意义的.看起来增加的长度似乎来自设计,而不仅仅是填充.

In golf, you try to get the lowest score (smallest application, most elegant, etc). In Bowling, you try to get the highest score. So if you follow, the goal of a Code-Bowling challenge is to make the biggest, most bastardized, hardest to maintain piece of code that still meets the requirements of the challenge. However, there's no point in making source longer just for the sake of it. It needs to seem like that added length was from design and not just padding.

这是挑战:

以您选择的语言编写一个程序,该程序创建一行文本输出并终止.创建的输出必须与此正则表达式匹配:

Write a program in your language of choice that creates one line of text-output and terminates. The output that's created must be matched by this regex:

/^Good (Morning|Afternoon|Evening|Night)$/

输出可能是随机的(使用语言或您自己的实现),也可能是混乱的(确定性的,但并非如此).

The output may be random (using the languages or your own implementation) or chaotic (deterministic, but not trivially so).

推荐答案

/*
 * TODO: write manpage
 */

#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define ERROR_MAX   2048    /* arbitrary, must account for argv[0] */

#define MSG_SIZE    (5+9+1) /* +1 for newline, +1 for NUL */

#if defined(linux) || defined(BSD) && BSD > 199300L
extern char const *__progname;
# define progname __progname
#else
static char *progname;
#endif

typedef enum _Progerr {
    IO_ERR = 1,
    RND_ERR = 2
} progerr_t;

static const char GREET_START[5] = "Good";  /* XXX move to Makefile? */

static const char *TIMES_OF_DAY[5] = {
    "Morning",
    "Afternoon",
    "Evening",
    "Night",
    NULL
};

int main()
{
    char errbuf[ERROR_MAX];
    char msgbuf[MSG_SIZE];
    char *slash;
    const char **time_of_day;
    int fd, rnd;
    size_t greet_len;

#ifndef progname
    /* we want proper error messages */
    progname = argv[0];
    if ((slash = strrchr(progname, '/')) != NULL)
        progname = slash+1;
#endif

    /* get REAL randomness; can't trust rand(3).
     * avoid stdio, it's slow. */
#ifdef DEBUG
    write(STDERR_FILENO, "getting random data\n", sizeof("getting random data\n")-1);
#endif
    if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
        if ((fd = open("/dev/random", O_RDONLY)) == -1)
            rnd = rand();   /* last resort, for MSYS etc. */
    }

    if (fd >= 0 && read(fd, &rnd, sizeof(int)) != sizeof(int)) {
        close(fd);
        goto rngerr;
    }

    /* higher bits of rand() have better entropy */
    assert(sizeof(int) >= 4);   /* should be compile-time assert */
    rnd = (rnd >> 24) & 0x03;

    for (time_of_day = TIMES_OF_DAY; *time_of_day && rnd; time_of_day++, rnd--)
        ;
    if (!time_of_day)
        goto rngerr;

    sprintf(msgbuf, "%s %s", GREET_START, *time_of_day);
    greet_len = strlen(msgbuf);
    msgbuf[greet_len] = '\n';
    if (write(STDOUT_FILENO, msgbuf, greet_len+1) == -1)
        goto write_err;

    return 0;

rngerr:
    sprintf(errbuf, "%s: cannot get random data\n", progname);
    write(STDERR_FILENO, errbuf, strlen(errbuf));
    return (int)RND_ERR;

write_err:
    sprintf(errbuf, "%s: cannot write to stdout\n", progname);
    write(STDERR_FILENO, errbuf, strlen(errbuf));
    return (int)IO_ERR;
}

这篇关于制作一个可以随机显示符合指定正则表达式的文本的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆