鱼C论坛

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

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

    [复制链接]
发表于 2024-2-6 11:37: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
回复 支持 反对

使用道具 举报

发表于 2024-2-6 13:25:11 | 显示全部楼层
Hello
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-6 14:21:50 | 显示全部楼层
66666
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-6 17:35:11 | 显示全部楼层
隐藏内容
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-6 19:41:34 | 显示全部楼层
102行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

发表于 2024-2-7 18:31:46 | 显示全部楼层
.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-2-7 19:22:24 | 显示全部楼层
11
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-2-8 13:19:22 | 显示全部楼层
哈哈哈哈哈哈哈哈哈哈哈哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-8 18:55:51 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-2-9 10:20:10 | 显示全部楼层
终于打完了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-10 11:44:41 | 显示全部楼层
只能识别计算机语言
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-11 05:36:20 | 显示全部楼层
666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-2-12 21:27:05 | 显示全部楼层
我为啥只能显示计算中?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-13 02:08:50 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-2-13 15:39:54 | 显示全部楼层
ok
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-2-13 16:30:12 | 显示全部楼层
我只能显示计算中,所以我自己学着写了新的#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>

#define MAX_FILE_SIZE 10000

int count_lines(char *filename) {
        FILE *file = fopen(filename, "r");
        if (file == NULL) {
                printf("无法打开文件 %s\n", filename);
                return 0;
        }
       
        int count = 0;
        char line[MAX_FILE_SIZE];
        while (fgets(line, sizeof(line), file) != NULL) {
                count++;
        }
       
        fclose(file);
        return count;
}

int count_lines_recursive(char *dir_path) {
        DIR *dir = opendir(dir_path);
        if (dir == NULL) {
                printf("无法打开目录 %s\n", dir_path);
                return 0;
        }
       
        int total_lines = 0;
       
        struct dirent *entry;
        while ((entry = readdir(dir)) != NULL) {
                char entry_path[MAX_FILE_SIZE];
                sprintf(entry_path, "%s/%s", dir_path, entry->d_name);
               
                struct stat entry_info;
                if (stat(entry_path, &entry_info) == -1) {
                        printf("无法获取文件信息:%s\n", entry_path);
                        continue;
                }
               
                if (S_ISDIR(entry_info.st_mode)) {
                        // 忽略 "." 和 ".." 目录
                        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
                                continue;
                        }
                       
                        // 递归处理子目录并累加代码行数
                        total_lines += count_lines_recursive(entry_path);
                } else if (S_ISREG(entry_info.st_mode) && strstr(entry->d_name, ".exe") == NULL) {
                        // 处理普通文件,排除 .exe 文件
                        int lines = count_lines(entry_path);
                        printf("%s: %d 行\n", entry_path, lines);
                        total_lines += lines;
                }
        }
       
        closedir(dir);
        return total_lines;
}

int main() {
        char current_dir[MAX_FILE_SIZE];
        if (getcwd(current_dir, sizeof(current_dir)) == NULL) {
                printf("无法获取当前目录\n");
                return 1;
        }
       
        printf("当前目录:%s\n", current_dir);
        printf("开始统计代码行数...\n");
       
        int total_lines = count_lines_recursive(current_dir);
       
        printf("所有目录中代码行数的总和为:%d\n", total_lines);
        printf("统计完成!\n");
       
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-14 08:32:48 | 显示全部楼层
S1E2:第一个程序 | 课后测试题及答案 [修改]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-14 18:49:43 | 显示全部楼层
HAHA
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-2-14 18:50:00 | 显示全部楼层
AA
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-4 08:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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