xiaomianhua 发表于 2020-3-6 09:48:54

答案

从头开始学C 发表于 2020-3-6 10:17:45


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

daiqipei 发表于 2020-3-6 10:35:20

{:5_102:}

wheat1 发表于 2020-3-6 10:57:14

来了

daiqipei 发表于 2020-3-6 10:57:34

daiqipei 发表于 2020-3-6 10:35


老师,我复制了最后一题
int main()
{
      char path = ".";
      
      printf("计算中...\n");
      
      findAllCodes(path);
      findALLDirs(path);
      
      printf("目前你总共写了 %ld 行代码!\n\n", total);
      system("pause");
      
      return 0;

输出的结果怎么是 :目前你总共写了 0 行代码 ?

yxyr1994 发表于 2020-3-6 11:02:54

daan

惜沫 发表于 2020-3-6 11:17:14

终于能看答案了,开森

lonewolf96 发表于 2020-3-6 11:24:15

回复

MercyG 发表于 2020-3-6 11:31:23

{:10_277:}

ffgsan 发表于 2020-3-6 11:58:59

AAAAAA

编号7259 发表于 2020-3-6 12:17:11

#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)
{

维度间隙 发表于 2020-3-6 12:39:47

莱纳爱Coco 发表于 2020-3-6 12:47:01

康康答案

空白的脑 发表于 2020-3-6 13:21:56

本帖最后由 空白的脑 于 2020-3-6 14:52 编辑

感谢

choose 发表于 2020-3-6 13:47:16

谢谢鱼哥

不爱取名的鳄鳄 发表于 2020-3-6 14:04:49

程序错误不可运行

玻璃ing 发表于 2020-3-6 14:31:30

233

幻影游龙 发表于 2020-3-6 14:59:00

好多都不会

不大的小白 发表于 2020-3-6 15:05:34

感谢楼主无私奉献!

姜小巴 发表于 2020-3-6 15:16:49

很有意义,看了肯定会忘,还是要动动手。
页: 602 603 604 605 606 607 608 609 610 611 [612] 613 614 615 616 617 618 619 620 621
查看完整版本: S1E2:第一个程序 | 课后测试题及答案