Sunteți pe pagina 1din 12

Programare Delphi

Curs 2
Pascal versus C
1. Dezvoltarea programarii calculatoarelor
1940 - 1950 Programare manual n cod main (cod nativ)
1941 Z3, Konrad Zuse, limbajul Plankalkl;
1943 Colossus Mark 1, Bletchley Park, secret, folosit pentru decripare;
1945 ENIAC, University of Pennsylvania;
1949 EDVAC, primul calculator electronic cu program memorat,.
1950 1960 Programare n limbaj de asamblare
1951 UNIVAC I (Universal Automatic Computer), primul calculator de serie
1952 IBM 701 Electronic Data Processing Machine
1960 1970 Programare n cod spaghetti
1957 FORTRAN pentru IBM 704 GOTO etichet; IF cu salturi
1959 COBOL pentru calcule economice
1964 BASIC cu linii numerotate pentru GOTO
1970 1980 Programare procedural, programare structurat
1960 ALGOL 60 limbaj cu importan teoretic, introduce blocul begin-end
1970 Pascal, Niklaus Wirth, Swiss Federal Institute of Technology, Zurich.
1973 C, Dennis Ritchie, AT&T Bell Laboratories blocul { ... }
1983 Turbo Pascal, Anders Hejlsberg, Borland
1987 Turbo C, Borland
1980 1990 Programare orientat pe obiecte, programare modular
1979 C with Classes 1983 C++, Bjarne Stroustrup, AT&T Bell Laboratories;
1983 Clascal 1986 Apple Computer Object Pascal;
1989 Turbo Pascal 5.5 for DOS Borland Object Pascal
1990 Turbo C++, Borland
1990 2000 Programare orientat pe componente, programare pentru internet
1991 Microsoft Visual Basic
1993 Microsoft Visual C++
1994 Java, Sun Microsistems
1995 Delphi, Anders Hejlsberg, Borland
1997 Microsoft Visual Studio
2000 Microsoft Visual C#, Anders Hejlsberg, Microsoft
2000 2010 Programare interoperabil
1998 XML
2002 Visual Studio .NET, Microsoft
2004 Delphi 8 for .NET
2010 Delphi Prism, plug-in for MS Visual Studio
1

2. Why "Delphi"?
Danny Thorpe, Why the name "Delphi"?, http://edn.embarcadero.com/article/20396 .
Answer: If you want to talk to [the] Oracle, go to Delphi.
(Oracle database - http://www.oracle.com/us/index.html)
TIOBE Programming Community index:
http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
Position Position
Ratings
Delta in
Programming
Feb
Feb
Feb
Position
Language
2012
2011
2012

Delta
Feb
2011

Statu
s

Java

17.050% -1.43%

16.523% +1.54%

C#

8.653%

+1.84%

C++

7.853%

-0.33%

Objective-C

7.062%

+4.49%

PHP

5.641%

-1.33%

(Visual) Basic

4.315%

-0.61%

Python

3.148%

-3.89%

10

Perl

2.931%

+1.02%

10

JavaScript

2.465%

-0.09%

11

13

Delphi/Object Pascal

1.964%

+0.90%

12

11

Ruby

1.558%

-0.06%

13

14

Lisp

0.905%

-0.05%

14

26

Transact-SQL

0.846%

+0.29%

15

17

Pascal

0.813%

+0.08%

16

22

Visual Basic .NET

0.796%

+0.21%

A--

17

32

PL/SQL

0.792%

+0.38%

18

24

Logo

0.677%

+0.10%

19

16

Ada

0.632%

-0.17%

20

25

0.623%

+0.06%

Position Position
Feb
Feb
2013
2012

Delta in
Position

Programming
Language

Ratings
Feb
2013

Delta
Feb
2012

Statu
s

Java

18.387% +1.34%

17.080% +0.56%

Objective-C

9.803%

+2.74%

C++

8.758%

+0.91%

C#

6.680%

-1.97%

PHP

5.074%

-0.57%

Python

4.949%

+1.80%

(Visual) Basic

4.648%

+0.33%

Perl

2.252%

-0.68%

10

12

Ruby

1.752%

+0.19%

11

10

JavaScript

1.423%

-1.04%

12

16

Visual Basic .NET

1.007%

+0.21%

13

13

Lisp

0.943%

+0.04%

14

15

Pascal

0.932%

+0.12%

15

11

Delphi/Object Pascal

0.886%

-1.08%

16

14

Transact-SQL

0.773%

-0.07%

A--

17

75

Bash

0.741%

+0.61%

A--

18

26

MATLAB

0.648%

+0.15%

19

24

Assembly

0.640%

+0.12%

20

19

Ada

0.631%

0.00%

Feb
2014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Feb
2013
2
1
3
4
5
6
8
7
11
12
16
10
9
18
22
47
14
36
13
15

Change

Programming
Language

Ratings Change

18.334%

+1.25%

Java

17.316%

-1.07%

Objective-C

11.341%

+1.54%

C++

6.892%

-1.87%

C#

6.450%

-0.23%

PHP

4.219%

-0.85%

(Visual) Basic

2.759%

-1.89%

Python

2.157%

-2.79%

JavaScript

1.929%

+0.51%

Visual Basic .NET

1.798%

+0.79%

Transact-SQL

1.667%

+0.89%

Ruby

0.924%

-0.83%

Perl

0.887%

-1.36%

MATLAB

0.641%

-0.01%

PL/SQL

0.604%

-0.00%

F#

0.591%

+0.42%

Pascal

0.551%

-0.38%

0.529%

+0.23%

Lisp

0.523%

-0.42%

Delphi/Object Pascal

0.522%

-0.36%

Feb
2015
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Feb
2014
1
2
4
3
5
9
6
8
10
20
13
15
16
11
30
14
44
17
12

Programming
Language

Change

Ratings Change

16.488%

-1.85%

Java

15.345%

-1.97%

C++

6.612%

-0.28%

Objective-C

6.024%

-5.32%

C#

5.738%

-0.71%

JavaScript

3.514%

+1.58%

PHP

3.170%

-1.05%

Python

2.882%

+0.72%

Visual Basic .NET

2.026%

+0.23%

Visual Basic

1.718%

+1.72%

Delphi/Object Pascal

1.574%

+1.05%

Perl

1.390%

+0.50%

PL/SQL

1.263%

+0.66%

F#

1.179%

+0.59%

Transact-SQL

1.124%

-0.54%

ABAP

1.048%

+0.69%

MATLAB

1.033%

+0.39%

0.963%

+0.71%

Pascal

0.960%

+0.41%

Ruby

0.873%

-0.05%

3. Comparaie

C / Pascal

C/C++/ Visual C++/C#


1973/1983/1993/2000

<=>
<=>

Pascal /ObjectPascal/Delphi
1970/1985/1995

#include

<=>

uses

//unit-uri, fiiere .pas

main( ){ }
<=>
void f(int a){ }
<=>
double f(int a) { return a+1;} <=>

program ...; begin ... end. //fiierul .dpr \/ .lpr


procedure f(a:integer); begin ... end;
function f(a:integer): double; begin Exit(a+1) end;

typedef int vector[100];

<=>

type vector=array[0..99] of integer;

int i, j, k;
double a,b;
a=10.2;
int *p;
p=&a;
*p=13;
i++;
{ ... }

<=>
<=>
<#>
<=>
<=>
<=>
<=>
<=>

i, j, k: integer;
a,b: double;
a:=10.2;
// instruciunea de atribuire!
p: ^integer;
p:=@a;
p^:=13;
Inc(i);
// procedur
begin ... end;

if (a<b) x=a;
if (a<b) x=a; else x=b;
switch(i){ case 1: ... }

<=>
<=>
<=>

if a<b then x=a;


if a<b then x:=a else x:=b;
case i of 1: ... end;

for(i=0; i<n; i++) { ... }


while(i<100){ ... }
do { ... } while(k<100);

<=>
<=>
<=>

for i=0 to n do begin ... end; // to < > downto


while i<0 do begin ... end;
repeat ... until k>=100;

break;
continue;
return;
return i+j;
goto myLabel;

<=>
<=>
<=>
<=>
<=>

Break;
// procedur sistem
Continue;
// procedur sistem
Exit;
// procedur sistem
Result:=i+j; Exit; sau Exit(i+j);
goto myLabel;

struct
class
this
null

<=>
<=>
<=>
<=>

record
class
self
nil

comentariu
/* ... */
comentariu
//
case sensitive

<=>
<=>
<#>

(* ... *) sau { ... }


//
case insensitive

// Lazarus=lazarus

cout<<Salut!<<endl;
cin>>text;

<=>
<=>

WriteLn(Salut);
ReadLn(text);

// procedur sistem
// procedur sistem

4. Ce este n Pascal altfel dect n C:


4.1. Subprogramele (subrutinele) sunt de dou tipuri, subrutine de tip function (care returneaz un rezultat de un tip bine precizat) i subrutine de tip procedure (care nu returneaz nimic). Subprogramele pot comunica n ambele direcii ntre ele prin parametrii de intrare/ieire transmii prin valoare sau prin referin (ca n C++, C#).
program comp1; {$mode objfpc}{$H+}//{$APPTYPE CONSOLE}//pt.DELPHI
// subprogram de tip functie
function f(x: double): double;
const
a = 10.0;
begin
f := a * x;
end;
// subprogram de tip procedura
procedure Prelucrare(x: double; f1: double; var f2: double; out f3:
double);
const
a = 10.0;
begin
f1 := a * x;
f2 := a * x;
f3 := a * x;
end;
// Programul principal
var
ff1: double = 11;
ff2: double = 11;
ff3: double = 11;
begin
WriteLn('f(5)=', f(5):0:2);
Prelucrare(7, ff1, ff2, ff3);
WriteLn('ff1=', ff1:0:2);
WriteLn('ff2=', ff2:0:2);
WriteLn('ff3=', ff3:0:2);
WriteLn('Press ENTER to continue ...');
ReadLn;
end.
{ Rezultat:
f(5)=50.00
ff1=11.00
ff2=70.00
ff3=70.00
Press ENTER to continue ...
}

In procedura Prelucrare avem trei categorii de parametri:


- x i f1 sunt parametri de intrare, transmii prin valoare (valorile parametrilor actuali
sunt copiate pe stiv, x si f1 sunt variabile locale);
- f2 este parametru de intrare/ieire, transmis prin referin (n lista parametrilor formali, var f2:double din Pascal este echivalent cu double& f2 n C++ i cu ref double f2 n
7

C#), pe stiv ajunge adresa parametrului actual (este implicat un mecanism de lucru cu pointeri);
- f3 este parametru de ieire (exact ca n C#), transmis prin referin. Cuvantul cheie
out semnaleaz c variabila respectiv ar putea fi neiniializat;
Observaii:
- la apelare nu apar cuvintele cheie var i out;
- n lista parametrilor formali se utilizeaz separatorul punct-virgul (ca i cum am
avea o secven de declaraii), iar in lista parametrilor actuali se utilizeaz virgula;

4.2. Declaraiile se fac ntre antetul subprogramului i corpul acestuia declaraiile nu au


statut de instruciune. Zona declaraiilor are seciuni distincte (type, const, var, label i
seciunea declarailor de subrutine), care pot apare n orice ordine i se pot repeta.
program Comp2; {$mode objfpc}{$H+}//{$APPTYPE CONSOLE}//pt.DELPHI
// ZONA DECLARATIILOR PROGRAMULUI PRINCIPAL
var
w: integer = 100;
// variabila globala initializata
procedure test;
// zona declaratiilor subprogramului test
type
TNumarMic = 0 .. 100; // tip subdomeniu
const
c = 10.00;
// constanta simbolica
v: integer = 50;
// constanta cu tip
var
i, n: TNumarMic;
// blocul subprogramului test
begin
n := Round(c);
for i := 0 to 10 * n do
WriteLn(i / c:0:2);
// c:=13.5;
//Error:Left side cannot be assigned to
// v:=50;
//Error:Left side cannot be assigned to
w := 10;
WriteLn('w=', w);
ReadLn;
end;
// BLOCUL PROGRAMULULUI PRINCIPAL
begin
test;
end.

4.3. Subrutinele pot fi imbricate se pot declara funcii i proceduri n interiorul unei funcii
sau proceduri (n zona declaraiilor, evident);
program comp3; {$mode objfpc}{$H+}//{$APPTYPE CONSOLE}//pt.DELPHI
function alfa(x: integer): integer;
function beta(y: integer): integer;
var
a: integer;
begin
a := 100;
Result := a + 10 * y + x;
end;
var
b: integer;
begin
b := 1000;
Result := b + beta(x);
end;
var
z: integer;
c: integer = 10000;
begin
z := c + alfa(7);
WriteLn(z); // 11177
// z:=beta(7);
//Error Undeclared identifier: 'beta'
WriteLn('Press ENTER to continue ...');
ReadLn;
end.

4.4. Tipul parametrilor formali ai unui subprogram nu poate fi precizat printr-un descriptor de
tip, ci numai printr-un nume de tip de aici larga utilizare a tipurilor utilizator (introduse n
seciunea type);
// function suma(tab:array[10..13] of integer):integer;
type
TVector = array [10 .. 13] of integer;
function suma(tab: TVector): integer;

//nu merge

//merge

4.5. Atribuirile se execut numai prin instruciunea (i nu expresia) de atribuire, se pot face
atribuiri de tablouri (dar numai dac au acelai tip); funciile pot returna tablouri:
type
TVector = array [10 .. 13] of integer;
function ordoneaza(tab: TVector): TVector;

//merge

4.6. Indexarea tablourilor poate porni de la orice index;


program comp6; {$mode objfpc}{$H+}//{$APPTYPE CONSOLE}//pt.DELPHI
type
TVector = array [10 .. 13] of integer;
function suma(tab: TVector): integer;
var
i: integer;
begin
Result := 0;
for i := Low(tab) to High(tab) do
Result := Result + tab[i];
end;
var
tab1: TVector = (1, 2, 3, 4);
tab2: TVector;
tab3: array [10 .. 13] of integer = ( 11,22, 33, 44 );
s: integer;
begin
tab2 := tab1;
s := suma(tab2);
WriteLn('s=', s); // s=10
// tab2:=tab3; //Delphi:Error Incompatible types: 'TVector' and 'Array'
// s:=suma(tab3);// Delphi: Error Incompatible types: ...
ReadLn;
end.

4.7. Lungimea unui string este memorat odat cu stringul, stringurile nu au terminator de
string;
program comp7; {$mode objfpc}{$H+}//{$APPTYPE CONSOLE}//pt.DELPHI
var s:string[3];
//short string
(cel mult 255)
t,tt,ttt:string;
//long string
(UnicodeString)
i:integer;
begin
s:='ABCDE';
Writeln(s);
for i := -3 to 10 do Write(Ord(s[i]):5);
Writeln;
t:='ABCDEF';
Writeln(t);
for i := -3 to 10 do Write(Ord(t[i]):5);
Readln;
end.
{REZULTAT DELPHI:
ABC
0
0
0
3
65
66
67
0
0
0
0
0
0
ABCDEF
3
0
6
0
65
66
67
68
69
70
0
0
0
{REZULTAT LAZARUS:
ABC
0
0
0
3
65
66
67
0
0
0
0
0
0
ABCDEF
6
0
0
0
65
66
67
68
69
70
0
0
0

10

0
0}

0
0}

4.8. Tablourile transmise n subrutine prin valoare sunt copiate pe stiv;


program comp8; {$mode objfpc}{$H+}//{$APPTYPE CONSOLE}//pt.DELPHI
type TVector=array[0..2] of integer;
var vec:TVector=(0,10,20);
procedure proc1(tab:TVector);
//transmitere prin valoare
begin
tab[1]:=100;
Writeln('adresa lui tab[1] din proc1=',integer(@tab[1]));
end;
procedure proc2(var tab:TVector); //transmitere prin referinta
begin
tab[1]:=100;
Writeln('adresa lui tab[1] din proc2=',integer(@tab[1]));
end;
begin
WriteLn('---->',vec[1]);
proc1(vec);
Writeln('adresa lui tab[1] din main =',integer(@vec[1]));
WriteLn('unu->',vec[1]);
proc2(vec);
Writeln('adresa lui tab[1] din main =',integer(@vec[1]));
WriteLn('doi->',vec[1]);
ReadLn;
end.
{ REZULTAT:
---->10
adresa lui tab[1]
adresa lui tab[1]
unu->10
adresa lui tab[1]
adresa lui tab[1]
doi->100
}

din proc1=1245080
din main =4224980
din proc2=4224980
din main =4224980

4.9. Prioritatea operatorilor difer de cea din C, n Pascal sunt numai patru nivele de prioritate, de reinut c operatorii logici leag mai tare dect cei de comparaie (and este
operator multiplicativ, or este aditiv)
Corect:
if i<10 then if j=3 then a:=5;

Greit:
if i<10 and j=3 then a:=5;

Corect:
if (i<10) and (j=3) then a:=5;

11

4.10. Organizarea modular a programelor (unit-urile) este mai dezvoltat (i mai clar) n
Pascal dect n C.
Fiierul comp10.dpr:
program comp10; {$mode objfpc}{$H+}//{$APPTYPE CONSOLE}//pt.DELPHI
uses
primulMeuUnit in 'primulMeuUnit.pas';
begin
FaCeva;
WriteLn('Press ENTER to continue...');
ReadLn;
end.

Fiierul primulMeuUnit.pas:
unit primulMeuUnit;
interface
procedure FaCeva;
implementation
procedure FaCeva;
var
i: integer;
begin
for i := 0 to 1000 do
Write(i, ' bzz! ');
end;
end.

12

S-ar putea să vă placă și