斩鞍 发表于 2021-4-17 08:48:57

C语言入门S1E2的课后作业

自己打了一遍程序,显示未找到findALLFiles的函数定义,然后我直接复制课后作业的代码也是一样的提示https://xxx.ilovefishc.com/forum/202104/16/202500v33tiaga0o3z3ydi.png

LuLD 发表于 2021-4-17 08:51:18

把你代码搞上来看看

斩鞍 发表于 2021-4-17 08:53:14


#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 *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 thePath, target;
      
      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;
      
      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 = ".";
      
      printf("计算中...\n");
      
      findAllCodes(path);
      findALLDirs(path);
      
      printf("目前你总共写了 %ld 行代码!\n\n", total);
      system("pause");
      
      return 0;
}

斩鞍 发表于 2021-4-17 08:56:38

LuLD 发表于 2021-4-17 08:51
把你代码搞上来看看


#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 *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 thePath, target;
      
      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;
      
      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 = ".";
      
      printf("计算中...\n");
      
      findAllCodes(path);
      findALLDirs(path);
      
      printf("目前你总共写了 %ld 行代码!\n\n", total);
      system("pause");
      
      return 0;
}
提示的是第14行

LuLD 发表于 2021-4-17 09:01:10

斩鞍 发表于 2021-4-17 08:56
提示的是第14行

哦哦你这是可以运行,没有报错,但是显示了这个函数未定义是吧?

斩鞍 发表于 2021-4-17 09:06:06

LuLD 发表于 2021-4-17 09:01
哦哦你这是可以运行,没有报错,但是显示了这个函数未定义是吧?

是自己手打的,别的错误也没有,就是提示14行的findALLFiles未找到函数定义,然后程序也运行不了https://xxx.ilovefishc.com/forum/202104/16/202500v33tiaga0o3z3ydi.png

LuLD 发表于 2021-4-17 09:06:52

斩鞍 发表于 2021-4-17 08:56
提示的是第14行



因为 小甲鱼代码中写错了   看上图

声明了 三个函数,但是在实际运用的代码中,有一个函数名写成了其他的

为什么代码还可以运行,是因为 main 函数在代码最后,在运行到某个函数的时候是往main函数以上的代码中找

斩鞍 发表于 2021-4-17 09:06:55

LuLD 发表于 2021-4-17 09:01
哦哦你这是可以运行,没有报错,但是显示了这个函数未定义是吧?

也没法运行

斩鞍 发表于 2021-4-17 09:08:05

LuLD 发表于 2021-4-17 09:06
因为 小甲鱼代码中写错了   看上图

声明了 三个函数,但是在实际运用的代码中,有一个函数名写成 ...

好的,感谢

LuLD 发表于 2021-4-17 09:08:34

斩鞍 发表于 2021-4-17 09:06
也没法运行

没办法运行?就是在报这个函数问题?

那你把这个函数名子 改改

void findALLFiles(const char *path);===>>void findALLDirs(const char *path)
再试试

bool想学C 发表于 2021-4-17 09:10:03

学习

斩鞍 发表于 2021-4-17 09:12:09

LuLD 发表于 2021-4-17 09:08
没办法运行?就是在报这个函数问题?

那你把这个函数名子 改改


好了,可以了,万分感谢

LuLD 发表于 2021-4-17 09:15:50

斩鞍 发表于 2021-4-17 09:12
好了,可以了,万分感谢

能帮到你是最好的
页: [1]
查看完整版本: C语言入门S1E2的课后作业