枫_R 发表于 2022-7-3 08:09:35

第一课的课后作业看看哪里错了


#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, “path %s is error!\n”,path);
返回;
      }
   

      {      
if (!strcmp(fa.name, “.”) || !strcmp(fa.name, “..”))
继续;
                  
if( fa.attrib == _A_SUBDIR)
                {      
sprintf(thePath, “%s/%s”, path, fa.name);
findAllCodes(thePath);
findALLDirs(thePath);
                }
}while (_findnext(handle, &fa) == 0);
   
_findclose(手柄);
}

int main()
{
字符路径 = “.”;
      
printf(“计算中...\n”);
      
findAllCodes(path);
findALLDirs(path);
      
printf(“目前你总共写了 %ld 行代码!\n\n”, total);
系统(“暂停”);
      
返回 0;
}






大佬们这哪里错了吗,解析栏一堆红还不提醒哪里错了ewe

傻眼貓咪 发表于 2022-7-3 09:18:45

(一)代码上面声明函数 void findALLFiles(const char *path); 但没有定义?还是说自己取的名字自己忘记了?
(二)代码下半部分,中英文标点符号不要混用,记得切换。

枫_R 发表于 2022-7-3 09:56:26

傻眼貓咪 发表于 2022-7-3 09:18
(一)代码上面声明函数 void findALLFiles(const char *path); 但没有定义?还是说自己取的名字自己忘记了 ...

介个我听不懂啊才
看第一课的零基础小萌新

枫_R 发表于 2022-7-3 10:01:24

本帖最后由 枫_R 于 2022-7-3 10:16 编辑

傻眼貓咪 发表于 2022-7-3 09:18
(一)代码上面声明函数 void findALLFiles(const char *path); 但没有定义?还是说自己取的名字自己忘记了 ...

然后我直接复制鱼哥的代码也不行
   运行的文字是这个乱码
   璁$畻涓?..
鐩墠浣犳   

傻眼貓咪 发表于 2022-7-3 11:04:53

枫_R 发表于 2022-7-3 10:01
然后我直接复制鱼哥的代码也不行
   运行的文字是这个乱码
   璁$畻涓?..


#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;
}

枫_R 发表于 2022-7-3 11:15:32

本帖最后由 枫_R 于 2022-7-3 11:17 编辑

傻眼貓咪 发表于 2022-7-3 11:04


还是乱码啊 ,哪里出错了吗,是我编译器问题吗我用的是dev-c++

璁$畻涓?..
鐩墠浣犳

傻眼貓咪 发表于 2022-7-3 11:34:53

枫_R 发表于 2022-7-3 11:15
还是乱码啊 ,哪里出错了吗,是我编译器问题吗我用的是dev-c++

璁$畻涓?..


可能是因为你的编译器无法显示中文(只能显示 ASCII 里的可打印字符)吧。
你试试把所有中文字符改成英文字符,比如:

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

printf("目前你总共写了 %ld 行代码!\n\n", total);
printf("you have written a total of %d lines of code\n\n", total);

枫_R 发表于 2022-7-3 11:54:21

傻眼貓咪 发表于 2022-7-3 11:34
可能是因为你的编译器无法显示中文(只能显示 ASCII 里的可打印字符)吧。
你试试把所有中文字符改成英 ...

不行哦,改完编译不了{:10_243:}

傻眼貓咪 发表于 2022-7-3 12:08:25

枫_R 发表于 2022-7-3 11:54
不行哦,改完编译不了

#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("calculating...\n");

    findAllCodes(path);
    findALLDirs(path);

    printf("you have written a total of %d lines of code\n\n", total);
    system("pause");

    return 0;
}

枫_R 发表于 2022-7-3 12:51:38

本帖最后由 枫_R 于 2022-7-3 13:01 编辑

傻眼貓咪 发表于 2022-7-3 12:08


这可以了,{:10_297:} 但是中文是出什么问题了吗

枫_R 发表于 2022-7-3 17:02:20

解决了,下载蓝色图标的DEV-C++就可以了
页: [1]
查看完整版本: 第一课的课后作业看看哪里错了