本文介绍了K& R2部分2.7类型转换(练习)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是上述部分的练习2-3。我为它创建了一个 的解决方案,但是我看到了错误,我试图纠正它们但仍然是 它们在那里而且大多数都不在我的脑海中: -------------------------------计划---------- ---------------- / *第2.7节类型转换 我们被要求写一个函数 ; htoi(数组)完成 这个: 语句:这个程序转换一串十六进制数字in / 整数。字符串可以包含也可以不包含0X字符串。或0x或0x并且 允许的数字是0-9,af和AF。 它分4个步骤运行: 1.)它使用for进行用户的冒险循环在主中 2.)htoi呼叫heca without lead。 3.)" hexa_without_lead"删除前导0x或0X,如果有的话。 4.)然后它调用hexa_to_int。 onhexa_without_lead把这个 十六进制数转换成一个整数。 5.)" hexa_into_int"使用2个辅助函数:arr_size和 " base_conv" 完成将convertint十六进制数转换为 整数的工作。 * / #include< stdio.h> #include< ctype.h> unsigned long int htoi(char input_array); char hexa_without_lead(char arr []); unsigned long int hexa_to_int(char s []); int arr_size(char arr []); int main(void){ int i,c; unsigned long uli; const int MAXELEMENTS = 1000; char input_array [MAXELEMENTS]; for (i = 0; i< MAXELEMENTS&&((c = getchar())!= EOF || c!=''\ n''); + + i) { if(isdigit(c)|| c ==''a''|| c ==''A''|| c =='' b''|| c ==''B'' || c ==''c''|| c ==''C'' c = ='''''|| c ==''D''|| c ==''e''|| c ==''E' '|| c ==''f''c ==''F'') { input_array [i] = c; } 其他 printf(\ n你是IDIOT,我要你输入一个十六进制数字 \ n"); } if(c ==''\ n'') input_array [i] =''\'''; uli = htoi(input_array); printf(" \ n\t the输入的数字是:%ld",uli); 返回0; } unsigned long int htoi(char input_array) { unsigned long uli; char modified_hexa []; modified_hexa [] = hexa_without_lead(input_array); uli = hexa_to_int(modified_hexa); 返回uli; } char hexa_without_lead(char arr []) { char s []; int i; 为(i = 0; arr [i]!=''\'''; ++ i) { if(i == 0&& arr [i] ==''0''&&(arr [i +1] ==''x''|| arr [i + 1] == ''X'')) i = i + 2; s [i] = arr [i]; } s [i] =''\'0' '; 返回s; } unsigned long hexa_to_int(char s []) { unsigned long uli = 0; int i,len; enum {hexa_a = 10; hexa_b,hexa_c,hexa_d,hexa_e,hexa_f}; / * 这将给出hexa_b = 11,hexa_c = 12 ...... * / len = array_size(s) - 1; for(i = 0; s [i]!=''\ 0''&& len> = 0; ++ i) { if(isdigit(s [i])) uli = uli +(s [i] * base_conv(len)); else if(s [i] ==''a''||''A'') uli = uli +(hexa_a * base_conv(len)); else if(s [i] ==''b''||''B'') uli = uli +(hexa_b * base_conv(len)) ; else if(s [i] ==''c''||''C'') uli = uli +(hexa_c * base_conv(len)); else if(s [i] ==''d''||''D'') uli = uli +(hexa_d * base_conv(len)); else if(s [i] ==''e''||''E'') uli = uli +(hexa_e * base_conv(len)); else uli = uli +(hexa_f * base_conv(len)); --len; } 返回uli; } unsigned long base_conv(int n) { int base_unit = 16; while(n> = 0) { if(n == 0) base_unit = base_unit * 1; else base_unit = base_unit * base_unit; --n; } 返回base_unit; } int arr_size(char arr [ ]) { int i; for(i = 0; arr [i]!= EOF; ++ i) ; 返回i; } - -------------------------------输出 ---------- ----------------------------- [arch @ voodo kr2] $ gcc -std = c99 - Wall -Wextra 27_ex-2-3.c 27_ex-2-3.c:在函数''main''中: 27_ex-2-3.c: 44:错误:预期'')''在''c'之前''/ b 27_ex-2-3.c:57:警告:传递''htoi''的参数1使得整数 来自没有演员的指针 27_ex-2-3.c:在函数''htoi''中: 27_ex-2-3.c: 69:错误:''modified_hexa'中缺少数组大小 27_ex-2-3.c:72:错误:'''''之前的预期表达式 27_ex-2-3.c:72:警告:传递''hexa_without_lead''的参数1' 从没有投射的整数中生成指针 27_ex-2-3。 c:在函数''hexa_without_lead''中: 27_ex-2-3.c:82:错误:'s'中缺少数组大小 27_ex-2-3.c:94:警告:返回从指针生成整数而没有 cast 27_ex-2-3.c:94:警告:函数返回局部变量的地址 27_ex-2-3.c:在函数''hexa_to_int''中: 27_ex-2-3.c:103:错误:预期'',''或''}''之前''''''令牌 27_ex-2-3.c:106:警告:隐含的功能声明 ''array_size'' 27_ex-2-3.c:111:警告:隐式声明函数''base_conv'' 27_ex-2-3.c: 117:错误:''hexa_b''未声明(首次使用此 函数) 27_ex-2-3.c:117:错误:(每个未声明的标识符是报告只有 一次 27_ex-2-3.c:117:错误:对于它出现的每个功能。) 27_ex-2 -3.c:120:错误:''hexa_c''未声明(首次使用此 函数) 27_ex-2-3.c:123:错误: ''hexa_d''未声明(首次使用此 功能离子) 27_ex-2-3.c:126:错误:''hexa_e''未声明(首次使用此 函数) 27_ex-2-3.c:129:错误:''hexa_f''未声明(首次使用此 函数) 27_ex-2-3.c:顶级: 27_ex-2-3.c:139:错误:''base_conv'的冲突类型' 27_ex-2-3.c:111:错误:先前隐式声明''base_conv'' 在这里 [arch @ voodo kr2] $ this is exercise 2-3 from the mentioned section. i have created asolution for it but i see errors and i tried to correct them but stillthey are there and mostly are out of my head: ------------------------------- PROGRAMME --------------------------/* Section 2.7 type conversions we are asked to write a function "htoi(an array)" that accomplishesthis: STATEMENT: this programme converts a string of hexadecimal digits inotintegers. a string may or may not contain "0X" or "0x" andallowable digits are 0-9, a-f and A-F.it runs in 4 steps: 1.) it take sinout from user using "for" loop in "main"2.) "htoi" calls "heca without lead".3.) "hexa_without_lead" removes leading "0x" or "0X", if any.4.) it then calls "hexa_to_int" on "hexa_without_lead" to convert thishexadeccimal number into an integer. 5.) "hexa_into_int" uses 2 helper functions: "arr_size" and"base_conv"to complete its job of convertint the hexadecimal number into aninteger. */#include <stdio.h>#include <ctype.h> unsigned long int htoi(char input_array);char hexa_without_lead(char arr[]);unsigned long int hexa_to_int(char s[]);int arr_size(char arr[]); int main(void) { int i, c;unsigned long uli;const int MAXELEMENTS = 1000;char input_array[MAXELEMENTS]; for(i=0; i < MAXELEMENTS && ((c = getchar()) != EOF || c != ''\n''); ++i){if( isdigit(c) || c == ''a'' || c == ''A'' || c == ''b'' || c == ''B''|| c == ''c'' || c == ''C''c == ''d'' || c == ''D'' || c == ''e'' || c == ''E'' || c == ''f'' c == ''F'' ){input_array[i] = c;} elseprintf("\nYou are an IDIOT, i asked you to ENTER a hexadecimal number\n");} if( c == ''\n'')input_array[i] = ''\0'';uli = htoi(input_array); printf("\n\t the number entered is: %ld", uli); return 0;} unsigned long int htoi(char input_array){unsigned long uli;char modified_hexa[];modified_hexa[] = hexa_without_lead(input_array);uli = hexa_to_int(modified_hexa);return uli;}char hexa_without_lead(char arr[]){char s[];int i; for(i = 0; arr[i] != ''\0''; ++i){if(i == 0 && arr[i] == ''0'' && ( arr[i+1] == ''x'' || arr[i+1] ==''X''))i = i+2; s[i] = arr[i];} s[i] = ''\0'';return s;}unsigned long hexa_to_int(char s[]){unsigned long uli = 0;int i, len; enum { hexa_a = 10; hexa_b, hexa_c, hexa_d, hexa_e, hexa_f }; /*this will give hexa_b = 11, hexa_c = 12...... */len = array_size(s) - 1; for(i=0; s[i] != ''\0'' && len >= 0; ++i){if(isdigit(s[i]))uli = uli + (s[i] * base_conv(len)); else if(s[i] == ''a'' || ''A'')uli = uli + (hexa_a * base_conv(len)); else if(s[i] == ''b'' || ''B'')uli = uli + ( hexa_b * base_conv(len) ); else if(s[i] == ''c'' || ''C'')uli = uli + ( hexa_c * base_conv(len) ); else if(s[i] == ''d'' || ''D'')uli = uli + ( hexa_d * base_conv(len) ); else if(s[i] == ''e'' || ''E'')uli = uli + ( hexa_e * base_conv(len) ); elseuli = uli + ( hexa_f * base_conv(len) ); --len;} return uli;}unsigned long base_conv(int n){int base_unit = 16; while(n >= 0){if(n == 0)base_unit = base_unit * 1; elsebase_unit = base_unit * base_unit; --n;} return base_unit;} int arr_size(char arr[]){int i; for(i=0; arr[i] != EOF; ++i); return i;} -------------------------------- OUTPUT---------------------------------------[arch@voodo kr2]$ gcc -std=c99 -Wall -Wextra 27_ex-2-3.c27_ex-2-3.c: In function ''main'':27_ex-2-3.c:44: error: expected '')'' before ''c''27_ex-2-3.c:57: warning: passing argument 1 of ''htoi'' makes integerfrom pointer without a cast27_ex-2-3.c: In function ''htoi'':27_ex-2-3.c:69: error: array size missing in ''modified_hexa''27_ex-2-3.c:72: error: expected expression before '']'' token27_ex-2-3.c:72: warning: passing argument 1 of ''hexa_without_lead''makes pointer from integer without a cast27_ex-2-3.c: In function ''hexa_without_lead'':27_ex-2-3.c:82: error: array size missing in ''s''27_ex-2-3.c:94: warning: return makes integer from pointer without acast27_ex-2-3.c:94: warning: function returns address of local variable27_ex-2-3.c: In function ''hexa_to_int'':27_ex-2-3.c:103: error: expected '','' or ''}'' before '';'' token27_ex-2-3.c:106: warning: implicit declaration of function''array_size''27_ex-2-3.c:111: warning: implicit declaration of function ''base_conv''27_ex-2-3.c:117: error: ''hexa_b'' undeclared (first use in thisfunction)27_ex-2-3.c:117: error: (Each undeclared identifier is reported onlyonce27_ex-2-3.c:117: error: for each function it appears in.)27_ex-2-3.c:120: error: ''hexa_c'' undeclared (first use in thisfunction)27_ex-2-3.c:123: error: ''hexa_d'' undeclared (first use in thisfunction)27_ex-2-3.c:126: error: ''hexa_e'' undeclared (first use in thisfunction)27_ex-2-3.c:129: error: ''hexa_f'' undeclared (first use in thisfunction)27_ex-2-3.c: At top level:27_ex-2-3.c:139: error: conflicting types for ''base_conv''27_ex-2-3.c:111: error: previous implicit declaration of ''base_conv''was here[arch@voodo kr2]$ 推荐答案 gcc -std = c99 -Wall -Wextra 27_ex-2-3.c 27_ex-2-3.c:在函数''main'' : 27_ex-2-3.c:44:错误:预期'')''在''c'之前' 27_ex-2-3.c:57 :警告:传递''htoi''的参数1使得整数 来自没有投射的指针 27_ex-2-3.c:在函数''htoi''中: 27_ex-2-3.c:69:错误:''modified_hexa'中缺少数组大小 27_ex-2-3.c:72:错误或:'''''之前的预期表达式令牌 27_ex-2-3.c:72:警告:传递参数1''hexa_without_lead'' 制作指针来自没有演员的整数 27_ex-2-3.c:在函数''hexa_without_lead''中: 27_ex-2-3.c:82:错误:数组's'中缺少大小 27_ex-2-3.c:94:警告:返回使指针整数没有 施放 27_ex-2-3.c:94:警告:函数返回局部变量的地址 27_ex-2-3.c:在函数''hexa_to_int''中: 27_ex-2-3.c:103:错误:预期'',''或'''''''''''''令牌 27_ex-2-3.c:106 :警告:隐式声明函数 ''array_size'' 27_ex-2-3.c:111:警告:隐式声明函数''base_conv'' 27_ex-2-3.c:117:错误:''hexa_b''未声明(首次使用此 函数) 27_ex- 2-3.c:117:错误r :(每个未声明的标识符仅报告 一次 27_ex-2-3.c:117:错误:对于它出现的每个函数。) 27_ex-2-3.c:120:错误:''hexa_c''未声明(首次使用此 函数) 27_ex-2-3 .c:123:错误:''hexa_d''未声明(首次使用此 函数) 27_ex-2-3.c:126:错误:'' hexa_e''未声明(首次使用此 函数) 27_ex-2-3.c:129:错误:''hexa_f''未声明(首次使用此处) 函数) 27_ex-2-3.c:顶级: 27_ex-2-3.c:139:错误: ''base_conv'的冲突类型' 27_ex-2-3.c:111:错误:先前隐含的''base_conv'声明' 在这里 [arch @ voodo kr2] gcc -std=c99 -Wall -Wextra 27_ex-2-3.c27_ex-2-3.c: In function ''main'':27_ex-2-3.c:44: error: expected '')'' before ''c''27_ex-2-3.c:57: warning: passing argument 1 of ''htoi'' makes integerfrom pointer without a cast27_ex-2-3.c: In function ''htoi'':27_ex-2-3.c:69: error: array size missing in ''modified_hexa''27_ex-2-3.c:72: error: expected expression before '']'' token27_ex-2-3.c:72: warning: passing argument 1 of ''hexa_without_lead''makes pointer from integer without a cast27_ex-2-3.c: In function ''hexa_without_lead'':27_ex-2-3.c:82: error: array size missing in ''s''27_ex-2-3.c:94: warning: return makes integer from pointer without acast27_ex-2-3.c:94: warning: function returns address of local variable27_ex-2-3.c: In function ''hexa_to_int'':27_ex-2-3.c:103: error: expected '','' or ''}'' before '';'' token27_ex-2-3.c:106: warning: implicit declaration of function''array_size''27_ex-2-3.c:111: warning: implicit declaration of function ''base_conv''27_ex-2-3.c:117: error: ''hexa_b'' undeclared (first use in thisfunction)27_ex-2-3.c:117: error: (Each undeclared identifier is reported onlyonce27_ex-2-3.c:117: error: for each function it appears in.)27_ex-2-3.c:120: error: ''hexa_c'' undeclared (first use in thisfunction)27_ex-2-3.c:123: error: ''hexa_d'' undeclared (first use in thisfunction)27_ex-2-3.c:126: error: ''hexa_e'' undeclared (first use in thisfunction)27_ex-2-3.c:129: error: ''hexa_f'' undeclared (first use in thisfunction)27_ex-2-3.c: At top level:27_ex-2-3.c:139: error: conflicting types for ''base_conv''27_ex-2-3.c:111: error: previous implicit declaration of ''base_conv''was here[arch@voodo kr2] arnuld写道: arnuld wrote: 这是来自上述部分的练习2-3。我已经为它创建了一个 的解决方案,但我看到了错误,我试图纠正它们但仍然是 它们在那里并且大多数都不在我的脑海中: this is exercise 2-3 from the mentioned section. i have created asolution for it but i see errors and i tried to correct them but stillthey are there and mostly are out of my head: < snip> 好了,修复gcc报告的错误和警告。你正在制造几个基本的错误。你还没有理解如何将 数组传递给函数,如何将原型与函数定义匹配, 在尝试使用它们之前声明对象以及各种类型的范围 的物品。 我怀疑你没有完全跳过K& R $ 了解之前的资料。这个组不能重复 纠正你犯的同样错误。你必须证明你能够学习并避免以前的错误。 开始正确,或者尝试纠正,gcc所抱怨的所有错误(不是 警告)。从错误开始 首先提到并在每次更正后,重新编译并记下新的 诊断消息。这样做直到所有错误都被删除。然后我们 可以进展到产生警告的构造。 总是在-std = XX时使用-pedantic选项,否则gcc可以接受 实现特定扩展。 <snip> Well, fix the errors and warnings gcc is reporting. You''re makingseveral fundamental mistakes. You''ve not yet understood how to passarrays to functions, how to match prototype with function definition,declaring objects before trying to use them and scope of various typesof objects. I suspect that you''re skipping ahead in K&R without fullyunderstanding the previous material. This group cannot repeatedlycorrect the same errors you make. You''ll have to demonstrate thatyou''re able to learn and desist from previous mistakes. To start with correct, or try to correct, all the errors, (notwarnings), that gcc is complaining about. Start from the errorsmentioned first and after each correction, recompile and note the newdiagnostic messages. Do this till all the errors are removed. Then wecan progress to constructs that generate a warning. Always use the -pedantic option with -std=XX, otherwise gcc may acceptimplementation specific extensions. 这篇关于K& R2部分2.7类型转换(练习)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 11:10