Data types and records · 数据类型与记录
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| data type/ˈdeɪtə taɪp/ | 数据类型 | shù jù lèi xíng |
| field/fiːld/ | 字段 | zì duàn |
| record/ˈrekɔːd/ | 记录 | jì lù |
| record structure/ˈrekɔːd ˈstrʌktʃə/ | 记录结构 | jì lù jié gòu |
| dot notation/dɒt nəʊˈteɪʃn/ | 点号表示法 | diǎn hào biǎo shì fǎ |
The most expensive data-type decision in history
- In the 1960s memory cost more than programmers. To save two characters per date, years were stored as two digits:
99meant 1999. - Nobody expected the code to outlive the century. It did, and on 1 January 2000 every one of those fields would read as 1900. Governments and companies spent an estimated $300 billion checking and fixing it.
- The bug was not a typo. It was a data type 数据类型 chosen for the memory of 1965 and kept for the dates of 2000.
- This lesson is how to pick the type that fits the data, and how to bundle values that belong together into a record 记录.
史上最昂贵的数据类型决定
- 20 世纪 60 年代,内存比程序员贵。为了每个日期省两个字符,年份存成两位数:
99表示 1999。 - 没人料到这些代码会活过那个世纪。它们活过了,而 2000 年 1 月 1 日,每一个这样的字段都会读成 1900。政府和企业花了约 3000 亿美元检查和修复。
- 这个 bug 不是打错字。它是一个为 1965 年的内存选择、却一直用到 2000 年的日期的数据类型(data type)。
- 这一课讲怎样选择适合数据的类型,以及怎样把属于一起的值捆成一条记录(record)。
The six basic types
| Type | Holds | Use it for |
|---|---|---|
INTEGER |
a whole number, 42, -7 |
counts, indexes, IDs |
REAL |
a number with a fractional part, 3.14 |
measurements, averages |
CHAR |
a single character, 'A' |
a grade letter, a menu choice |
STRING |
characters in quotes, "Hello" |
text |
BOOLEAN |
TRUE or FALSE |
flags |
DATE |
a calendar date | dates of birth, deadlines |
Pseudocode also names two structured types: ARRAY and FILE
六种基本类型
| 类型 | 存放 | 用于 |
|---|---|---|
INTEGER |
整数,42、-7 |
计数、索引、ID |
REAL |
带小数部分的数,3.14 |
测量值、平均数 |
CHAR |
单个字符,'A' |
等级字母、菜单选项 |
STRING |
引号中的字符,"Hello" |
文本 |
BOOLEAN |
TRUE 或 FALSE |
标志 |
DATE |
日历日期 | 出生日期、截止日期 |

伪代码还有两种结构化类型:ARRAY 和 FILE
Which data type best stores a yes/no flag like "in stock"? · 哪种数据类型最好地存储一个像“有库存”的是/否标志?
A flag has two states, so BOOLEAN (TRUE/FALSE) is the precise choice — not the strings "yes"/"no". · 一个标志有两个状态,所以 BOOLEAN(TRUE/FALSE)是精确的选择——而不是字符串“yes”/“no”。
A real (floating-point) type should be used to store a measurement such as a temperature. · 一个实数(浮点)类型应该用来存储一个像温度的测量。
Whole counts use integers; measurements use reals. · 整数计数用整数;测量用实数。
Choose by how the value is used
- A type is decided by what the program will do with the value, not by how it looks.
- The number of students is
INTEGER; the class's average mark isREAL, because it has a fractional part; whether a student has paid isBOOLEAN. - An array index is always
INTEGER. A single grade letter isCHAR; an email address isSTRING. - A phone number is a
STRING: it starts with0, and nobody ever adds two phone numbers.
按值的用法选择
- 类型由程序将对这个值做什么决定,而不是它看起来像什么。
- 学生人数是
INTEGER;班级平均分是REAL,因为它有小数部分;学生是否已付费是BOOLEAN。 - 数组索引永远是
INTEGER。单个等级字母是CHAR;电子邮件地址是STRING。 - 电话号码是
STRING:它以0开头,而且没有人会把两个电话号码相加。
Match each value to the best data type for it. · 把每个值与最适合它的数据类型配对。
Pick the precise type: BOOLEAN for a flag, REAL for fractional values, INTEGER for whole counts, STRING for text. · 选精确的类型:BOOLEAN 给一个标志,REAL 给小数值,INTEGER 给整数计数,STRING 给文本。
Which data type should store a phone number such as 07700 900123? · 07700 900123 这样的电话号码应该用哪种数据类型存储?
An INTEGER would drop the leading zero, and no program adds or multiplies phone numbers. The digits are text. · INTEGER 会丢掉开头的零,也没有程序会把电话号码相加或相乘。这些数字是文本。
Worked example: give the appropriate data type
| Data | Type | Because |
|---|---|---|
| number of items in stock | INTEGER |
a whole count |
| price of an item | REAL |
has pence, a fractional part |
| whether the item is on offer | BOOLEAN |
only two states |
the item's category code, 'F' |
CHAR |
exactly one character |
| the supplier's name | STRING |
text |
| the date the item was delivered | DATE |
a calendar date |
- Give the pseudocode name, not the language you happen to know:
BOOLEAN, notbool;REAL, notfloat.
例题:给出合适的数据类型
| 数据 | 类型 | 因为 |
|---|---|---|
| 库存商品数量 | INTEGER |
整数计数 |
| 商品价格 | REAL |
有便士,即小数部分 |
| 商品是否在促销 | BOOLEAN |
只有两种状态 |
商品类别代码 'F' |
CHAR |
恰好一个字符 |
| 供应商名称 | STRING |
文本 |
| 商品送达日期 | DATE |
日历日期 |
- 写伪代码的名称,不是你恰好会的语言:
BOOLEAN而不是bool;REAL而不是float。
The data type of an array index is always ____. · 数组索引的数据类型永远是 ____。
An index counts positions, so it is a whole number. Names[2.5] has no meaning. · 索引计数的是位置,所以是整数。Names[2.5] 没有意义。
Records
- A record (a record structure 记录结构) holds a set of data items of different data types under one identifier. Each item is a field 字段.
- Use one when several values always describe one thing: a stock item, a customer, a student.
One identifier, four fields, four types
记录
- 记录(记录结构,record structure)在一个标识符下保存一组不同数据类型的数据项。每一项是一个字段(field)。
- 当几个值总是描述同一个事物时使用它:一件库存商品、一位顾客、一名学生。
TYPE TStockItem
DECLARE ItemID : INTEGER
DECLARE Category : STRING
DECLARE ItemCost : REAL
DECLARE InStock : BOOLEAN
ENDTYPE

一个标识符,四个字段,四种类型
A record is used to: · 一个记录用来:
A record bundles fields of (possibly) different types that describe one thing. An array holds many values of the same type. · 一个记录捆绑描述一件事的(可能)不同类型的字段。一个数组保存许多相同类型的值。
Declaring and using a record
TYPE … ENDTYPEdefines the type. Then declare variables of it, singly or as an array:
- Reach a field with dot notation 点号表示法, to write and to read:
声明和使用记录
TYPE … ENDTYPE定义类型。然后声明该类型的变量,单个或数组:
DECLARE Item1 : TStockItem
DECLARE Items : ARRAY[1:100] OF TStockItem
- 用点号表示法(dot notation)访问字段,写入和读取都是:
Item1.Category ← "Fruit"
OUTPUT Item1.Category, " costs ", Item1.ItemCost
Items[7].InStock ← FALSE
A record groups fields under one name · 一个记录在一个名字下把字段分组
A record bundles related fields together. Each field is a named label you reach with dot notation — Item1.Category — not by a numeric index. · 一个记录把相关的字段捆绑在一起。每个字段是一个有名字的标签,你用点记法到达它——Item1.Category——而不是用一个数字索引。
To set the Category field of a record variable Item1, you write: · 要设置一个记录变量 Item1 的 Category 字段,你写:
Dot notation record.field reaches a field of a record. · 点记法 record.field 到达一个记录的一个字段。
Worked example: the club membership file
- A club stores, for each student, a student ID (a string), a name, a date of birth and up to three club numbers. Declare the record type, an array for 3000 students, and store a name in the first element.
- The marks:
TYPEwith the identifier andENDTYPE; every field declared with a suitable type; the array with its bounds andOF Student; the field reached with the index and the dot.
例题:俱乐部会员文件
- 一个俱乐部为每名学生存储学号(字符串)、姓名、出生日期和最多三个俱乐部编号。声明记录类型、容纳 3000 名学生的数组,并在第一个元素中存储一个姓名。
TYPE Student
DECLARE StudentID : STRING
DECLARE Name : STRING
DECLARE DateOfBirth : DATE
DECLARE Club : ARRAY[1:3] OF INTEGER
ENDTYPE
DECLARE Membership : ARRAY[1:3000] OF Student
Membership[1].Name ← "Li Wei"
- 得分点:带标识符的
TYPE和ENDTYPE;每个字段用合适的类型声明;数组带边界和OF Student;用索引和点号访问字段。
Put the lines of the club membership declaration in order. · 把俱乐部会员声明的各行按顺序排列。
Define the type, close it, declare a variable or array of it, then use index and dot to reach a field. · 定义类型,结束它,声明该类型的变量或数组,再用索引和点号访问字段。
Worked example: state the error in the declaration
- Three errors, each a mark: the type has no
ENDTYPE; the fieldPricehas no data type; andTotalwill holdSeats * Price, an arithmetic result, so it cannot be aSTRING. - The examiner's usual three: a missing
ENDTYPE, a field without a type, a field of the wrong type for what is done with it.
例题:指出声明中的错误
TYPE TBooking
DECLARE Seats : INTEGER
DECLARE Price
DECLARE Total : STRING
- 三个错误,各一分:类型没有
ENDTYPE;字段Price没有数据类型;Total将保存Seats * Price这个算术结果,所以不能是STRING。 - 考官常用的三种:缺
ENDTYPE、字段没有类型、字段的类型与它的用途不符。
Which of these are errors in a record declaration? Select all · 所有 that apply. · 以下哪些是记录声明中的错误?选出所有适用的。
Different types in one record is the whole point of a record. The other three are the errors examiners plant. · 一条记录中有不同类型正是记录的意义所在。另外三项是考官埋的错误。
Record, array, or plain variables
- Values that always belong together and have different types: a record.
- Many values of the same type under one name: an array (next lesson).
- A table of rows where each row has mixed fields: an array of records,
Members[i].Name. - Unrelated values: separate variables. Bundling them gains nothing.
记录、数组,还是普通变量
- 总是属于一起且类型不同的值:记录。
- 一个名字下同一类型的许多值:数组(下一课)。
- 每行有混合字段的表:记录数组,
Members[i].Name。 - 不相关的值:分开的变量。捆在一起没有任何好处。
An array of records lets one identifier hold a table in which every row has fields of different types. · 记录数组让一个标识符保存一张表,其中每一行都有不同类型的字段。
Members[i] is one row (a record); Members[i].Name is one cell. The array gives the rows, the record gives the mixed columns. · Members[i] 是一行(一条记录);Members[i].Name 是一个单元。数组提供行,记录提供混合的列。
Marks that slip away
- A yes/no value is a
BOOLEAN, not the strings"yes"and"no". - A phone number or an ID that starts with
0is aSTRING, never anINTEGER. - Money in a
REALpicks up rounding errors; many systems store whole pennies as anINTEGER. - Every field is a
DECLAREline with a type, and the type ends withENDTYPE. The index goes on the array, the dot on the field:Members[3].Name.
容易丢掉的分
- 是/否的值是
BOOLEAN,不是字符串"yes"和"no"。 - 以
0开头的电话号码或 ID 是STRING,绝不是INTEGER。 - 用
REAL存钱会产生舍入误差;许多系统用INTEGER存整数便士。 - 每个字段都是一行带类型的
DECLARE,类型以ENDTYPE结束。索引放在数组上,点号放在字段上:Members[3].Name。
You've got it
- six basic types,
INTEGER,REAL,CHAR,STRING,BOOLEAN,DATE, chosen by what the program does with the value - a record holds fields of different types under one identifier:
TYPE … DECLARE field : type … ENDTYPE - declare variables or arrays of the type, and reach a field with dot notation:
Membership[1].Name ← "Li Wei" - the errors the examiner plants: no
ENDTYPE, a field with no type, a field typed wrongly for its use
你掌握了
- 六种基本类型,
INTEGER、REAL、CHAR、STRING、BOOLEAN、DATE,按程序对值做什么来选 - 记录在一个标识符下保存不同类型的字段:
TYPE … DECLARE 字段 : 类型 … ENDTYPE - 声明该类型的变量或数组,用点号表示法访问字段:
Membership[1].Name ← "Li Wei" - 考官埋的错误:没有
ENDTYPE、字段没有类型、字段的类型与用途不符