超级亮宝宝 发表于 2019-3-16 16:07:55

111

tereya 发表于 2019-3-16 16:25:26

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

喝牛奶的何牛奶 发表于 2019-3-16 18:09:12

{:5_90:}

热恋中的茶叶蛋 发表于 2019-3-17 09:21:33

加油

苍华木 发表于 2019-3-17 09:38:21

你一共写了109行代码

82457097 发表于 2019-3-17 12:06:26

瞅一哈

aloneKing 发表于 2019-3-17 13:39:14

111

xjc123 发表于 2019-3-17 13:50:28

1

stephe 发表于 2019-3-17 15:01:07

谢谢

宛若处子 发表于 2019-3-17 15:16:10

“查看答案”

DaK0122 发表于 2019-3-17 17:37:03

0.只能听懂一种语言
1.机器语言
2.编译
3.工作效率
4.
5.翻译器
6.查表
7.

a741136822 发表于 2019-3-17 17:52:18

看看答案

鱼c__枫 发表于 2019-3-17 18:42:25

1

堂灼丶 发表于 2019-3-17 19:45:16

7行

Hammer07 发表于 2019-3-17 19:59:06

第一个回复嗷!我爱小甲鱼!

倥白 发表于 2019-3-17 23:03:36

加油

lsy641 发表于 2019-3-18 09:08:16

1

nothinghui 发表于 2019-3-18 16:41:40

谢谢 分享

野原白的助 发表于 2019-3-18 16:59:21

(๑•̀ㅂ•́)و✧棒!

Nicccyyy 发表于 2019-3-18 18:13:16

1
页: 260 261 262 263 264 265 266 267 268 269 [270] 271 272 273 274 275 276 277 278 279
查看完整版本: S1E2:第一个程序 | 课后测试题及答案