鱼C论坛

 找回密码
 立即注册
查看: 1793|回复: 2

[已解决]甲鱼S1E2:第一个程序 的课后作业,求大佬帮忙纠正

[复制链接]
发表于 2023-5-4 20:43:48 | 显示全部楼层 |阅读模式

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

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

x
求大佬指点

这些个是我的错误
16        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] expected identifier or '(' before '{' token
27        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] stray '\266' in program
27        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] stray '\370' in program
C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        In function 'findAllCodes':
42        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] unknown type name 'struct_finddata_t'
47        54        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] 'fa' undeclared (first use in this function)
47        54        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Note] each undeclared identifier is reported only once for each function it appears in
47        59        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] expected ')' before '!' token
C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        In function 'findALLDirs':
61        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] unknown type name 'struct_finddata_t'
66        52        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] 'fa' undeclared (first use in this function)
80        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] stray '\243' in program
80        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] stray '\273' in program
81        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] expected ';' before 'findALLDirs'
85        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] stray '\243' in program
85        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] stray '\273' in program
86        1        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] expected ';' before '}' token
C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        In function 'main':
94        14        C:\Users\29245\Desktop\芜湖起飞C\第一节课\课后作业\统计.c        [Error] 'thePath' undeclared (first use in this function)

我的代码如下
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 256

long total;

int countLines (const char *filename);
void findAllCodes (const char*path);
void findALLFiles (const char *path);

int countLines (const char *finename);
{
        FILE *fp;
        int count = 0;
        int temp;

        if ((fp = fopen(filename, "r")) ==NULL)
           {
fprintf (stderr, "Can not open the file: %s\n",filename);
return 0;
                }
               
而 ((temp = fgetc (fp)) ! = EOF)
                {
if (temp == '\n')
               {
count++;
                           }
                 }
                 
FCLOSE (FP) ;

return count;
}

void findAllCodes (const char *path)
{
struct_finddata_t FA;
long handle;
char thePath[MAX], target[MAX];

strcpy (thePath, path) ;
if ((handle = _findfirst (strcat (thePath, "/*.c"), &fa)) ! = -1L)
                {
do
                                {
sprintf (target, "%s%s", path, fa.name);
total += countLines (target) ;
}while (_findnext (handle, &fa) == 0) ;
                 }
                 
_findclose(handle) ;
}

void findALLDirs (const char *path)
{
struct_finddata_t FA;
long handle;
char thePath[MAX];

strcpy (thePath, path);
if ((handle = _findfirst (strcat (thePath, "/*"), &fa)) == -1L)
                {
fprintf (stderr, "the path %s is wrong! \n", path);
return;
                 }
                 
do
                {
if (! strcmp (fa.name, ".") || ! strcmp (fa.name, ".."))
continue;

if (fa.attrib == _A_SUBDIR)
                                {
sprintf (thePath, "%s%s", path, fa.name);
findAllCodes(thePath) ;
findALLDirs (thepath) ;
                                 }
}while (_findnext (handle, &fa) == 0);

_findclose (handle);
}

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

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

findAllCodes(thePath);
findALLDirs (path) ;

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

return 0 ;
}
最佳答案
2023-5-4 20:47:02
本帖最后由 isdkz 于 2023-5-4 20:48 编辑

根据你提供的错误信息,以及附带的代码,我为你提供以下修复建议:

1. 第15行:在函数声明中删除分号,并将 finename 更正为 filename 。

  1. int countLines (const char *filename);
复制代码

改为:
  1. int countLines (const char *filename)
复制代码


2. 第26行以及第79行和第84行:删除非法字符,可能是复制粘贴时产生的。

3. 第41行和第60行: struct_finddata_t  更正为  _finddata_t

4. 第46和第65行: fa  更正为  FA

5. 第46和第65行:使用双等号  ==  替换感叹号和等号  !=

6. 第80行:将  findALLDirs  更正为  findALLFiles

7. 第93行:将  thePath  更正为  path

请将代码修改为以下形式:

  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.     FILE *fp;
  13.     int count = 0;
  14.     int temp;

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

  19.     while ((temp = fgetc(fp)) != EOF) {
  20.         if (temp == '\n') {
  21.             count++;
  22.         }
  23.     }

  24.     fclose(fp);

  25.     return count;
  26. }

  27. void findAllCodes(const char *path) {
  28.     _finddata_t FA;
  29.     long handle;
  30.     char thePath[MAX], target[MAX];

  31.     strcpy(thePath, path);
  32.     if ((handle = _findfirst(strcat(thePath, "/*.c"), &FA)) != -1L) {
  33.         do {
  34.             sprintf(target, "%s%s", path, FA.name);
  35.             total += countLines(target);
  36.         } while (_findnext(handle, &FA) == 0);
  37.     }

  38.     _findclose(handle);
  39. }

  40. void findALLFiles(const char *path) {
  41.     _finddata_t FA;
  42.     long handle;
  43.     char thePath[MAX];

  44.     strcpy(thePath, path);
  45.     if ((handle = _findfirst(strcat(thePath, "/*"), &FA)) == -1L) {
  46.         fprintf(stderr, "the path %s is wrong! \n", path);
  47.         return;
  48.     }

  49.     do {
  50.         if (!strcmp(FA.name, ".") || !strcmp(FA.name, ".."))
  51.             continue;

  52.         if (FA.attrib == _A_SUBDIR) {
  53.             sprintf(thePath, "%s%s", path, FA.name);
  54.             findAllCodes(thePath);
  55.             findALLFiles(thePath);
  56.         }
  57.     } while (_findnext(handle, &FA) == 0);

  58.     _findclose(handle);
  59. }

  60. int main() {
  61.     char path[MAX] = ".";

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

  63.     findAllCodes(path);
  64.     findALLFiles(path);

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

  67.     return 0;
  68. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-5-4 20:46:25 | 显示全部楼层
发代码请用代码格式
[code][/code]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-4 20:47:02 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-5-4 20:48 编辑

根据你提供的错误信息,以及附带的代码,我为你提供以下修复建议:

1. 第15行:在函数声明中删除分号,并将 finename 更正为 filename 。

  1. int countLines (const char *filename);
复制代码

改为:
  1. int countLines (const char *filename)
复制代码


2. 第26行以及第79行和第84行:删除非法字符,可能是复制粘贴时产生的。

3. 第41行和第60行: struct_finddata_t  更正为  _finddata_t

4. 第46和第65行: fa  更正为  FA

5. 第46和第65行:使用双等号  ==  替换感叹号和等号  !=

6. 第80行:将  findALLDirs  更正为  findALLFiles

7. 第93行:将  thePath  更正为  path

请将代码修改为以下形式:

  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.     FILE *fp;
  13.     int count = 0;
  14.     int temp;

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

  19.     while ((temp = fgetc(fp)) != EOF) {
  20.         if (temp == '\n') {
  21.             count++;
  22.         }
  23.     }

  24.     fclose(fp);

  25.     return count;
  26. }

  27. void findAllCodes(const char *path) {
  28.     _finddata_t FA;
  29.     long handle;
  30.     char thePath[MAX], target[MAX];

  31.     strcpy(thePath, path);
  32.     if ((handle = _findfirst(strcat(thePath, "/*.c"), &FA)) != -1L) {
  33.         do {
  34.             sprintf(target, "%s%s", path, FA.name);
  35.             total += countLines(target);
  36.         } while (_findnext(handle, &FA) == 0);
  37.     }

  38.     _findclose(handle);
  39. }

  40. void findALLFiles(const char *path) {
  41.     _finddata_t FA;
  42.     long handle;
  43.     char thePath[MAX];

  44.     strcpy(thePath, path);
  45.     if ((handle = _findfirst(strcat(thePath, "/*"), &FA)) == -1L) {
  46.         fprintf(stderr, "the path %s is wrong! \n", path);
  47.         return;
  48.     }

  49.     do {
  50.         if (!strcmp(FA.name, ".") || !strcmp(FA.name, ".."))
  51.             continue;

  52.         if (FA.attrib == _A_SUBDIR) {
  53.             sprintf(thePath, "%s%s", path, FA.name);
  54.             findAllCodes(thePath);
  55.             findALLFiles(thePath);
  56.         }
  57.     } while (_findnext(handle, &FA) == 0);

  58.     _findclose(handle);
  59. }

  60. int main() {
  61.     char path[MAX] = ".";

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

  63.     findAllCodes(path);
  64.     findALLFiles(path);

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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-9 21:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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