嗨:)我想做的是编写一个简单的程序以从最短的入口开始扩展
例如
az或0-9或abc或a-z0-9
最长写
abc … xyz或0123456789或abc或abcdefghijklmnouprstwxyz0123456789
第一个示例最短条目=第一个示例结果,应给出:)
到目前为止,我写这样的东西,它只适用于从a到z的字母:
expand(char s[]) { int i,n,c; n=c=0; int len = strlen(s); for(i = 1;s[i] > '0' && s[i]<= '9' || s[i] >= 'a' && s[i] <= 'z' || s[i]=='-';i++) { /*c = s[i-1]; g = s[i]; n = s[i+1];*/ if( s[0] == '-') printf("%c",s[0]); else if(s[i] == '-') { if(s[i-1]<s[i+1]) { while(s[i-1] <= s[i+1]) { printf("%c", s[i-1]); s[i-1]++; } } else if(s[i-1] == s[i+1]) printf("%c",s[i]); else if(s[i+1] != '-') printf("%c",s[i]); else if(s[i-1] != '-') printf("%c",s[i]); } else if(s[i] == s[i+1]) { while(s[i] == s[i+1]) { printf("%c",s[i]); s[i]++; } } else if( s[len] == '-') printf("%c",s[len]); }
}
但现在我被卡住了:(
任何想法,我应该检查我的程序正常工作吗?
编辑1: @Andrew Kozak(1)abcd(2)01234
感谢您的提前:)
完整的测试程序,包括您的测试用例,地雷和一些酷刑测试,可以在http://ideone.com/sXM7b#info_3915048上实时查看。
我敢肯定我夸大了要求,但是
a-c-b
(char*) 0
printf("%c", c)
我发表了一些评论来解释为什么会发生什么,但是总的来说,您会发现该代码无论如何都更加清晰易读
*it=='-'
predicate(*it)
最后但并非最不重要的是,我 这样做 的原因:
事不宜迟,实现包括测试用例:
#include <stdlib.h> #include <stdio.h> #include <string.h> int alpha_range(char c) { return (c>='a') && (c<='z'); } int digit_range(char c) { return (c>='0') && (c<='9'); } char* expand(const char* s) { char buf[2048]; const char* in = s; char* out = buf; // parser state int (*predicate)(char) = 0; // either: NULL (free state), alpha_range (in alphabetic range), digit_range (in digit range) char lower=0,upper=0; // tracks lower and upper bound of character ranges in the range parsing states // init *out = 0; while (*in) { if (!predicate) { // free parsing state if (alpha_range(*in) && (in[1] == '-') && alpha_range(in[2])) { lower = upper = *in++; predicate = &alpha_range; } else if (digit_range(*in) && (in[1] == '-') && digit_range(in[2])) { lower = upper = *in++; predicate = &digit_range; } else *out++ = *in; } else { // in a range if (*in < lower) lower = *in; if (*in > upper) upper = *in; if (in[1] == '-' && predicate(in[2])) in++; // more coming else { // end of range mode, dump expansion char c; for (c=lower; c<=upper; *out++ = c++); predicate = 0; } } in++; } *out = 0; // null-terminate buf return strdup(buf); } void dotest(const char* const input) { char* ex = expand(input); printf("input : '%s'\noutput: '%s'\n\n", input, ex); if (ex) free(ex); } int main (int argc, char *argv[]) { dotest("a-z or 0-9 or a-b-c or a-z0-9"); // from the original post dotest("This is some e-z test in 5-7 steps; this works: a-b-c. This works too: b-k-c-e. Likewise 8-4-6"); // from my C++ answer dotest("-x-s a-9 9- a-k-9 9-a-c-7-3"); // assorted torture tests return 0; }
测试输出:
input : 'a-z or 0-9 or a-b-c or a-z0-9' output: 'abcdefghijklmnopqrstuvwxyz or 0123456789 or abc or abcdefghijklmnopqrstuvwxyz0123456789' input : 'This is some e-z test in 5-7 steps; this works: a-b-c. This works too: b-k-c-e. Likewise 8-4-6' output: 'This is some efghijklmnopqrstuvwxyz test in 567 steps; this works: abc. This works too: bcdefghijk. Likewise 45678' input : '-x-s a-9 9- a-k-9 9-a-c-7-3' output: '-stuvwx a-9 9- abcdefghijk-9 9-abc-34567'