马叉虫AAA 发表于 2021-3-29 14:35:28

1

sentokiryu 发表于 2021-3-29 15:25:24

前来观摩学习

niejianqing 发表于 2021-3-29 15:28:16

感谢小甲鱼~

修maker 发表于 2021-3-29 15:36:05


颜若心颐 发表于 2021-3-29 16:12:07

新手来报道

yg041968 发表于 2021-3-29 16:15:54

做好了,来求答案{:5_109:}

血色的救赎 发表于 2021-3-29 16:17:36

给我个答案谢谢

血色的救赎 发表于 2021-3-29 16:35:55

为什么我用的是win7,程序运行之后用了102行

1329745734 发表于 2021-3-29 16:41:08


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

客心愁未澜 发表于 2021-3-29 16:52:25

{:5_109:}

jiamingxin 发表于 2021-3-29 17:16:39

查看参考答案

大地郡主 发表于 2021-3-29 18:04:21

为什么这个网页会乱码了?

ck2870530199 发表于 2021-3-29 18:35:29

666

HelloZW 发表于 2021-3-29 18:56:31

1

1852837203 发表于 2021-3-29 19:11:50

1

1434854808 发表于 2021-3-29 19:54:52

终于打出来了,谢谢

bairuijie123 发表于 2021-3-29 20:04:52

1

Mysticc 发表于 2021-3-29 20:40:25

学习

w1746710632 发表于 2021-3-29 20:48:02

win10系统,用的VS2019,我该怎么把统计代码行数的这段代码放到编写代码学习的主目录啊,真的是有点看不懂,第一次学,没看明白主目录是什么意思,VS2019那个要新建解决方案,然后再新建项目,

天坑逃脱者 发表于 2021-3-29 20:49:05

真想知道
页: 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 [1136] 1137 1138 1139 1140 1141 1142 1143 1144 1145
查看完整版本: S1E2:第一个程序 | 课后测试题及答案