洛清烟 发表于 2021-10-27 03:11:21

牛!

测绘农民工 发表于 2021-10-27 09:55:25

1

leon1011 发表于 2021-10-27 11:11:36

selvaamazing 发表于 2021-10-27 16:01:41

1111111111

小厌 发表于 2021-10-27 16:43:17

1

努力的大肥兔 发表于 2021-10-27 17:04:23

查看参考答案

蜜雪冰城 发表于 2021-10-27 17:55:46

回复

DirectFeng 发表于 2021-10-27 19:21:24

学习打卡

121194161 发表于 2021-10-27 19:56:41

#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 countLines(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 main()
{
      char path = ".";

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

      findAllDirs(path);

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

      return 0;
}

qwerl 发表于 2021-10-27 20:40:41

ok

吾生俞 发表于 2021-10-27 20:44:35

芜湖

温胜己 发表于 2021-10-27 20:51:54

答案

张健时 发表于 2021-10-27 21:21:17

m

舟随咲 发表于 2021-10-27 21:26:57

kan

冷风uzi 发表于 2021-10-27 21:43:13

{:5_91:}

laurac 发表于 2021-10-27 21:53:29

加油!

Gandalf1 发表于 2021-10-27 21:54:41

1

yang2019 发表于 2021-10-27 22:04:10

看答案https://cdn.jsdelivr.net/gh/master-of-forums/master-of-forums/public/images/patch.gif

q2269000545 发表于 2021-10-27 22:54:01

感谢小甲鱼

zsh1225 发表于 2021-10-27 23:08:26

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