鱼C论坛

 找回密码
 立即注册
查看: 2181|回复: 8

C第一节课课后题统计代码行数的这个程序只统计这个一个文件的吗?

[复制链接]
发表于 2019-12-19 10:24:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys / stat.h>

#定义MAX 256

总长

int countLines(const char *文件名);
int isCode(const char *文件名);
无效findAllDirs(const char * path);

int countLines(const char *文件名)
{
        文件* fp;
        int count = 0;
        温度

        如果((fp = fopen(文件名,“ r”))== NULL)
        {
                fprintf(stderr,“无法打开文件:%s \ n”,文件名);
                返回0;
        }

        而((temp = fgetc(fp))!= EOF)
        {
                如果(temp =='\ n')
                {
                        数++;
                }
        }

        fclose(fp);

        返回计数;
}

int isCode(const char *文件名)
{
        整数长度

        长度= strlen(文件名);
        
        如果(!strcmp(文件名+(长度-2),“ .c”))
        {
                返回1;
        }
        其他
        {
                返回0;
        }
}

无效findAllDirs(const char * path)
{
        DIR * dp;
        struct dirent * entry;
        struct stat statbuf;

        如果((dp = opendir(path))== NULL)
        {
                fprintf(stderr,“%s路径错误!\ n”,路径);
                返回;
        }

        chdir(路径);
        而((entry = readdir(dp))!= NULL)
        {
                lstat(entry-> d_name,&statbuf);

                if(!strcmp(“。”,entry-> d_name)||!strcmp(“ ..”,entry-> d_name))
                        继续;

                如果(S_ISDIR(statbuf.st_mode))
                {
                        findAllDirs(entry-> d_name);
                }
                其他
                {
                        如果(isCode(entry-> d_name))
                        {
                                总数+ = countLines(entry-> d_name);
                        }
                }
        }

        chdir(“ ..”);
        closeir(dp);
}

int main()
{
        char path [MAX] =“。”;

        printf(“计算中... \ n”);

        findAllDirs(path);

        printf(“目前你总共写了%ld行代码!\ n \ n”,总计);

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

使用道具 举报

发表于 2019-12-19 10:37:18 | 显示全部楼层
当然是统计所有符合要求的文件。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 10:37:23 | 显示全部楼层
我记得小甲鱼说可以看自己十万行代码,但是我之后写了别的程序之后运行还是这一个程序的行数,是为什么呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 11:28:45 | 显示全部楼层
ba21 发表于 2019-12-19 10:37
当然是统计所有符合要求的文件。

那为什么之后写程序再编译运行不计算在内呢,是有文件统计范围吗,我这个程序单独mkdir了一个文件夹?顺便麻烦问一下怎么上传图片啊,我点图片只有相册和网络图片,没有本地的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 11:35:49 | 显示全部楼层
supermly 发表于 2019-12-19 11:28
那为什么之后写程序再编译运行不计算在内呢,是有文件统计范围吗,我这个程序单独mkdir了一个文件夹?顺 ...

发现了发送图片好像是等级不够。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-19 12:31:33 | 显示全部楼层
supermly 发表于 2019-12-19 11:28
那为什么之后写程序再编译运行不计算在内呢,是有文件统计范围吗,我这个程序单独mkdir了一个文件夹?顺 ...

统计范围肯定是有。.c  .cpp  .pas .py .php .java等等这些扩展名不都是源代码文件。
代码怎么写是你自己的事。统计范围当然是根据文件的扩展名来确定
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 14:53:14 | 显示全部楼层
ba21 发表于 2019-12-19 12:31
统计范围肯定是有。.c  .cpp  .pas .py .php .java等等这些扩展名不都是源代码文件。
代码怎么写是你自 ...

才发现复制的代码有些是浏览器翻译成汉字的,我在下一楼发没有翻译的。请问一下这个程序的统计范围是怎样的呢,我看这里统计的应该是.c的文件,是在哪些范围内找.c的文件呢?是不是必须在同一个文件夹下?新学看不太懂这个程序,麻烦了。
int isCode(const char *filename)
{
        int length;

        length = strlen(filename);
        
        if (!strcmp(filename + (length - 2), ".c"))
        {
                return 1;
        }
        else
        {
                return 0;
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 14:54:01 | 显示全部楼层
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

#define MAX 256

long total;

int countLines(const char *filename);
int isCode(const char *filename);
void findAllDirs(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;
}

int isCode(const char *filename)
{
        int length;

        length = strlen(filename);
        
        if (!strcmp(filename + (length - 2), ".c"))
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

void findAllDirs(const char *path)
{
        DIR *dp;
        struct dirent *entry;
        struct stat statbuf;

        if ((dp = opendir(path)) == NULL)
        {
                fprintf(stderr, "The path %s is wrong!\n", path);
                return;
        }

        chdir(path);
        while ((entry = readdir(dp)) != NULL)
        {
                lstat(entry->d_name, &statbuf);

                if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
                        continue;

                if (S_ISDIR(statbuf.st_mode))
                {
                        findAllDirs(entry->d_name);
                }
                else
                {
                        if (isCode(entry->d_name))
                        {
                                total += countLines(entry->d_name);
                        }
                }
        }

        chdir("..");
        closedir(dp);
}

int main()
{
        char path[MAX] = ".";

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

        findAllDirs(path);

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

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 19:16:17 | 显示全部楼层
supermly 发表于 2019-12-19 14:53
才发现复制的代码有些是浏览器翻译成汉字的,我在下一楼发没有翻译的。请问一下这个程序的统计范围是怎样 ...

我把程序移到上层文件夹就可以统计了,谢谢,以后能看懂了我再慢慢研究
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 03:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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