1 / 18

拆数深入

拆数深入. Pascal 程序形式. Program 程序名(程序参数表); Label 标号说明; Const 常量说明; Type 类型说明; Var 变量说明; Function 函数说明; Procedure 过程说明; Begin 程序语句; …… ; 程序语句; End. 1 、每一个完整语句由分号结束。 2 、 具体程序不一定包括全部说明,但如果出现,必须按这里所指定的前后次序编写 。 3 、程序体不可少,程序体以 END. 结束,且最后一个句号不能漏掉。 4 、 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. 拆数深入

  2. Pascal 程序形式 Program 程序名(程序参数表); Label 标号说明; Const 常量说明; Type 类型说明; Var 变量说明; Function 函数说明; Procedure 过程说明; Begin 程序语句; ……; 程序语句; End. 1、每一个完整语句由分号结束。 2、具体程序不一定包括全部说明,但如果出现,必须按这里所指定的前后次序编写。 3、程序体不可少,程序体以END.结束,且最后一个句号不能漏掉。 4、END前一句语句的分号可有可无,有则编译时多一个空行。 说明部分 程序体

  3. 3^4 试一试 program ls; uses math; begin write(power(3,4):0:0); end. program ls; begin write(exp(4*ln(3))); end. {打开数学库}

  4. 补充 (1) inc(x) 等同 x:=x+1; (2) inc(x, n) 等同 x:=x+n; (3) dec(x) 等同 x:=x-1; (4) dec(x,n) 等同 x:=x-n;

  5. 回顾 位数对调:输入一个三位自然数,把这个数的百位与个位数对调,输出对调后的数。 program exam1; var n,g,s,b:integer; begin read(n); g:=n mod 10; b:=n div 100; s:=n div 10 mod 10; write(g,s,b); end. 参考程序 随机产生三位数,此程序如何调?

  6. Pascal的System单元提供了两个与随机数有关的子程序:Randomize和Random。Pascal的System单元提供了两个与随机数有关的子程序:Randomize和Random。 Randomize过程用于初始化随机种子,其初始值取决于当前的系统时钟。 Random函数用于获取随机数. 它有两种调用形式: Random,返回一个0到1之间(不包括1)的随机实数; Random(N),返回0至N之间(不包括N)的随机整数,N为Word类型整数。 例如:Random(100) 返回[0 100)之间的整数

  7. 随机产生一个三位自然数,分离出百位、十位与个位上的数字随机产生一个三位自然数,分离出百位、十位与个位上的数字 program w_sj; var g,s,b,x:integer; beginrandomize;x:=trunc(random*900)+100;g:=x mod 10;b:=x div 100;s:=(x div 10) mod 10;writeln('x=',x );writeln('b=',b);writeln('s=',s);writeln('g=',g); end. Random(900)+100 [0 900)

  8. 如果输入时是作为字符或字符串,程序如何改?如果输入时是作为字符或字符串,程序如何改?

  9. 三位数翻转 字符串输入 字符输入 program ls; var a,b,c:char; begin read(a,b,c); write(c,b,a); end. program ls; var n,a,b,c:string; begin read(n); a:=copy(n,1,1); b:=copy(n,2,1); c:=copy(n,3,1); write(c,b,a); end.

  10. 思考: • 如果数字不止3位,但知道位数,也就是输入时除了数字信息还有位数信息,程序应当如何改? • 如果不知道数字的位数,用回车或“#”结束,本程序如何改?

  11. 一个整型数据用来存放整数。Pascal支持五种预定义整型,它们是shortint(短整型)、 integer(整型)、 longint(长整型)、 byte(字节型)和 word(字类型), Pascal分别用相同的名字作为他们的表识符。每一种类型规定了相应的整数取值范围以及所占用的内存字节数。

  12. 一个实型数据用类存放实数。 Pascal支持五种预定义实型,它们是real(基本实型)、 single(但精度实型)、double(双精度实型)、extended(扩展实型)、comp(装配实型), Pascal分别用相同的名字作为他们的表识符。每一种类型规定了相应的实数取值范围、所占用的内存字节数以及它们所能达到的精度。

  13. 布尔表达式 布尔表达式 then 语句一 语句二 语句 简单的IF语句 一、格式IF <布尔表达式>THEN 语句;IF <布尔表达式>THEN 语句1 ELSE 语句2;    then else

  14. 分支  program ex; var x:real;y:integer; begin wrtie('Input x:');readln(x); if x>0then y:=1{x>0时,y的值为1} else {x≤0时} if x=0then y:=0 else y:=-1; writeln('x=',x:6:2,'y=',y); end.

  15. 此处无符号 IF语句中复合语句用法 IF <布尔表达式>THEN begin 一个语句以上 end; IF <布尔表达式>THEN begin 一个语句以上 end ELSE begin 一个语句以上 end;

  16. case <表达式> of <情况标号表1>:语句1; <情况标号表2>:语句2; : <情况标号表n>:语句n else 语句; end;

  17. var x,y:real; begin write('Input x:');readln(x); case trunc(x/100) of 0:y:=x+1; 1:y:=x-1; else y:=0; end;{end of case} writeln('x=',x:8:2),'y=',y:8:2); end.

  18. 输入两个实数,再输入+、-、* 或 / 号,根据运算符输出这两个数的和、差、积或商。 Program xx; var a,b,s:real; operator:char; begin readln(a,b); read (operator); case operator of '+’: s:=a+b; '-’:s:=a-b; '*’:s:=a*b; '/’:s:=a/b; end; writeln(a,operator,b, '=’,s:5:4) end.

More Related