鱼C论坛

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

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

    [复制链接]
发表于 2024-3-25 16:58:31 | 显示全部楼层
零基础入门学习C语言封面
《零基础入门学习C语言》
小甲鱼 著
立即购买
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-25 17:18:35 | 显示全部楼层
本帖最后由 黎明§末日 于 2024-3-25 17:21 编辑

课后作业:
测试题:
0:cpu它只能读的懂2进制的0和1
1:机器语言
2:编译
3:编译型语言编译出来的机器语言是可执行文件,可以直接被cpu执行
解释型语言不直接编译成机器码,而是将源代码转化为中间代码,发送给解释器由解释器逐句翻译给cpu执行
4:不能
5:解释器可以把源码编译成不同平台的代码,从而实现跨平台
6:是查表
7:qin men chen di ren chi fan shi fa dong
亲们 趁敌人吃饭时发动
动动手:

#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
回复 支持 反对

使用道具 举报

发表于 2024-3-25 17:22:28 | 显示全部楼层
学习学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-25 21:01:46 | 显示全部楼层
完成
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-25 22:29:11 | 显示全部楼层
查看参考答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-25 23:08:01 | 显示全部楼层
加油!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-26 10:01:04 | 显示全部楼层
.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-26 11:08:50 | 显示全部楼层
学习!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-26 12:54:00 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-26 14:41:57 | 显示全部楼层
111
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-26 16:08:05 | 显示全部楼层
抄完了,执行不了一点,把原代码复制过去和自己敲的错误数一样,不知道咋解决
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-26 18:26:51 | 显示全部楼层
我来啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-26 18:47:17 | 显示全部楼层
0:难道说因为是二进制的原因?
1:汇编语言
2:编译
3:一个是全部编写完再编译,一个是可以随时添加?
4:可以
5:因为他是转换成字节码,再有解释器翻译
6:二进制,0和1组成的代码
7:qfnmen chend irenc hifan shif adong jingo ng(猜不出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-26 20:59:49 | 显示全部楼层
6
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-26 21:13:56 | 显示全部楼层
想看答案

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

使用道具 举报

发表于 2024-3-26 21:22:14 | 显示全部楼层
啊,为什么我抄完但结果是0行(Linux)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-26 21:25:56 | 显示全部楼层

#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
回复 支持 反对

使用道具 举报

发表于 2024-3-27 13:14:33 | 显示全部楼层
0
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-27 13:16:52 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-27 14:19:20 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-4 04:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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