本帖最后由 jackz007 于 2021-11-7 02:04 编辑
这个代码太烧脑了!#include <stdio.h>
bool alpha(char c)
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ;
}
bool upper(char c)
{
return c >= 'A' && c <= 'Z' ;
}
bool lower(char c)
{
return c >= 'a' && c <= 'z' ;
}
bool digit(char c)
{
return c >= '0' && c <= '9' ;
}
bool check(char s[])
{
int m ;
bool r = false ;
for(m = 0 ; s[m] && m < 3 ; m ++) ;
if(m > 2 && s[1] == '-') {
if((alpha(s[0]) && alpha(s[2])) || (digit(s[0]) && digit(s[2]))) {
if((upper(s[0]) && upper(s[2])) || (lower(s[0]) && lower(s[2]))) {
r = true ;
} else if(digit(s[0]) && digit(s[2])) {
r = true ;
}
}
}
return r ;
}
char * expand(char s[])
{
int c , d , e , i , k , m , n ;
bool f ;
for(m = 0 ; s[m] ; m ++) ;
for(n = m , f = false , i = 0 ; i < n - 2 ; i ++) {
if(check(& s[i])) {
e = (s[i + 2] > s[i]) ? s[i + 2] - s[i] + 1 : s[i] - s[i + 2] + 1 ;
if(e > 3) {
for(k = 0 ; k < n - i - 1 ; k ++) s[n - 1 + e - 3 - k] = s[n - 1 - k] ;
n += e - 3 ;
} else if(e < 3) {
for(k = 0 ; k < n - i - 1 ; k ++) s[i + (e - 1) + k] = s[i + 2 + k] ;
n -= 3 - e ;
}
if(e > 2) {
d = (s[i + 2] > s[i]) ? 1 : -1 ;
for(c = s[i] , k = 0 ; k < e ; c += d , k ++) s[i + k] = c ;
}
i += e - 1 ;
}
}
return s ;
}
int main(void)
{
char s[256] = {0} ;
gets(s) ;
printf("%s\n" , expand(s)) ;
}
编译、运行实况:D:\00.Excise\C>g++ -o x x.c
D:\00.Excise\C>x
12345K-Abcdefg-x123-9abcdef9-1
12345KJIHGFEDCBAbcdefghijklmnopqrstuvwx123456789abcdef987654321
D:\00.Excise\C>
|