鱼C论坛

 找回密码
 立即注册
查看: 2607|回复: 12

[已解决]课后作业S1E2

[复制链接]
发表于 2020-8-26 13:44:46 | 显示全部楼层 |阅读模式

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

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

x
47        strcpy(thePath, path);
48        if(handle = _findfirst(strcat(thepath,"/*.c"),&fa)) != -1L)
C语言入门S1E2的课后作业,
47        9        C:\Users\omega\Desktop\FishC\S1E2\未命名4.cpp        [Error] 'thePath' was not declared in this scope
48        32        C:\Users\omega\Desktop\FishC\S1E2\未命名4.cpp        [Error] 'thepath' was not declared in this scope
48        54        C:\Users\omega\Desktop\FishC\S1E2\未命名4.cpp        [Error] expected primary-expression before '!=' token
这哪错了啊???
最佳答案
2020-8-26 14:44:23


  1. #include<io.h>
  2. #include<direct.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>

  6. #define MAX   256 // MAX不是max

  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)// thePath不是thepath
  38.         {
  39.                 do {
  40.                         sprintf(target, "%s/%s", path, fa.name);//这里抄错了
  41.                         total +=countLines(target);
  42.                 } while (_findnext(handle, &fa) == 0);
  43.         }

  44.         _findclose(handle);
  45. }

  46. void findAllDirs(const char *path)// findAllDirs不是finndcAllDirs
  47. {
  48.         struct _finddata_t fa;
  49.         long handle;
  50.         char thePath[MAX];

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

  57.         do
  58.         {
  59.                
  60.                 if(!strcmp(fa.name,".")|| !strcmp(fa.name, ".."))//这里抄错了
  61.                         continue;// continue 不是countinue

  62.                 if( fa.attrib == _A_SUBDIR)
  63.                 {
  64.                         sprintf(thePath,"%s/%s",path,fa.name);// thePath
  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);// findAllDirs不是findALLDirs

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

  79.         return 0;
  80. }
复制代码

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

使用道具 举报

发表于 2020-8-26 13:45:35 | 显示全部楼层
发完整代码,你有一些抄错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-26 14:02:07 | 显示全部楼层
本帖最后由 sunrise085 于 2020-8-26 14:03 编辑

47        9        C:\Users\omega\Desktop\FishC\S1E2\未命名4.cpp        [Error] 'thePath' was not declared in this scope
48        32        C:\Users\omega\Desktop\FishC\S1E2\未命名4.cpp        [Error] 'thepath' was not declared in this scope

前两个错误是说变量名未定义,猜测可能是你拼写错了。你查一下代码,thePaththepath 应该都是拼写错了

48        54        C:\Users\omega\Desktop\FishC\S1E2\未命名4.cpp        [Error] expected primary-expression before '!=' token

这个应该是你多写了半个括号导致的错误
if(handle = _findfirst(strcat(thepath,"/*.c"),&fa)) != -1L)
红色括号去掉应该就没问题了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-8-26 14:23:37 | 显示全部楼层
这是原课后作业47,48行:
       strcpy(thePath, path);
        if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
这是作业链接https://fishc.com.cn/forum.php?m ... peid%26typeid%3D570
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-26 14:25:58 | 显示全部楼层
Omega. 发表于 2020-8-26 14:23
这是原课后作业47,48行:
       strcpy(thePath, path);
        if((handle = _findfirst(strcat(theP ...

你发完整代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-26 14:29:50 | 显示全部楼层


#include<io.h>
#include<direct.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.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 *filename)
{
        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;
         }
         
         while ((temp = fgetc(fp)) != EOF)
         {
                 if (temp=='\n')
                 {
                         count++;
                 }
         }
         
         fclose(fp);
         
         return count;
}

void findAllCodes(const char path)
{
        struct _finddata_t fa;
        long handle;
        char thePathMAX, targetMAX;
       
        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 finndcAllDirs(const char *path)
        {
                struct _finddata_t fa;
                long handle;
                char thePath[MAX];
               
                strcpy(thepath,);
                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, ".."))
                        countinue;
                       
                        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(path);
                findALLDirs(path);
               
                printf("目前你总共写了%1d行代码!\n\n,total");
                system("pause");
               
                return 0;
        }
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-26 14:44:23 | 显示全部楼层    本楼为最佳答案   


  1. #include<io.h>
  2. #include<direct.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>

  6. #define MAX   256 // MAX不是max

  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)// thePath不是thepath
  38.         {
  39.                 do {
  40.                         sprintf(target, "%s/%s", path, fa.name);//这里抄错了
  41.                         total +=countLines(target);
  42.                 } while (_findnext(handle, &fa) == 0);
  43.         }

  44.         _findclose(handle);
  45. }

  46. void findAllDirs(const char *path)// findAllDirs不是finndcAllDirs
  47. {
  48.         struct _finddata_t fa;
  49.         long handle;
  50.         char thePath[MAX];

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

  57.         do
  58.         {
  59.                
  60.                 if(!strcmp(fa.name,".")|| !strcmp(fa.name, ".."))//这里抄错了
  61.                         continue;// continue 不是countinue

  62.                 if( fa.attrib == _A_SUBDIR)
  63.                 {
  64.                         sprintf(thePath,"%s/%s",path,fa.name);// thePath
  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);// findAllDirs不是findALLDirs

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

  79.         return 0;
  80. }
复制代码

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

使用道具 举报

发表于 2020-8-26 14:47:21 | 显示全部楼层
Omega. 发表于 2020-8-26 14:29
#include
#include
#include

通篇全是错误。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-26 14:59:53 | 显示全部楼层

  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>

  6. #define MAX   256 // MAX不是max

  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)// thePath不是thepath 少了一对括号
  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)// findALLDirs不是finndcAllDirs
  48. {
  49.         struct _finddata_t fa;
  50.         long handle;
  51.         char thePath[MAX];

  52.         strcpy(thePath,path);// 少了path  thePath不是thepath
  53.         if((handle = _findfirst(strcat(thePath, "/*"),&fa)) == -1L) // thePath不是thepath
  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;// continue 不是countinue

  62.                 if( fa.attrib == _A_SUBDIR)
  63.                 {
  64.                         sprintf(thePath,"%s/%s",path,fa.name);// thePath
  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("目前你总共写了%ld行代码!\n\n",total);//这里抄错了
  78.         system("pause");

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

使用道具 举报

 楼主| 发表于 2020-8-26 15:01:23 | 显示全部楼层

谢谢了,第一次抄代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-26 15:02:03 | 显示全部楼层
Omega. 发表于 2020-8-26 15:01
谢谢了,第一次抄代码


看我刚发的,最开始的差个98行没修改
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-26 15:04:13 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-26 15:09:14 | 显示全部楼层
baige 发表于 2020-8-26 15:02
看我刚发的,最开始的差个98行没修改

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-1 20:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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