Apuntes
D1
Contenido del apunte (1)
Página 1
9 div 2 gives 4 (integer division)
9 mod 2 gives 1 (remainder)
abs(-3.3) = 3.3
sqr
sqrt
int(-10.9)=-10.0
frac(-10.9)= -0.9
pi
odd(3) = true
pred(5)=4
succ('A')='B'
chr(65)='Á'
ord('A')=65
ord(65)=65
round(-4,5)=-5
trunc(-4.5)=-4
function square(num:real):real;
begin
cube:= num*num;
end;
begin //main//
write(cube(8.1):4:2)
end.
parameter (e.g. procedure which swap two variables by using temp)
Call be reference
procedure LOL(num:integer);
begin
write(num+1);
writeln;
end;
begin
readln(N);
LOL(N)
end.
Call be ref + value:
procedure ABC(B:integer ;var A:integer);
Test:
typical, extreme, illegal data
path testing
tools: program trace, program watch, breakpoints,.
Manual: scarffolding,stubs
error: truncation errors, rounding errors, overflow/underflow errors
length
copy(string,start position,count)
concat(string1,string2)
pos(substring,string)-> cant find -> 0
No.-> string
str(number,string variable)
String-> No.
val(string, numeric variable,error code);
ABC:array[1..2,1..3] of string;
1 2 3
2
type
daytype=(sun,mon,tue,wed,thu,fri,sat);
var
Price:array[datatype] of real;
today:daytype
for Today := sun to sat do
Price[Today]:= normal;
price[tue]:=1;
getdate(YY,MM,DD,dayofweek);
today:=daytype(dayofweek)
case today of
sun: writeln('sunday');
sat:writeln('Saturday');
end;
Sets
type
chartype=set of char;
var
ABC:type;
begin
ABC:=[' A' ,' B' ,'C' ];
if __ in ABC then...
Record
type
ABCtype=record
ID:integer;
name:string;
end;
var
LOL:ABCtype;
with LOL do.....
Textfile
eof
eoln (end of line)
assign(diskfile,'data.txt');
Write to txt:
rewrite(diskfile);
write(diskfile,'Hello');
close(diskfile);
read from txt:
reset(diskfile);
readln(diskfile,abc);
close(diskfile);
Append(diskfile);
writeln(diskfile, 'HI');
close(diskfile);
Search
Linear search
Uses:short lists
no requirement
sucessful(N+1)/2 unsucessful: N
Repeat
count:=count+1;
with xxx[count] do
if name = target then begin
index:=count;
found:=true;
end
until found or (count=num);
binary search
uses: long and sorted lists
must be sorted support random access
logN/log2 (success/unsucessful)
(11+1)/2 = 6th
Repeat
count:=count+1;
middle:=(top+bot)div 2;
with xxx[middle] do
begin
if target>name
then top:=middle+1
else if target then bot:=middle-1
else begin
index:=middle;
found:=true
end
end
until found or (top>bottom);
Sorting
Bubble
repeat
pass:=pass+1;
swapped:=false;
for i:= 1 to N- pass do
if list[i] > list[i+1]
then begin
temp:= list[i];
list[i]:= list[i+1];
list[i+1]:=temp;
swapped:=true
end
until (pass=N-1) or not swapped
Insertion
for pass:= 1 to N-1 do
begin
element:= list[pass+1];
position:=1;
for i:= 1 to pass do
if element> list[i]
then position:= i+1;
for count:= pass downto position do
List[count+1]:= list[count];
list[position]:=element;
end
end;
Stack (first in first out)
Initialize: create empty stack
push : add entry
pop : remove entry
type
stacktype= reocrd
top: 0..max;
entry:array[1..max] of string;
end;
var list:stacktype;
procedure push(strg:string; var list:stacktype);
begin
with list do
if top=max then....
else begin
top:=top+1;
entry[top]:=strg
end
end;
procedure pop( var strg:string; var list:stacktype);
with list do
if top=0 then...
else with list do
begin
strg:=entry[top];
entry:=' ';
top:=top-1;
end
end;
Queue (first in last out)
type
queuetype= record
head,tail:0..max;
count:integer;
entry:array[1..max] of string;
end;
var
list:queuetype;
procedure enqueue(strg:string; var list:queuetype);
begin
with list do
if count= max then...
else begin
count:=count+1;
tail:= tail mod max+1;
entry[tail]:=strg
end
end;
procedure dequeue(var strg:string; var list: queuetype);
begin
with list do if count =0 then...
else with list do
begin
count:=count-1;
strg:=entry[head];
entry[head]:=' ' ;
head:=headmod max+1;
end
end;
Linked list
type
nodetype=record
entry:string;
next:nullpoint..max
end;
listtype=record
head:integer
list:array[1..max] of nodetype
end;
var linklist:listtype;
insertbegin
insertafter
deleteafter('A','B',linklist);
Program
Información
- Descripción
- Apunte sobre D1, creado por Shek Sunny el 02/11/2013.
- Creado por
- Shek Sunny
- Visitas
- 13
- Valoración