INFI Exercises FP 20210219
INFI Exercises FP 20210219
INFI Exercises FP 20210219
Exercises on Lazarus
Informática Industrial
Informática Industrial
i
ii CONTENTS
Chapter 1
A set of exercises are recommended to students. These exercises will cover all fundamentals of
the free pascal programming language including:
• data types;
• data manipulation;
• statements.
1
2 Exercises on Free Pascal: Part 1
After saving the project (”Save All”), you can notice that there is a Project1(.lpi) and Unit1(.pas),
and you are invited to change the filenames of both.
1 u n i t Unit1 ;
2
3 {$mode objfpc}{$H+}
4
5 interface
6
7 uses
8 C l a s s e s , S y s U t i l s , Forms , C o n t r o l s , G r a p h i c s , D i a l o g s ;
9
10 type
11 TForm1 = c l a s s ( TForm )
12 private
13
14 public
15
16 end ;
17
18 var
19 Form1 : TForm1 ;
20
21 implementation
22
23 {$R *.lfm}
24
25 end .
code/exampleApplication.txt
• integer;
• real;
1.2 Data Types 3
• boolean;
• strings.
• BT_Calculate: TButton;
• Ed_Numb1: TEdit;
• Ed_Numb2: TEdit;
• Label_Numb1: TLabel;
• Label_Numb2: TLabel;
• Label_Result: TLabel;
• Memo_ShowResult: TMemo;
By double-clicking on the Button, you will create automatically the event: ”procedure BT_CalculateClick(Sen
TObject);”. This event will happen every time that the button is pressed.
Now, you want to code your program that will add two integer numbers using Editboxes as
inputs and the Memobox as output. Several questions arrive:
• Assignment operator.
• Adding operator.
1 procedure TForm1 . B T _ C a l c u l a t e C l i c k ( S e n d e r : T O b j e c t ) ;
2 var
3 numb1 : integer ;
4 numb2 : integer ;
5 r e s _ c a l : integer = 0 ;
6
7 begin
8 numb1 : = S t r T o I n t D e f ( Ed_Numb1 . Text , 0 ) ;
9 numb2 : = S t r T o I n t D e f ( Ed_Numb2 . Text , 0 ) ;
10 r e s _ c a l : = numb1 + numb2 ;
11
12 Memo_ShowResult . C l e a r ;
13 Memo_ShowResult . Append ( I n t T o S t r ( r e s _ c a l ) ) ;
14 end ;
code/ex1Adding.txt
2. string data-type.
• locate the index of ”20” (the location of the first appearance in the string).
1.2.3 String
Read a string and write it out with a space between each adjacent pair of characters. Example:
• Enter: waffle
• Print: w a f f l e
Read a password P using an EditBox, then clear the MemoBox and repeatedly read password
guesses in the other EditBox until the password P is entered. Example:
• Password: apple
• Print: wrong!
Write a program that reads a single line of input text, and determines which letter from A to Z
appears most frequently in the input text. You should ignore case, considering ”a” and ”A” to be
the same letter.
Write a function capitalize(s: string): string that capitalizes the first letter of every word in a string,
where words are separated by one or more spaces.
Declare two string variables: Name and Surname. Declare an integer variable: Age. Use three
EditBoxes: to assign to Name and Surname your name and surname and, your age to Age. Use a
”ShowMessage” to display the text ”My name is $name $surname. I am $age years old.” after the
button is pressed.
6 Exercises on Free Pascal: Part 1
• Enter X: 4
• Enter Y: 6
• Print: Y is greater
1.2.9 Quotient
Read two integers X and Y, and print their quotient if X is exactly divisible by Y, or ”indivisible”
otherwise. Example:
• Enter X: 10
• Enter Y: 3
• indivisible
• (X / Y)
• (X * Y)
• (X div Y)
• (X mod Y)
• (X AND Y)
• (X OR Y)
• (NOT X)
• (X XOR Y)
1.3 Statements 7
1.3 Statements
1.3.1 IF and ELSE
Read three numbers and write whether or not they can form a triangle. Note - to form the sides
of a triangle, each value must be less than the sum of the other two. You should categorize the
triangle into: ”EQUILÁTERO”, ”ISÓSCELES” or ”ESCALENO”.
Read a year from 1980 to 2020 using the EditBox. Print the decade that it was in. Example:
• Year: 1995
1.3.3 FOR
Ask the user for a number N, then write the word ”strawberries” N times. Example:
• strawberries
• strawberries
• strawberries
8 Exercises on Free Pascal: Part 1
• Enter N: 6
• *
• **
• ***
• ...
• ******
Simulate coin flips, printing an ”H” each time the coin falls on heads and ”T” each time it falls on
tails. Once you reach the third H, stop and write how many coins were flipped. Example:
• HTTTHTH
• 7 flips
• ===
1.3.6 While
1 procedure TForm1 . B T _ C a l c u l a t e C l i c k ( S e n d e r : T O b j e c t ) ;
2 var
3 numb1 : integer ;
4 numb2 : integer ;
5 r e s _ c a l : integer = 0 ;
6 i : integer ;
7
8 begin
9 Memo_ShowResult . C l e a r ;
10
11 for i : = 1 to 5 do
12 Memo_ShowResult . Append ( I n t T o S t r ( i ) ) ;
13 end ;
code/ex2While.txt
1.3 Statements 9
These exercises will cover the following topics: record data type; procedures; functions and,
passing arguments by value or reference.
• BT_AddOrder: TButton;
• CB_PartType: TComboBox;
• CB_Destination: TComboBox;
• Ed_Numb1: TEdit;
• Label_Numb1: TLabel;
11
12 Exercises on Free Pascal: Part 2
• Label_PartType: TLabel;
• Label_Destination: TLabel;
• Label_Result: TLabel;
• Memo_ShowResult: TMemo;
This exercise introduces the ”TComboBox”. Please explore this component, in particular, the
”items” and ”itemIdex”. Note that you can access the item that was selected by the user with both
properties: ”CB_PartType.items[CB_PartType.ItemIndex]”. We want to create a structure called
”TOrder” which will be formed by the following members:
• id : integer;
• fromType: tPartType;
• destination : tRouter;
By pressing the TButton, a new instance of ”TOrder” should be locally created and printed in the
TMemoBox. Create a record data type (”TOrder”) using the:
• ”id” variable that can assume an integer value (no repetition should be allowed).
• ”tPartType” variable that can assume a value of ”A”, ”B”, ... or ”G”;
Use the ”With” statement for printing each order that is created when the TButton is pressed.
Try yourself and then compare with the following code. Analyze and discuss the differences.
1 u n i t Unit1 ;
2
3 {$mode objfpc}{$H+}
4
5 interface
6
7 uses
8 C l a s s e s , S y s U t i l s , Forms , C o n t r o l s , G r a p h i c s , D i a l o g s ,
StdCtrls ;
Exercises on Free Pascal: Part 2 13
10 type
11 tRouter = 0 . . 7 ;
12 t P a r t T y p e = ( P a r t A = 0 , P a r t B , P a r t C , PartD , P a r t E ,
PartF , PartG ) ;
13 TOrder = record
14 id : integer ;
15 PartType : tPartType ;
16 Route : tRouter ;
17 end ;
18
19 { TForm1 }
20
21 TForm1 = c l a s s ( TForm )
22 BT_AddOrder : T B u t t o n ;
23 CB_PartType : TComboBox ;
24 C B _ D e s t i n a t i o n : TComboBox ;
25 Ed_Numb1 : T E d i t ;
26 Label_Numb1 : T L a b e l ;
27 Label_PartType : TLabel ;
28 L a b e l _ D e s t i n a t i o n : TLabel ;
29 L a b e l _ R e s u l t : TLabel ;
30 Memo_ShowResult : TMemo ;
31 procedure BT_AddOrderClick ( S e n d e r : T O b j e c t ) ;
32 private
33
34 public
35
36 end ;
37
38 var
39 Form1 : TForm1 ;
40
41 implementation
42
43 {$R *.lfm}
44
45 { TForm1 }
46
14 Exercises on Free Pascal: Part 2
56 With l o c a l O r d e r do
57 Begin
58 Memo_ShowResult . C l e a r ;
59 Memo_ShowResult . Append ( ’ID: ’+ I n t T o S t r ( i d ) ) ;
60 Memo_ShowResult . Append ( ’PartType:
’+ I n t T o S t r ( Integer ( P a r t T y p e ) ) ) ;
61 Memo_ShowResult . Append ( ’Route: ’+ I n t T o S t r ( R o u t e ) ) ;
62 End ;
63 end ;
64
65 end .
code/exampleAppRecord.txt
2.0.1.3 Procedure
Declare a procedure that will be responsible for printing an instance of the type TOrder in a
MemoBox. Then, this procedure will have two arguments: TOrder and a TMemo.
Update the procedure created in the last exercise. This new procedure should be able to print all
information and copy the ID of an instance TOrder to an argument. Then, this procedure will have
three arguments: TOrder, a TMemo and an integer variable.
Develop an function that adds the ID members of two instances TOrder and returns the instance
TOrder with a lower Route destination. This function will have three arguments: two TOrder and
one integer.
Create other TButton to call this function. Moreover, you must implement a dynamic array of
TOrder that is incremented by a new order when the BT_AddOrder is pressed.
Exercises on Free Pascal: Part 2 15
The code below can assist you during the resolution of exercises presented in section 2.0.1.
1 u n i t Unit1 ;
2
3 {$mode objfpc}{$H+}
4
5 interface
6
7 uses
8 C l a s s e s , S y s U t i l s , Forms , C o n t r o l s , G r a p h i c s , D i a l o g s ,
StdCtrls ;
9
10 type
11 tRouter = 0 . . 7 ;
12 t P a r t T y p e = ( P a r t A = 0 , P a r t B , P a r t C , PartD , P a r t E ,
PartF , PartG ) ;
13
14 TOrder = record
15 id : integer ;
16 PartType : tPartType ;
17 Route : tRouter ;
18 end ;
19
20 t a r r a y _ o r d e r s = array of TOrder ;
21
22 { Ts }
23
24 Ts = c l a s s ( TForm )
25 BT_AddOrder : T B u t t o n ;
26 Bt_MinOrder : T B u t t o n ;
27 CB_PartType : TComboBox ;
28 C B _ D e s t i n a t i o n : TComboBox ;
29 Ed_Numb1 : T E d i t ;
30 Label_Numb1 : T L a b e l ;
31 Label_PartType : TLabel ;
32 L a b e l _ D e s t i n a t i o n : TLabel ;
33 L a b e l _ R e s u l t : TLabel ;
34 Memo_ShowResult : TMemo ;
16 Exercises on Free Pascal: Part 2
35 procedure BT_AddOrderClick ( S e n d e r : T O b j e c t ) ;
36 procedure B t _ M i n O r d e r C l i c k ( S e n d e r : T O b j e c t ) ;
37 procedure F o r m C r e a t e ( S e n d e r : T O b j e c t ) ;
38 private
39
40 public
41 array_orders : tarray_orders ;
42 n u m b _ o r d e r s : integer ;
43 end ;
44
45 var
46 s : Ts ;
47
48
49
50 implementation
51
52 {$R *.lfm}
53
54 { Ts }
55
56 procedure Ts . F o r m C r e a t e ( S e n d e r : T O b j e c t ) ;
57 begin
58 numb_orders := 0;
59 end ;
60
61
73
108 l o c a l O r d e r . Route :=
StrToInt ( CB_Destination . Items [ CB_Destination . ItemIndex ] ) ;
109 / / p r i n t O r d e r _ A n d C o p y ( l o c a l O r d e r , Memo1 , c _ i d x ) ;
110 / / Memo_ShowResult . Append ( ’Variable received from
procedure:’ + I n t T o S t r ( c _ i d x ) ) ;
111
112
124 procedure Ts . B t _ M i n O r d e r C l i c k ( S e n d e r : T O b j e c t ) ;
125 var
126 addIDS : integer ;
127 a u x O r d e r : TOrder ;
128 begin
129 auxOrder := minRoute_Order ( a r r a y _ o r d e r s [ 0 ] ,
a r r a y _ o r d e r s [ 1 ] , addIDS ) ;
130 p r i n t O r d e r ( a u x O r d e r , Memo_ShowResult ) ;
131 Memo_ShowResult . Append ( ’Variable received from
function:’ + I n t T o S t r ( addIDS ) ) ;
132 end ;
133
134
135
136
137 end .
code/solutionsEx2.txt
Exercises on Free Pascal: Part 2 19
2.0.3 TTimers
Lazarus works with ”events”. For a procedure or function be capable of running with a specific
frequency, you must use a TTimer (that is available on the TAB ”System”). Please add one TTimer
and create the event onTimer that will present a ShowMessage with the text ”This is an event”.
Modify the property ”Interval” and analyze the change in the program behavior.
20 Exercises on Free Pascal: Part 2