1 / 10

几个基本概念的理解

几个基本概念的理解. 信息奥赛教学 _pascal 入门篇. Pacal 语言程序结构. program pname; const n=4; type arr=array [1..4] of integer; var i:integer; a:arr; begin for i:=1 to n do read(a[i]); readln; for i:=n downto 1 do write(a[i]:4); writeln; end.

Download Presentation

几个基本概念的理解

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 几个基本概念的理解 信息奥赛教学_pascal入门篇

  2. Pacal语言程序结构 program pname; const n=4; type arr=array [1..4] of integer; var i:integer; a:arr; begin for i:=1 to n do read(a[i]); readln; for i:=n downto 1 do write(a[i]:4); writeln; end. Program prog_name; var 变量申明; begin 程序体; end. 这是一个PASCAL程序。从键盘读入4个数据,逆序输出。

  3. Pacal语言程序结构 program pname; const n=4; type arr=array [1..4] of integer; Vari:integer; a:arr; begin for i:=1 to n do read(a[i]); readln; for i:=n downto 1 do write(a[i]:4); writeln; end. program pname; 其中,program是保留字,表示程序从这个地方开始, pname是标识符,是程序的名字,可由程序员自定。 保留字是PASCAL选定的,具有固定意义和用法的专用单词或缩写,这些单词不允许作其它使用。 “program”就有“程序从这里开始”这样一种特别的意义 而“const”就有“常量说明从这里开始”的意义。 我们不能再用"program"、"const"来作为其它变量、常量等的名字。

  4. Pacal语言程序结构 program pname; const n=4; type arr=array[1..4] of integer; var i:integer; a:arr; begin for i:=1 to n do read(a[i]); readln; for i:=n downto 1 do write(a[i]:4); writeln; end. • 标识符是以字母开头的字母数字串,其长度最大为8个字符。用来表示常量、变量、类型、文件、过程、函数和程序的名字。如“pname”、“i”、“j”、“a1”就是合法的标识符;但“1a”、“#a”是非法的标识符。 • 有一点要注意的是,在PASCAL中,字母除了作为字符值或字符串值之外,其大小写是无关的。如标识符“A1”和“a1”在PASCLA看来是同一标识符。 • 在PASCAL中除了保留字和自定义的标识符外,还有一类有特殊含义的标识符,这类标识符称为标准标识符。它们是用来标记程序中经常引用的处理对象,如常量、函数。(PASCAL定义的保留字和标准标识符附后)

  5. PASCAL使用的保留字有: • and、array、begin、case、const、div、do、downto、else、end、file、for、function、goto 、if、in、label、mod、nil、not、of、packed、procedure、program、record、repeat、set、then、to、type、until、var、while、with、forward 常用的标准标识符有: • 标准常量:false true maxint maxlongint • 标准类型:integer boolean real char text • 标准文件:input output • 标准函数:abs actan chr cos eof elon exp ln odd • ord pred round sin sqr sqrt succ trunc • 标准过程:assign get new dispose pack put read • readln reset rewrite unpack write writeln

  6. 说明部分 const n=4; type ar=array [1..4] of integer; var i:integer; a:ar; • const部分是常量说明,说明一些在以下部分用到的,在整个程序执行过程不改变值的量。这些量PASCAL称为常量。在程序中用到这个值的地方均用常量名来代替。常量说明在保留字“const”下开始。可以有多个语句。常量说明语句的格式是:“常量名=值;”。如“n=4;”。 • type部分是类型说明,说明一些在以下部分用到的数据类型。如数组、记录、指针等。类型说明在保留字“type”下开始。可以有多个语句。类型说明语句的格式是:“类型名=类型说明;”。 • var部分是变量说明。变量是指在程序执行过程中可以通过赋值语句或读语句来改变值的量变量说明在保留字“var”下开始。可以有多个语句。变量说明语句的格式是:"变量名:变量类型;"。其中,如果有多个变量同一类型,则变量名与变量名之间用逗号分隔,变量名与变量类型之间用冒号分隔。

  7. 程序体: begin for i:=1 to n do read(a[i]); readln; for i:=n downto 1 do write(a[i]:4); writeln; end. 程序体是以begin end.括起来的语句系列。 “end”后面是一个小圆点,标识着程序结束,整个程序只有一个是一个程序的主要部分。编程要完成的工作大部分都在这里完成。 程序体中每一语句均以“;”作为结束符。 在书写程序时,以"分层缩进"的风格来写,以便提高程序的可读性。所谓的"分层缩进"是指在逻辑上同一级的语句其起始点对齐,下一级的语句向右缩进。

  8. 运算符 表达式 • +加号;-减号;* 乘号( 数学中写为 × );/除号( 数学中写为 ÷);MOD取余 如:8 MOD 2=0,7 MOD 2=1,2 MOD 3=2;DIV 取整 如:8 DIV 2=4,7 DIV 2=3,2 DIV 3=0。 • 在PASCAL只有上面6种数学运算。其它的就只能利用这6种运算的组合通过语句来实现。如a^2(a的平方)可以化成a*a。XY 可写成exp(y*ln(X)) • > 大于;< 小于;<> 不等于(数学中写为 ≠);<= 小于等于(数学中写为≤);>= 大于等于(数学中写为 ≥),

  9. 练习一 • 输出”ni hao”这个字符串 Program ls1; begin write(‘ni hao’); End. 步骤: 进入界面 输入代码 按F9编译 ,看是否有错误_调试 Ctrl+F9运行 Alt+F5 看运行结果

  10. 练习二 • 通过键盘读入名字,然后输出”某某 ni hao!” Program ls2; var xm:string; begin read(xm); write(xm,‘ni hao!’); End.

More Related