C,860个835 794字节
绝对可怕,但可能会变得更短。在此站点上添加了许多换行符以用于格式化。实际的源代码在#includes和#defines之后都有换行符,但是从char *到最后一个w(“。\ n”);}的所有内容都在一行中。我通过从字符串数组中的22,...,29中删除值来缩短了它,而对2,...,9重新使用了这些字符串,并在适当的时候添加了“ vingt-”。(我真的希望我没有引入错误!)
#include <sys/time.h>
#define w printf
#define q tm_min
#define r tm_hour
char*a[]={"minuit","une","deux","trois","quatre","cinq","six","sept","huit","neuf","dix",
"onze","douze","treize","quatorze","quart","seize","dix-sept","dix-huit","dix-
neuf","vingt","vingt-et-une",0,0,0,0,0,0,0,0,"demi"};
h=1,s=1,m,e,l,t,p,o,v;struct tm j;
main(){struct timeval i;gettimeofday(&i,0);localtime_r(&i.tv_sec,&j);
j.q>30?m=1,++j.r,j.q=60-j.q:j.q==15||j.q==30?e=1:0;j.q>21&&j.q<30?v=1:0;
j.r%12?j.r<12?t=1:j.r-m<17?p=1:(o=1):0;
j.q==15&&m?l=1:0;j.r%12?j.r%12==1?s=0:0:(h=0,s=0);
w("Il est ");j.r==12?w("midi"):w("%s",a[j.r%12]);h?w(" heure"):0;s?w("s"):0;
m?w(" moins"):0;e?w(" et"):0;l?w(" le"):0;
j.q?v?w(" vingt-"):w(" "),w("%s",a[j.q-20*v]):0;
t?w(" du matin"):p?w(" de l'apres-midi"):o?w(" du soir"):0;w(".\n");}
像这样:
#include <sys/time.h>
#define w printf
#define q tm_min
#define r tm_hour
char*a[]={"minuit","une","deux","trois","quatre","cinq","six","sept","huit","neuf","dix","onze","douze","treize","quatorze","quart","seize","dix-sept","dix-huit","dix-neuf","vingt","vingt-et-une",0,0,0,0,0,0,0,0,"demi"};h=1,s=1,m,e,l,t,p,o,v;struct tm j;main(){struct timeval i;gettimeofday(&i,0);localtime_r(&i.tv_sec,&j);j.q>30?m=1,++j.r,j.q=60-j.q:j.q==15||j.q==30?e=1:0;j.q>21&&j.q<30?v=1:0;j.r%12?j.r<12?t=1:j.r-m<17?p=1:(o=1):0;j.q==15&&m?l=1:0;j.r%12?j.r%12==1?s=0:0:(h=0,s=0);w("Il est ");j.r==12?w("midi"):w("%s",a[j.r%12]);h?w(" heure"):0;s?w("s"):0;m?w(" moins"):0;e?w(" et"):0;l?w(" le"):0;j.q?v?w(" vingt-"):w(" "),w("%s",a[j.q-20*v]):0;t?w(" du matin"):p?w(" de l'apres-midi"):o?w(" du soir"):0;w(".\n");}
非高尔夫版本,没有“空间优化”(也很丑陋):
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
struct timeval tv;
struct tm local_time;
char *nums[] = {"minuit", "une", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix", "onze", "douze", "treize", "quatorze", "quart", "seize", "dix-sept", "dix-huit", "dix-neuf", "vingt", "vingt-et-une", "vingt-deux", "vingt-trois", "vingt-quatre", "vingt-cinq", "vingt-six", "vingt-sept", "vingt-huit", "vingt-neuf", "demi"};
int heure = 1;
int s = 1;
int moins = 0;
int et = 0;
int le = 0;
int matin = 0, aprem = 0, soir = 0;
memset(&local_time, 0, sizeof local_time);
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &local_time);
#if 0
local_time.tm_min = atoi(argv[1]);
local_time.tm_hour = atoi(argv[2]);
#endif
if (local_time.tm_min > 30) {
moins = 1;
local_time.tm_hour += 1;
local_time.tm_min = 60 - local_time.tm_min;
} else if (local_time.tm_min == 15 || local_time.tm_min == 30) {
et = 1;
}
if (local_time.tm_hour % 12) {
if (local_time.tm_hour < 12)
matin = 1;
else if (local_time.tm_hour < 17)
aprem = 1;
else if (local_time.tm_hour == 17 && moins)
aprem = 1;
else
soir = 1;
}
if (local_time.tm_min == 15 && moins)
le = 1;
if (local_time.tm_hour % 12 == 0) {
heure = 0;
s = 0;
} else if (local_time.tm_hour % 12 == 1) {
s = 0;
}
printf("Il est ");
if (local_time.tm_hour == 12)
printf("midi");
else
printf("%s", nums[local_time.tm_hour % 12]);
if (heure)
printf(" heure");
if (s)
printf("s");
if (moins)
printf(" moins");
if (et)
printf(" et");
if (le)
printf(" le");
if (local_time.tm_min)
printf(" %s", nums[local_time.tm_min]);
if (matin)
printf(" du matin");
else if (aprem)
printf(" de l'apres-midi");
else if (soir)
printf(" du soir");
printf(".\n");
return 0;
}
(#if 0内容仅用于通过命令行测试不同的时间值)。