鱼C论坛

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

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

    [复制链接]
发表于 2023-9-5 17:24:05 | 显示全部楼层
6666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-5 20:08:39 | 显示全部楼层
38781
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-5 20:14:57 | 显示全部楼层
加1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-5 20:22:46 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-5 20:23:00 | 显示全部楼层
#include <stdio.h>
#include <unistd.h>
#include < 啧啧啧>
#include < 字符串.h>
#include <stdlib.h>
#include <sys/stat.h>

#define 最大 256

长总计;

int countLines (const char *filename);
int isCode(const char *filename);
void findALLDirs(const char *path);

int countLines(const char *filename)
{
        文件 *fp;
        整数计数 = 0;
        整数温度:

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

        而 ( (temp = fgetc (fp) ) ! = EOF)
        {
                if (temp == '\n')
"test2.c" 108L, 1532C                                                                   13,1         顶端
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-5 20:44:01 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-5 21:13:27 | 显示全部楼层
kanakn
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-5 22:02:59 | 显示全部楼层
计算机只能读懂特定语言
机器语言
编译
编译型语言在源程序编译后即可在该平台运行,解释型语言在运行期间才编译
不能
主要是通过语言解释器来实现跨平台
由简单的短和长的脉冲及其间隔来表示字母和数字的相互转换
不知道
1693922490228.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-6 07:47:38 | 显示全部楼层
本帖最后由 Lost_Mi_Yang 于 2023-9-6 08:13 编辑

感谢小甲鱼。

我觉得有两点需要提一下:

1、本次练习中windows系统的示例代码中声明了findALLFiles这个函数,但是后面没有定义它,对应的,后面的findALLDirs这个函数没有和其他函数一起声明,而是声明的同时进行了定义。我想这里应该是您一时疏忽吧,不过不影响代码运行结果,所以我就不去找茬区专门发帖了。

2、即使抄代码的过程中没有错误,最后运行结果也可能不对,因为学生相关的设置可能没有做好(唉)。我使用的是windows平台的VS Code,默认使用UTF-8编码,运行结果是乱码,改成GB 2312就可以输出正常的结果了。

不知道您有没有空刷到这里呢。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-6 10:48:22 | 显示全部楼层
答案
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-6 10:50:05 | 显示全部楼层
ddd
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-6 10:52:28 | 显示全部楼层
不知道错哪
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-6 11:12:32 | 显示全部楼层
好的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-6 11:16:50 | 显示全部楼层

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

使用道具 举报

发表于 2023-9-6 16:30:55 | 显示全部楼层
ddd
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-6 17:04:09 | 显示全部楼层

                               
登录/注册后可看大图



不知道为什么看不出来区别,却死活运行不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-6 19:18:43 | 显示全部楼层
成功了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-6 21:29:27 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-6 22:38:46 | 显示全部楼层
嘿嘿
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-6 22:57:04 | 显示全部楼层
aa
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-8 04:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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