鱼C论坛

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

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

    [复制链接]
发表于 2019-7-13 07:18:58 | 显示全部楼层
零基础入门学习C语言封面
《零基础入门学习C语言》
小甲鱼 著
立即购买
#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;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 12:20:08 From FishC Mobile | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-13 12:40:24 From FishC Mobile | 显示全部楼层
emmmmm
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 14:38:00 | 显示全部楼层
。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-13 14:47:26 | 显示全部楼层
0.计算机只能识别二进制
1.0和1组成的二进制语言
2.编译
3.编译的方式不同
4.大部分可以,也许需要修改
5.在编译过程中有跳转环节,也正是这点是的解释性语言速度较慢
6.用二进制表示字母和数字
7.亲们,趁敌人吃饭时发动进攻
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 15:25:07 | 显示全部楼层
PU只懂一种语言,那就是机器码,即二进制指令或数据代码啦。其他计算机语言,是要经编译为二进制机器码,CPU才能执行啦。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 17:34:12 | 显示全部楼层
手动提答案是啥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 17:53:10 | 显示全部楼层
XD
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-13 18:18:49 | 显示全部楼层
回复什么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 19:59:25 | 显示全部楼层
答案出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 20:05:05 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-13 20:46:24 | 显示全部楼层
微信图片_20190713204606.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 20:48:09 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-13 21:16:38 | 显示全部楼层
朕想知道
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 21:24:11 | 显示全部楼层
计算中...
目前你写了 0 行代码!

请按任意键继续. . .
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 21:28:07 | 显示全部楼层
0,应为它只能识别二进制代码。
1,机器语言。
2,编译。
3,解释行语言不直接告诉CPU该做什么。
4,不能。
5,通过虚拟机,这样只用给不同平台编写虚拟机就可以轻松的实现大部分程序跨平台了。
6,其实就是个密码表,只要查表翻译就可以了。
7,亲们趁敌人吃饭时发动进攻
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 21:43:32 | 显示全部楼层
感谢鱼c
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 22:46:20 From FishC Mobile | 显示全部楼层
做完啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-14 00:00:55 | 显示全部楼层
查看参考答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-14 04:45:59 | 显示全部楼层
#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;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-12 11:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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