鱼C论坛

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

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

[复制链接]
发表于 2019-8-10 14:49:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我是C语言初学,问一下,这个计算代码量的程序写完也运行了。但是结果却是0,是哪里出了问题?存储路径有问题吗?

运行后显示0

运行后显示0
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

使用道具 举报

 楼主| 发表于 2019-8-10 15:12:06 | 显示全部楼层
jackz007 发表于 2019-8-10 15:03
你是在说单口相声吗?上代码呀!

    代码不要贴成图片,更简单的方法是贴到代码框里。试试快捷工具 ...


#include <io.h>
#include <direct.h>
#include <stdio.h>
#include  <stdlib.h>
#include <string.h>

#define MAX             256


long total;

int countlines(const char *filename);
void findAllCodes(const char*path);
void findALLFiles(const char*path);

int  countLines(const char*filename)
{
       
        FILE*fp;
        int count = 0;
        int temp;
       
       
        if((fp = fopen(filename,"r")) == NULL)
        {
                fprintf(stderr,"Can not open the file:%s\n",filename);
                return 0;
               
        }
       
       
        while ((temp = fgetc(fp))!= EOF)
        {
                if(temp == '\n')
                {
                        count++;
                }
                               
               
        }
       
       
        fclose(fp);
       
        return count;
       
       
       
       
       
}



void findAllCodes(const char*path)
{
       
        struct _finddata_t fa;
        long handle;
        char thePath[MAX],target[MAX];
       
       
        strcpy(thePath,path);
        if((handle = _findfirst(strcat(thePath,"/.c"),&fa))!= -1L)
        {
               
                do
                {
                         sprintf(target,"%s/%s",path,fa.name);
                         total += countLines(target);
                         
                       
                }while(_findnext(handle,&fa) == 0);
               
               
               
               
               
        }
          
       
       
       
        _findclose(handle);
       
       
       
}




void findALLDirs(const char*path)
{
        struct _finddata_t fa;
        long handle;
        char thePath[MAX];
       
        strcpy(thePath,path);
        if((handle = _findfirst(strcat(thePath,"/*"),&fa)) == -1L)
        {
                fprintf(stderr,"The path %s is wrong!\n",path);
                return;
         }
         
         do
         {
                
                 if(!strcmp(fa.name,".")|| !strcmp(fa.name,".."))
            continue;
                       
                       
                       
                        if(fa.attrib == _A_SUBDIR)
                        {
                                sprintf(thePath,"%s/%s",path,fa.name);
                                findAllCodes(thePath);
                                findALLDirs(thePath);
                               
                               
                               
                                         }                
                
         }while(_findnext(handle,&fa) == 0);
         
         _findclose(handle);
         
         
         
}

int main()

{
       
        char path[MAX] = ".";
       
        printf("计算值...\n");
       
        findAllCodes(path);
        findALLDirs(path);
       
        printf("目前你总共写了%ld 行代码!\n\n",total);
        system("pause");
       
       
       
        return 0;
       
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-10 15:13:04 | 显示全部楼层

  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;

  8. int countlines(const char *filename);
  9. void findAllCodes(const char*path);
  10. void findALLFiles(const char*path);

  11. int  countLines(const char*filename)
  12. {
  13.        
  14.         FILE*fp;
  15.         int count = 0;
  16.         int temp;
  17.        
  18.        
  19.         if((fp = fopen(filename,"r")) == NULL)
  20.         {
  21.                 fprintf(stderr,"Can not open the file:%s\n",filename);
  22.                 return 0;
  23.                
  24.         }
  25.        
  26.        
  27.         while ((temp = fgetc(fp))!= EOF)
  28.         {
  29.                 if(temp == '\n')
  30.                 {
  31.                         count++;
  32.                 }
  33.                                
  34.                
  35.         }
  36.        
  37.        
  38.         fclose(fp);
  39.        
  40.         return count;
  41.        
  42.        
  43.        
  44.        
  45.        
  46. }



  47. void findAllCodes(const char*path)
  48. {
  49.        
  50.         struct _finddata_t fa;
  51.         long handle;
  52.         char thePath[MAX],target[MAX];
  53.        
  54.        
  55.         strcpy(thePath,path);
  56.         if((handle = _findfirst(strcat(thePath,"/.c"),&fa))!= -1L)
  57.         {
  58.                
  59.                 do
  60.                 {
  61.                          sprintf(target,"%s/%s",path,fa.name);
  62.                          total += countLines(target);
  63.                          
  64.                        
  65.                 }while(_findnext(handle,&fa) == 0);
  66.                
  67.                
  68.                
  69.                
  70.                
  71.         }
  72.           
  73.        
  74.        
  75.        
  76.         _findclose(handle);
  77.        
  78.        
  79.        
  80. }




  81. void findALLDirs(const char*path)
  82. {
  83.         struct _finddata_t fa;
  84.         long handle;
  85.         char thePath[MAX];
  86.        
  87.         strcpy(thePath,path);
  88.         if((handle = _findfirst(strcat(thePath,"/*"),&fa)) == -1L)
  89.         {
  90.                 fprintf(stderr,"The path %s is wrong!\n",path);
  91.                 return;
  92.          }
  93.          
  94.          do
  95.          {
  96.                 
  97.                  if(!strcmp(fa.name,".")|| !strcmp(fa.name,".."))
  98.             continue;
  99.                        
  100.                        
  101.                        
  102.                         if(fa.attrib == _A_SUBDIR)
  103.                         {
  104.                                 sprintf(thePath,"%s/%s",path,fa.name);
  105.                                 findAllCodes(thePath);
  106.                                 findALLDirs(thePath);
  107.                                
  108.                                
  109.                                
  110.                                          }                
  111.                 
  112.          }while(_findnext(handle,&fa) == 0);
  113.          
  114.          _findclose(handle);
  115.          
  116.          
  117.          
  118. }

  119. int main()

  120. {
  121.        
  122.         char path[MAX] = ".";
  123.        
  124.         printf("计算值...\n");
  125.        
  126.         findAllCodes(path);
  127.         findALLDirs(path);
  128.        
  129.         printf("目前你总共写了%ld 行代码!\n\n",total);
  130.         system("pause");
  131.        
  132.        
  133.        
  134.         return 0;
  135.        
  136. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-10 15:14:00 | 显示全部楼层
jackz007 发表于 2019-8-10 15:03
你是在说单口相声吗?上代码呀!

    代码不要贴成图片,更简单的方法是贴到代码框里。试试快捷工具 ...

代码我都是照着抄的,没报错运行了,结果显示0 - - ,醉了
小甲鱼最新课程 -> 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
回复 支持 反对

使用道具 举报

发表于 2019-8-10 15:43:24 From FishC Mobile | 显示全部楼层
大佬  商量个事行不  加个微信一起学   主要是我想时不时借一下你的号  给钱也行     我晚上用也行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-10 21:13:22 | 显示全部楼层
我也是跟楼主遇到同样的问题,请问哪位大佬知道怎么回事?!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-10 21:22:40 | 显示全部楼层
buddy85 发表于 2019-8-10 21:13
我也是跟楼主遇到同样的问题,请问哪位大佬知道怎么回事?!

很奇怪,我代码重抄了一遍又好了。关键没报错运行了,检查了好几遍代码也没抄错- - ,打算学到后面再回头来看看啥问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-10 21:23:34 | 显示全部楼层
辉煌耀世 发表于 2019-8-10 15:43
大佬  商量个事行不  加个微信一起学   主要是我想时不时借一下你的号  给钱也行     我晚上用也行

我这号也是跟同学两个人买的。AA
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-10 21:24:07 | 显示全部楼层
jackz007 发表于 2019-8-10 15:21
楼主再抄这个代码看看呢:

辛苦辛苦,我主要是想知道我原来代码错在哪里- -,谢谢你啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-10 21:35:49 | 显示全部楼层
King.c 发表于 2019-8-10 21:22
很奇怪,我代码重抄了一遍又好了。关键没报错运行了,检查了好几遍代码也没抄错- - ,打算学到后面再回头 ...

你在吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-10 22:08:15 | 显示全部楼层

在线的,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-10 22:46:12 From FishC Mobile | 显示全部楼层
楼主,你看下你的编译器保存的C文件后缀名是不是“.c”文件。如果不是,注意代码if((handle = _findfirst(strcat(thePath,"/.c"),&fa))!= -1L),把“.c”改一下,改成你保存的文件后缀名。比如C++的“.cpp”格式。你可以这样试一下。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-8-12 07:54:26 | 显示全部楼层

在吗,能加个微信或者QQ吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-20 19:26:30 | 显示全部楼层

可以实验一下,既然你写的代码运行结果为零,那把小甲鱼的代码复制粘贴在运行,看看结果是什么。要是结果.还是零,那可能是IDE的原因了。之后就建议百度 了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-21 05:48:27 | 显示全部楼层
主楼我想问一下第六页的这个第一个程序这章的开发工具 code blocks和Dev-c++是在哪下载的 书上的网址没有
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-21 11:57:57 | 显示全部楼层
黄皮耗子 发表于 2019-8-21 05:48
主楼我想问一下第六页的这个第一个程序这章的开发工具 code blocks和Dev-c++是在哪下载的 书上的网址没有

这两个IDE可以在“论坛”    → “《带你学C带你飞》【第一季】”  →  扩展阅读里面有,自己找下
需要弄百度网盘哦!里面有链接你点击保存,然后在下载。
温馨提示:百度网盘限速,建议使用PanDownload上网下载,注:先要保存要下的文件,或你可以提取链接
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-3 17:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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