Difference between revisions of "Basic Pascal Tutorial/Chapter 5/1-dimensional arrays/zh CN"

From Free Pascal wiki
Jump to navigationJump to search
m (bypass language bar/categorization template redirect [cf. discussion])
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{1-dimensional arrays}}
+
{{Basic Pascal Tutorial/Chapter 5/1-dimensional arrays}}
  
 
5C - 一维数组 (原作者: Tao Yue, 状态: 未更改)
 
5C - 一维数组 (原作者: Tao Yue, 状态: 未更改)
Line 11: Line 11:
  
 
一个数组包含多个存储空间,类型相同。你只需要指定数组名和下标就可以使用了:
 
一个数组包含多个存储空间,类型相同。你只需要指定数组名和下标就可以使用了:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   typename = array [下标类型] of 数据类型;
 
   typename = array [下标类型] of 数据类型;
Line 18: Line 18:
 
数据类型任意,甚至是一个数组。方括号中的“下标类型”可以是任何的顺序类型,即可以是整型,布尔型,字符类型,枚举类型,子界类型。
 
数据类型任意,甚至是一个数组。方括号中的“下标类型”可以是任何的顺序类型,即可以是整型,布尔型,字符类型,枚举类型,子界类型。
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   enum_type = 1..50;
 
   enum_type = 1..50;
Line 25: Line 25:
  
 
is equivalent to
 
is equivalent to
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   arraytype = array [1..50] of integer;
 
   arraytype = array [1..50] of integer;
Line 31: Line 31:
  
 
注:在此之前,程序员需要自己处理字符串存储问题。通过声明:
 
注:在此之前,程序员需要自己处理字符串存储问题。通过声明:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   String = packed array [0..255] of char;
 
   String = packed array [0..255] of char;
Line 42: Line 42:
 
如果你需要在程序中存储大量数据,数据的下标可以循环读取出来,例如:
 
如果你需要在程序中存储大量数据,数据的下标可以循环读取出来,例如:
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   arraytype = array[1..50] of integer;
 
   arraytype = array[1..50] of integer;
Line 51: Line 51:
  
 
使用:
 
使用:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
for count := 1 to 50 do
 
for count := 1 to 50 do
 
   read (myarray[count]);
 
   read (myarray[count]);
Line 58: Line 58:
 
使用数组时,使用方括号<tt>[ ]</tt>括起下标。
 
使用数组时,使用方括号<tt>[ ]</tt>括起下标。
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
myarray[5] := 6;
 
myarray[5] := 6;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
|[[Subranges/zh_CN|上一页]]
+
|[[Basic Pascal Tutorial/Chapter 5/Subranges/zh_CN|上一页]]
|[[Contents/zh_CN|目录]]
+
|[[Basic Pascal Tutorial/Contents/zh CN|目录]]
|[[Multidimensional_arrays/zh_CN|下一页]]
+
|[[Basic Pascal Tutorial/Chapter 5/Multidimensional arrays/zh_CN|下一页]]
 
|}
 
|}
  
 
[[Category:zh]]
 
[[Category:zh]]

Latest revision as of 15:20, 20 August 2022

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

5C - 一维数组 (原作者: Tao Yue, 状态: 未更改)

假设你需要使用5000个整数,你会如何存储?你可以定义5000个变量:

aa, ab, ac, ad, ... aaa, aab, ... aba, ...


但这很繁琐(声明这些变量后,你需要读取每个变量)。

一个数组包含多个存储空间,类型相同。你只需要指定数组名和下标就可以使用了:

type
  typename = array [下标类型] of 数据类型;

数据类型任意,甚至是一个数组。方括号中的“下标类型”可以是任何的顺序类型,即可以是整型,布尔型,字符类型,枚举类型,子界类型。

type
  enum_type = 1..50;
  arraytype = array [enum_type] of integer;

is equivalent to

type
  arraytype = array [1..50] of integer;

注:在此之前,程序员需要自己处理字符串存储问题。通过声明:

type
  String = packed array [0..255] of char;

使用某种特殊字符来表示字符串结束,大多使用空字符(0或ord(0)),packed指定了占用空间。

在C或C++编程语言中,把200个字符存入长度为150的数组,会造成缓冲区溢出。如果缓冲区被覆盖并包含可执行代码,那么,攻击者将会设法进入你的系统。这就是Slammer蠕虫病毒猖獗的原因。试试在Pascal中会发什么。

如果你需要在程序中存储大量数据,数据的下标可以循环读取出来,例如:

type
  arraytype = array[1..50] of integer;

var
  myarray : arraytype;

使用:

for count := 1 to 50 do
  read (myarray[count]);

使用数组时,使用方括号[ ]括起下标。

myarray[5] := 6;
上一页 目录 下一页