鱼C论坛

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

S1E2课后答案疑问

[复制链接]
发表于 2017-8-5 16:51:40 | 显示全部楼层 |阅读模式

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

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

x
按照S1E2里的那个   统计当前目录和所以子目录。。。。  抄写了代码  但是我们输出就是不对呢  不是小甲鱼那样的界面

我的是WIN7系统  

编译效果
截图00.jpg

运行效果  
截图01.jpg

很奇怪

是哪里错了呢?

用的是Code::Blocks 软件  
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-8-5 17:05:52 | 显示全部楼层
不要直接拷贝代码哦~

自己敲一遍代码试试?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-5 17:35:35 | 显示全部楼层
表示我代码可以运行但最终结果是0行代码   以及不知道怎么截图放在回复中
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-7 08:18:53 | 显示全部楼层
小甲鱼 发表于 2017-8-5 17:05
不要直接拷贝代码哦~

自己敲一遍代码试试?

报告  代码确实是增加敲的  之前        struct _finddata_t fa;这句struct后没加空格   出现编译不过去   后来检查出来了    但是编译通过后运行就出现上面的界面了   不知道是不是跟编译软件或者编译器有关系    Code::Blocks 16.01下载来是不带编译器的   后来下了个GCC编译器   
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-7 15:56:11 | 显示全部楼层
dxl1120 发表于 2017-8-7 08:18
报告  代码确实是增加敲的  之前        struct _finddata_t fa;这句struct后没加空格   出现编译不过去   后来 ...


如果是自己敲的话编译过后出现乱码,那应该是编辑的设置问题哦,重新安装下试试。

Dev-C++ 和 Code::blocks 虽然可以完成 90% 以上的课程内容,但学习效果其实不如使用原生态的 GCC 好。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-7 15:57:22 | 显示全部楼层
霁清d 发表于 2017-8-5 17:35
表示我代码可以运行但最终结果是0行代码   以及不知道怎么截图放在回复中

点击“高级模式”,随后点击图片即可。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-7 23:06:40 | 显示全部楼层

  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.                 while ((temp = fgetc(fp)) != EOF)
  23.                 {
  24.                                 if (temp == '\n')
  25.                                 {
  26.                                                 count++;
  27.                                 }
  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.                
  50.                 _findclose(handle);
  51. }

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

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


对着小甲鱼给的代码两边没发现错误(有可能眼瞎了..)
然后代码可以运行却是零


QQ图片20170807230331.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-8 09:55:49 | 显示全部楼层
霁清d 发表于 2017-8-7 23:06
对着小甲鱼给的代码两边没发现错误(有可能眼瞎了..)
然后代码可以运行却是零

复制你的代码  运行  完美运行啊

也没有我自己那个的问题了

真是奇了怪了啊

截图00.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-8 12:02:22 | 显示全部楼层
我刚刚自己又去复制了下 出现了140行代码和38行都不对.........
可能是我电脑问题??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-8 17:27:30 | 显示全部楼层
小甲鱼 发表于 2017-8-7 15:56
如果是自己敲的话编译过后出现乱码,那应该是编辑的设置问题哦,重新安装下试试。

Dev-C++ 和 Code: ...

小甲鱼老师,我照你的教程安装linux,修改完网络配置后可以ping 8.8.8.8,但是ping其他网站都是卡在那里一会,然后显示unknown host。 yum gcc也装不了。之前装的时候也是同样的问题。  崩溃!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-8 17:37:40 | 显示全部楼层
fanzongsao 发表于 2017-8-8 17:27
小甲鱼老师,我照你的教程安装linux,修改完网络配置后可以ping 8.8.8.8,但是ping其他网站都是卡在那里 ...

建议按照这个百度经验修改下设置 -> http://jingyan.baidu.com/article/4d58d54137d2a19dd5e9c050.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-8 17:38:43 | 显示全部楼层
霁清d 发表于 2017-8-8 12:02
我刚刚自己又去复制了下 出现了140行代码和38行都不对.........
可能是我电脑问题?? ...

它只是检查当前文件夹以及其子文件夹下的代码行数哦,所以如果生成的可执行程序在其他文件夹,是检测不到的~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-8 18:15:30 | 显示全部楼层


不知道怎么查看自己的网关地址,百度了几个方法都搞不定
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-9 16:03:58 | 显示全部楼层
小甲鱼 发表于 2017-8-8 17:38
它只是检查当前文件夹以及其子文件夹下的代码行数哦,所以如果生成的可执行程序在其他文件夹,是检测不到 ...

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

使用道具 举报

发表于 2017-8-9 16:22:40 | 显示全部楼层
霁清d 发表于 2017-8-7 23:06
对着小甲鱼给的代码两边没发现错误(有可能眼瞎了..)
然后代码可以运行却是零

哥们 你这个 保存的时候 不是C文件
保存默认是C++ 文件  要改后缀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-9-2 16:13:38 | 显示全部楼层
屁哥 发表于 2017-8-9 16:22
哥们 你这个 保存的时候 不是C文件
保存默认是C++ 文件  要改后缀

正解!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-3 19:18:44 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-2 11:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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