# Pascal程序设计（二级）——基础篇04简单数据类型

2016-11-07 21:49:12来源:CSDN作者:baidu_32651739人点击

program 给学生赋字母等级;
{给每个学生赋字母等级}

{\$APPTYPE CONSOLE}

uses
SysUtils,
Windows,
Classes;

var
mystring:string[20];

const
n=10;

type
student=record
name:string[20];
score:real;
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
{计算平均成绩}
ave:=0;
for i := 1 to n do
ave:=ave+students[i].score;
ave:=ave/n;
{计算字母等级并输出姓名、成绩、等级}
for i := 1 to n do
with students[i] do
begin
end;{with}
end.

student=record                                   //这是一个记录数组

score:real;                              //让你捉迷藏找半天，郁闷好你，呵呵
end;

program 转义符测试;
{类似于C的Pascal转义符
要记得常用的#13#10回车换行噢！}

{\$APPTYPE CONSOLE}

uses
SysUtils,
Windows,
Classes;

var
n,i:integer;

begin
for i := 1 to n do
begin
write('#'+inttostr(i)+'是'+chr(i)+'  ');
if (i mod 8=0) then
write(#13#10);
end;
end.

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');
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 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

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 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');
end.

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

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);
end.