苦苦的巧克力 发表于 2022-8-29 09:30:01

本帖最后由 苦苦的巧克力 于 2022-8-29 09:32 编辑

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

lovemyselftoo 发表于 2022-8-29 10:18:32

字符串那个地方报错

kkjjyy 发表于 2022-8-29 11:45:22

1

啊呸呸呸 发表于 2022-8-29 12:12:58

感谢!

zhangjinxuan 发表于 2022-8-29 12:51:08

回复

zhangjinxuan 发表于 2022-8-29 12:55:25

致命错误:direct.h:没有那个文件或目录
#include <direct.h>
                           ^
编译中断。{:10_266:}

zcca 发表于 2022-8-29 16:08:10

查看参考答案

heart030921 发表于 2022-8-29 16:20:45

0

李杰潼专插本 发表于 2022-8-29 16:38:01

查看参考答案

砬铋硝锌 发表于 2022-8-29 18:47:37

好多报错

葱狗 发表于 2022-8-29 21:25:44

6

杨某入门到入土 发表于 2022-8-29 22:03:54

感觉都一样,还是错了

csbhsh 发表于 2022-8-29 23:22:19

为啥小甲鱼打代码是彩色的我的只有绿色啊

呜呜呜懒狗 发表于 2022-8-30 00:34:09

呜呜呜

Letch 发表于 2022-8-30 04:02:50

计算中...
目前你一共写了 218 行代码

RoyYyf 发表于 2022-8-30 08:48:03

答案

Hokin 发表于 2022-8-30 08:58:57

支持

heigui 发表于 2022-8-30 09:30:19

CPU唯一认识的语言是机器语言

ZedCheung 发表于 2022-8-30 10:30:32

1

kevinde2022 发表于 2022-8-30 11:22:11

答案
页: 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 [1722] 1723 1724 1725 1726 1727 1728 1729 1730 1731
查看完整版本: S1E2:第一个程序 | 课后测试题及答案