2bytesprog2 course_2014_c8_units

23
Prog_2 course- 2014 2 bytes team Kinan keshkeh IT Engineering-Damascus University 3 rd year

Upload: kinan-ke

Post on 14-Jul-2015

32 views

Category:

Software


1 download

TRANSCRIPT

Page 1: 2Bytesprog2 course_2014_c8_units

Prog_2 course- 2014

2 bytes team

Kinan keshkeh

IT Engineering-Damascus University

3rd year

Page 2: 2Bytesprog2 course_2014_c8_units

Units

Page 3: 2Bytesprog2 course_2014_c8_units

Why units ?

Page 4: 2Bytesprog2 course_2014_c8_units

Introduction

How is the compilation process ??

the principle of compilation is based on fragmentation

the program into several files then the compiler compile

each file separately then Connect all parts of the

compiled to configure executable file (conversion

program form machine language to “code” language )

Page 5: 2Bytesprog2 course_2014_c8_units

Unit stop here

compilation

source file in

Pascal language

Text files

for some

procedure

or function

Unit

programing

ready to use

Libraries

ready to use

linked

Execution

Page 6: 2Bytesprog2 course_2014_c8_units

unit Unit unitname

Interface

Implementation

Definition :

Uses

Const

Type

Var

The header of procedure or function

Uses

Const

Type

Var

The object of procedure or function

founded in interface part

End. After last end we have to put (.) not (;)

Unit divided into two

sections :

1) Interface :

2) Implementation :

Page 7: 2Bytesprog2 course_2014_c8_units

Interface : or what we call it <<public part>> , in this part we can

define the visible things to any program or another unit for

example :

Uses wincrt;

Const pi=3.14;

Type arr=array[1..100] of integer;

var x:integer;

Function sum(x,y:integer):integer;

Function max(a,b:integer):integer;

Note: include interface part we only define the header of

procedure or function

Page 8: 2Bytesprog2 course_2014_c8_units

Implementation : or what we call it <<private part>> in this part we can define invisible things, we can‟t deal with them when we go out from the definition of the unit

Uses wincrt;

Const max=100;

Type person=record

fname,lname:string;

end;

var c:char ;

Function sum(x,y:integer): integer;

Begin

Sum:=x+y;

end;

Function max(a,b:integer):integer;

Begin

if (a > b) then

Max:=a;

Else

Max:=b;

end;

Page 9: 2Bytesprog2 course_2014_c8_units

After writing unit what do we have to do ?

1. Correct the errors, if any

2. Compilation

3. Save this file under unit‟s name with (TPU) <<unitname.TPU>>

For example :

Unit my_unit;

Save as:<<my_unit.TPU>>

4. Recall this unit in main program using

reserved word (uses)

uses my_unit ;

Note : unit is subprogram unexecutable

we cannot run it ……

Page 10: 2Bytesprog2 course_2014_c8_units

Unit my_unit1;

Interface

Const val=200;

Var w : integer ;

procedure swap(var x,y:integer);

Implementation

var temp :integer ;

procedure swap(var x,y:integer);

begin

temp:=x;

x:=y;

y:=temp;

end;

End.

Compilation

Attention !!!

Page 11: 2Bytesprog2 course_2014_c8_units

Unit my_unit1;

Interface

Const val=200;

Var w : integer ;

procedure swap(var x,y:integer);

Implementation

var temp :integer ;

readln(x);

procedure swap(var x,y:integer);

begin

temp:=x;

x:=y;

y:=temp;

end;

End.

{false}

There is no

Instructions in unit

Page 12: 2Bytesprog2 course_2014_c8_units

Program prog1;

Uses my_unit1;

Var a,b :integer;

Begin

w:=5; temp:=10; a:=2; b:=3;

End.

{true} {false}

a= 3

b= 2

swap(a,b);

Writeln(„a= „,a);

Writeln(„b= „,b);

swap(w,b);

Writeln(„w= „,w);

Writeln(„b= „,b);

w= 3

b= 5

Page 13: 2Bytesprog2 course_2014_c8_units

Program prog1;

Uses my_unit1;

Var a,b,w :integer;

Begin

w:=5; temp:=10; a:=2; b:=3;

End.

swap(w,b);

Writeln(„w= „,w);

Writeln(„b= „,b);

My_unit1.w If you want to deal

with w in my_unit

You should write Which w ????

{this}

my_unit1.w:=10

Page 14: 2Bytesprog2 course_2014_c8_units

Lets make it

real :D !!

Page 15: 2Bytesprog2 course_2014_c8_units

Exercise : وإجشائٍت ( a^n )أنشئ وحذة بشهجٍت وعشف فٍها تابع ٌقىم بحساب

تقىم بحساب هعكىس عذد طبٍعً وإجشائٍت تقىم بطباعت عناصش سلسلت على أن تتن كتابت ( ) هن النهاٌت حتى البذاٌت )أحادٌت بشكل هعكىس

ثن استخذم هزه الىحذة فً بشناهج ( اإلجشائٍاث السابقت بشكل عىدي سئٍسً ورلك الستذعاء التىابع السابقت

Page 16: 2Bytesprog2 course_2014_c8_units

Recursion

It‟s process that procedure or function recall

itself within the body of that procedure or

function

Recursion always need to stop condition

Page 17: 2Bytesprog2 course_2014_c8_units

Unit recursion_unit;

Interface

type list=^node;

node=record

value:integer;

next : list;

end;

Function y(a,n:integer): longint ;

Procedure reverse(z:integer ; var z1:integer);

Procedure printlist (p: list );

Implementation

Function y(a,n:integer): longint ;

Begin

If n=0 then

y:=1

else

y:=y(a,n-1)*a;

end;

Page 18: 2Bytesprog2 course_2014_c8_units

Procedure reverse(z:integer ;var z1:integer);

Begin

If (z<>0) then

begin

Z1:=z1*10 + z mod 10;

Reverse (z div 10 , z1);

end;

end;

Procedure printlist(p : list );

Begin

If (p^.next <> nil ) then

Printlist(p^.next);

Write(p^.value:5);

end;

End.

Page 19: 2Bytesprog2 course_2014_c8_units

Program pro2bytes_team;

uses recursion_unit;

var a,n,z,z1,m,x,i:integer;

p,temp:list;

Begin

Readln(a,n);

If (n < 0) then

Writeln(„a^n = „,1/y(a,n))

Else

Writeln(„a^n = „,y(a,n));

Writeln(„ input positive number „);

Readln(z);

z1:=0;

Reverse (z,z1);

Writeln(„z1 = „,z1);

Page 20: 2Bytesprog2 course_2014_c8_units

Writeln(„input the number of nodes „);

Readln(m);

Writeln(„input the value „);

Readln(x);

new(p);

p^.value:=x;

temp:=p;

for i:=2 to m do

begin

new(temp^.next) ;

temp:=temp^.next;

Writeln(„input the value „);

Readln(x);

temp^.value:=x;

end;

temp^.next:=nil;

Printlist(p);

End.

Page 21: 2Bytesprog2 course_2014_c8_units

Execution : Function y(a,n:integer): longint ;

Begin

If n=0 then

y:=1

else

y:=y(a,n-1)*a;

end;

n=3 a=2

a = n

y= * 2

n= 3

n= 2

n= 1

n= 0

y= * 2

y= * 2

y = 1

waiting waiting waiting stop condition

8

4 2 1

End.

Page 22: 2Bytesprog2 course_2014_c8_units

Procedure printlist(p : list );

Begin

If (p^.next <> nil ) then

Printlist(p^.next);

Write(p^.value:5);

end;

End.

Execution :

next 20

15

10

5

p

nil

p

p

p

stop condition !!!

output

20

15

10

5

Page 23: 2Bytesprog2 course_2014_c8_units

Group : group link

Mobile phone- Kinan : 0994385748

Facebook account : kinan’s account

2 bytes team