How to think like a computer scientist(C++)笔记
1. The way of the program
1.1 what’s a program language?
high-level languages and low-level languages.
2 ways to translate a program: interpreting or compiling.
1.2 what is a program?
input
output
math
testing
repeatition.
1.3 what is debugging?
1.3.1 compile-time errors
1.3.2 run-time errors
1.3.3 logic errors and semantics
1.3.4 experimental debugging
1.4 Formal and natural languages
Programming languages are formal languages that have been designed to express computations.
Differences between formal and natural languages:
ambiguity
redundancy
literalness
1.5 The first program
1.6 Glossary
2. Variables and types
2.1 More output
2.2 Values
2.3 Variables
2.4 Assignment
2.5 Outputting variables
2.6 Keywords
2.7 Operators
A possible alternative in integer division is to calculate a percentage rather than a fraction
2.8 Order of operations
2.9 Operators for characters
2.10 Composition
2.11 Glossary
3. Function
3.1 Floating-point
3.2 Converting from double to int
3.3 Math functions
3.4 Composition
3.5 Adding new functions
why is it worth the trouble to create all these new functions:
a. Creating a new function gives you an opportunity to give a name to group of statements.
b. Creating a new function can make a program smaller by eliminating repetitive code.
3.6 Definitions and uses
3.7 Programs with multiple functions
3.8 Parameters and arguments
3.9 Parameters and variables are local
3.10 Functions with multiple parameters
3.11 Functions with results
Any time you have a question about what is legal or illegal in C++, a good way to find out is to ask the compiler.
3.12 Glossary
4. Conditionals and recursion
4.1 The modulus operator
4.2 Conditional execution
4.3 Alternative execution
4.4 Chained conditionals
4.5 Nested conditionals
4.6 The return statement
4.7 Recursion
4.8 Infinite recursion
If a recursion never reaches a base case, it will never terminate. This is known as infinite recursion.
4.9 Stack diagrams for recursive functions
4.10 Glossary
5. Fruitful functions
5.1 Return values
fruitful functions ==> functions with return value
5.2 Program development
incremental development.
The key aspects of the process are:
a. Start with a working program and make small, incremental changes. At any point, if there is an error, you will know exactly where it it.
b. Use temporary variables to hold intermediate values so you can output and check them.
c. Once the program is working, you might want to remove some of the scaffolding or consolidate multiple statements into compound expressions, but only if it does not make the program difficult to read.
5.3 Composition
5.4 Overloading
Make sure that the version of the program you are looking at is the version of the program that is running!
5.5 Boolean values
5.6 Boolean variables
5.7 Logical operators
5.8 Bool functions
5.9 Returning from main
5.10 More recursion
5.11 Leap of faith
5.12 One more example
5.13 Glossary
6. Iteration
6.1 Multiple assignment
6.2 Iteration
6.3 The while statement
6.4 Tables
6.5 Two-dimensional tables
6.6 Encapsulation and generalization
6.7 Functions
Some of the reasons functions are useful:
a. By giving a name to a sequence of statements, you make your program easier to read and debug.
b. Dividing a long program into functions allows you to separate parts of the program, debug them in isolation, and then compose them into a whole.
c. Functions facilitate both recursion and iteration.
d. Well-designed functions are often useful for many programs. Once you write and debug one, you can reuse it.
6.8 More encapsulation
6.9 Local variables
6.10 More generalization
6.11 Glossary
7. Strings and things
7.1 Containers for strings
7.2 strings variables
7.3 Extracting characters from a string
7.4 Length
7.5 Traversal
7.6 A run-time error
7.7 The find function
7.8 Our own version of find
7.9 Looping and counting
7.10 Increment and decrement operators
7.11 String concatenation
7.12 strings are mutable
7.13 strings are comparable
7.14 Character classification
7.15 Other string functions
7.16 Glossary
8. Structures
8.1 Compound values
structures
classes
8.2 Point objects
8.3 Accessing instance variables
8.4 Operations on structures
8.5 Structures as parameters
8.6 Call by value
8.7 Call by reference
8.8 Rectangles
8.9 Structures as return types
8.10 Passing other types by reference
8.11 Getting user input
8.12 Glossary
9. More structures
9.1 Time
9.2 printTime
display the instance variables in a human-readable form.
9.3 Functions for objects
9.4 Pure functions
Takes objects and/or basic types as arguments but does not modify the objects.
9.5 const parameters
9.6 Modifiers
Takes objects as parameters and modifies some or all of them.
9.7 Fill-in functions
One of the parameters is an “empty” object that gets filled in by the function.
9.8 Which is best?
9.9 Incremental development versus planning
9.10 Generalization
9.11 Algorithms
9.12 Glossary
10. Vectors
10.1 Accessing elements
10.2 Copying vectors
10.3 for loops
10.4 Vector size
10.5 Vector functions
10.6 Random numbers
10.7 Statistics
10.8 Vector of random numbers
10.9 Counting
10.10 Checking the other values
10.11 A histogram
10.12 A single-pass solution
10.13 Random seeds
10.14 Glossary
11. Member functions
11.1 Objects and functions
Member functions differ from the other functions we have written in two ways:
a. When we call the function, we invoke it on an object, rather than just call it.
b. The function is declared inside the struct or class definition, in order to make the relationship between the structure and the function explicit.
11.2 print
11.3 Implicit variable access
11.4 Another example
11.5 Yet another example
11.6 A more complicated example
11.7 Constructors
11.8 Initialize or construct?
11.9 One last example
11.10 Header files
11.11 Glossary
12. Vectors of Objects
12.1 Composition
12.2 Card objects
12.3 The printCard function
12.4 The equals function
12.5 The isGreater function
12.6 Vectors of cards
12.7 The printDeck function
12.8 Searching
12.9 Bisection search
12.10 Decks and subdecks
12.11 Glossary
13. Objects of Vectors
13.1 Enumerated types
13.2 switch statement
13.3 Decks
13.4 Another constructor
13.5 Deck member functions
13.6 Shuffling
perfect shuffle
13.7 Sorting
13.8 Subdecks
13.9 Shuffling and dealing
13.10 Mergesort
13.11 Glossary
14. Classes and invariants
14.1 Private data and classes
14.2 What is a class?
14.3 Complex numbers
14.4 Accessor functions
14.5 Output
14.6 A function on Complex numbers: add
14.7 Another function on Complex numbers: mul
14.8 Invariants
14.9 Preconditions
14.10 Private functions
14.11 Glossary
15. File Input/Output and apmatrixes
15.1 Streams
15.2 File input
15.3 File output
15.4 Parsing input
15.5 Parsing numbers
15.6 The Set data structure
Ordering
Uniqueness
Arbitrary size
15.7 apmatrix
15.8 A distance matrix
15.9 A proper distance matrix
15.10 Glossary