三个鸽Ge 发表于 2020-7-27 10:29:22

小甲鱼真棒!

三个鸽Ge 发表于 2020-7-27 11:15:24


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

三个鸽Ge 发表于 2020-7-27 11:16:07

三个鸽Ge 发表于 2020-7-27 11:15


总算打好了,但是怎么只显示7行,哭了哭了

壶中天 发表于 2020-7-27 11:34:14

{:5_109:}

waj2260 发表于 2020-7-27 12:07:36

感谢楼主无私奉献

Pilo 发表于 2020-7-27 12:41:16

感谢小甲鱼

[WARN] 发表于 2020-7-27 13:23:20

23333

baihaotian612 发表于 2020-7-27 14:16:35

打卡学习

piopio 发表于 2020-7-27 14:34:23

yexiangzhen 发表于 2020-7-27 15:12:46

你好

yzh1111 发表于 2020-7-27 16:01:23

0.只认识01

白槿霜风 发表于 2020-7-27 16:10:23

答案

rsthan 发表于 2020-7-27 17:07:46


#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 findAllCode(const char *patn)
{
        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_finddate_t fa;
        long handle;
        char rhePath;
       
        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;
}

Ezgnahz 发表于 2020-7-27 17:18:35

感谢楼主无私奉献!

a754200575 发表于 2020-7-27 17:28:09

感谢楼主无私奉献!

花花果冻 发表于 2020-7-27 17:55:29

回复啥子蛮{:5_100:}

romon2000 发表于 2020-7-27 18:16:02

C:\Users\YAO\Desktop\collect2.exe        ld returned 1 exit status
第一课课后作业得到最后的报错信息,不知道是哪里出问题了

天空的问候 发表于 2020-7-27 18:27:06

1

眼前的都是浮云 发表于 2020-7-27 20:14:15


感谢楼主无私奉献!

Chong冲啊 发表于 2020-7-27 20:17:45

{:5_109:}
页: 776 777 778 779 780 781 782 783 784 785 [786] 787 788 789 790 791 792 793 794 795
查看完整版本: S1E2:第一个程序 | 课后测试题及答案