dfysnbb 发表于 2019-11-28 21:26:36

1

小豆包 发表于 2019-11-28 22:08:18

我想看答案

duan段 发表于 2019-11-28 23:03:10

查看答案

cccrrrr7 发表于 2019-11-28 23:07:57

hi

YennyS 发表于 2019-11-28 23:21:21

敲了十多分钟才敲了课后作业,看来打字还是慢啊

姜文轩 发表于 2019-11-29 08:59:53

看答案

break白 发表于 2019-11-29 09:18:39

测试代码能测其他文件吗

magic_ant 发表于 2019-11-29 10:42:00

#include <stdio.h>

int main()
{
    printf(" Hello World!");
    return 0;
}

新人报到,请多关照!!

科目三太难了 发表于 2019-11-29 11:09:30

让我康康答案

小言。 发表于 2019-11-29 11:14:33

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

sanguine_boy 发表于 2019-11-29 11:17:02

对比答案

qq850336121 发表于 2019-11-29 11:30:35

编码错的真多,跟小甲鱼的代码对了好几遍,不知道错哪里了。{:5_104:}

ykhkh 发表于 2019-11-29 11:34:54

小甲鱼牛皮

hantianchi 发表于 2019-11-29 11:49:57

查看参考答案

SakuraManor 发表于 2019-11-29 12:45:43

哇哦

fanhua 发表于 2019-11-29 14:57:58

答案

鱼子流 发表于 2019-11-29 15:26:04

???

rww 发表于 2019-11-29 16:02:48

嗯嗯

wts0715 发表于 2019-11-29 16:11:57

耶耶耶耶耶

周周周周的周周 发表于 2019-11-29 19:04:44

1
页: 465 466 467 468 469 470 471 472 473 474 [475] 476 477 478 479 480 481 482 483 484
查看完整版本: S1E2:第一个程序 | 课后测试题及答案