鱼C论坛

 找回密码
 立即注册
查看: 2337|回复: 4

[技术交流] 《带你学C带你飞》S1E2学习笔记

[复制链接]
发表于 2017-7-7 15:14:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 hxliu 于 2017-7-7 17:32 编辑

结合课后习题编写笔记。
S1E2

0x00:C诡异力气,缺陷重重,却获得了巨大成功。
                                                ——Dennis Ritchie

0、C语言优势:效率高、灵活度高、可移植性高。

1、CPU是计算机的“大脑”,负责日常计算,原理上只认识0和1。

2、CPU唯一认识的语言是机器语言(机器码),由纯数字组成。

3、区别
   (编译型语言)
    C语言->汇编语言->机器语言->CPU执行
    源代码=》编译器(编译)->机器码=>CPU(执行)
   
   (解释型语言)
    JAVA->字节码->解释器->CPU执行
    源代码(转换)-》中间代码(发送)->解释器(逐句翻译)=》CPU(执行)   
4、不同操作系统上C语言编译的可执行和程序格式不同,无法跨平台直接使用。例如:Linux大部分可执行文件为ELF格式,Windows大部分为PE格式。

5、解释型编程语言以不同操作系统的定制解释器作为中转,解释器提供统一入口,实现跨平台。

6、莫斯密码的原理:查表。

先和C语言打个招呼~

  1. #include <stdio.h>

  2. int main()
  3. {
  4.         printf("Hello World!\n");
  5.         return 0;
  6. }
复制代码


接下来是课后作业的第一个程序、以及多种注释方式
  1. // 统计当前目录及所有子目录下,C语言源文件的代码总行数。
  2. /*
  3. 注释1
  4. 注释2
  5. 注释3
  6. */
  7. #if 0
  8. 注释123456
  9. 注释123456
  10. #endif

  11. #include <io.h>
  12. #include <direct.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>

  16. #define MAX                        256

  17. long total;

  18. int countLines(const char *filename);
  19. int isCode(const char *filename);
  20. void findALLFiles(const char *path);

  21. int countLines(const char *filename)
  22. {
  23.                 FILE *fp;
  24.                 int count = 0;
  25.                 int temp;
  26.                
  27.                 if ((fp = fopen(filename,"r")) == NULL)
  28.                 {
  29.                                 fprintf(stderr,"Can not open the file:%s\n", filename);
  30.                                 return 0;
  31.                 }
  32.                
  33.                 while ((temp = fgetc(fp)) != EOF)
  34.                 {
  35.                                 if (temp == '\n')
  36.                                 {
  37.                                                 count++;
  38.                                 }
  39.                 }
  40.                
  41.                 fclose(fp);
  42.                
  43.                 return count;
  44.   }  
  45.   
  46.   void findAllCodes(const char *path)
  47.   {
  48.                     struct _finddata_t fa;
  49.                     long handle;
  50.                     char thePath[MAX], target[MAX];
  51.                     
  52.                     strcpy(thePath,path);
  53.                     if((handle = _findfirst(strcat(thePath,"/*.c"), &fa)) != -1L)
  54.                     {
  55.                                     do
  56.                                     {
  57.                                                     sprintf(target,"%s/%s",path, fa.name);
  58.                                                     total += countLines(target);
  59.                                         }while (_findnext(handle, &fa) == 0);
  60.                         }
  61.                        
  62.                         _findclose(handle);
  63.   }
  64.   
  65.   void findALLDirs(const char *path)
  66.   {
  67.                           struct _finddata_t fa;
  68.                           long handle;
  69.                           char thePath[MAX];
  70.                          
  71.                           strcpy(thePath, path);
  72.                           if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  73.                           {
  74.                                           fprintf(stderr, "The path %s is wrong!\n",path);
  75.                                           return;
  76.                         }
  77.                        
  78.                         do
  79.                         {
  80.                                         if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  81.                                                         continue;
  82.                                                        
  83.                                         if( fa.attrib == _A_SUBDIR)
  84.                                         {
  85.                                                         sprintf(thePath, "%s/%s", path, fa.name);
  86.                                                         findAllCodes(thePath);
  87.                                                         findALLDirs(thePath);
  88.                                         }
  89.                         }while (_findnext(handle, &fa) == 0);
  90.                        
  91.                         _findclose(handle);
  92.   }
  93.   
  94.   int main()
  95.   {
  96.                           char path[MAX] = ".";
  97.                          
  98.                           printf("计算中...\n");
  99.                          
  100.                           findAllCodes(path);
  101.                           findALLDirs(path);
  102.                          
  103.                           printf("目前你总共写了 %ld 行代码!\n\n", total);
  104.                           system("pause");
  105.                          
  106.                           return 0;
  107.   }
  108.   
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
小甲鱼 + 3 + 3 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-7 17:31:12 | 显示全部楼层
啥也没有
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-7 17:33:07 | 显示全部楼层

2333现在有啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-13 15:19:46 | 显示全部楼层
我是真喜欢你的排版~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-13 15:52:46 | 显示全部楼层
小甲鱼 发表于 2017-7-13 15:19
我是真喜欢你的排版~

哇!兴奋地跳起来!O(∩_∩)O谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 09:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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