ddCyuyan 发表于 2021-5-1 18:21:34

参考答案

ZRWZRW 发表于 2021-5-1 18:58:48

小甲鱼大大我爱你

JJCDSG 发表于 2021-5-1 19:52:04

嘿,亲爱的鱼油,每天都要过得开心哦^ _ ^

丶断感 发表于 2021-5-1 21:16:26

1

于12 发表于 2021-5-1 23:38:05

冲冲冲{:10_256:}

檬檬君 发表于 2021-5-2 09:47:35

1

136346 发表于 2021-5-2 10:18:37

看看

17597808618 发表于 2021-5-2 10:32:28

haohaohao

xde.hz 发表于 2021-5-2 10:53:38

答案

Q741ko 发表于 2021-5-2 11:16:47

{:5_108:}

kisaki123 发表于 2021-5-2 11:34:12

看看

yuyuyulin0111 发表于 2021-5-2 17:14:06

111

先生赵 发表于 2021-5-2 17:17:53

亲们加油

iiiiiflame 发表于 2021-5-2 18:09:18

bang

luciaspade 发表于 2021-5-2 18:12:35

答完了,来看看答案

一只 发表于 2021-5-2 19:54:51

#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 count_lines_code(const char *filename);
int isCode(const char *filename);
void findAllDirs(const char *path);

int count_lines_code(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 += count_lines_code(entry->d_name);
                        }
                }
      }

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

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

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

      findAllDirs(path);

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

      return 0;
}


水吉雨文 发表于 2021-5-2 20:55:26

{:10_277:}

gy5254 发表于 2021-5-2 21:11:42

回复

GIORNO123 发表于 2021-5-2 23:09:11

123456

Eternal随风 发表于 2021-5-3 01:02:43

1
页: 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 [1176] 1177 1178 1179 1180 1181 1182 1183 1184 1185
查看完整版本: S1E2:第一个程序 | 课后测试题及答案