鱼C论坛

 找回密码
 立即注册
查看: 3693|回复: 16

《带你学C带你飞》(s1e2)感悟

[复制链接]
发表于 2019-8-7 00:04:41 | 显示全部楼层 |阅读模式

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

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

x
刚做完课后习题,第一题我的理解“二傻子”我以为计算机二就二在它只能识别二进制的二(0和1)
然后既然一个合格的程序员要有10万行代码,不知道哪位大佬真的有这种计算自己编译器从下载到电脑到现在自己所敲的代码行数的代码(我只是个新手我不会啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-7 00:05:33 | 显示全部楼层
突然发现这个主题挺好看的,论坛也挺好玩的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 00:09:38 | 显示全部楼层
  1. 不知道哪位大佬真的有这种计算自己编译器从下载到电脑到现在自己所敲的代码行数的代码
复制代码


这句话读不懂,不知道你想说什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 08:02:01 | 显示全部楼层
计算机的确只知道0和1
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 08:08:54 | 显示全部楼层
你的意识是想知道自己敲了多少代码,就是编译器能不能计算出曾经敲的代码的总和?对吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-7 10:26:01 | 显示全部楼层
2164930278 发表于 2019-8-7 08:08
你的意识是想知道自己敲了多少代码,就是编译器能不能计算出曾经敲的代码的总和?对吗

对的,差不多就这个意思,10w行嘛,是一个程序员的基本素养
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-7 10:27:57 | 显示全部楼层
人造人 发表于 2019-8-7 00:09
这句话读不懂,不知道你想说什么

如何计算自己敲的代码行数,请用一个程序实现
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 11:04:33 | 显示全部楼层
千语绯月 发表于 2019-8-7 10:27
如何计算自己敲的代码行数,请用一个程序实现

就是相当于把自己写的文件放在一个目录下,然后检查文件后缀,打开文件,一个‘\n’算一行,关闭文件,打开下一个文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 12:44:49 | 显示全部楼层
课后题就有呀
Linux 和 MacOS:
  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. }
复制代码


Windows:


  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.         
  31.         fclose(fp);
  32.         
  33.         return count;
  34. }

  35. void findAllCodes(const char *path)
  36. {
  37.         struct _finddata_t fa;
  38.         long handle;
  39.         char thePath[MAX], target[MAX];
  40.         
  41.         strcpy(thePath, path);
  42.         if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
  43.         {
  44.                 do
  45.                 {
  46.                         sprintf(target, "%s/%s", path, fa.name);
  47.                         total += countLines(target);
  48.                 }while (_findnext(handle, &fa) == 0);
  49.         }
  50.    
  51.         _findclose(handle);
  52. }

  53. void findALLDirs(const char *path)
  54. {
  55.         struct _finddata_t fa;
  56.         long handle;
  57.         char thePath[MAX];
  58.         
  59.         strcpy(thePath, path);
  60.         if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  61.         {
  62.                 fprintf(stderr, "The path %s is wrong!\n",path);
  63.                 return;
  64.         }
  65.    
  66.         do
  67.         {        
  68.                 if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  69.                         continue;
  70.                     
  71.                 if( fa.attrib == _A_SUBDIR)
  72.                 {        
  73.                         sprintf(thePath, "%s/%s", path, fa.name);
  74.                         findAllCodes(thePath);
  75.                         findALLDirs(thePath);
  76.                 }
  77.         }while (_findnext(handle, &fa) == 0);
  78.    
  79.         _findclose(handle);   
  80. }

  81. int main()
  82. {
  83.         char path[MAX] = ".";
  84.         
  85.         printf("计算中...\n");
  86.         
  87.         findAllCodes(path);
  88.         findALLDirs(path);
  89.         
  90.         printf("目前你总共写了 %ld 行代码!\n\n", total);
  91.         system("pause");
  92.         
  93.         return 0;
  94. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-7 18:09:43 | 显示全部楼层
蓝炎彼岸花 发表于 2019-8-7 12:44
课后题就有呀
Linux 和 MacOS:

我试了,那是固定的行数
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 18:22:09 | 显示全部楼层
千语绯月 发表于 2019-8-7 18:09
我试了,那是固定的行数

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

使用道具 举报

发表于 2019-8-7 20:09:29 | 显示全部楼层
二傻子是不认识二的,不然怎么叫做“二”傻子呢?
第一次课后题应该有计算代码量的源程序代码。
源程序代码如下:
  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.        
  31.         fclose(fp);
  32.        
  33.         return count;
  34. }

  35. void findallcodes(const char *path)
  36. {
  37.         struct _finddata_t fa;
  38.         long handle;
  39.         char thepath[MAX],target[MAX];
  40.        
  41.         strcpy(thepath,path);
  42.         if((handle=_findfirst(strcat(thepath,"/*.c"),&fa))!=-1L)
  43.         {
  44.                 do
  45.                 {
  46.                         sprintf(target,"%s/%s",path,fa.name);
  47.                         total+=countlines(target);
  48.                 }while(_findnext(handle,&fa) == 0);
  49.         }
  50.        
  51.         _findclose(handle);
  52. }

  53. void findalldirs(const char *path)
  54. {
  55.         struct _finddata_t fa;
  56.         long handle;
  57.         char thepath[MAX];
  58.        
  59.         strcpy(thepath,path);
  60.         if((handle=_findfirst(strcat(thepath,"/*"),&fa))==-1L)
  61.         {
  62.                 fprintf(stderr,"The path %s is wrong!\n",path);
  63.                 return;
  64.         }
  65.        
  66.         do
  67.         {
  68.                 if(!strcmp(fa.name,".")||!strcmp(fa.name,".."))
  69.                         continue;
  70.                
  71.                 if(fa.attrib == _A_SUBDIR)
  72.                 {
  73.                         sprintf(thepath,"%s/%s",path,fa.name);
  74.                         findallcodes(thepath);
  75.                         findalldirs(thepath);
  76.                 }
  77.         }while(_findnext(handle,&fa)==0);
  78.        
  79.         _findclose(handle);
  80. }

  81. int main()
  82. {
  83.         char path[MAX]=".";
  84.        
  85.         printf("计算中...\n");
  86.        
  87.         findallcodes(path);
  88.         findalldirs(path);
  89.        
  90.         printf("目前你总共写了%ld行代码!\n\n",total);
  91.         system("pause");
  92.        
  93.         return 0;
  94. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 20:14:16 | 显示全部楼层
你所要求的的程序估计要链接编译程序,随时动态地记录你所敲的代码行数。对不起,这种程序我无能为力。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 20:40:24 | 显示全部楼层
有啥意义?
有功夫在这纠结这个,这时间都够你敲个几十行了。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-8-7 22:22:35 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-8 09:15:55 From FishC Mobile | 显示全部楼层
课后题我做了,也正确运行了,结果显示0行代码。这个是什么问题。尴尬。也放在一个文件夹了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-8 21:20:42 | 显示全部楼层
蓝炎彼岸花 发表于 2019-8-7 12:44
课后题就有呀
Linux 和 MacOS:

WINDOWS版,文件夹路径和后缀名,是那几句语句?该怎么修改路径?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 02:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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