Wlz-030424 发表于 2022-8-26 15:58:27

{:10_282:}

wengxq 发表于 2022-8-26 16:09:45

111

Dwindle 发表于 2022-8-26 16:32:08

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

#define MAX 256

long total;

int coutLines(const char *filename);
int isCode(const char *filename);
void findAllDirs(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;
}

int isCode(const char *filename)
{
    int length;

    length = strlen(filename);

    if(!strcmp(filename + (length -2), ".c")) {
      return 1;
    } else {
      return 0;
    }
}

void findAllDirs(const char *path)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;

    if((dp = opendir(path)) == NULL) {
      fprintf(stderr, "The path %s is wrong!\n", path);
        return;
    }

    chdir(path);
    while((entry = readdir(dp)) != NULL)
    {
      lstat(entry -> d_name, &statbuf);

        if(!strcmp(".", entry -> d_name) || !strcmp("..", entry -> d_name)) {
          continue;
        }

        if(S_ISDIR(statbuf.st_mode)) {
          findAllDirs(entry -> d_name);
        } else {
          if(isCode(entry -> d_name)) {
                total += countLines(entry -> d_name);
          }
        }
    }

    chdir("..");
    closedir(dp);
}

int HelloWorld()
{
    printf("HelloWorld\n");

    return 0;
}

int main()
{
    char path = ".";

    printf("计算中...\n");

    findAllDirs(path);

    printf("目前你总共写了 %ld 行代码!\n\n", total);

    HelloWorld();

    return 0;
}

莫克思morx 发表于 2022-8-26 16:58:24

1

AIRSE鹤 发表于 2022-8-26 17:42:33

23

2291634540 发表于 2022-8-26 19:46:37

1

氢硼钨 发表于 2022-8-26 20:28:43

计算中。。。。。。

百分百少女 发表于 2022-8-26 21:29:06

小甲鱼

zwb12345 发表于 2022-8-27 02:46:38

666

谭深 发表于 2022-8-27 09:17:36

答案

1142904841 发表于 2022-8-27 09:29:24

1

wyx1235 发表于 2022-8-27 10:06:31

111

wyx1235 发表于 2022-8-27 10:45:56

为啥我运行出来是输入了0行代码的

下坡不刹车 发表于 2022-8-27 11:16:36

答案

小东西答案发 发表于 2022-8-27 12:29:09

答案

铭繁 发表于 2022-8-27 13:49:30

记得不牢 想要答案

月下山 发表于 2022-8-27 14:43:09

来核对一下答案!

wsndd 发表于 2022-8-27 16:41:09

答案

苏子傲 发表于 2022-8-27 17:34:28

1

沼气驱动 发表于 2022-8-27 20:46:41

康康答案,康康答案
页: 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 [1716] 1717 1718 1719 1720 1721 1722 1723 1724 1725
查看完整版本: S1E2:第一个程序 | 课后测试题及答案