Quatuordecimner 发表于 2021-4-16 01:56:28

略微学过java,看下C好不好学

wuwuwuwwu 发表于 2021-4-16 09:02:48

第一天开始

小奕同学 发表于 2021-4-16 10:00:42

#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX      256

long total;

int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(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;
}

void findAllCodes(const char *path)
{
      struct _finddata_t fa;
      long handle;
      char thePath, target;
      
      strcpy(thePath, path);
      if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
      {
                do
                {
                        sprintf(target, "%s/%s", path, fa.name);
                        total += countLines(target);
                }while (_findnext(handle, &fa) == 0);
      }
   
      _findclose(handle);
}

void findALLDirs(const char *path)
{
      struct _finddata_t fa;
      long handle;
      char thePath;
      
      strcpy(thePath, path);
      if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
      {
                fprintf(stderr, "The path %s is wrong!\n",path);
                return;
      }
   
      do
      {      
                if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
                        continue;
                  
                if( fa.attrib == _A_SUBDIR)
                {      
                        sprintf(thePath, "%s/%s", path, fa.name);
                        findAllCodes(thePath);
                        findALLDirs(thePath);
                }
      }while (_findnext(handle, &fa) == 0);
   
      _findclose(handle);   
}

int main()
{
      char path = ".";
      
      printf("计算中...\n");
      
      findAllCodes(path);
      findALLDirs(path);
      
      printf("目前你总共写了 %ld 行代码!\n\n", total);
      system("pause");
      
      return 0;
}

Leung7 发表于 2021-4-16 10:47:21

感谢分享

Mr.escapist 发表于 2021-4-16 11:54:44

查看参考答案

zjf1465385413 发表于 2021-4-16 13:42:55

1

炤靈 发表于 2021-4-16 15:27:02

親們趁敵人喫飯發動進攻

396861085 发表于 2021-4-16 16:44:36

123

2578575296 发表于 2021-4-16 17:14:26

.

Lucky- 发表于 2021-4-16 18:42:19

.

西子. 发表于 2021-4-16 19:12:49

(1)因为计算机的大脑是CPU,CPU负责计算机的大部分日常的计算功能,而CPU只认识0和1
(2)汇编语言
(3)
(4)能
(5)再不团的平台上装上对应的编译器就可以跨平台
(6)
(7)

斩鞍 发表于 2021-4-16 20:25:11

课后作业的代码我自己写了遍,然后提示未找到findALLFiles的函数定义,程序也运行不了

heejh 发表于 2021-4-16 21:13:14

6666666666

轻狂绝代 发表于 2021-4-16 21:33:05

666

goking 发表于 2021-4-16 21:41:25

111

duxiangying2021 发表于 2021-4-16 21:47:14

来看答案了

2769382599 发表于 2021-4-17 01:19:48

好好学习,天天向上,坚持

24kchunshuai 发表于 2021-4-17 09:40:30

ok

事不过三。 发表于 2021-4-17 10:41:16

0

Noa_ 发表于 2021-4-17 13:16:58

s1e2
页: 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 [1160] 1161 1162 1163 1164 1165 1166 1167 1168 1169
查看完整版本: S1E2:第一个程序 | 课后测试题及答案