Loser_YiMo 发表于 2021-4-25 01:17:31

第一课的代码计算程序求解!

重温C语言,本来还挺自信的哈哈,因为第一次打这个的时候错了一堆,这次只是空格少打了2个,但是一运行就这样子,为啥呀,求大佬们解答!

人造人 发表于 2021-4-25 02:02:47

把你写的代码贴出来

Loser_YiMo 发表于 2021-4-26 00:41:48

人造人 发表于 2021-4-25 02:02
把你写的代码贴出来

#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-26 00:51:45

要认真检查代码

Loser_YiMo 发表于 2021-4-26 01:10:45

人造人 发表于 2021-4-26 00:51
要认真检查代码

感谢指点!!!其实打的时候注意到了这个奇怪的位置的,我在想%s和%s加个/是什么意思,写的时候感觉好像也加了啊,运行代码也没提示错误,真心感觉到了一个字符影响一整个程序!
页: [1]
查看完整版本: 第一课的代码计算程序求解!