粉红喵喵拳 发表于 2021-1-9 12:22:24

第一课的小程序为啥我的只有7行?

我用的是WIN10系统编译器是DEV
抄完也只有7行?这是为什么这和答案的101行差距也太大了。

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

风过无痕1989 发表于 2021-1-9 12:38:41

本帖最后由 风过无痕1989 于 2021-1-9 12:41 编辑

我用VS2015运行,没有问题,101行。请问你用什么编译器编译?用DEV_C++不正常,186行

程序有问题,只能一行一行帮你查,需要一点时间

风过无痕1989 发表于 2021-1-9 13:10:59

怕你等久了,你先试试下面的代码:
// 有些编译器对缩进有严格的要求,所以,请不要改变每一行前的空格!
// 如果程序文件是 *.cpp,则将第 50 行的 "/*.c" 直接改为 "/*.cpp"

#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)// 如果程序文件是 *.cpp,将此行的 "/*.c" 直接改为 "/*.cpp"
    {
      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("目前您总共写了 %1d 行代码! \n\n", total);
    system("pause");
      
    return 0;
}

风过无痕1989 发表于 2021-1-9 13:48:05

试了一下,win10 系统下安装的 DEV_C++,运行这个程序,(无论程序的后缀是 .c 还是 .cpp)默认的都是 C++ 程序,只要将第 50 行的 "/*.c" 直接改为 "/*.cpp",运行就正确,输出103行。若不修改第50行,后缀为 .c 的程序,输出186行,后缀为 .cpp 的程序,输出为103行

粉红喵喵拳 发表于 2021-1-9 21:17:08

风过无痕1989 发表于 2021-1-9 13:48
试了一下,win10 系统下安装的 DEV_C++,运行这个程序,(无论程序的后缀是 .c 还是 .cpp)默认的都是 C++...

可我的输出是7行就离谱

粉红喵喵拳 发表于 2021-1-9 21:19:28

风过无痕1989 发表于 2021-1-9 13:10
怕你等久了,你先试试下面的代码:

现在 合适了谢谢大佬
页: [1]
查看完整版本: 第一课的小程序为啥我的只有7行?