鱼C论坛

 找回密码
 立即注册
楼主: 小甲鱼

[课后作业] S1E2:第一个程序 | 课后测试题及答案

    [复制链接]
发表于 2023-8-25 01:12:27 | 显示全部楼层
鱼C有你更精彩^_^
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-25 01:14:45 | 显示全部楼层
#include<io.h>
#include<direct.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define MAX 256
long total;
int countLines(const char *filename);
void fineAllCodes(const char *path);
void fineAllFiles(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,"/*.c"),&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("目前你总写了%ld行代码!\n\n",total);
                return 0;
        }
运行出来是0
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-25 08:18:21 | 显示全部楼层
6666666666666
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-25 12:04:56 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-25 23:47:25 | 显示全部楼层
爱你爱你鱼鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-26 17:29:03 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-26 21:28:35 | 显示全部楼层
.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-26 23:26:26 | 显示全部楼层
102行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-26 23:56:35 | 显示全部楼层
COOL!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-27 10:56:17 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-27 14:56:56 | 显示全部楼层
11111
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-27 20:11:07 | 显示全部楼层

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-27 20:14:01 | 显示全部楼层
sedfsd
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-27 23:33:34 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-28 01:37:23 | 显示全部楼层
你好
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-28 10:44:55 | 显示全部楼层
111
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-28 17:40:09 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-28 19:18:36 | 显示全部楼层
1111111
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-28 20:15:08 | 显示全部楼层
答案在哪???呜呜

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-28 21:46:09 | 显示全部楼层
看看答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-8 20:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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