鱼C论坛

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

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

    [复制链接]
发表于 2019-9-3 14:41:16 | 显示全部楼层
零基础入门学习C语言封面
《零基础入门学习C语言》
小甲鱼 著
立即购买
:上课
:起立
:老师好,
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
:好的,同学们今天这节课就到这里
:起立
:小甲鱼老师辛苦了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 14:48:23 | 显示全部楼层
本帖最后由 b_74 于 2019-9-3 14:52 编辑
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sys/stat.h>

  7. #define MAX 256

  8. long total;

  9. int countLines(const char *filename);
  10. int isCode(const char *filename);
  11. void findAllDirs(const char *path);

  12. int countLines(const char *filename)
  13. {
  14.         FILE *fp;
  15.         int count = 0;
  16.         int temp;

  17.         if ((fp = fopen(filename,"r")) ==NULL)
  18.         {
  19.                 fprintf(stderr,"Can not open the file: %s\n",filename);
  20.                 return 0;
  21.         }

  22.         while ((temp = fgetc(fp)) != EOF)
  23.         {
  24.                 if (temp == '\n')
  25.                 {
  26.                         count++;
  27.                 }
  28.         }

  29.         fclose(fp);

  30.         return count;
  31. }

  32. int isCode(const char *filename);
  33. {
  34.         int length;

  35.         length = strlen(filename);

  36.         if (!strcmp(filename + (length - 2), ".c"))
  37.         {
  38.                 return 1;
  39.         }
  40.         else
  41.         {
  42.                 return 0;
  43.         }
  44. }

  45. void findAllDirs(const char *path)
  46. {
  47.         DIR *dp;
  48.         struct dirent *entry;
  49.         struct stat statbuf;

  50.         if ((dp = opendir(path)) == NULL)
  51.         {
  52.                 fprintf(stderr, "The path %s is wrong!\n",path);
  53.                 return;
  54.         }

  55.         chdir(path);
  56.         while ((entry = readdir(dp)) != NULL)
  57.         {
  58.                 lstat(entry->d_name, &statbuf);

  59.                 if (!strcmp(".", entry->d_name) || !strcmp("..",entry->d_name))
  60.                   continue;

  61.                 if (S_ISDIR(statbuf.st_mode))
  62.                 {
  63.                         findAllDirs(entry->d_name);
  64.                 }
  65.                 else
  66.                 {
  67.                         if (isCode(entry->d_name));
  68.                         {
  69.                                 total += countLines(entry->d_name);
  70.                         }
  71.                 }
  72.                 }

  73.         chdir("..");
  74.         closedir(dp);
  75. }

  76. int main()
  77. {
  78.         char path[MAX] = ".";

  79.         printf("计算中...\n");

  80.         findAllDirs(path);

  81.         printf("目前你总共写了 %ld 行代码!\n\n",total);

  82.         return 0;
  83. }
复制代码



提示42行第1个字符错误

root@wang-virtual-machine:/home/myfiles# gcc s1e2.c -o s1e2.exe
s1e2.c:42:1: error: expected identifier or ‘(’ before ‘{’ token
{
^

请小甲鱼老师百忙之中不吝赐教,谢谢

环境是Ubuntu 18.04.02
Vim版本8.0
gcc版本7.4
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 14:50:35 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-3 15:06:37 | 显示全部楼层
看看看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 15:36:48 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-3 16:16:41 | 显示全部楼层
226
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-3 16:25:11 | 显示全部楼层
22hang you cuowu ei
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 18:27:59 From FishC Mobile | 显示全部楼层
巴拉巴拉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 19:36:16 | 显示全部楼层
1

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

使用道具 举报

发表于 2019-9-3 19:40:51 | 显示全部楼层
0.它只认识二进制代码
1.机器码
2.编译
3.编译型语言可以直接cpu执行
解释型语言需要将源代码转化为中间代码再给解释器翻译给cpu
4.不行
5.不同的解释器可实现在不同操作系统下文件的执行
6.查表 特定符号对应一个字母
7.亲们趁敌人吃饭时发动进攻
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 19:49:15 | 显示全部楼层
啊啊啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 19:49:33 | 显示全部楼层
努力学习天天向上
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

发表于 2019-9-3 21:49:06 | 显示全部楼层
你好,我刚学,我想知道课后作业程序从一开始就是这么编译的,如何到达count-lines
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 22:30:10 | 显示全部楼层
0.机器只能识别二进制0 1的代码
1.机器语言
2.编译
3.解释型语言需要解释器将语言进行翻译
4.能
5.使用解释器
6.使用符号组合来代表实际功能
7.QINMENCHENDiR
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 22:41:34 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-3 22:46:22 | 显示全部楼层

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

使用道具 举报

发表于 2019-9-4 00:10:06 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-4 01:10:03 | 显示全部楼层
本帖最后由 WITPEN白羽 于 2019-9-4 01:11 编辑

[Witpen@Witpen s1e2]$ gcc cs.c -o cs
cs.c: 在函数‘countLines’中:
cs.c:19: 错误:‘FTLF’未声明(在此函数内第一次使用)
cs.c:19: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
cs.c:19: 错误:所在的函数内也只报告一次。)
cs.c:19: 错误:‘fp’未声明(在此函数内第一次使用)
cs.c: 在函数‘findAllDirs’中:
cs.c:64: 错误:‘null’未声明(在此函数内第一次使用)
[Witpen@Witpen s1e2]$


这事为什么啊,大佬 ,头文件我都写了啊  没有少写
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-4 07:24:45 | 显示全部楼层
认真做题,积极学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-8-10 20:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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