[问答题]

请编写一个函数fun,它的功能是:将一个表示正整数的数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。例如,若输入字符串“1234”,则函数把它转换为整数值1234。函数fun中给出的语句仅供参考。
  注意:部分源程序存在文件PROGC文件中。
  请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
/**********code.c**********/
#include <stdio.h>
#include <string.h>
long fun ( char *p)
{
   int i,len; /* len为串长*/
   long x=0;
   len=strlen(p);
   /*以下完成数字字符串转换为数字,注意字符'0'不是数字0*/
   
   
   return x;
}
void main()  
{
   char s[20] = {"46512"};void NONO ();
   long n = fun(s);
   printf("%ld ",n);
   NONO ( );
}
void NONO ()
{
   /*本函数用于执行用例,勿动*/
   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++)
   {
       fscanf(fp, "%s", s) ;
       n = fun(s);
       fprintf(wf, "%ld ", n) ;
   }
   fclose(fp) ;
   fclose(wf) ;
}
/**********-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**********/

参考答案与解析:

相关试题

请编写一个函数fun,它的功能是:将一个表示正整数的数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。例如,若输入字符串“1234”,则函数把它转换为整数值1234。函数fun