好,介绍本节课,还有个话题,就是阅读网页的技巧,你可以把网页全部复制到Word中保存阅读,这样,你会发现,图形和文字都变大了,行距等也变得有规则了,Word提供一个好的视图利于阅读!!
大家好,这次就要学习具体的程序设计内容了,开题之前,还有些需要讨论的,第一,上面介绍的都是参考资料,并不能完全作为教程,因为新的编译环境,具有的编译特性是不同的,我们写这个教程,主要是参考官方的教程,大都是英文,自然,我们这里不会都翻译出的,只捡一些常用的说说,就像二级C一样,开题都是拿了很多好用的直接用,介绍给大家,最后才细讲,比如C中的格式输入输出,转义符,预处理命令,函数库等等。。。
就是说,我们用实践,应用学习语言,不是到时候,堆了一堆知识,然后,死记知识,二级C的优点移植到我们Pascal中来。。。
好,鉴于我们接触过一些Pascal语言编译环境,我们需要做些不同的标识,例如,以前的Turbo Pascal7.0环境,我们暂且称为Pascal1990,Delphi7.0环境,暂且称为Pascal2000,我们的Delphi2010就成为Pascal2010,就类似于C一样,有个标准参照,好接着的话题哈。。。
从新认识新的环境是必要的,请瞧下面的例子程序:
program 给学生赋字母等级;
{给每个学生赋字母等级}
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows,
Classes;
var
mystring:string[20];
const
n=10;
type
student=record
name:string[20];
score:real;
grade:char;
end;
studenta=array [1..n] of student;
var
students:studenta;
ave:real;
i:integer;
begin
{输入全班学生姓名,成绩}
for i := 1 to n do
with students[i] do
readln(score,name);
{计算平均成绩}
ave:=0;
for i := 1 to n do
ave:=ave+students[i].score;
ave:=ave/n;
{计算字母等级并输出姓名、成绩、等级}
writeln('name':20,'score':10,'grade':10);
for i := 1 to n do
with students[i] do
begin
if score>=ave+10 then grade:='A'
else if score>=ave-10 then grade:='B'
else grade:='C';
writeln(name:20,students[i].score:10:1,students[i].grade:8);
end;{with}
readln;
end.
这是一个,分数转换成字母等级的试验程序,好多学校现在为了迎合素质教育,部分学校都把分数做成了字母等级了,相信大家不陌生了!~解说之前先把程序运行截图给大家,这是个控制台程序:
大家瞧瞧这个程序运行好了,很多人就说了,这是个好的可以运行的程序,对啊,程序编写对了,不得了??很显然,如此不好吧!大家学过物理都知道,对一个做过的题,拿过来多次分析,有时不仅不会浪费时间,还会获得意外的惊喜,也好像古人所说,书读百遍,其意自现!!~~呵呵,从方法论上说,好的方法就迁移过来,这就是我们非常熟悉的迁移能力啊!~~
好,转回正题,这个题目,实际上,并不是我们按照循序渐进的规则,按照章节排序来学习的,之所以拿它出来,一是我们解说后就很容易理解,而是它有些代表性。。。
大家回顾一下这个程序,有什么特点??(学过PASCAL程序设计 清华大学出版社出版 第2版 郑启华这本书的感觉,太熟悉了,但仿佛又有些不同!)
是啊,大家仔细观察一下就会发现是有不少改进的地方,如果你要是用原书的程序,是不能RUN的,所以说语言特性改变了!!
第一,PASCAL程序设计 清华大学出版社出版 第2版中说了,你设计程序时,各种类型说明不能乱放的,但是你瞧上面的程序,有多少个var啊??有2个,一个还跑到了const的上面,这就是Pascal2010的灵活性,只要你需要,只要你有用,随时可以声明类型变量,是不是和C有点类似哈?自然了,还是有些规则的,比如后面的用到了前面的,你就必须先把前面的声明好,例如本例中const就不能放到第二个var后面,为什么??嘿嘿,留给大家想了。。。
第二,不管是有没有PACKED定义的ARRAY,想在Pascal2010中编译,只要你用原例题中的字符数组alfa给name定义,你都要感情受伤千万遍。。。呵呵,人家Pascal2010认的是string特性!~~
第三,Delphi7.0也就是Pascal2000中,教程中怎么说的,mystring:string[20];定义好后,你就可以给
student=record //这是一个记录数组
name:mystring; //这里呢,但是你如果如此赋值,我们的Pascal2010就告诉你,不行了,而是还不是在这里告诉你,是在下面的readln,writeln中出现问题
score:real; //让你捉迷藏找半天,郁闷好你,呵呵
grade:char;
end;
这也就是我们为什么在这个例题程序中又多定义了一个mystring:string[20]。。。
好,大家瞧瞧,Pascal2010语言比以前更简洁,灵活了。有许多特性都需要好好了解,琢磨一番的。。。
好,本课程Pascal二级教程,是节选 (Delphi编程教程(第二版) 电子工业出版社 郑阿奇 陈瑞芬 这本书的1-3章,第6章,第9章内容)与 ( PASCAL程序设计 清华大学出版社出版 第2版 郑启华 的教学逻辑结构,也即是章节结构) 在参考众多资料的情况下分配的教学章节(毕竟一年时间还是较短的,去好好的深入理解一门语言还是需要循序渐进的),自然了,我们的最终资料仍然是官方的那个英文版的资料,就是说教学逻辑结构是上面两本书揉合的,而主要的语言特性是依据具体的官方资料,加上上机实践完成的,因为不管官方资料多匹配,最终的试验事实才是真理,就像你搞物理研究一样,不和准确的实验结果一致,理论造的再漂亮,什么用啊?呵呵。。。
这就是Pascal2010的二级教程(移植了二级C的教程优点),那么小基础就问了,还有很多内容怎么办?好,我们在第一节就谈过了,一年时间很短滴,而需要讨论学习研究的是很多滴,并且二级教程Pascal开设的语言新特性你会发现很多滴,可以说,会很丰富多彩的,所以就像有句词说的,“让风暂时飘过吧”,以后的章节,我们放到Pascal三级和Pascal四级教程中谈论。。。以后的美好,以后再去挽留。。。
好,伴随着大家,神游了一小会,呵呵,接着回到正题。。。好了,章节结构,大家能有个了解了,这里就不重码开列了,今天我们就像题目所说的,至少要学到简单数据类型,那小基础就说了,这还不简单吗??呵呵,虽然都有简单二字,还是需要好好研究琢磨滴。。。
对于本节的从概述,到保留字,标识符,常量变量等概念,请自行学习,这里不重述了。。。对于简单数据类型,说之前,我们想想二级C里面是不是有个\n的回车换行的转义符啊,特简单好用,想想好的就要移植,我们Pascal也有滴,请瞧程序:
program 转义符测试;
{类似于C的Pascal转义符
要记得常用的#13#10回车换行噢!}
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows,
Classes;
var
n,i:integer;
begin
readln(n);
for i := 1 to n do
begin
write('#'+inttostr(i)+'是'+chr(i)+' ');
if (i mod 8=0) then
write(#13#10);
end;
readln;
end.
运行截图:
咦怪了,怎么上传2幅图呢??呵呵,细心的朋友就会发现,2幅还是不同的,第一个转义符运行的测试n=128,输出的是什么啊??对了标准ASCII码,好多教材后面都有的,但第二幅图是什么啊(n=1280)??对了,我们的Pascal2010可是支持多国语言的噢,上面有希腊字母和俄文字母的编码!!以后想输入个特殊的都行了,比如实变函数论中的阿列夫零,嘿嘿,谁知道该是多少??留给大家找了!!~~
上面的控制台的的代码属性是简体中文的哈,换成英文的又会不同了!~
好了,熟悉我们的基本数据类型吧,注意了哈,编译环境不同,结果可能是不同的,我们一个一个瞧程序(这里大家可以不细读程序与运行结果,可以直接瞧分析):
program 整型占用宽度统计;
{$APPTYPE CONSOLE}
uses
SysUtils,math;
var
a:integer;
b:cardinal;
c:shortint;
d:smallint;
e:longint;
f:int64;
g:byte;
h:word;
i:longword;
j:uint64;
k:integer;
v:variant;
begin
writeln('integer is '+inttostr(sizeof(a))+' byte');
writeln('cardinal is '+inttostr(sizeof(b))+' byte');
writeln('shortint is '+inttostr(sizeof(c))+' byte');
writeln('smallint is '+inttostr(sizeof(d))+' byte');
writeln('longint is '+inttostr(sizeof(e))+' byte');
writeln('int64 is '+inttostr(sizeof(f))+' byte');
writeln('byte is '+inttostr(sizeof(g))+' byte');
writeln('word is '+inttostr(sizeof(h))+' byte');
writeln('longword is '+inttostr(sizeof(i))+' byte');
writeln('uint64 is '+inttostr(sizeof(j))+' byte');
writeln;
writeln('10 is '+inttostr(sizeof(10))+' byte');
writeln('100 is '+inttostr(sizeof(100))+' byte');
writeln('1000 is '+inttostr(sizeof(1000))+' byte');
writeln('10000 is '+inttostr(sizeof(10000))+' byte');
writeln('100000 is '+inttostr(sizeof(100000))+' byte');
writeln('100000000000000000 is '+inttostr(sizeof(10000000000000000000))+' byte');
writeln('第 1行 1 is '+inttostr(sizeof(1))+' byte');
for a := 1 to 10 do
begin
if (a<9) then write('第 '+inttostr(a+1)+'行 '+'1');
if (a>=9) then write('第'+inttostr(a+1)+'行 '+'1');
for k:=1 to a do
write('0');
writeln(' is '+inttostr(sizeof(power(10,a)))+' byte');
end;
writeln;
writeln('#1 is '+inttostr(sizeof(#1))+' byte');
writeln('v=10+6.6 is '+inttostr(sizeof((v=10+6.6)))+' byte');
writeln('shortint(10) is '+inttostr(sizeof(shortint(10)))+' byte');
writeln('smallint(10) is '+inttostr(sizeof(smallint(10)))+' byte');
writeln('word(10) is '+inttostr(sizeof(word(10)))+' byte');
writeln('longword(10) is '+inttostr(sizeof(longword(10)))+' byte');
writeln;
writeln('power(10,10) is '+inttostr(sizeof(power(10,10)))+' byte');
writeln('power(100,100) is '+inttostr(sizeof(power(100,100)))+' byte');
writeln('power(1000,1000) is '+inttostr(sizeof(power(1000,1000)))+' byte');
writeln('uint64(10) is '+inttostr(sizeof(uint64(10)))+' byte');
writeln;
writeln('power(shortint(10),smallint(10)) is '+inttostr(sizeof(power(shortint(10),smallint(10))))+' byte');
writeln('power(smallint(10),10) is '+inttostr(sizeof(power(smallint(10),10)))+' byte');
writeln('power(uint64(10),10) is '+inttostr(sizeof(power(uint64(10),10)))+' byte');
writeln('power(uint64(10),uint64(10)) is '+inttostr(sizeof(power(uint64(10),uint64(10))))+' byte');
writeln('power(uint64(100),smallint(100)) is '+inttostr(sizeof(power(uint64(100),smallint(100))))+' byte');
writeln('power(uint64(100),uint64(100)) is '+inttostr(sizeof(power(uint64(100),uint64(100))))+' byte');
writeln('power(uint64(1000),int64(1000)) is '+inttostr(sizeof(power(uint64(1000),int64(1000))))+' byte');
writeln('power(uint64(1000),uint64(1000)) is '+inttostr(sizeof(power(uint64(1000),uint64(1000))))+' byte');
readln;
end.
运行截图:
这里的结果有点长,把控制台的内容复制给大家:
integer is 4 byte
cardinal is 4 byte
shortint is 1 byte
smallint is 2 byte
longint is 4 byte
int64 is 8 byte
byte is 1 byte
word is 2 byte
longword is 4 byte
uint64 is 8 byte
10 is 1 byte
100 is 1 byte
1000 is 2 byte
10000 is 2 byte
100000 is 4 byte
100000000000000000 is 8 byte
第 1行 1 is 1 byte
第 2行 10 is 4 byte
第 3行 100 is 4 byte
第 4行 1000 is 4 byte
第 5行 10000 is 4 byte
第 6行 100000 is 4 byte
第 7行 1000000 is 4 byte
第 8行 10000000 is 4 byte
第 9行 100000000 is 4 byte
第10行 1000000000 is 4 byte
第11行 10000000000 is 4 byte
#1 is 2 byte
v=10+6.6 is 1 byte
shortint(10) is 1 byte
smallint(10) is 2 byte
word(10) is 2 byte
longword(10) is 4 byte
power(10,10) is 4 byte
power(100,100) is 4 byte
power(1000,1000) is 4 byte
uint64(10) is 8 byte
power(shortint(10),smallint(10)) is 4 byte
power(smallint(10),10) is 4 byte
power(uint64(10),10) is 4 byte
power(uint64(10),uint64(10)) is 10 byte
power(uint64(100),smallint(100)) is 4 byte
power(uint64(100),uint64(100)) is 10 byte
power(uint64(1000),int64(1000)) is 10 byte
power(uint64(1000),uint64(1000)) is 10 byte
这里面做了整型的不同测试,把所有类型都很好的熟悉了一次,是不是比硬背好些?~关键是,这里测试出了一些重要的内容(是不是很熟悉啊,对二级C也有这个sizeof):
第一,pascal2010各种整型在64位系统的实际占用字节
integer is 4 byte
cardinal is 4 byte
shortint is 1 byte
smallint is 2 byte
longint is 4 byte
int64 is 8 byte
byte is 1 byte
word is 2 byte
longword is 4 byte
uint64 is 8 byte
第二,具体的数占用的字节注意哈,100000000000000000是最大的了,如果再加个零,系统会告诉你溢出了,这是单纯的输入数字的测试,你瞧系统是自动分配字节的。
10 is 1 byte
100 is 1 byte
1000 is 2 byte
10000 is 2 byte
100000 is 4 byte
100000000000000000 is 8 byte
第 1行 1 is 1 byte
第三,我们这里用了math函数库,并且,先用power函数测试了单纯的数字函数的输出字节,是受函数power限制的,因此它和单纯的数字系统自动分配不同,就是说和第二点不同!~~
第 2行 10 is 4 byte
第 3行 100 is 4 byte
第 4行 1000 is 4 byte
第 5行 10000 is 4 byte
第 6行 100000 is 4 byte
第 7行 1000000 is 4 byte
第 8行 10000000 is 4 byte
第 9行 100000000 is 4 byte
第10行 1000000000 is 4 byte
第11行 10000000000 is 4 byte
第四,虽然pascal类型是可以类型强制转换的,Delphi教程中有介绍,但是并不像C一样的转换方式,这里是不同的,小心!
#1 is 2 byte //转义符也可以判断的
v=10+6.6 is 1 byte //(这是什么类型,给大家想)
shortint(10) is 1 byte //这里强制了没?
smallint(10) is 2 byte //这里强制了没?
word(10) is 2 byte
longword(10) is 4 byte
第五,虽然,函数的影响是有滴,但是我们的类型转换就不一定让它噢!~
power(10,10) is 4 byte
power(100,100) is 4 byte
power(1000,1000) is 4 byte
uint64(10) is 8 byte
power(shortint(10),smallint(10)) is 4 byte
power(smallint(10),10) is 4 byte
power(uint64(10),10) is 4 byte
power(uint64(10),uint64(10)) is 10 byte
power(uint64(100),smallint(100)) is 4 byte
power(uint64(100),uint64(100)) is 10 byte
power(uint64(1000),int64(1000)) is 10 byte
power(uint64(1000),uint64(1000)) is 10 byte //有我uint64在,你power凭什么说了算??呵呵!
呵呵,好,上面是整型测试,还有的基础类型,瞧程序:
program 简单数据类型占用字节测试;
{简单数据类型占用字节测试}
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows,
Classes,
Math;
begin
writeln('Boolean is '+inttostr(sizeof(Boolean))+' byte');
writeln('char is '+inttostr(sizeof(char))+' byte');
writeln('ansichar is '+inttostr(sizeof(ansichar))+' byte');
writeln('widechar is '+inttostr(sizeof(widechar))+' byte');
writeln;
writeln('real is '+inttostr(sizeof(real))+' byte');
writeln('real48 is '+inttostr(sizeof(real48))+' byte');
writeln('single is '+inttostr(sizeof(single))+' byte');
writeln('double is '+inttostr(sizeof(double))+' byte');
writeln('extended is '+inttostr(sizeof(extended))+' byte');
writeln('comp is '+inttostr(sizeof(comp))+' byte');
writeln('currency is '+inttostr(sizeof(currency))+' byte');
writeln;
writeln('10.0 is '+inttostr(sizeof(10.0))+' byte');
writeln('10000000000.0000000000 is '+inttostr(sizeof(10000000000.0000000000))+' byte');
writeln('sqrt(10) is '+inttostr(sizeof(sqrt(10)))+' byte');
writeln('sqrt(10.0) is '+inttostr(sizeof(sqrt(10.0)))+' byte');
writeln('arctan(10) is '+inttostr(sizeof(arctan(10)))+' byte');
writeln('arctan(10.0) is '+inttostr(sizeof(arctan(10.0)))+' byte');
writeln('tanh(10) is '+inttostr(sizeof(tanh(10)))+' byte');
writeln('tanh(10.0) is '+inttostr(sizeof(tanh(10.0)))+' byte');
readln;
end.
运行截图:
这里面也用到了Math库函数噢,sqrt求根,arctan反正切函数,tanh双曲正切函数,Pascal2010数学函数可是很多的噢,有了就要经常用,否则就生锈了!~~
上面是各种类型的字节占用,不过大家注意噢
10.0 is 10 byte
10000000000.0000000000 is 10 byte
10.0和10000000000.0000000000,一样的!~
他们,
sqrt(10) is 10 byte
sqrt(10.0) is 10 byte
arctan(10) is 10 byte
arctan(10.0) is 10 byte
tanh(10) is 10 byte
tanh(10.0) is 10 byte
也和10.0 is 10 byte是一样的!~
还有个可变类型的程序,这里不详谈了,大家回去讨论,瞧程序:
program 可变类型占用字节测试;
{可变类型占用字节测试}
{$APPTYPE CONSOLE}
uses
SysUtils,Windows,Classes,Math;
var
v:variant;
begin
writeln(sizeof(v));
writeln('v=10+6.6 is '+inttostr(sizeof((v=10+6.6)))+' byte');
v:=10+6.6;
writeln(sizeof(v));
writeln(v);
readln;
end.
好了,是不是我们以前见到的简单数据类型也很有趣呢。。。。。。它们陪我们过了快乐的一节。。。
希望有识之士能组成团队,共同研究出一幅类似于C的类型转换图表出来。。。
好,今天和简单数据类型一块做了快乐的游戏,下节课接着我们的程序之旅。。。