鱼C论坛

 找回密码
 立即注册
查看: 2712|回复: 8

C第一节课课后题统计代码行数的这个程序只统计这个一个文件的吗?

[复制链接]
发表于 2019-12-19 10:24:32 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sys / stat.h>

  7. #定义MAX 256

  8. 总长

  9. int countLines(const char *文件名);
  10. int isCode(const char *文件名);
  11. 无效findAllDirs(const char * path);

  12. int countLines(const char *文件名)
  13. {
  14.         文件* fp;
  15.         int count = 0;
  16.         温度

  17.         如果((fp = fopen(文件名,“ r”))== NULL)
  18.         {
  19.                 fprintf(stderr,“无法打开文件:%s \ n”,文件名);
  20.                 返回0;
  21.         }

  22.         而((temp = fgetc(fp))!= EOF)
  23.         {
  24.                 如果(temp =='\ n')
  25.                 {
  26.                         数++;
  27.                 }
  28.         }

  29.         fclose(fp);

  30.         返回计数;
  31. }

  32. int isCode(const char *文件名)
  33. {
  34.         整数长度

  35.         长度= strlen(文件名);
  36.         
  37.         如果(!strcmp(文件名+(长度-2),“ .c”))
  38.         {
  39.                 返回1;
  40.         }
  41.         其他
  42.         {
  43.                 返回0;
  44.         }
  45. }

  46. 无效findAllDirs(const char * path)
  47. {
  48.         DIR * dp;
  49.         struct dirent * entry;
  50.         struct stat statbuf;

  51.         如果((dp = opendir(path))== NULL)
  52.         {
  53.                 fprintf(stderr,“%s路径错误!\ n”,路径);
  54.                 返回;
  55.         }

  56.         chdir(路径);
  57.         而((entry = readdir(dp))!= NULL)
  58.         {
  59.                 lstat(entry-> d_name,&statbuf);

  60.                 if(!strcmp(“。”,entry-> d_name)||!strcmp(“ ..”,entry-> d_name))
  61.                         继续;

  62.                 如果(S_ISDIR(statbuf.st_mode))
  63.                 {
  64.                         findAllDirs(entry-> d_name);
  65.                 }
  66.                 其他
  67.                 {
  68.                         如果(isCode(entry-> d_name))
  69.                         {
  70.                                 总数+ = countLines(entry-> d_name);
  71.                         }
  72.                 }
  73.         }

  74.         chdir(“ ..”);
  75.         closeir(dp);
  76. }

  77. int main()
  78. {
  79.         char path [MAX] =“。”;

  80.         printf(“计算中... \ n”);

  81.         findAllDirs(path);

  82.         printf(“目前你总共写了%ld行代码!\ n \ n”,总计);

  83.         返回0;
  84. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-19 10:37:18 | 显示全部楼层
当然是统计所有符合要求的文件。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 10:37:23 | 显示全部楼层
我记得小甲鱼说可以看自己十万行代码,但是我之后写了别的程序之后运行还是这一个程序的行数,是为什么呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 11:28:45 | 显示全部楼层
ba21 发表于 2019-12-19 10:37
当然是统计所有符合要求的文件。

那为什么之后写程序再编译运行不计算在内呢,是有文件统计范围吗,我这个程序单独mkdir了一个文件夹?顺便麻烦问一下怎么上传图片啊,我点图片只有相册和网络图片,没有本地的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 11:35:49 | 显示全部楼层
supermly 发表于 2019-12-19 11:28
那为什么之后写程序再编译运行不计算在内呢,是有文件统计范围吗,我这个程序单独mkdir了一个文件夹?顺 ...

发现了发送图片好像是等级不够。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-19 12:31:33 | 显示全部楼层
supermly 发表于 2019-12-19 11:28
那为什么之后写程序再编译运行不计算在内呢,是有文件统计范围吗,我这个程序单独mkdir了一个文件夹?顺 ...

统计范围肯定是有。.c  .cpp  .pas .py .php .java等等这些扩展名不都是源代码文件。
代码怎么写是你自己的事。统计范围当然是根据文件的扩展名来确定
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 14:53:14 | 显示全部楼层
ba21 发表于 2019-12-19 12:31
统计范围肯定是有。.c  .cpp  .pas .py .php .java等等这些扩展名不都是源代码文件。
代码怎么写是你自 ...

才发现复制的代码有些是浏览器翻译成汉字的,我在下一楼发没有翻译的。请问一下这个程序的统计范围是怎样的呢,我看这里统计的应该是.c的文件,是在哪些范围内找.c的文件呢?是不是必须在同一个文件夹下?新学看不太懂这个程序,麻烦了。
  1. int isCode(const char *filename)
  2. {
  3.         int length;

  4.         length = strlen(filename);
  5.         
  6.         if (!strcmp(filename + (length - 2), ".c"))
  7.         {
  8.                 return 1;
  9.         }
  10.         else
  11.         {
  12.                 return 0;
  13.         }
  14. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 14:54:01 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sys/stat.h>

  7. #define MAX 256

  8. long total;

  9. int countLines(const char *filename);
  10. int isCode(const char *filename);
  11. void findAllDirs(const char *path);

  12. int countLines(const char *filename)
  13. {
  14.         FILE *fp;
  15.         int count = 0;
  16.         int temp;

  17.         if ((fp = fopen(filename, "r")) == NULL)
  18.         {
  19.                 fprintf(stderr, "Can not open the file: %s\n", filename);
  20.                 return 0;
  21.         }

  22.         while ((temp = fgetc(fp)) != EOF)
  23.         {
  24.                 if (temp == '\n')
  25.                 {
  26.                         count++;
  27.                 }
  28.         }

  29.         fclose(fp);

  30.         return count;
  31. }

  32. int isCode(const char *filename)
  33. {
  34.         int length;

  35.         length = strlen(filename);
  36.         
  37.         if (!strcmp(filename + (length - 2), ".c"))
  38.         {
  39.                 return 1;
  40.         }
  41.         else
  42.         {
  43.                 return 0;
  44.         }
  45. }

  46. void findAllDirs(const char *path)
  47. {
  48.         DIR *dp;
  49.         struct dirent *entry;
  50.         struct stat statbuf;

  51.         if ((dp = opendir(path)) == NULL)
  52.         {
  53.                 fprintf(stderr, "The path %s is wrong!\n", path);
  54.                 return;
  55.         }

  56.         chdir(path);
  57.         while ((entry = readdir(dp)) != NULL)
  58.         {
  59.                 lstat(entry->d_name, &statbuf);

  60.                 if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
  61.                         continue;

  62.                 if (S_ISDIR(statbuf.st_mode))
  63.                 {
  64.                         findAllDirs(entry->d_name);
  65.                 }
  66.                 else
  67.                 {
  68.                         if (isCode(entry->d_name))
  69.                         {
  70.                                 total += countLines(entry->d_name);
  71.                         }
  72.                 }
  73.         }

  74.         chdir("..");
  75.         closedir(dp);
  76. }

  77. int main()
  78. {
  79.         char path[MAX] = ".";

  80.         printf("计算中...\n");

  81.         findAllDirs(path);

  82.         printf("目前你总共写了 %ld 行代码!\n\n", total);

  83.         return 0;
  84. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 19:16:17 | 显示全部楼层
supermly 发表于 2019-12-19 14:53
才发现复制的代码有些是浏览器翻译成汉字的,我在下一楼发没有翻译的。请问一下这个程序的统计范围是怎样 ...

我把程序移到上层文件夹就可以统计了,谢谢,以后能看懂了我再慢慢研究
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-2 15:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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