鱼C论坛

 找回密码
 立即注册
查看: 4539|回复: 17

大家好,我是新学C语言的小白,目前刚接触带你学C带你飞的课程。

[复制链接]
发表于 2019-8-10 15:03:14 | 显示全部楼层
    你是在说单口相声吗?上代码呀!

    代码不要贴成图片,更简单的方法是贴到代码框里。试试快捷工具栏里标有 "<>" 的按钮。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-10 15:21:32 | 显示全部楼层
      楼主再抄这个代码看看呢:
  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #define MAX 256

  7. long total = 0 ;

  8. bool filter(const char * fn)
  9. {
  10.         bool ret                                                                                                                       ;
  11.         int k , m                                                                                                                      ;

  12.         ret = false                                                                                                                    ;
  13.         m = strlen(fn)                                                                                                                 ;
  14.         for(k = m - 1 ; k > 0 && fn[k] != '.' ; k --)                                                                                  ;
  15.         if((k > 0 && k < m - 1) && (! stricmp(& fn[k] , ".c") || ! stricmp(& fn[k] , ".h") || ! stricmp(& fn[k] , ".cpp"))) ret = true ;
  16.         return ret                                                                                                                     ;
  17. }

  18. int countLines(const char * filename)
  19. {
  20.         FILE * fp                                                ;
  21.         int count = 0                                            ;

  22.         if((fp = fopen(filename , "r")) != NULL) {
  23.                 while(! feof(fp)) if(fgetc(fp) == '\n') count ++ ;
  24.                 fclose(fp)                                       ;
  25.         } else {
  26.                 count = - 1                                      ;
  27.         }
  28.         printf("%6d | %s\n" , count , filename)                  ;
  29.         return count                                             ;
  30. }

  31. void findall(const char * path)
  32. {
  33.         struct _finddata_t fa                                                                       ;
  34.         long handle                                                                                 ;
  35.         char bf[MAX] , wb[MAX]                                                                      ;
  36.         int m                                                                                       ;

  37.         strcpy(bf , path)                                                                           ;
  38.         if(bf[strlen(bf) - 1] != '\\') strcat(bf , "\")                                            ;
  39.         strcpy(wb , bf)                                                                             ;
  40.         strcat(wb , "*.*")     ;
  41.         if((handle = _findfirst(wb , & fa)) != -1L) {
  42.                 strcpy(wb , bf)                                                                     ;
  43.                 strcat(wb , fa . name)                                                              ;               
  44.                 if (fa . attrib & _A_SUBDIR) {
  45.                         if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) findall(wb)         ;
  46.                 } else {
  47.                         if(filter(wb) && ((m = countLines(wb)) > 0)) total += m                     ;
  48.                 }
  49.                 while(! _findnext(handle , & fa)) {
  50.                         strcpy(wb , bf)                                                             ;
  51.                         strcat(wb , fa . name)                                                      ;               
  52.                         if (fa . attrib & _A_SUBDIR) {
  53.                                 if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) findall(wb) ;
  54.                         } else {
  55.                                if(filter(wb) && ((m = countLines(wb)) > 0)) total += m              ;                     
  56.                         }
  57.                 }
  58.                 _findclose(handle)                                                                  ;
  59.         }                        
  60. }

  61. int main(void)
  62. {
  63.         char wb[MAX]                                                 ;

  64.         total = 0                                                    ;
  65.         printf("Input the search path [exit]: ")                     ;
  66.         scanf("%s" , wb)                                             ;
  67.         if(strlen(wb) > 0) {
  68.                 findall(wb)                                          ;
  69.                 printf("    Total source code lines : %d\n" , total) ;
  70.         }
  71. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-1 17:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表