鱼C论坛

 找回密码
 立即注册
查看: 1314|回复: 3

[已解决]关于C语言第一个课后作业计数代码的一点问题

[复制链接]
发表于 2021-9-29 23:30:30 | 显示全部楼层 |阅读模式

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

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

x
问题1:
不论是自己手敲的代码还是答案复制的代码,运行下来都是已敲代码为0

                               
登录/注册后可看大图


  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.         FILE*fp;
  14.         int count=0;
  15.         int temp;
  16.        
  17.         if((fp=fopen(filename,"r"))==NULL)
  18.     {
  19.             fprintf(stderr,"Can not open the file:%s\n",filename);
  20.             return 0;
  21.         }
  22.        
  23.         while((temp=fgetc(fp))!=EOF)
  24.         {
  25.                 if(temp=='\n')
  26.                 {
  27.                         count++;
  28.                 }
  29.         }
  30.         fclose(fp);
  31.        
  32.         return count;
  33. }
  34. void findAllCodes(const char*path)
  35. {
  36.         struct _finddata_t fa;
  37.         long handle;
  38.         char thePath[MAX],target[MAX];
  39.        
  40.         strcpy(thePath,path);
  41.         if((handle=_findfirst(strcat(thePath,"/*.c"),&fa))!=-1L)
  42.         {
  43.                 do
  44.                 {
  45.                         sprintf(target,"%s/%s",path,fa.name);
  46.             total += countLines(target);
  47.                 }while(_findnext(handle,&fa)==0);
  48.         }
  49.         _findclose(handle);
  50. }
  51. void findALLDirs(const char*path)
  52. {
  53.         struct _finddata_t fa;
  54.         long handle;
  55.         char thePath[MAX];
  56.        
  57.         strcpy(thePath,path);
  58.         if((handle=_findfirst(strcat(thePath,"/*"),&fa))==-1L)
  59.         {
  60.                 fprintf(stderr,"The path%s is wrong!\n",path);
  61.                 return;
  62.         }
  63.        
  64.         do
  65.         {
  66.                 if(!strcmp(fa.name,".")||!strcmp(fa.name,".."))
  67.                     continue;
  68.                
  69.                 if(fa.attrib==_A_SUBDIR)
  70.                 {
  71.                         sprintf(thePath,"%s/%s",path,fa.name);
  72.                         findAllCodes(thePath);
  73.                         findALLDirs(thePath);
  74.                 }
  75.         }while(_findnext(handle,&fa)==0);
  76.        
  77.         _findclose(handle);
  78. }

  79. int main()
  80. {
  81.         char path[MAX]=".";
  82.        
  83.         printf("计算中...\n");
  84.        
  85.         findAllCodes(path);
  86.         findALLDirs(path);
  87.        
  88.         printf("目前你总共写了%ld行代码!\n\n",total);
  89.         system("pause");
  90.         return 0;
  91. }
复制代码


问题2:
是不是我放的位置不对?以后写的代码是都要放在当前目录下还是新建一个文件夹存放?
https://static01.imgkr.com/temp/ ... 24b7c23314819e0.jpg
以后写的代码是都要放在当前目录下还是新建一个文件夹存放?
最佳答案
2021-9-29 23:32:45
这是 C语言,不是 C++,把文件后缀改成 .c
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-29 23:32:45 | 显示全部楼层    本楼为最佳答案   
这是 C语言,不是 C++,把文件后缀改成 .c
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-29 23:59:10 | 显示全部楼层
后续代码想要用这个计数应该怎么做呀?
该放在哪个目录?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-30 00:15:09 | 显示全部楼层
自己摸索后,发现所有代码的后缀改成.c就能被计数上了。nice呀!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 06:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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