C,124个 107 98字节
作为功能:
x,i;W(char*v){for(i=strcspn(" MRUYH NSWEI OTBFK PADGL",v);x<5;++x)putchar(x^i%5?x^i/5?124:92:47);}
// main(int c,char**v){W(v[1]);}
通过将网格旋转45度并从生成的块中查找行/列来工作。
作为完整的可执行文件(107字节):
x;main(i,v)char**v;{for(i=strcspn(" MRUYH NSWEI OTBFK PADGL",v[1]);x<5;++x)putchar(x^i%5?x^i/5?124:92:47);}
另一个完整的可执行文件:(相同的字节数,但从stdin接收输入,并在输出之后包含换行符)
main(i){char*r=" MRUYH NSWEI OTBFK PADGL",b[]="|||||";i=strchr(r,getchar())-r;b[i%5]=47;b[i/5]=92;puts(b);}
分解:
x; // Implicit int declaration
main(i,v)char**v;{ // K&R function declaration to save a byte
for(i=strcspn("<...>",v[1]); // Find index of input in lookup table
x<5;++x) // Loop 0 to 4
putchar(x^i%5?x^i/5?124:92:47); // Print /, \ or | depending on value of i
}
备用细目:
main(i){
char*r="<...>", // Store lookup table
b[]="|||||"; // Malleable base string for return
i=strchr(r,getchar())-r; // Find input in lookup table
b[i%5]=47; // Set correct char in output to /
b[i/5]=92; // Set correct char in output to \
puts(b); // Print result
}
奖励:维基百科页面的0-9扩展名:
x;main(i,v)char**v;{for(i=strcspn(" MRUY6H NSW7EI OT8BFK P9ADGL 012345",v[1]);x<5;++x)putchar(x^i%6?x^i/6?124:92:47);}
红利红利:用于编码和解码消息的完整程序(如果杂乱无章):
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
static const char *REF = " MRUY6H NSW7EI OT8BFK P9ADGL 012345 ";
char sub(char c) {
c = toupper(c);
if(c == 'C') { c = 'K'; }
if(c == 'J') { c = 'G'; }
if(c == 'Q') { c = 'K'; }
if(c == 'V') { c = 'W'; }
if(c == 'X') { c = 'S'; }
if(c == 'Z') { c = 'S'; }
return c;
}
void print_encoded(char c) {
char b[] = "|||||";
const char *p = strchr(REF, sub(c));
if(!p) { return; }
int i = p - REF;
if(i) {
if(i%6 < 5) { b[i%6] = '/'; }
if(i/6 < 5) { b[i/6] = '\\';}
}
puts(b);
}
char decode(const char *m) {
int pf = 5;
int pb = 5;
for(int x=0;x<5;++x) {
if(m[x] == '/') {
pf=x;
} else if(m[x] == '\\') {
pb=x;
} else if(m[x] == '\0') {
return '!';
}
}
return REF[pb*6+pf];
}
int main(int c, const char **v) {
int inArg;
bool isDecode;
if(c > 1 && (strcmp(v[1], "-h") == 0 || strcmp(v[1], "--help") == 0)) {
printf("Usage:\n %s [-d] [<input>]\n\n", v[0]);
printf("Converts input to/from Cooke and Wheatstone 5-needle encoding.\n\n");
printf("If no input arguments are given, takes input from stdin.\n\n");
printf("Parameters:\n");
printf(" -h --help Displays help.\n");
printf(" -d --decode Switches to decode mode.\n");
printf("\n");
return 0;
} else if(c > 1 && (strcmp(v[1], "-d") == 0 || strcmp(v[1], "--decode") == 0)) {
inArg = (c > 2 ? 2 : 0);
isDecode = true;
} else if(c > 1) {
inArg = 1;
isDecode = false;
} else {
inArg = 0;
isDecode = false;
}
if(isDecode) {
if(inArg == 0) {
char ln[6];
while(scanf("%5s", ln) == 1) {
putchar(decode(ln));
}
} else {
for(int p = inArg; p < c; ++p) {
for(const char *q = v[p], *e = strchr(v[p], '\0'); q < e; q += 5) {
while(*q == ' ') { ++q; }
putchar(decode(q));
}
}
}
putchar('\n');
} else {
if(inArg == 0) {
int c;
while((c = getchar()) != EOF) {
print_encoded(c);
}
} else {
for(const char *p = v[inArg]; *p; ++p) {
print_encoded(*p);
}
}
}
return 0;
}
find
代替index
-1个字节。