004 判断一个数为几位数

#include  <stdio.h>
void NONO();
//判断形参中的n是几位数,并将结果通过函数值返回
int  fun(int  n)
{
    int bits=1;//临时变量统计位数
    while(n/10)//判断是否为0,若不为0,bits加1,再将n除以10,继续判断
    {
        bits++;
        n=n/10;
    }
    return bits;
}
void main()
{  int  n, place ;
   do{
      printf("请输入一个4位以内的正整数:    ");
      scanf("%d", &n);
     }  while (n<0 || n>9999);
   place = fun(n);
   printf( "输入的数字是%d位\n", place );
   NONO();
}

void NONO()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  FILE *fp, *wf ;
  int i, n, place ;

  fp = fopen("in.dat","r") ;
  wf = fopen("out.dat","w") ;
  for( i=0; i<10; i++ )
  {
     fscanf(fp, "%d ", &n);
     place = fun(n);
     fprintf(wf, "%d\n", place);
  }
  fclose(fp) ;
  fclose(wf) ;
}

猜你喜欢

转载自blog.csdn.net/baidu_28916787/article/details/82155157
004