题目:编写函数expand(s1, s2),将字符串中类似于a-z一类的速记符号在字符串
s2中扩展为等价的完整列表abc...xyz,并可处理a-b-c、a-z0-9、于-a-z等类似
的情况。作为前导和尾随的的-字符原样排印。
1)假设s2的的空间足够存放扩展后的s1。
2)为简化程序,我就不考虑前导和尾随字符了。
1 #include < iostream > 2 4 using namespace std; 5 6 // 扩展s1到s2,假设s2的空间足够大 7 void Expand( char s1[], char s2[]) 8 { 9 int j = 0 ; 10 for ( int i = 0 ; s1[i] != ' \0 ' ; i ++ ) 11 { 12 if (s1[i] == ' - ' ) 13 { 14 char min = s1[i - 1 ] + 1 ; 15 while (s1[i + 2 ] == ' - ' ) 16 i += 2 ; 17 char max = s1[i + 1 ] - 1 ; 18 while (min <= max) 19 s2[j ++ ] = min ++ ; 20 } 21 else 22 { 23 s2[j ++ ] = s1[i]; 24 } 25 } 26 s2[j] = ' \0 ' ; 27 } 28 29 void Print( char s[]) 30 { 31 for ( int i = 0 ; s[i] != ' \0 ' ; i ++ ) 32 { 33 cout << s[i]; 34 } 35 } 36 37 int main() 38 { 39 char s1[ 100 ] = { " a-b-c-da-b-c-da-b-c-d " }; 40 char s2[ 500 ]; 41 Expand(s1, s2); 42 Print(s2); 43 cout << endl; 44 45 return 0 ; 46 }