鱼C论坛

 找回密码
 立即注册
查看: 1444|回复: 4

[已解决]帮我同学问的小甲鱼第一次作业的问题

[复制链接]
发表于 2023-2-24 21:22:55 | 显示全部楼层 |阅读模式
10鱼币
一个同学出现的问题,关于小甲鱼的第一次课后作业的,问题太多加之我能力有限,故来求助
第一次课后作业链接:https://fishc.com.cn/thread-66283-1-1.html

https://fishc.com.cn/thread-66283-1-1.html
先上源码
#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);
viod findALLFiles(const char *path);

int countLines(const char *filename)
{
        FILEn *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;
}

viod findAllCodes(const char *path)
{
        struct_finddata_t fa;
        long handle;
        char thePath[MAX],target[MAX];
       
        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[MAX];
       
        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 patj[MAX]=".";
       
        printf("计算中...\n");
       
        findAllCodes(path);
        findALLDirs(path);
       
        printf("目前你总共写了%1d行代码!\n\n",total);
        system("pause");
       
        return 0;
       
}
使用的是DevC++集成开发环境

QQ图片20230224131603.jpg
QQ图片20230224131621.png
QQ图片20230224131629.jpg

报错信息全部截下来了
最佳答案
2023-2-24 21:22:56
本帖最后由 ExiaGN001 于 2023-2-25 15:18 编辑

首先强调一下发帖格式问题:
2. 代码大于20行以上时,最好写清自己的思路和注释,这样回答的人才能尽快给大家答案!(避免扔上一大堆代码,说是有错误,请人指点!)
3. 发代码请务必使用编辑器的“添加代码文字”(这个符号:<>)
有用请设置最佳答案谢谢

                               
登录/注册后可看大图



代码问题如下(斜体红字为编译器报错信息(翻译过),斜体蓝字为运行逻辑的问题,蓝字为C语言预留关键字,绿字为C++ STL库):

第13行 没有viod类型(您的意思是"void"吗?)   
                把viod改成void

第17行 没有FILEn类型(您的意思是"FILE"吗?)
                把FILEn 改成FILE (注意大小写,这里要表扬把*写在靠变量一侧的好习惯)

第18行 在int前缺少','或';' (报错显示是19行,但错误在第18行)
                行尾漏加分号(一定是英文分号,初学者常犯错误)

第40行 没有viod类型(您的意思是"void"吗?)
                viod改成void

第42行 没有struct_finddata_t类型(您的意思是"_finddata_t"吗?)
                需要用空格把struct 和_finddata_t 隔开

第51行 sprintf字符串的输出不对
                改成这样:sprintf(target,"%s\\%s",path,fa.name);

第61行 没有struct_finddata_t类型(您的意思是"_finddata_t"吗?)
                需要用空格把struct 和_finddata_t 隔开

第79行 sprintf的参数不对
                改成这样:sprintf(thePath,"%s\\%s" ,path,fa.name);

第90行 没有定义过path(您的意思是"patj"吗?)(报错显示是第94行)
                把90行的patj改成path即可。

第51行和第79行的输出其实不对,在C/C++中,需要用转义字符\\来输出\


                               
登录/注册后可看大图


改完错误之后的代码,可以正常运行:
#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[MAX],target[MAX];

        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[MAX];

        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[MAX]=".";

        printf("计算中...\n");

        findAllCodes(path);
        findALLDirs(path);

        printf("目前你总共写了%1d行代码!\n\n",total);
        system("pause");

        return 0;

}
有用请设置最佳答案谢谢

最佳答案

查看完整内容

首先强调一下发帖格式问题: 2. 代码大于20行以上时,最好写清自己的思路和注释,这样回答的人才能尽快给大家答案!(避免扔上一大堆代码,说是有错误,请人指点!) 3. 发代码请务必使用编辑器的“添加代码文字”(这个符号:) 有用请设置最佳答案谢谢 代码问题如下(斜体红字为编译器报错信息(翻译过),斜体蓝字为运行逻辑的问题,蓝字为C语言预留关键字,绿字为C++ STL库): 第13行 没有viod类型(您的意思是"void ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-24 21:22:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ExiaGN001 于 2023-2-25 15:18 编辑

首先强调一下发帖格式问题:
2. 代码大于20行以上时,最好写清自己的思路和注释,这样回答的人才能尽快给大家答案!(避免扔上一大堆代码,说是有错误,请人指点!)
3. 发代码请务必使用编辑器的“添加代码文字”(这个符号:<>)
有用请设置最佳答案谢谢

                               
登录/注册后可看大图



代码问题如下(斜体红字为编译器报错信息(翻译过),斜体蓝字为运行逻辑的问题,蓝字为C语言预留关键字,绿字为C++ STL库):

第13行 没有viod类型(您的意思是"void"吗?)   
                把viod改成void

第17行 没有FILEn类型(您的意思是"FILE"吗?)
                把FILEn 改成FILE (注意大小写,这里要表扬把*写在靠变量一侧的好习惯)

第18行 在int前缺少','或';' (报错显示是19行,但错误在第18行)
                行尾漏加分号(一定是英文分号,初学者常犯错误)

第40行 没有viod类型(您的意思是"void"吗?)
                viod改成void

第42行 没有struct_finddata_t类型(您的意思是"_finddata_t"吗?)
                需要用空格把struct 和_finddata_t 隔开

第51行 sprintf字符串的输出不对
                改成这样:sprintf(target,"%s\\%s",path,fa.name);

第61行 没有struct_finddata_t类型(您的意思是"_finddata_t"吗?)
                需要用空格把struct 和_finddata_t 隔开

第79行 sprintf的参数不对
                改成这样:sprintf(thePath,"%s\\%s" ,path,fa.name);

第90行 没有定义过path(您的意思是"patj"吗?)(报错显示是第94行)
                把90行的patj改成path即可。

第51行和第79行的输出其实不对,在C/C++中,需要用转义字符\\来输出\


                               
登录/注册后可看大图


改完错误之后的代码,可以正常运行:
#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[MAX],target[MAX];

        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[MAX];

        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[MAX]=".";

        printf("计算中...\n");

        findAllCodes(path);
        findALLDirs(path);

        printf("目前你总共写了%1d行代码!\n\n",total);
        system("pause");

        return 0;

}
有用请设置最佳答案谢谢

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-25 09:15:56 | 显示全部楼层
少 “;”
void  , 拼写错误
FILE , 写成了FILEn
struct 【这时有空格】_finddata_t fa;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-25 09:30:06 | 显示全部楼层
两手空空儿 发表于 2023-2-25 09:15
少 “;”
void  , 拼写错误
FILE , 写成了FILEn

请问这个84行这里是哪里出错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-25 14:58:52 | 显示全部楼层
本帖最后由 yinda_peng 于 2023-2-25 15:00 编辑

@夏季的春秋 你自己看
忘了你没好友权限,通知你了记得看帖
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 17:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表