kula402 发表于 2021-3-11 15:53:04

{:5_109:}{:5_109:}

411042 发表于 2021-3-11 16:33:57

1

wy19960427 发表于 2021-3-11 16:39:12

打到麻木= =

一定一定 发表于 2021-3-11 16:42:09

查看参考答案

wsa1226 发表于 2021-3-11 17:14:27

d

fishwangba 发表于 2021-3-11 17:17:46

参考答案

爱学习的孩子王 发表于 2021-3-11 17:26:40

参考答案

Niesure懒人 发表于 2021-3-11 17:59:10

打卡

阿拉拉丁丁 发表于 2021-3-11 18:29:15

朕想知道

PYY彭于晏 发表于 2021-3-11 18:35:54

S1E2

解唯 发表于 2021-3-11 18:40:08

...

RRRRZZ 发表于 2021-3-11 18:43:40

1

悬崖网络 发表于 2021-3-11 18:50:23

APTX0898 发表于 2021-3-11 19:30:30

谢谢楼主 感激不尽

Haven_Y 发表于 2021-3-11 19:33:29

加油努力

he843533019 发表于 2021-3-11 20:06:29

1

Yangtuo00 发表于 2021-3-11 20:07:58

{:5_106:}

YUDAN 发表于 2021-3-11 20:41:51

q

翔崽子 发表于 2021-3-11 20:45:59

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

godfather900 发表于 2021-3-11 20:55:45

感谢无私奉献
页: 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 [1106] 1107 1108 1109 1110 1111 1112 1113 1114 1115
查看完整版本: S1E2:第一个程序 | 课后测试题及答案