The C programing language 第三章课后题

网友投稿 690 2022-09-16

The C programing language 第三章课后题

The C programing language 第三章课后题

#include #include "chap1.c"/*这个是上一章的课后题,里面的函数,刚好这次需要,但是需要把chap1.c 中的main方面注释掉因为,c语言中,一个文件中只能有一个main方法*/#include "chap2.c" /* chap2.c 是第二章的课后题 里面有的函数,这里需要使用使用 */#define abs(x) ((x)<0?-(x):(x)) /* 注意这里只是abs() 进行替换,如果看不懂可以看看这个/*3-3 : expand(s1,s2)*/int is_digit_letter(int c){ if(c >= 'a' && c <= 'z') return 1; else if(c >= '0' && c <= '9') return 1; return 0; }int expand_s2(char s2[], int start_point, int start, int end){ while(start <= end) s2[start_point++] = start++; s2[start_point] = '

#include #include "chap1.c"/*这个是上一章的课后题,里面的函数,刚好这次需要,但是需要把chap1.c 中的main方面注释掉因为,c语言中,一个文件中只能有一个main方法*/#include "chap2.c" /* chap2.c 是第二章的课后题 里面有的函数,这里需要使用使用 */#define abs(x) ((x)<0?-(x):(x)) /* 注意这里只是abs() 进行替换,如果看不懂可以看看这个/*3-3 : expand(s1,s2)*/int is_digit_letter(int c){ if(c >= 'a' && c <= 'z') return 1; else if(c >= '0' && c <= '9') return 1; return 0; }int expand_s2(char s2[], int start_point, int start, int end){ while(start <= end) s2[start_point++] = start++; s2[start_point] = '\0'; return start_point; //return s2 length }void expand(char s1[], char s2[]){ int i ,start , end; int index = 0; (s1[0] == '-')?(i=1,s1[0]='a'):(i=0); while(s1[i] != '\0') { if(is_digit_letter(s1[i])==1 && s1[i-1]=='-') { end = s1[i]; printf("end : %c : %d \n ", end, end); index = expand_s2(s2, index, start, end); printf("index : %c : %d \n ", index, index); if(s1[i+1] == '-') { start = ++end; printf("start : %c : %d \n ", start, start); } } else if(is_digit_letter(s1[i])) { start =s1[i]; printf("start : %c : %d \n ", start, start); } i++; }}/*看完答案之后,修改如下*/void expand_(char s1[], char s2[]){ char c; int i, j; i = j = 0; while((c=s1[i++]) != '\0') { if(is_digit_letter(c) && s1[i] == '-' && s1[i+1] >= c) { i++; while(c <= s1[i]) s2[j++] = c++; (s1[i+1] == '-')?(j--):' '; //处理 a-b-g ,重复出现b这种情况 } } s2[j] = '\0';}/*test_expand function: evaluating the expand function*/void test_expand(){ char s1[] = "a-z"; char s2[] = "a-c-g"; char s3[] = "a-c0-9"; char s4[] = "-a-c-g"; char s5[50]; expand_(s1,s5); printf("%s \n",s5); expand_(s2,s5); printf("%s \n",s5); expand_(s3,s5); printf("%s \n",s5); expand_(s4,s5); printf("%s \n",s5);}/* 3-4 首先,我们知道,有符号char 的取值范围: -128 到 127 同样的有符号的 int 取值范围是: 2^(sizeof(int)*8-1) 到 2^(sizeof(int)*8-1)-1 这个时候 : 2^(sizeof(int)*8-1) 这个值,在计算机输出的时候是一个负值,这个是最大负值 其实在补码表示的时候,他的补码和反码一样,举个例子char -128 补码 反码都是 1000 0000 所以,假如我们要把最大负数,转成字符串输出,就不能直接对n进行 n=-n 赋值操作,这样的操作 计算的结果肯定有问题,我们把n=-n 赋值操作,间接的放到处理数据的过程中去,这样就不存在 最大值的问题,因为处理数据的过程中,结果范围是 0-9 itoa : convert n to characters in s and can handle the largest negative numbner n = -2^(wordsize-1),regardless of the machine on which it runs*/void itoa(int n, char s[]) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; s[i] = '\0'; reverse(s,_strlen(s)); }/*test_itoa evaluating the brove of function*/void test_itoa(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 0; printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 23; printf("%d \n",n); itoa(n,s); printf("%s \n",s);}/*3-5 itob function : converts the integer n into a base b character representation in the string s */void itob(int n, char s[], int b){ int i = 0; int sign = n; int temp; do{ temp = abs(n%b); s[i++]= (temp >9)? (temp+55): (temp+'0'); }while(n /= b); (sign < 0)? (s[i++]='-'): ' '; s[i] = '\0'; reverse(s,_strlen(s));}void test_itob(){ char s[55]; itob(18, s, 10); printf("%s \n",s); itoa(18, s); printf("%s \n",s); itob(18, s, 16); printf("%s \n",s); itob(-18, s, 8); printf("%s \n",s); itob(255, s, 16); printf("%s \n",s);}/*itoa : convert n to characters in sand can handle the largest negative numbnern = -2^(wordsize-1),regardless of the machine on which it runsminwidth is the least width of s that must be padded with blankson the left if necessary to make it wide enough*/void itoa_three(int n, char s[], int minwidth) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; while(i < minwidth) s[i++] = ' '; s[i] = '\0'; reverse(s,_strlen(s)); }/* test_itoa evaluating the brove of function */void test_itoa_three(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa_three(n, s,1); printf("%s \n",s); itoa_three(n, s, 38); printf("%s \n",s);}main(){test_itoa_three();}

'; return start_point; //return s2 length }void expand(char s1[], char s2[]){ int i ,start , end; int index = 0; (s1[0] == '-')?(i=1,s1[0]='a'):(i=0); while(s1[i] != '

#include #include "chap1.c"/*这个是上一章的课后题,里面的函数,刚好这次需要,但是需要把chap1.c 中的main方面注释掉因为,c语言中,一个文件中只能有一个main方法*/#include "chap2.c" /* chap2.c 是第二章的课后题 里面有的函数,这里需要使用使用 */#define abs(x) ((x)<0?-(x):(x)) /* 注意这里只是abs() 进行替换,如果看不懂可以看看这个/*3-3 : expand(s1,s2)*/int is_digit_letter(int c){ if(c >= 'a' && c <= 'z') return 1; else if(c >= '0' && c <= '9') return 1; return 0; }int expand_s2(char s2[], int start_point, int start, int end){ while(start <= end) s2[start_point++] = start++; s2[start_point] = '\0'; return start_point; //return s2 length }void expand(char s1[], char s2[]){ int i ,start , end; int index = 0; (s1[0] == '-')?(i=1,s1[0]='a'):(i=0); while(s1[i] != '\0') { if(is_digit_letter(s1[i])==1 && s1[i-1]=='-') { end = s1[i]; printf("end : %c : %d \n ", end, end); index = expand_s2(s2, index, start, end); printf("index : %c : %d \n ", index, index); if(s1[i+1] == '-') { start = ++end; printf("start : %c : %d \n ", start, start); } } else if(is_digit_letter(s1[i])) { start =s1[i]; printf("start : %c : %d \n ", start, start); } i++; }}/*看完答案之后,修改如下*/void expand_(char s1[], char s2[]){ char c; int i, j; i = j = 0; while((c=s1[i++]) != '\0') { if(is_digit_letter(c) && s1[i] == '-' && s1[i+1] >= c) { i++; while(c <= s1[i]) s2[j++] = c++; (s1[i+1] == '-')?(j--):' '; //处理 a-b-g ,重复出现b这种情况 } } s2[j] = '\0';}/*test_expand function: evaluating the expand function*/void test_expand(){ char s1[] = "a-z"; char s2[] = "a-c-g"; char s3[] = "a-c0-9"; char s4[] = "-a-c-g"; char s5[50]; expand_(s1,s5); printf("%s \n",s5); expand_(s2,s5); printf("%s \n",s5); expand_(s3,s5); printf("%s \n",s5); expand_(s4,s5); printf("%s \n",s5);}/* 3-4 首先,我们知道,有符号char 的取值范围: -128 到 127 同样的有符号的 int 取值范围是: 2^(sizeof(int)*8-1) 到 2^(sizeof(int)*8-1)-1 这个时候 : 2^(sizeof(int)*8-1) 这个值,在计算机输出的时候是一个负值,这个是最大负值 其实在补码表示的时候,他的补码和反码一样,举个例子char -128 补码 反码都是 1000 0000 所以,假如我们要把最大负数,转成字符串输出,就不能直接对n进行 n=-n 赋值操作,这样的操作 计算的结果肯定有问题,我们把n=-n 赋值操作,间接的放到处理数据的过程中去,这样就不存在 最大值的问题,因为处理数据的过程中,结果范围是 0-9 itoa : convert n to characters in s and can handle the largest negative numbner n = -2^(wordsize-1),regardless of the machine on which it runs*/void itoa(int n, char s[]) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; s[i] = '\0'; reverse(s,_strlen(s)); }/*test_itoa evaluating the brove of function*/void test_itoa(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 0; printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 23; printf("%d \n",n); itoa(n,s); printf("%s \n",s);}/*3-5 itob function : converts the integer n into a base b character representation in the string s */void itob(int n, char s[], int b){ int i = 0; int sign = n; int temp; do{ temp = abs(n%b); s[i++]= (temp >9)? (temp+55): (temp+'0'); }while(n /= b); (sign < 0)? (s[i++]='-'): ' '; s[i] = '\0'; reverse(s,_strlen(s));}void test_itob(){ char s[55]; itob(18, s, 10); printf("%s \n",s); itoa(18, s); printf("%s \n",s); itob(18, s, 16); printf("%s \n",s); itob(-18, s, 8); printf("%s \n",s); itob(255, s, 16); printf("%s \n",s);}/*itoa : convert n to characters in sand can handle the largest negative numbnern = -2^(wordsize-1),regardless of the machine on which it runsminwidth is the least width of s that must be padded with blankson the left if necessary to make it wide enough*/void itoa_three(int n, char s[], int minwidth) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; while(i < minwidth) s[i++] = ' '; s[i] = '\0'; reverse(s,_strlen(s)); }/* test_itoa evaluating the brove of function */void test_itoa_three(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa_three(n, s,1); printf("%s \n",s); itoa_three(n, s, 38); printf("%s \n",s);}main(){test_itoa_three();}

') { if(is_digit_letter(s1[i])==1 && s1[i-1]=='-') { end = s1[i]; printf("end : %c : %d \n ", end, end); index = expand_s2(s2, index, start, end); printf("index : %c : %d \n ", index, index); if(s1[i+1] == '-') { start = ++end; printf("start : %c : %d \n ", start, start); } } else if(is_digit_letter(s1[i])) { start =s1[i]; printf("start : %c : %d \n ", start, start); } i++; }}/*看完答案之后,修改如下*/void expand_(char s1[], char s2[]){ char c; int i, j; i = j = 0; while((c=s1[i++]) != '

#include #include "chap1.c"/*这个是上一章的课后题,里面的函数,刚好这次需要,但是需要把chap1.c 中的main方面注释掉因为,c语言中,一个文件中只能有一个main方法*/#include "chap2.c" /* chap2.c 是第二章的课后题 里面有的函数,这里需要使用使用 */#define abs(x) ((x)<0?-(x):(x)) /* 注意这里只是abs() 进行替换,如果看不懂可以看看这个/*3-3 : expand(s1,s2)*/int is_digit_letter(int c){ if(c >= 'a' && c <= 'z') return 1; else if(c >= '0' && c <= '9') return 1; return 0; }int expand_s2(char s2[], int start_point, int start, int end){ while(start <= end) s2[start_point++] = start++; s2[start_point] = '\0'; return start_point; //return s2 length }void expand(char s1[], char s2[]){ int i ,start , end; int index = 0; (s1[0] == '-')?(i=1,s1[0]='a'):(i=0); while(s1[i] != '\0') { if(is_digit_letter(s1[i])==1 && s1[i-1]=='-') { end = s1[i]; printf("end : %c : %d \n ", end, end); index = expand_s2(s2, index, start, end); printf("index : %c : %d \n ", index, index); if(s1[i+1] == '-') { start = ++end; printf("start : %c : %d \n ", start, start); } } else if(is_digit_letter(s1[i])) { start =s1[i]; printf("start : %c : %d \n ", start, start); } i++; }}/*看完答案之后,修改如下*/void expand_(char s1[], char s2[]){ char c; int i, j; i = j = 0; while((c=s1[i++]) != '\0') { if(is_digit_letter(c) && s1[i] == '-' && s1[i+1] >= c) { i++; while(c <= s1[i]) s2[j++] = c++; (s1[i+1] == '-')?(j--):' '; //处理 a-b-g ,重复出现b这种情况 } } s2[j] = '\0';}/*test_expand function: evaluating the expand function*/void test_expand(){ char s1[] = "a-z"; char s2[] = "a-c-g"; char s3[] = "a-c0-9"; char s4[] = "-a-c-g"; char s5[50]; expand_(s1,s5); printf("%s \n",s5); expand_(s2,s5); printf("%s \n",s5); expand_(s3,s5); printf("%s \n",s5); expand_(s4,s5); printf("%s \n",s5);}/* 3-4 首先,我们知道,有符号char 的取值范围: -128 到 127 同样的有符号的 int 取值范围是: 2^(sizeof(int)*8-1) 到 2^(sizeof(int)*8-1)-1 这个时候 : 2^(sizeof(int)*8-1) 这个值,在计算机输出的时候是一个负值,这个是最大负值 其实在补码表示的时候,他的补码和反码一样,举个例子char -128 补码 反码都是 1000 0000 所以,假如我们要把最大负数,转成字符串输出,就不能直接对n进行 n=-n 赋值操作,这样的操作 计算的结果肯定有问题,我们把n=-n 赋值操作,间接的放到处理数据的过程中去,这样就不存在 最大值的问题,因为处理数据的过程中,结果范围是 0-9 itoa : convert n to characters in s and can handle the largest negative numbner n = -2^(wordsize-1),regardless of the machine on which it runs*/void itoa(int n, char s[]) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; s[i] = '\0'; reverse(s,_strlen(s)); }/*test_itoa evaluating the brove of function*/void test_itoa(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 0; printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 23; printf("%d \n",n); itoa(n,s); printf("%s \n",s);}/*3-5 itob function : converts the integer n into a base b character representation in the string s */void itob(int n, char s[], int b){ int i = 0; int sign = n; int temp; do{ temp = abs(n%b); s[i++]= (temp >9)? (temp+55): (temp+'0'); }while(n /= b); (sign < 0)? (s[i++]='-'): ' '; s[i] = '\0'; reverse(s,_strlen(s));}void test_itob(){ char s[55]; itob(18, s, 10); printf("%s \n",s); itoa(18, s); printf("%s \n",s); itob(18, s, 16); printf("%s \n",s); itob(-18, s, 8); printf("%s \n",s); itob(255, s, 16); printf("%s \n",s);}/*itoa : convert n to characters in sand can handle the largest negative numbnern = -2^(wordsize-1),regardless of the machine on which it runsminwidth is the least width of s that must be padded with blankson the left if necessary to make it wide enough*/void itoa_three(int n, char s[], int minwidth) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; while(i < minwidth) s[i++] = ' '; s[i] = '\0'; reverse(s,_strlen(s)); }/* test_itoa evaluating the brove of function */void test_itoa_three(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa_three(n, s,1); printf("%s \n",s); itoa_three(n, s, 38); printf("%s \n",s);}main(){test_itoa_three();}

') { if(is_digit_letter(c) && s1[i] == '-' && s1[i+1] >= c) { i++; while(c <= s1[i]) s2[j++] = c++; (s1[i+1] == '-')?(j--):' '; //处理 a-b-g ,重复出现b这种情况 } } s2[j] = '

#include #include "chap1.c"/*这个是上一章的课后题,里面的函数,刚好这次需要,但是需要把chap1.c 中的main方面注释掉因为,c语言中,一个文件中只能有一个main方法*/#include "chap2.c" /* chap2.c 是第二章的课后题 里面有的函数,这里需要使用使用 */#define abs(x) ((x)<0?-(x):(x)) /* 注意这里只是abs() 进行替换,如果看不懂可以看看这个/*3-3 : expand(s1,s2)*/int is_digit_letter(int c){ if(c >= 'a' && c <= 'z') return 1; else if(c >= '0' && c <= '9') return 1; return 0; }int expand_s2(char s2[], int start_point, int start, int end){ while(start <= end) s2[start_point++] = start++; s2[start_point] = '\0'; return start_point; //return s2 length }void expand(char s1[], char s2[]){ int i ,start , end; int index = 0; (s1[0] == '-')?(i=1,s1[0]='a'):(i=0); while(s1[i] != '\0') { if(is_digit_letter(s1[i])==1 && s1[i-1]=='-') { end = s1[i]; printf("end : %c : %d \n ", end, end); index = expand_s2(s2, index, start, end); printf("index : %c : %d \n ", index, index); if(s1[i+1] == '-') { start = ++end; printf("start : %c : %d \n ", start, start); } } else if(is_digit_letter(s1[i])) { start =s1[i]; printf("start : %c : %d \n ", start, start); } i++; }}/*看完答案之后,修改如下*/void expand_(char s1[], char s2[]){ char c; int i, j; i = j = 0; while((c=s1[i++]) != '\0') { if(is_digit_letter(c) && s1[i] == '-' && s1[i+1] >= c) { i++; while(c <= s1[i]) s2[j++] = c++; (s1[i+1] == '-')?(j--):' '; //处理 a-b-g ,重复出现b这种情况 } } s2[j] = '\0';}/*test_expand function: evaluating the expand function*/void test_expand(){ char s1[] = "a-z"; char s2[] = "a-c-g"; char s3[] = "a-c0-9"; char s4[] = "-a-c-g"; char s5[50]; expand_(s1,s5); printf("%s \n",s5); expand_(s2,s5); printf("%s \n",s5); expand_(s3,s5); printf("%s \n",s5); expand_(s4,s5); printf("%s \n",s5);}/* 3-4 首先,我们知道,有符号char 的取值范围: -128 到 127 同样的有符号的 int 取值范围是: 2^(sizeof(int)*8-1) 到 2^(sizeof(int)*8-1)-1 这个时候 : 2^(sizeof(int)*8-1) 这个值,在计算机输出的时候是一个负值,这个是最大负值 其实在补码表示的时候,他的补码和反码一样,举个例子char -128 补码 反码都是 1000 0000 所以,假如我们要把最大负数,转成字符串输出,就不能直接对n进行 n=-n 赋值操作,这样的操作 计算的结果肯定有问题,我们把n=-n 赋值操作,间接的放到处理数据的过程中去,这样就不存在 最大值的问题,因为处理数据的过程中,结果范围是 0-9 itoa : convert n to characters in s and can handle the largest negative numbner n = -2^(wordsize-1),regardless of the machine on which it runs*/void itoa(int n, char s[]) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; s[i] = '\0'; reverse(s,_strlen(s)); }/*test_itoa evaluating the brove of function*/void test_itoa(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 0; printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 23; printf("%d \n",n); itoa(n,s); printf("%s \n",s);}/*3-5 itob function : converts the integer n into a base b character representation in the string s */void itob(int n, char s[], int b){ int i = 0; int sign = n; int temp; do{ temp = abs(n%b); s[i++]= (temp >9)? (temp+55): (temp+'0'); }while(n /= b); (sign < 0)? (s[i++]='-'): ' '; s[i] = '\0'; reverse(s,_strlen(s));}void test_itob(){ char s[55]; itob(18, s, 10); printf("%s \n",s); itoa(18, s); printf("%s \n",s); itob(18, s, 16); printf("%s \n",s); itob(-18, s, 8); printf("%s \n",s); itob(255, s, 16); printf("%s \n",s);}/*itoa : convert n to characters in sand can handle the largest negative numbnern = -2^(wordsize-1),regardless of the machine on which it runsminwidth is the least width of s that must be padded with blankson the left if necessary to make it wide enough*/void itoa_three(int n, char s[], int minwidth) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; while(i < minwidth) s[i++] = ' '; s[i] = '\0'; reverse(s,_strlen(s)); }/* test_itoa evaluating the brove of function */void test_itoa_three(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa_three(n, s,1); printf("%s \n",s); itoa_three(n, s, 38); printf("%s \n",s);}main(){test_itoa_three();}

';}/*test_expand function: evaluating the expand function*/void test_expand(){ char s1[] = "a-z"; char s2[] = "a-c-g"; char s3[] = "a-c0-9"; char s4[] = "-a-c-g"; char s5[50]; expand_(s1,s5); printf("%s \n",s5); expand_(s2,s5); printf("%s \n",s5); expand_(s3,s5); printf("%s \n",s5); expand_(s4,s5); printf("%s \n",s5);}/* 3-4 首先,我们知道,有符号char 的取值范围: -128 到 127 同样的有符号的 int 取值范围是: 2^(sizeof(int)*8-1) 到 2^(sizeof(int)*8-1)-1 这个时候 : 2^(sizeof(int)*8-1) 这个值,在计算机输出的时候是一个负值,这个是最大负值 其实在补码表示的时候,他的补码和反码一样,举个例子char -128 补码 反码都是 1000 0000 所以,假如我们要把最大负数,转成字符串输出,就不能直接对n进行 n=-n 赋值操作,这样的操作 计算的结果肯定有问题,我们把n=-n 赋值操作,间接的放到处理数据的过程中去,这样就不存在 最大值的问题,因为处理数据的过程中,结果范围是 0-9 itoa : convert n to characters in s and can handle the largest negative numbner n = -2^(wordsize-1),regardless of the machine on which it runs*/void itoa(int n, char s[]) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; s[i] = '

#include #include "chap1.c"/*这个是上一章的课后题,里面的函数,刚好这次需要,但是需要把chap1.c 中的main方面注释掉因为,c语言中,一个文件中只能有一个main方法*/#include "chap2.c" /* chap2.c 是第二章的课后题 里面有的函数,这里需要使用使用 */#define abs(x) ((x)<0?-(x):(x)) /* 注意这里只是abs() 进行替换,如果看不懂可以看看这个/*3-3 : expand(s1,s2)*/int is_digit_letter(int c){ if(c >= 'a' && c <= 'z') return 1; else if(c >= '0' && c <= '9') return 1; return 0; }int expand_s2(char s2[], int start_point, int start, int end){ while(start <= end) s2[start_point++] = start++; s2[start_point] = '\0'; return start_point; //return s2 length }void expand(char s1[], char s2[]){ int i ,start , end; int index = 0; (s1[0] == '-')?(i=1,s1[0]='a'):(i=0); while(s1[i] != '\0') { if(is_digit_letter(s1[i])==1 && s1[i-1]=='-') { end = s1[i]; printf("end : %c : %d \n ", end, end); index = expand_s2(s2, index, start, end); printf("index : %c : %d \n ", index, index); if(s1[i+1] == '-') { start = ++end; printf("start : %c : %d \n ", start, start); } } else if(is_digit_letter(s1[i])) { start =s1[i]; printf("start : %c : %d \n ", start, start); } i++; }}/*看完答案之后,修改如下*/void expand_(char s1[], char s2[]){ char c; int i, j; i = j = 0; while((c=s1[i++]) != '\0') { if(is_digit_letter(c) && s1[i] == '-' && s1[i+1] >= c) { i++; while(c <= s1[i]) s2[j++] = c++; (s1[i+1] == '-')?(j--):' '; //处理 a-b-g ,重复出现b这种情况 } } s2[j] = '\0';}/*test_expand function: evaluating the expand function*/void test_expand(){ char s1[] = "a-z"; char s2[] = "a-c-g"; char s3[] = "a-c0-9"; char s4[] = "-a-c-g"; char s5[50]; expand_(s1,s5); printf("%s \n",s5); expand_(s2,s5); printf("%s \n",s5); expand_(s3,s5); printf("%s \n",s5); expand_(s4,s5); printf("%s \n",s5);}/* 3-4 首先,我们知道,有符号char 的取值范围: -128 到 127 同样的有符号的 int 取值范围是: 2^(sizeof(int)*8-1) 到 2^(sizeof(int)*8-1)-1 这个时候 : 2^(sizeof(int)*8-1) 这个值,在计算机输出的时候是一个负值,这个是最大负值 其实在补码表示的时候,他的补码和反码一样,举个例子char -128 补码 反码都是 1000 0000 所以,假如我们要把最大负数,转成字符串输出,就不能直接对n进行 n=-n 赋值操作,这样的操作 计算的结果肯定有问题,我们把n=-n 赋值操作,间接的放到处理数据的过程中去,这样就不存在 最大值的问题,因为处理数据的过程中,结果范围是 0-9 itoa : convert n to characters in s and can handle the largest negative numbner n = -2^(wordsize-1),regardless of the machine on which it runs*/void itoa(int n, char s[]) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; s[i] = '\0'; reverse(s,_strlen(s)); }/*test_itoa evaluating the brove of function*/void test_itoa(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 0; printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 23; printf("%d \n",n); itoa(n,s); printf("%s \n",s);}/*3-5 itob function : converts the integer n into a base b character representation in the string s */void itob(int n, char s[], int b){ int i = 0; int sign = n; int temp; do{ temp = abs(n%b); s[i++]= (temp >9)? (temp+55): (temp+'0'); }while(n /= b); (sign < 0)? (s[i++]='-'): ' '; s[i] = '\0'; reverse(s,_strlen(s));}void test_itob(){ char s[55]; itob(18, s, 10); printf("%s \n",s); itoa(18, s); printf("%s \n",s); itob(18, s, 16); printf("%s \n",s); itob(-18, s, 8); printf("%s \n",s); itob(255, s, 16); printf("%s \n",s);}/*itoa : convert n to characters in sand can handle the largest negative numbnern = -2^(wordsize-1),regardless of the machine on which it runsminwidth is the least width of s that must be padded with blankson the left if necessary to make it wide enough*/void itoa_three(int n, char s[], int minwidth) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; while(i < minwidth) s[i++] = ' '; s[i] = '\0'; reverse(s,_strlen(s)); }/* test_itoa evaluating the brove of function */void test_itoa_three(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa_three(n, s,1); printf("%s \n",s); itoa_three(n, s, 38); printf("%s \n",s);}main(){test_itoa_three();}

'; reverse(s,_strlen(s)); }/*test_itoa evaluating the brove of function*/void test_itoa(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 0; printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 23; printf("%d \n",n); itoa(n,s); printf("%s \n",s);}/*3-5 itob function : converts the integer n into a base b character representation in the string s */void itob(int n, char s[], int b){ int i = 0; int sign = n; int temp; do{ temp = abs(n%b); s[i++]= (temp >9)? (temp+55): (temp+'0'); }while(n /= b); (sign < 0)? (s[i++]='-'): ' '; s[i] = '

#include #include "chap1.c"/*这个是上一章的课后题,里面的函数,刚好这次需要,但是需要把chap1.c 中的main方面注释掉因为,c语言中,一个文件中只能有一个main方法*/#include "chap2.c" /* chap2.c 是第二章的课后题 里面有的函数,这里需要使用使用 */#define abs(x) ((x)<0?-(x):(x)) /* 注意这里只是abs() 进行替换,如果看不懂可以看看这个/*3-3 : expand(s1,s2)*/int is_digit_letter(int c){ if(c >= 'a' && c <= 'z') return 1; else if(c >= '0' && c <= '9') return 1; return 0; }int expand_s2(char s2[], int start_point, int start, int end){ while(start <= end) s2[start_point++] = start++; s2[start_point] = '\0'; return start_point; //return s2 length }void expand(char s1[], char s2[]){ int i ,start , end; int index = 0; (s1[0] == '-')?(i=1,s1[0]='a'):(i=0); while(s1[i] != '\0') { if(is_digit_letter(s1[i])==1 && s1[i-1]=='-') { end = s1[i]; printf("end : %c : %d \n ", end, end); index = expand_s2(s2, index, start, end); printf("index : %c : %d \n ", index, index); if(s1[i+1] == '-') { start = ++end; printf("start : %c : %d \n ", start, start); } } else if(is_digit_letter(s1[i])) { start =s1[i]; printf("start : %c : %d \n ", start, start); } i++; }}/*看完答案之后,修改如下*/void expand_(char s1[], char s2[]){ char c; int i, j; i = j = 0; while((c=s1[i++]) != '\0') { if(is_digit_letter(c) && s1[i] == '-' && s1[i+1] >= c) { i++; while(c <= s1[i]) s2[j++] = c++; (s1[i+1] == '-')?(j--):' '; //处理 a-b-g ,重复出现b这种情况 } } s2[j] = '\0';}/*test_expand function: evaluating the expand function*/void test_expand(){ char s1[] = "a-z"; char s2[] = "a-c-g"; char s3[] = "a-c0-9"; char s4[] = "-a-c-g"; char s5[50]; expand_(s1,s5); printf("%s \n",s5); expand_(s2,s5); printf("%s \n",s5); expand_(s3,s5); printf("%s \n",s5); expand_(s4,s5); printf("%s \n",s5);}/* 3-4 首先,我们知道,有符号char 的取值范围: -128 到 127 同样的有符号的 int 取值范围是: 2^(sizeof(int)*8-1) 到 2^(sizeof(int)*8-1)-1 这个时候 : 2^(sizeof(int)*8-1) 这个值,在计算机输出的时候是一个负值,这个是最大负值 其实在补码表示的时候,他的补码和反码一样,举个例子char -128 补码 反码都是 1000 0000 所以,假如我们要把最大负数,转成字符串输出,就不能直接对n进行 n=-n 赋值操作,这样的操作 计算的结果肯定有问题,我们把n=-n 赋值操作,间接的放到处理数据的过程中去,这样就不存在 最大值的问题,因为处理数据的过程中,结果范围是 0-9 itoa : convert n to characters in s and can handle the largest negative numbner n = -2^(wordsize-1),regardless of the machine on which it runs*/void itoa(int n, char s[]) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; s[i] = '\0'; reverse(s,_strlen(s)); }/*test_itoa evaluating the brove of function*/void test_itoa(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 0; printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 23; printf("%d \n",n); itoa(n,s); printf("%s \n",s);}/*3-5 itob function : converts the integer n into a base b character representation in the string s */void itob(int n, char s[], int b){ int i = 0; int sign = n; int temp; do{ temp = abs(n%b); s[i++]= (temp >9)? (temp+55): (temp+'0'); }while(n /= b); (sign < 0)? (s[i++]='-'): ' '; s[i] = '\0'; reverse(s,_strlen(s));}void test_itob(){ char s[55]; itob(18, s, 10); printf("%s \n",s); itoa(18, s); printf("%s \n",s); itob(18, s, 16); printf("%s \n",s); itob(-18, s, 8); printf("%s \n",s); itob(255, s, 16); printf("%s \n",s);}/*itoa : convert n to characters in sand can handle the largest negative numbnern = -2^(wordsize-1),regardless of the machine on which it runsminwidth is the least width of s that must be padded with blankson the left if necessary to make it wide enough*/void itoa_three(int n, char s[], int minwidth) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; while(i < minwidth) s[i++] = ' '; s[i] = '\0'; reverse(s,_strlen(s)); }/* test_itoa evaluating the brove of function */void test_itoa_three(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa_three(n, s,1); printf("%s \n",s); itoa_three(n, s, 38); printf("%s \n",s);}main(){test_itoa_three();}

'; reverse(s,_strlen(s));}void test_itob(){ char s[55]; itob(18, s, 10); printf("%s \n",s); itoa(18, s); printf("%s \n",s); itob(18, s, 16); printf("%s \n",s); itob(-18, s, 8); printf("%s \n",s); itob(255, s, 16); printf("%s \n",s);}/*itoa : convert n to characters in sand can handle the largest negative numbnern = -2^(wordsize-1),regardless of the machine on which it runsminwidth is the least width of s that must be padded with blankson the left if necessary to make it wide enough*/void itoa_three(int n, char s[], int minwidth) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; while(i < minwidth) s[i++] = ' '; s[i] = '

#include #include "chap1.c"/*这个是上一章的课后题,里面的函数,刚好这次需要,但是需要把chap1.c 中的main方面注释掉因为,c语言中,一个文件中只能有一个main方法*/#include "chap2.c" /* chap2.c 是第二章的课后题 里面有的函数,这里需要使用使用 */#define abs(x) ((x)<0?-(x):(x)) /* 注意这里只是abs() 进行替换,如果看不懂可以看看这个/*3-3 : expand(s1,s2)*/int is_digit_letter(int c){ if(c >= 'a' && c <= 'z') return 1; else if(c >= '0' && c <= '9') return 1; return 0; }int expand_s2(char s2[], int start_point, int start, int end){ while(start <= end) s2[start_point++] = start++; s2[start_point] = '\0'; return start_point; //return s2 length }void expand(char s1[], char s2[]){ int i ,start , end; int index = 0; (s1[0] == '-')?(i=1,s1[0]='a'):(i=0); while(s1[i] != '\0') { if(is_digit_letter(s1[i])==1 && s1[i-1]=='-') { end = s1[i]; printf("end : %c : %d \n ", end, end); index = expand_s2(s2, index, start, end); printf("index : %c : %d \n ", index, index); if(s1[i+1] == '-') { start = ++end; printf("start : %c : %d \n ", start, start); } } else if(is_digit_letter(s1[i])) { start =s1[i]; printf("start : %c : %d \n ", start, start); } i++; }}/*看完答案之后,修改如下*/void expand_(char s1[], char s2[]){ char c; int i, j; i = j = 0; while((c=s1[i++]) != '\0') { if(is_digit_letter(c) && s1[i] == '-' && s1[i+1] >= c) { i++; while(c <= s1[i]) s2[j++] = c++; (s1[i+1] == '-')?(j--):' '; //处理 a-b-g ,重复出现b这种情况 } } s2[j] = '\0';}/*test_expand function: evaluating the expand function*/void test_expand(){ char s1[] = "a-z"; char s2[] = "a-c-g"; char s3[] = "a-c0-9"; char s4[] = "-a-c-g"; char s5[50]; expand_(s1,s5); printf("%s \n",s5); expand_(s2,s5); printf("%s \n",s5); expand_(s3,s5); printf("%s \n",s5); expand_(s4,s5); printf("%s \n",s5);}/* 3-4 首先,我们知道,有符号char 的取值范围: -128 到 127 同样的有符号的 int 取值范围是: 2^(sizeof(int)*8-1) 到 2^(sizeof(int)*8-1)-1 这个时候 : 2^(sizeof(int)*8-1) 这个值,在计算机输出的时候是一个负值,这个是最大负值 其实在补码表示的时候,他的补码和反码一样,举个例子char -128 补码 反码都是 1000 0000 所以,假如我们要把最大负数,转成字符串输出,就不能直接对n进行 n=-n 赋值操作,这样的操作 计算的结果肯定有问题,我们把n=-n 赋值操作,间接的放到处理数据的过程中去,这样就不存在 最大值的问题,因为处理数据的过程中,结果范围是 0-9 itoa : convert n to characters in s and can handle the largest negative numbner n = -2^(wordsize-1),regardless of the machine on which it runs*/void itoa(int n, char s[]) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; s[i] = '\0'; reverse(s,_strlen(s)); }/*test_itoa evaluating the brove of function*/void test_itoa(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 0; printf("%d \n",n); itoa(n,s); printf("%s \n",s); n = 23; printf("%d \n",n); itoa(n,s); printf("%s \n",s);}/*3-5 itob function : converts the integer n into a base b character representation in the string s */void itob(int n, char s[], int b){ int i = 0; int sign = n; int temp; do{ temp = abs(n%b); s[i++]= (temp >9)? (temp+55): (temp+'0'); }while(n /= b); (sign < 0)? (s[i++]='-'): ' '; s[i] = '\0'; reverse(s,_strlen(s));}void test_itob(){ char s[55]; itob(18, s, 10); printf("%s \n",s); itoa(18, s); printf("%s \n",s); itob(18, s, 16); printf("%s \n",s); itob(-18, s, 8); printf("%s \n",s); itob(255, s, 16); printf("%s \n",s);}/*itoa : convert n to characters in sand can handle the largest negative numbnern = -2^(wordsize-1),regardless of the machine on which it runsminwidth is the least width of s that must be padded with blankson the left if necessary to make it wide enough*/void itoa_three(int n, char s[], int minwidth) { int sign, i; i = 0; sign = n; do { s[i++] = abs(n%10) + '0'; }while(n /= 10); (sign<0)? (s[i++] = '-'):' '; while(i < minwidth) s[i++] = ' '; s[i] = '\0'; reverse(s,_strlen(s)); }/* test_itoa evaluating the brove of function */void test_itoa_three(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa_three(n, s,1); printf("%s \n",s); itoa_three(n, s, 38); printf("%s \n",s);}main(){test_itoa_three();}

'; reverse(s,_strlen(s)); }/* test_itoa evaluating the brove of function */void test_itoa_three(){ char s[100]; int n = power(2,sizeof(int)*8-1); printf("%d \n",n); itoa_three(n, s,1); printf("%s \n",s); itoa_three(n, s, 38); printf("%s \n",s);}main(){test_itoa_three();}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:DELL PowerEdge R740xd Lifecycle Controller常用操作介绍
下一篇:通过PowerShell批量修改AD用户的UPN后缀
相关文章

 发表评论

暂时没有评论,来抢沙发吧~