shilinbing 发表于 2021-4-11 20:22:53

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

wmhbyw 发表于 2021-4-11 20:27:28

嘤嘤嘤

拨云见日yh 发表于 2021-4-11 21:08:16

回复

cmp 发表于 2021-4-11 21:44:11

被耍了有没有{:10_266:}

liumouxianren 发表于 2021-4-11 21:52:56

1

a791299 发表于 2021-4-11 22:44:26

1

1578531675 发表于 2021-4-11 23:08:50

答案

Shion_Hashimoto 发表于 2021-4-11 23:18:03

答案是0行代码

Pacino. 发表于 2021-4-12 10:11:57

.

丫丁丁 发表于 2021-4-12 11:21:32

加油

季寞空城 发表于 2021-4-12 12:06:46

6

深海盐汽水 发表于 2021-4-12 14:55:02

亲们趁敌人吃饭发动进攻哦

aa359068484 发表于 2021-4-12 15:02:45

看下答案

自游飞想 发表于 2021-4-12 15:39:54

1

SmoothCriminal 发表于 2021-4-12 16:20:47

1

范小范Yoki 发表于 2021-4-12 16:26:29

目前你总共写了109行代码!

时间不是时间 发表于 2021-4-12 17:16:31

0,计算机只懂得“0”和“1”
1,机器码,机器语言
2,编译
3,编译型语言整体翻译后发出指令,解释型语言逐个翻译
4,不行
5,解释型编程语言先转换成字节码,再由平台对应的编译器翻译成相应的汇编语言
6,编程语言
7,亲们趁敌人吃饭时发动进攻

小菜鸟1125 发表于 2021-4-12 18:58:10

Extrememax 发表于 2021-4-12 19:20:48

#include<stdio.h>
int main(){

}

Snxsky 发表于 2021-4-12 20:09:08

加油加油
页: 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 [1154] 1155 1156 1157 1158 1159 1160 1161 1162 1163
查看完整版本: S1E2:第一个程序 | 课后测试题及答案