Jinx- 发表于 2020-1-16 00:29:19

S1E2课后题,我完全照抄的了诶,运行结果还是不对

把自己的代码照着答案对过了qaq真的没找出来错
我是大一新生,C语言学得浅,里面很多代码我都看不懂,求帮忙看看{:10_266:}
下面是代码

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

Jinx- 发表于 2020-1-16 00:32:01

然后我发现..复制过来的话运行结果也是10行{:10_266:}

zltzlt 发表于 2020-1-16 07:59:16

是不是你的程序存储为 .cpp 格式了?如果是,改成 .c 格式。

Jinx- 发表于 2020-1-16 13:15:46

zltzlt 发表于 2020-1-16 07:59
是不是你的程序存储为 .cpp 格式了?如果是,改成 .c 格式。

咋改呀qaq

zltzlt 发表于 2020-1-16 13:17:10

Jinx- 发表于 2020-1-16 13:15
咋改呀qaq

将你的文件另存为 .c 格式的文件。

如果这都不会改,那……建议你还是先学学如何操作计算机吧

qkdgz 发表于 2020-1-16 13:51:14

Jinx- 发表于 2020-1-16 00:32
然后我发现..复制过来的话运行结果也是10行

我的是零行...

段黄鱼 发表于 2020-1-16 19:53:22

这个程序好像只能检测后缀名是.c的文件

Jinx- 发表于 2020-1-16 20:12:13

段黄鱼 发表于 2020-1-16 19:53
这个程序好像只能检测后缀名是.c的文件

懂了,改对了,谢谢。

Jinx- 发表于 2020-1-16 20:13:00

qkdgz 发表于 2020-1-16 13:51
我的是零行...

我昨天改了一下也改成0行了hhh现在改对了,你可以看看最佳答案
页: [1]
查看完整版本: S1E2课后题,我完全照抄的了诶,运行结果还是不对