小吴学C语言 发表于 2021-10-26 09:19:05

1

agony吖吖 发表于 2021-10-26 11:44:02

daan

LYSWZY 发表于 2021-10-26 12:38:15

0

kur 发表于 2021-10-26 13:18:49

查看参考答案

hulka 发表于 2021-10-26 14:26:01

我想看看答案

Spear-- 发表于 2021-10-26 14:37:47

0

xixize 发表于 2021-10-26 15:05:20

终于找到队伍,开心

Newbie丶 发表于 2021-10-26 17:55:40

加油

408323750 发表于 2021-10-26 18:26:44

11

ycylhy 发表于 2021-10-26 18:37:52

答案!

跑步是一种时尚 发表于 2021-10-26 19:23:17

讲的很好

跑步是一种时尚 发表于 2021-10-26 19:24:56

讲的很好

sLDD 发表于 2021-10-26 19:31:42


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

zzxxcc553311 发表于 2021-10-26 19:53:39

{:5_109:}

cool-w 发表于 2021-10-26 21:06:38

1

miracle-99 发表于 2021-10-26 21:27:38

加油!

song666 发表于 2021-10-26 22:09:41

答案

执棋 发表于 2021-10-26 22:28:07

答案

qisiwhdqew 发表于 2021-10-26 23:30:42

1

大梦想家hhh 发表于 2021-10-26 23:41:08

答案
页: 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 [1435] 1436 1437 1438 1439 1440 1441 1442 1443 1444
查看完整版本: S1E2:第一个程序 | 课后测试题及答案