鱼C论坛

 找回密码
 立即注册
查看: 2405|回复: 4

[已解决]帮我同学问的小甲鱼第一次作业的问题

[复制链接]
发表于 2023-2-24 21:22:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ExiaGN001 于 2023-2-25 15:18 编辑

首先强调一下发帖格式问题:
2. 代码大于20行以上时,最好写清自己的思路和注释,这样回答的人才能尽快给大家答案!(避免扔上一大堆代码,说是有错误,请人指点!)
3. 发代码请务必使用编辑器的“添加代码文字”(这个符号:<>)
有用请设置最佳答案谢谢

                               
登录/注册后可看大图



代码问题如下(斜体红字为编译器报错信息(翻译过),斜体蓝字为运行逻辑的问题,蓝字为C语言预留关键字,绿字为C++ STL库):

第13行 没有viod类型(您的意思是"void"吗?)   
                把viod改成void

第17行 没有FILEn类型(您的意思是"FILE"吗?)
                把FILEn 改成FILE (注意大小写,这里要表扬把*写在靠变量一侧的好习惯)

第18行 在int前缺少','或';' (报错显示是19行,但错误在第18行)
                行尾漏加分号(一定是英文分号,初学者常犯错误)

第40行 没有viod类型(您的意思是"void"吗?)
                viod改成void

第42行 没有struct_finddata_t类型(您的意思是"_finddata_t"吗?)
                需要用空格把struct 和_finddata_t 隔开

第51行 sprintf字符串的输出不对
                改成这样:sprintf(target,"%s\\%s",path,fa.name);

第61行 没有struct_finddata_t类型(您的意思是"_finddata_t"吗?)
                需要用空格把struct 和_finddata_t 隔开

第79行 sprintf的参数不对
                改成这样:sprintf(thePath,"%s\\%s" ,path,fa.name);

第90行 没有定义过path(您的意思是"patj"吗?)(报错显示是第94行)
                把90行的patj改成path即可。

第51行和第79行的输出其实不对,在C/C++中,需要用转义字符\\来输出\


                               
登录/注册后可看大图


改完错误之后的代码,可以正常运行:
  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.         if((fp=fopen(filename,"r")) == NULL)
  17.         {
  18.                 fprintf(stderr,"Can not open the file:%s\n", filename);
  19.                 return 0;
  20.         }

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

  28.         fclose(fp);

  29.         return count;
  30. }

  31. void findAllCodes(const char *path)
  32. {
  33.         struct _finddata_t fa;
  34.         long handle;
  35.         char thePath[MAX],target[MAX];

  36.         strcpy(thePath,path);
  37.         if((handle=_findfirst(strcat(thePath,"/*.c"), &fa)) !=-1L)
  38.         {
  39.                 do
  40.                 {
  41.                     sprintf(target,"%s\\%s",path,fa.name);
  42.                         total +=countLines(target);
  43.                 }while(_findnext(handle,&fa)==0);
  44.         }

  45.         _findclose(handle);
  46. }

  47. void findALLDirs(const char*path)
  48. {
  49.         struct _finddata_t fa;
  50.         long handle;
  51.         char thePath[MAX];

  52.         strcpy(thePath,path);
  53.         if((handle=_findfirst(strcat(thePath,"/*"),&fa))==-1L)
  54.         {
  55.                 fprintf(stderr,"The path %s is wrong!\n",path);
  56.                 return;
  57.         }

  58.     do
  59.     {
  60.             if(!strcmp(fa.name,".") || !strcmp(fa.name,".."))
  61.                    continue;

  62.                 if(fa.attrib==_A_SUBDIR)
  63.             {
  64.                     sprintf(thePath,"%s\\%s",path,fa.name);
  65.                     findAllCodes(thePath);
  66.                     findALLDirs(thePath);
  67.                 }
  68.     }while (_findnext(handle,&fa)==0);

  69.         _findclose(handle);
  70. }

  71. int main()
  72. {
  73.         char path[MAX]=".";

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

  75.         findAllCodes(path);
  76.         findALLDirs(path);

  77.         printf("目前你总共写了%1d行代码!\n\n",total);
  78.         system("pause");

  79.         return 0;

  80. }
复制代码

有用请设置最佳答案谢谢

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-26 04:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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