没有合适的资源?快使用搜索试试~ 我知道了~
首页How to Think Like a Computer Scientist: C version
资源详情
资源评论
资源推荐
How to Think Like a Computer Scientist
C Version
Allen B. Downey
C-Version by Thomas Scheffler
Version 1.05
April 11th, 2010
2
Copyright (C) 1999 Allen B . Downey
This book is an Ope n Source Textbook (OST). Permission is granted to repr o-
duce, store or transmit the text of this book by any means, electrical, mecha n-
ical, or biological, in accordance with the terms of the GNU General Public
License as published by the Free Software Foundation (version 2).
This book is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
The original form of this book is LaTeX source code. Compiling this LaTeX
source has the effect of generating a device-independent representation of a
textbook, which can be converted to other formats and printed. All intermediate
representations (including DVI and Postscript), and all printed copies of the
textbook are also covered by the GNU General Public License.
The LaTeX source for this book, and mor e information about the Open Source
Textbook project, is available from
http://www.cs.colby.edu/~downey/ost
or by writing to Allen B. Downey, 5850 Mayflower Hill, Waterville, ME 04901.
The GNU General P ublic License is available from www.gnu.org or by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 3 30, Boston, MA
02111-1307, USA.
This book was typeset by the author using LaTeX a nd dvips, which are both
free, open-source programs .
Contents
1 The way of the program 1
1.1 What is a programming language? . . . . . . . . . . . . . . . . . 1
1.2 What is a program? . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 What is debugging? . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3.1 Compile-time errors . . . . . . . . . . . . . . . . . . . . . 4
1.3.2 Run-time errors . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.3 Logic errors and semantics . . . . . . . . . . . . . . . . . 4
1.3.4 Experimental debugging . . . . . . . . . . . . . . . . . . . 5
1.4 Formal and natural la nguages . . . . . . . . . . . . . . . . . . . . 5
1.5 The first program . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2 Variables and types 13
2.1 More output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 Outputting variables . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.6 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.7 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.8 Order of oper ations . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.9 Operators for characters . . . . . . . . . . . . . . . . . . . . . . . 19
ii Contents
2.10 Compos itio n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3 Function 23
3.1 Floating-point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.3 Converting from double to int . . . . . . . . . . . . . . . . . . . 25
3.4 Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.5 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.6 Adding new functions . . . . . . . . . . . . . . . . . . . . . . . . 27
3.7 Definitions and uses . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.8 Programs with multiple functions . . . . . . . . . . . . . . . . . . 29
3.9 Parameters and arguments . . . . . . . . . . . . . . . . . . . . . 30
3.10 Parameters and variables are local . . . . . . . . . . . . . . . . . 31
3.11 Functions with multiple parameters . . . . . . . . . . . . . . . . . 32
3.12 Functions with results . . . . . . . . . . . . . . . . . . . . . . . . 32
3.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4 Conditionals and recursion 37
4.1 Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . 37
4.2 The modulus ope rator . . . . . . . . . . . . . . . . . . . . . . . . 38
4.3 Alternative execution . . . . . . . . . . . . . . . . . . . . . . . . . 38
4.4 Chained conditionals . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.5 Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.6 The return statement . . . . . . . . . . . . . . . . . . . . . . . . 40
4.7 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.8 Infinite recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.9 Stack diagrams for recursive functions . . . . . . . . . . . . . . . 43
4.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Contents iii
5 Fruitful functions 47
5.1 Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.2 Program development . . . . . . . . . . . . . . . . . . . . . . . . 4 9
5.3 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.4 Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5.5 Boolean variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.6 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.7 Bool functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.8 Returning from main . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6 Iteration 61
6.1 Multiple assignment . . . . . . . . . . . . . . . . . . . . . . . . . 61
6.2 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
6.3 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . 62
6.4 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.5 Two-dimensional tables . . . . . . . . . . . . . . . . . . . . . . . 66
6.6 Encapsulation and generalization . . . . . . . . . . . . . . . . . . 66
6.7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.8 More encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . 68
6.9 Local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
6.10 More generalization . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7 Arrays 75
7.1 Increment and decrement operators . . . . . . . . . . . . . . . . . 76
7.2 Accessing elements . . . . . . . . . . . . . . . . . . . . . . . . . . 76
7.3 Copying arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.4 for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
剩余124页未读,继续阅读
linmc
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论3