小木心 发表于 2019-5-15 23:05:36

支持小甲鱼

430975 发表于 2019-5-16 03:42:49

来学习了{:10_266:}

VCjun 发表于 2019-5-16 11:44:34

感谢鱼C

zhangll269 发表于 2019-5-16 14:49:45

为什么dp = opendir(path)) == NULL 这是True 打不开当前目录

咕叽咕叽古 发表于 2019-5-16 19:42:40


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

虺诚 发表于 2019-5-16 21:36:15

此评论仅作者可见

WX忘 发表于 2019-5-16 22:02:47

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

#define MAX 256

;

int countlines ( const char * filename ) ;
void ficdAllCodes ( const char * path );
viod findALLFiles ( const char * path );

int countLines ( const char*filename )
{
        FILE * fp;
        int count = 0;
        int temp;
       
        if ( ( fp = fopen ( filename, "r")) == NULL)
        {
                fprintf ( stderr, "无法打开文件: %s\n",filename);
                return 0;
               
        }
        while (( temp = fgetc ( fp ) ) ! = EOF )
        {
                if ( temp == '\n')
                {
                        计数++;
                       
                }
        }
       
        FCLOSE ( FP );
       
        返回计数;
       
}
void findALLCodes ( const char * path )
{
                strnct finddata t fa;
                长柄;
                char thePath , 目标;
               
                strcpy ( thePath, path);
                if ((handle = _findfirst (strcat (thePath,"/ *。c"), &fa)) ! = -1L)
                {
                        做
                        {
                                sprintf (target, "%s/%s",path, fa.name);
                                总计+ = countLines ( 目标 );
                               
                       } while (_findnext (handle, &fa ) == 0);
                       
                }
               
                _findclose (手柄);
               
}
void findALLDirs ( const char * path )
{
        struct _finddata_t fa;
        长柄;
        char thePath ;
       
        strcpy ( thePath, path );
        if (( handle = _findfirst (strcat ( thePath,"/ *" ) ,&fa ) ) == -1L)
        {
                        fprintf( stderr,"路径%s错了!\n",路径);
                        return;
                       
       }
       
       做
       {
                       if(!strcmp(fa.name,"。")||!strcmp ( fa.name,".." ) )
                                       继续;
                                       
                                       if( fa.attirb == _A_SUBDIR )
                                       {
                                                       sprintf ( thePath,"%s/%s",path,fa.name);
                                                       findALLCodes ( thePath );
                                                       findALLDirs ( thePath );
                                                      
                                          }
                                          
          } while (_findnext ( handle, &fa ) == 0 );
          
          _findclose ( 手柄 );
          
       
}
int main()
{
                char path = "。";
               
                的printf( "计算中…\n");
               
                findALLCodes (路径);
                findALLDirs(路径);
               
                printf("目前你总共写了%ld行代码!\n\n",总计);
                系统("暂停");
                return 0;
}

林云天梦 发表于 2019-5-16 22:19:08

哈啊哈

shuziwen2019 发表于 2019-5-16 23:57:30

1

shuziwen2019 发表于 2019-5-17 00:48:54

为什么我做出来的结果是116行呢

幻影1 发表于 2019-5-17 01:13:27

努力,努力,再努力

andypete 发表于 2019-5-17 11:18:08

第一天学习

7cking白夜 发表于 2019-5-17 16:34:07

为什么我的是你已经写了0行代码,不是复制了那么多吗

john8556 发表于 2019-5-17 20:01:46

感谢分享

Remysal 发表于 2019-5-17 20:18:11

666

白汤圆 发表于 2019-5-17 22:18:10

为什么我抄都抄错了

lemonchang 发表于 2019-5-17 22:52:48

{:5_102:}

坤帅 发表于 2019-5-17 23:15:44

{:5_105:}

BrossAloha 发表于 2019-5-17 23:29:56

111

mochengkong 发表于 2019-5-18 10:59:20

55555555
页: 289 290 291 292 293 294 295 296 297 298 [299] 300 301 302 303 304 305 306 307 308
查看完整版本: S1E2:第一个程序 | 课后测试题及答案