鱼C论坛

 找回密码
 立即注册
查看: 3060|回复: 16

《带你学C带你飞》(s1e2)感悟

[复制链接]
发表于 2019-8-7 00:04:41 | 显示全部楼层 |阅读模式

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

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

x
刚做完课后习题,第一题我的理解“二傻子”我以为计算机二就二在它只能识别二进制的二(0和1)
然后既然一个合格的程序员要有10万行代码,不知道哪位大佬真的有这种计算自己编译器从下载到电脑到现在自己所敲的代码行数的代码(我只是个新手我不会啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-7 00:05:33 | 显示全部楼层
突然发现这个主题挺好看的,论坛也挺好玩的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 00:09:38 | 显示全部楼层
不知道哪位大佬真的有这种计算自己编译器从下载到电脑到现在自己所敲的代码行数的代码

这句话读不懂,不知道你想说什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 08:02:01 | 显示全部楼层
计算机的确只知道0和1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 08:08:54 | 显示全部楼层
你的意识是想知道自己敲了多少代码,就是编译器能不能计算出曾经敲的代码的总和?对吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-7 10:26:01 | 显示全部楼层
2164930278 发表于 2019-8-7 08:08
你的意识是想知道自己敲了多少代码,就是编译器能不能计算出曾经敲的代码的总和?对吗

对的,差不多就这个意思,10w行嘛,是一个程序员的基本素养
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-7 10:27:57 | 显示全部楼层
人造人 发表于 2019-8-7 00:09
这句话读不懂,不知道你想说什么

如何计算自己敲的代码行数,请用一个程序实现
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 11:04:33 | 显示全部楼层
千语绯月 发表于 2019-8-7 10:27
如何计算自己敲的代码行数,请用一个程序实现

就是相当于把自己写的文件放在一个目录下,然后检查文件后缀,打开文件,一个‘\n’算一行,关闭文件,打开下一个文件
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 12:44:49 | 显示全部楼层
课后题就有呀
Linux 和 MacOS:
#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;
}

Windows:
#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("目前你总共写了 %ld 行代码!\n\n", total);
        system("pause");
        
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-7 18:09:43 | 显示全部楼层
蓝炎彼岸花 发表于 2019-8-7 12:44
课后题就有呀
Linux 和 MacOS:

我试了,那是固定的行数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 18:22:09 | 显示全部楼层
千语绯月 发表于 2019-8-7 18:09
我试了,那是固定的行数

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

使用道具 举报

发表于 2019-8-7 20:09:29 | 显示全部楼层
二傻子是不认识二的,不然怎么叫做“二”傻子呢?
第一次课后题应该有计算代码量的源程序代码。
源程序代码如下:
#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("目前你总共写了%ld行代码!\n\n",total);
        system("pause");
        
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 20:14:16 | 显示全部楼层
你所要求的的程序估计要链接编译程序,随时动态地记录你所敲的代码行数。对不起,这种程序我无能为力。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-7 20:40:24 | 显示全部楼层
有啥意义?
有功夫在这纠结这个,这时间都够你敲个几十行了。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-8-7 22:22:35 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-8 09:15:55 From FishC Mobile | 显示全部楼层
课后题我做了,也正确运行了,结果显示0行代码。这个是什么问题。尴尬。也放在一个文件夹了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-8 21:20:42 | 显示全部楼层
蓝炎彼岸花 发表于 2019-8-7 12:44
课后题就有呀
Linux 和 MacOS:

WINDOWS版,文件夹路径和后缀名,是那几句语句?该怎么修改路径?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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