请编写一个函数fun,它的功能是:将一个表示正整数的数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。例如,若输入字符串“1234”,则函数把它转换为整数值1234。函数fun中给出的语句仅供参考。
注意:部分源程序存在文件PROG1.C文件中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
/**********code.c**********/
#include
#include
long fun ( char *p)
%7B*****B7%
/* 以下代码仅供参考*/
int i,len; /* len为串长 */
long x=0;
len=strlen(p);
/* 以下完成数字字符串转换为数字,注意字符’0’不是数字0*/
return x;
%7D*****D7%
void main()
%7B*****B7%
char s[6];void NONO ();
long n;
printf("Enter a string:\n") ;
gets(s);
n = fun(s);
printf("%ld\n",n);
NONO ( );
%7D*****D7%
void NONO ()
%7B*****B7%
/* 本函数用于执行用例,勿动*/
FILE *fp, *wf ;
int i ;
char s[20] ;
long n ;
fp = fopen("in.dat","r") ;
wf = fopen("out.dat","w") ;
for(i = 0 ; i < 10 ; i++)
%7B*****B7%
fscanf(fp, "%s", s) ;
n = fun(s);
fprintf(wf, "%ld\n", n) ;
%7D*****D7%
fclose(fp) ;
fclose(wf) ;
%7D*****D7%
/**********-code.c**********/
/**********indat**********/
1234
5553
6546
4242
6776
4522
5345
6643
8686
12101
/**********-indat**********/
/**********outdat**********/
1234
5553
6546
4242
6776
4522
5345
6643
8686
12101
/**********-outdat**********/