Intro, Key Concepts, Functions
Introduction
Programming Language
解释器与编译器
Program
一系列为了实现某种功能的指令集合
需要的部分:
input
output
math:比如加法等算数运算
testing:检查程序的执行是否正常
repetition:通常用于执行重复的工作
Debuging
错误的种类
Compile-time error
Run-time error
System Development
Life Cycle
Application 通常包含了各种程序,用于实现各种任务
这些任务的设计与组合通过系统分析师(System Analyst)在程序计划期完成
流程 Cycle:计划→分析→设计>实现→维护→计划
Modularization
Programming 中最重要的一点就是整合(调用)已经实现的代码到我们自己的程序中。调用通常通过函数(function)来实现。通过函数,我们可以将大型的 program 分解为若干的小的部分进行实现。
函数通常分为两种类型:
函数的建立方式
定义函数:撰写函数实际的代码
声明函数:告诉编译器我们已经有了某个函数的原型
调用函数
函数的交流
函数通过参数(parameter)进行数据的传递与交流。这种方式允许我们同时使用多个函数来完成复杂的任务。
C++ 中的函数
Special task function
函数的前置声明
函数的前置声明是为了使函数对编译器可见。通常在程序的撰写中,main() 函数处于其他函数实现的前面。main() 中可能会包含各种定义至于其后的函数的调用。调用此类后置定义的函数,必须要对其进行前置申明,也就是:声明→调用→定义
Programming Design
Pseudocode
为了描述逻辑而使用,不是真正的可编译代码。比如下面的例子:
当使用伪代码确定了程序的设计之后,我们就可以使用具体的程序语言,按照指定好的逻辑来实现程序。对于程序复杂的情况,每个子模块都需要以 IPO 的形式独立存在,并以 Hierarchy chart 的形式串联起来。这种方法被称为 HIPO(
Hierarchy IPO)
Test data
Syntax rules
IDE
IDE(integrated Development Environment)指集成开发环境,包括了:
编译过程
加载 pre-processor
将编程语言转换为机器指令,并将指令和对应数据&linker 的解决方案保存为 Object file
linker 通过 Object file 将对象,库连接到一起,生成可执行文件
IDE 通过 loader 来读取可执行文件进行和运行
编译过程中的错误
Compiler Directives
Key concepts
Variables and types
C++ 中的数据类型属性
Family | Data Type | Reserved Word | Represents | Standard Type |
Nothing | Null or nothing | void | No data | Yes |
Integer | Boolean | bool | Logical true and false | Yes |
Integer | Character | char | Single characters | Yes |
Integer | Integer | int | Whole numbers | Yes |
Floating Point | Floating Point | float | Fractional numbers | Yes |
Complex | String | string | A sequence (sting them along) of characters | No |
Complex | Array | N/A | A collection of elements of the same data type | No |
Complex | Pointer | N/A | A value that points to a location (an address) within the data area | No |
数据类型的特点
变量的类型决定了值的取值范围
赋值的变量必须符合声明的类型
输出变量指输出变量中存储的值
Identifier Names
C++ 中的 indentifier names
Good Practices
有意义的
尽量一致
不要以下划线开头
变量使用小写,常量使用全大写
Constants and Variables
constants 的三种形式
literal:字符串常量,是值
defined:指用字符(文本)代替的具体常量:
Data Manipulation
计算机中的数据操作是基于数学的
C++ 中的运算符分为单目,双目和三目
Assignment
Assignment 允许更新指定空间的值。C++ 中使用 =
作为 assignment 的运算符。运算符右边可以是值,也可以是表达式。
输出变量
Arithmetic
类型转换
隐式类型转换
指系统自动进行的类型转换
转换是否发生和结果取决于运算符的类型
Integer
Float-point
String 类型
Operators
Order of operations
Operator for chars
Lvalue & Rvalue
Functions
Floating-point
Math functions
Composition
自定义函数
函数的执行顺序
parameter & argument
parameter and variable are local
函数的结构分析
伪代码
结构表
Program Control Functions
Program Control Function
Void Data Type
可读文档的建立
文档:至于源程序顶部,用于说明程序的详情
Vertical Alignment
正确使用 comments
Function Banner
Block Markers
Indent Block Markers
Meaningful Identifier
正确使用 typedef
Conditionals and recursion
modulus operator
除整数,生成余数
可判断是否能被整除 / 求整数最右边的几位数
带条件的执行
nested condition
Recursion
函数调用自身
通过自身参数的变化来进行下一步的处理
Infinite Recursion
正常的递归需要
base case
趋向于 Base case 的条件
更多的递归形式
阶乘
base → 0!=1
递归 → f(n!) = n * f(n-1)
Furitful functions
return value
Development
Incremental development
使用小步骤渐进式的编写程序
使用临时变量测试中间值,保证每一步修改的正确性
移除所有的测试语句(scaffolding)
Composition
Overloading
Boolean
只有 ture 和 false 两种值
可以被用作 flag
Logocal operator
Bool functions
Main return