鱼C论坛

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

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

    [复制链接]
发表于 2020-4-22 01:42:58 | 显示全部楼层
零基础入门学习C语言封面
《零基础入门学习C语言》
小甲鱼 著
立即购买
0.计算机只懂二进制
1.机器码
2.编译
3.是否需要解释器
4.可以
5.通过解释器
6.查表,解码
7
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 02:38:37 | 显示全部楼层
回复
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-22 08:34:31 | 显示全部楼层
111
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-22 08:41:43 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-22 10:55:32 | 显示全部楼层
我来了,鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 11:09:02 | 显示全部楼层
66
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-22 12:05:09 | 显示全部楼层
本帖最后由 Tiehanhank 于 2020-4-22 12:08 编辑

#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;
}





哪里错了呜呜呜
结果如下:
计算中......
The path . is wrong!
The path s1e2 is wrong!
Can not open the file: count_lines.c
Can not open the file: count_lines.c
The path .DS_Store is wrong!
The path s1e2 is wrong!
The path count_lines is wrong!
The path count_lines.c is wrong!
The path a.out is wrong!
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
Can not open the file: count_lines.c
目前你总共写了 0 行代码!


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

使用道具 举报

发表于 2020-4-22 12:37:28 | 显示全部楼层
查看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-22 13:50:41 | 显示全部楼层

#include<io.h>
#include<direct.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX     256

long total;

int countlines(const char *fliename);
void findallcodes(const char *path);
void findallfiles(const char *parth);

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[MAC];
       
        strcpy(thepath,path);
        if((handle=_findfirst(strcat(thepath,"/*.c"),&fa))!=-1L)
        {
                do
                {
                        sprintf(target,"%s/%s",path,fa.name);
                        total+=countlinrs(target);
                 }while(_findnext(handle,&fa)==0)
         }
}
  void findalldirs(cont char *path)
  {
          struct_finddata_t fa;
          long handle;
          char thepath[MAX];
         
          strcpy(thepath,path);
          if((handle=_findfirst(strcat(thepath,"/*"),&fa))=-1L)
          {
                  fprintf
          }
          
          do
          {
                  if(!strcmp(fa.name,".")||!strcmp(fa.name,".."))
                  continue;
                 
                  if(fs.attri==_A_SUBDIR)
                  {
                          sprintf(thepath,"%s/%s",path,fa.name);
                          findallcodes(thepath);
                          findalldirs(thepath);
                  }
          }whule(_findclose(handle,&fa)==0);
          
          _findclose(handle);       
  }
  
  int main9
{
        char path[MAX]=".";
       
        printf("计算中...\n");
       
        findallcodes(path);
        findalldirs(path);
       
        printf("目前你总共写了%ld行代码!\n\n",total) ;
        system("pasue");
       
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 13:52:41 | 显示全部楼层
查看答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 14:36:38 | 显示全部楼层
0、因为计算机只能读懂0和1
1、机器语言
2、编译
3、编译型语言直接编译比较灵活、解释型语言还需要解释器
4、能
5、解释型编程语言执行时需要解释器
6、原理是编译,将密码按照表翻译成能读懂的语言
7、
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 15:05:29 | 显示全部楼层
修改
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-22 16:07:43 | 显示全部楼层
0.只认识二进制
1.机器语言
2.编译
3.解释型语言可跨平台
4.不能
5.例如Java就有jvm包可实现跨平台
6.二进制码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 16:44:31 | 显示全部楼层
9999999999
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 17:01:03 | 显示全部楼层
109行代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 17:16:45 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-22 17:21:52 | 显示全部楼层
看看答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 17:49:54 | 显示全部楼层
0,
1,机器语言
2,编译
3,能否直接编译成机器码
4,不能
5,将源码转换成中间代码,然后发送给解释器,由解释器逐句翻译给 CPU
6,查表
7,亲们,趁敌人吃饭时发动进攻
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-22 19:07:49 | 显示全部楼层
0.        因为计算机只认识二进制代码
1.        编程语言
2.        编译
3.        解释型语言需要解释器,编译型语言不需要
4.        是
5.        需要解释器
6.        查表
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

FCBarcelona 该用户已被删除
发表于 2020-4-22 20:02:10 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-24 11:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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