Last active
December 18, 2015 17:39
-
-
Save sunnylost/5820375 to your computer and use it in GitHub Desktop.
TCPL 习题集 第三章
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //escape 与 unescape,针对 \n 与 \t 做转义或逆操作。 | |
| #include <stdio.h> | |
| void escape(char[], char[]); | |
| void unescape(char[], char[]); | |
| int strlen(char[]); | |
| int main() { | |
| char s[] = "haha\n\tThe\ttime\tis\tcoming!"; | |
| char t[80]; | |
| char t1[80]; | |
| escape(s, t); | |
| printf("%s\n", t); | |
| unescape(t, t1); | |
| printf("%s\n", t1); | |
| } | |
| void escape(char s[], char t[]) { | |
| int i, j; | |
| for(i = j = 0; i < strlen(s); j++, i++) { | |
| switch(s[i]) { | |
| case '\n' : | |
| t[j] = '\\'; | |
| t[++j] = 'n'; | |
| break; | |
| case '\t' : | |
| t[j] = '\\'; | |
| t[++j] = 't'; | |
| break; | |
| default: | |
| t[j] = s[i]; | |
| } | |
| } | |
| t[j] = '\0'; | |
| } | |
| void unescape(char s[], char t[]) { | |
| int i, j; | |
| int preSlash = 0; | |
| for(i = j = 0; i < strlen(s); j++, i++) { | |
| switch(s[i]) { | |
| case '\\' : | |
| preSlash = 1; | |
| --j; | |
| break; | |
| case 't' : | |
| if(preSlash) { | |
| t[j] = '\t'; | |
| } else { | |
| t[j] = 't'; | |
| } | |
| preSlash = 0; | |
| break; | |
| case 'n' : | |
| if(preSlash) { | |
| t[j] = '\n'; | |
| } else { | |
| t[j] = 'n'; | |
| } | |
| preSlash = 0; | |
| break; | |
| default: | |
| preSlash = 0; | |
| t[j] = s[i]; | |
| } | |
| } | |
| t[j] = '\0'; | |
| } | |
| int strlen(char s[]) { | |
| int i = 0; | |
| while(s[i] != '\0') | |
| ++i; | |
| return i; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 编写函数 expand(s1, s2),将字符串 s1 中类似于 a-z 一类的速记符号在字符串 s2 中扩展为等价的完整列表 abc...xyz。 | |
| 该函数可以处理大小写字母和数字,并可以处理 a-b-c、a-z0-9 与 -a-z 等类似的情况。作为前导和尾随的 - 字符鸳鸯排印。 | |
| */ | |
| #include <stdio.h> | |
| void expand(char[], char[]); | |
| int strlen(char[]); | |
| int main() { | |
| char s1[] = "a-a-Z-D"; | |
| char s2[100]; | |
| expand(s1, s2); | |
| printf("%s\n", s2); | |
| } | |
| void expand(char s1[], char s2[]) { | |
| int len = strlen(s1); | |
| int i, j, isValid, isFulfill, isBegin, type = -1; // isValid: 字符是否是字母或数字;isFulfill: | |
| char c, before, after; | |
| i = j = isValid = isFulfill = isBegin = 0; | |
| before = after = '\0'; | |
| while((c = s1[i++]) != '\0') { | |
| if(c >= 'a' && c <= 'z') { | |
| if(type != -1) { | |
| if(type != 0 || c < before) { | |
| if(isBegin) { | |
| s2[j++] = '-'; | |
| } | |
| isBegin = 0; | |
| } | |
| } | |
| type = 0; | |
| isValid = 1; | |
| } else if(c >= '0' && c <= '9') { | |
| if(type != -1) { | |
| if(type != 1 || c < before) { | |
| if(isBegin) { | |
| s2[j++] = '-'; | |
| } | |
| isBegin = 0; | |
| } | |
| } | |
| type = 1; | |
| isValid = 1; | |
| } else if(c >= 'A' && c <= 'Z') { | |
| if(type != -1) { | |
| if(type != 2 || c < before) { | |
| if(isBegin) { | |
| s2[j++] = '-'; | |
| } | |
| isBegin = 0; | |
| } | |
| } | |
| type = 2; | |
| isValid = 1; | |
| } else { | |
| isValid = 0; | |
| } | |
| if(isValid) { | |
| if(isBegin) { | |
| while(++before <= c) { | |
| s2[j++] = before; | |
| } | |
| isBegin = isFulfill = 0; | |
| type = -1; | |
| } else { | |
| s2[j++] = c; | |
| isFulfill = 1; | |
| before = c; | |
| } | |
| } else if(c == '-') { | |
| if(isFulfill) { | |
| isBegin = 1; | |
| } else { | |
| s2[j++] = c; | |
| isBegin = isFulfill = 0; | |
| isValid = 0; | |
| } | |
| } else { | |
| s2[j++] = c; | |
| isValid = isBegin = isFulfill = 0; | |
| type = -1; | |
| } | |
| } | |
| s2[j] = '\0'; | |
| } | |
| int strlen(char s[]) { | |
| int i = 0; | |
| while(s[i] != '\0') | |
| ++i; | |
| return i; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //进制转换 | |
| #include <stdio.h> | |
| void itob(int, char[], int); | |
| int strlen(char[]); | |
| void reverse(char[]); | |
| int main() { | |
| char s[80]; | |
| itob(44, s, 2); | |
| printf("%s\n", s); | |
| } | |
| void itob(int n, char s[], int b) { | |
| char t[] = "0123456789abcdefghijklmnopqrstuvw"; | |
| int sign = n, i = 0, r; | |
| if(b <= 1 || b > 32) { | |
| s[0] = '\0'; | |
| return; | |
| } | |
| if(sign < 0) { | |
| n = -1 * n; | |
| } | |
| do { | |
| s[i++] = t[n % b]; | |
| } while((n /= b) > 0); | |
| if(sign < 0) { | |
| s[i++] = '-'; | |
| } | |
| s[i] = '\0'; | |
| reverse(s); | |
| } | |
| int strlen(char s[]) { | |
| int i = 0; | |
| while(s[i] != '\0') | |
| ++i; | |
| return i; | |
| } | |
| void reverse(char s[]) { | |
| char c; | |
| for(int i = 0, j = strlen(s) - 1; i < j; i++, j--) { | |
| c = s[i]; | |
| s[i] = s[j]; | |
| s[j] = c; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment