Сабақтар 30 с. СобөЖ 15 с. СӨЖ 45 с. Емтихан 6, 8 семестр Барлығы 90 с


Logical Operators and Boolean Expressions



жүктеу 0.61 Mb.
Pdf просмотр
бет4/6
Дата03.05.2017
өлшемі0.61 Mb.
түріСабақ
1   2   3   4   5   6

Logical Operators and Boolean Expressions 

What are the Logical Operators  

The logical operators are expressions which return a false or true result over a conditional 

expression. They are also used in assignments (an example of this would be shown later). Such  

operators consist of simple logical operators, such as 'Not' or 'And'. They should be used between 

two conditional expressions ; for example: 

If (x = 0) AND (a = 2) then... 

The Logical Operators  

There are three types of logical operators, each of which are concerned with conditional 

expressions. These are: 



   AND 

   OR 


   NOT 

All of these logical operators have a different effect on the conditional expressions. Let's see what 

each of the logical operator does two (or more) conditional expressions...  

 

If  *1(Str1 = 'a')  AND  *2(Str2 = 'b') then writeln('Yes, you got it right.'); 



Expression 1  Expression 2 

AND 

(result) 

true 


true 

true 

false 


true 

false 


true 

false 


false 

false 


false 

false 


If expression 1 and expression 2 are both true(i.e. the user inputs 'a' and 'b' into variables 'Str1' 

and 'Str2' respectively), the message will be displayed. Above  is a table showing the possible 

combinations.  

So, from the above table, one can conclude that for a logical operation such as AND, to give out a 

true result, both conditional expressions should be true. 

If  *1(Str1 = 'a')  OR  *2(Str2 = 'b') then writeln('Yes, you got it right.'); 



Expression 1 

Expression 2 

OR (result) 

true 


true 

true 

false 


true 

true 

true 


false 

true 

false 


false 

false 


Either expression 1 or expression 2 should be true to display the message. If for example 

expression 1 is true and any other conditional expressions are false, the result is true! Above is a 

table(the truth table) showing the possible combinations.  

So, from the above table, one can conclude that for a logical operation such as OR, to give out a 

true result, only one of the conditional expressions should be true.  

Not is almost different from the two logical gates. It only accepts one input and is well-known as 

the 'inverter'. If for example the result of two conditional expressions is true, the 'not' operator 

would invert the result to false! So, the of the logical operator, 'not', is to output the inverse of the 

input. The simple truth table for the not operator is: 

Input 

Output 

true 


false 

false 


true 

 

 



 

Procedures and Functions 

From now on, lessons are becoming somewhat more than basic. The most important thing is that 

you understand clearly the material especially the examples shown which are very important and 

you should keep on practicing more often. 



Procedures are just like small programs. Sometimes they are called sub--programs. They help the 

programmer to avoid repetitions. A procedure start off with a begin and ends up with an end;. It 

can also have its own variables, which cannot be used with the main-program. To have an exact 

definition of a procedure, you should compare a program which includes a repeated section with 

another program avoiding the repeated sections by using a procedure, which is called several 

times: 


There are some differences between these two programs which are very important to note. These 

are : 


 

Size of the program  

It is very important for a program to be small in size. The first program, say, its size is 1900 bytes, 

but the second one holds about 1350 bytes! 

 

Neatness  

Adopting a neat style of writing for a program helps the programmer (and other future debuggers) 

to cater with future bugs. I think that the first program is cumbersome, whilst the other is not! 

What do you think??!  

 

Repetitions  

Repetitions in a program can cause a hard time for a programmer. So procedures are an essential 

way to avoid repitions in a program. They also enlarge the size of a program! 

 

Debugging Efficiency  

When you are required to debug the program, bugs could be much more easier to find out as the 

program is sliced into smaller chunks. You may run the program and notice a mistake at a certain 

point and which is located in a particular procedure/function. It would be much more difficult to 

find a mistake in a program if it would be one whole piece of code. Do slice your program into 

smaller chunks, and this needs design of the whole problem in hand prior to coding. Coding (or 

writing up your program) is just one section of the whole software development process. The 

whole Software Development Process is an important part of nowadays scientific computing.  



Using Procedures with Parameters 

Returning back to program Lesson7_Program1b, the gotoxy statement before the DrawLine; 

could be "kicked off" so that we can avoid the repetition of the gotoxy! We cannot build up 

another procedure for the gotoxy, but it should be done by adding parameters with the procedure. 

Now, this program includes a procedure which uses parameters. Every time it is called, the 

parameters can be variable, so that the position of the line could be changed. This time, we have 

also eliminated the gotoxy statement before every DrawLine statement. The numbers in the 

brackets of the DrawLine are the parameters which state the position of the line. They also serve 

as a gotoxy statement.  

When you apply parameters to a procedure, variables should be declared on there own, and must 

be separated by a semi-colon ";". They are put in between the brackets, following the procedure 

name. The variables (known as the parameters) should be used by the procedure/sub-program 

only. 

I made another program, which prompts the user to enter his/her favourite text colour and 



background colour, which would be used to write text (in his/her favourite colours) further in the 

program. 

Parameters of procedures may be varaible. In this case, data may flow through the variable in 

both ways. What I am trying to say is that you can pass data and get data through the procedure 

using a variable parameter. Here is a declereation of a variable parameter: 

Procedure 


Var 
Variable_Name Type);> 

The second type of sub-program is called a function. The only difference from the procedure is 

that the function return a value at the end. Note that a procedure cannot return a value. A function 

start and end in a similar way to that of a procedure. If more than one value is required to be 

returned by a module, you should make use of the variable parameter. A function can have 

parameters too. If you change the sub-program from procedure to a function, of the previous 



program, there will be no difference in the output of the program. Just make sure which one is 

best when you can to implement a module. For example, if you don't need to return any values, a 

procedure is more best. However if a value should be returned after the module is executed, 

function should be used instead.  

 

Arrays 

An Array is a powerful data structure that stores variable data having the same data type. It is just 

like a small fixed number of boxes linked together one after the other storing things that are 

related to each other. An array is said to be a static data structure because, once declared, its 

original size that is specified by the programmer will remain the same throughout the whole 

program and cannot be changed.  

Up until now, we have used single variables only as a tool to store data. Now we will be using the 

array data structure and here is how it is declared: 



Var 

myArray : Array[1..20] of Integer; 

 : Array[n..m] of ; 

An array data structure defines the size of the array and the data type that it will use for storing 

data. In the above example, the array stores up to 20 integers however I may have used 30 

integers or more. This size depends on your program requirements. 

Arrays are used just like ordinary variables. They are used to store typed data just like the 

ordinary variables. You will now learn how to assign data to arrays and read data from arrays. 

In the example above, I have declared 20 integers and I should be able to access each and one of 

them and here is how I do it.  

To assign values to a particular integer of an array, we do it like this: 

myArray[5] := 10; 

myArray[1] := 25; 

 

[index] :=  

 

You just take the array in subject, specify the index of the variable of the array and assign it a 



value relevant to the data type of the array itself. 

Reading a value from an array is done as follows: 



Var 

    myVar   : Integer; 

    myArray : Array[1..5] of Integer; 

 

Begin 

 myArray[2] := 25; 

 myVar := myArray[2]; 



End

Just like ordinary variables, arrays should be initialised, otherwise scrap data will remain stored in 

them. If we want to intialise 2 whole 20-sized integer and boolean arrays to 0 and false 

respectively, we do it like this: 



Var 

    i           : Integer; 

    myIntArray  : Array[1..20] of Integer; 

    myBoolArray : Array[1..20] of Boolean

 

Begin 

 For i := 1 to 20 do 



  Begin 

   myIntArray[i]  := 0; 

   myBoolArray[i] := false

  End; 



End

 

Introducing User-Defined Data Types 

Now that we have used various built-in data types, we have arrived at a point were we want to use 

our defined data types. Built-in data types are the ones we used lately, such as IntegerBoolean 

and String. Now we will learn how to specify our own customised data types and this is just how 

it is done: 



Type 

 = 
;
 

The "Type" keyword is a reserved Pascal word used to define our own data types. So you start 

defining your own data types by using this keyword. After defining your own data types, you may 

start using them just like the other built-in data types as follows: 



Var 

 :  

Now lets define a new simple data type and note how it will be used in the program below: 



Type 

    nameType = String[50]; 

    ageType  = 0..150; { age range: from 0 to 150 } 

 

Var 

    name : nameType; 

    age  : ageType; 

 

Begin 

 Write('Enter your name: '); 

 Readln(name); 

 Write('Enter your age: '); 

 Readln(age); 

 Writeln; 

 Writeln('Your name:', name); 

 Writeln('Your age :', age); 

 Readln; 



End

In the above example we defined a String[50] and a 0..150 data type. The nameType only stores 

strings up to 50 characters long and the ageType stores numbers only from 0 to 150.  

We can define more complex user-defined data types. Here is an example of more complex user-

defined data types: 

In the above example I showed you how to incorporate arrays as user-defined data types. Note 

that you may use user-defined data types more than once. 

2 Dimensional arrays and multi-dimensional are arrays which store variables in a second or nth 

dimension having n*m storage locations. Mutli dimensional arrays including the 2 dimensional 

array, are declared by using multiple square brackets placed near each other or using commas 

with one sqaure brackets as an alternative. Here is how multi-dimensional are declared:  

my2DArray       : Array[i..j][k..l] of 

myMultiDimArray : Array[m..n][o..p][q..r][s..t]... of ;  

Let us have the 2 dimensional array defined first. Think of a grid where each box is located by 



using horizontal and vertical coordinates just in the example below: 





  

  



  

  



  

  

3,4 



  

  



  

  

  



  

5,3 



  

  

An example of a 5 by 5 2D array illustrated on a grid  

Having the vertical axis as the 1st dimension and the horizontal one as the 2nd dimension, the 

above example is illustrated as follows:  







  

  

10 

  



  



  

  

  



Multi-dimensional arrays are rare and are not important. The single and 2D dimensional arrays 

are the 2 most frequent dimensions.  

The following example is a bit more complex example than the previous examples and it also 

uses a 2 dimensional array to illustrate their use. This concludes the arrays lesson. In the next 

lesson we will learn about Record data structures, their uses and we will also learn how to use 

binary files used record data structures. 

 

 

 



Course Delphi. Compiling a project 

 Delphi combines the user-friendlyness of Visual Basic with the precise control and speed of 

C++. Without lots of "real" programming, you can develop very efficient and fast Windows 

applications.  

Delphi is  a  RAD  tool,  Rapid  Application  Development.  It  reduces  the  complicated task of 

programming Windows applications to the handling of "objects" in a visual environment.  

Typing of source code is limited to a strict minimum. As a result, you can fully concentrate 

on what the program should do: this is top-down programming at its highest level, designing 

a nice Windows GUI interface becomes a breeze. You don't have to "program" any standard 

Windows  elements,  just  a  few  mouse  clicks  and  there's  your  fully  functional  listbox,  file 

dialog box, or even a full fledged database grid.  

With Delphi, debugging is limited to the program lines that you entered yourself, because all 

the ready-made modules that you use are tested and ready-to-go.  

Compared  to  the  limited  possibilities  of  Visual  Basic  or  the  complexity  of  C++,  Delphi  is 

really  refreshing.  Once  you've  used  Delphi,  you'll  be  fascinated  by  its  sheer  development 

speed.  


For starters, a few general things about this Delphi Tutorial:  

  Will  it  work  with  Delphi  2007  or  Delphi  6  or  ...?  Sure!  All  the  lessons  are  100% 

compatible with Delphi XE down to Delphi 4.  

  VCL, IDE, OOP...? In order to get you up to speed quickly on acronyms, in one of the 

first 

lessons 


of 

our 


second 

tutorial, 

we'll 

make 


the 

program 


Acron

For those who are too impatient: the exe-file of Acron can be found in the Downloads 

section  as  ACRONX.ZIP.  Also  download  ACRODAT4.ZIP:  it  contains  the  list  of 

acronyms. This list will grow. Every new and enhanced version will get a higher number 

(yep,  the  next  one  will  be  ACRODAT5.ZIP,  and  afterwards...,  and  so  on). 

By  the  way  (BTW),  D7  means  Delphi  7,  so  D2007  is...?  Correct.  For  "the  32-bits 



versions of Delphi, D4 up to the latest..." I came up with the very original D4+.  

  Run before you can walk? Yes, that's possible with Delphi. No need to start studying 

the  old  Turbo  Pascal,  no  need  to  know  anything  about  OOP  (Object  Oriented 

Programming).  Just  learn  as  you  practice,  and  we'll  fill  in  the  gaps  later  on. 

So, no traditional "Hello, World" proggie in my courses. We'll start immediately with a 

real application. After all, it's R.A.D time -- let's go!  

 

 



Preparations 

1.

 



If you haven't done so already, create a new folder (directory, as they said in the old days) 

\DelphiLa  on  your  harddisk  (C:  or  D:  or  ...).  Next,  download  the  first  series  of  lessons 

les0103.zip from the Downloads section and unzip it to this directory.  

2.

 



If  you  haven't  done  so  already,  create  directory  EuroCCEN  "under"  \DelphiLa.  Next, 

download  euroccen.zip  (source  code  for  the  first  finished  project)  from  the  Downloads 

section and unzip it to this directory. When checking with Windows Explorer,  you'll see 

euroccen.dpr, euroform.dfm and euroform.pas.  

3.

 

Now  start  the  introduction  lesson:  in  Windows  Explorer,  doubleclick  on  index.html  (in 



folder \DelphiLa). You're all set to follow the first lesson offline!  

 

Forms, edits, buttons. Properties and events 

In this lesson we'll create a simplified version, the application that was introduced in lesson 1.  

For now, the accent will not be on what the program does, but rather on how to start a project, 

how to save the files, add components, and so on. But very soon, we'll continue in a higher gear. 

    


Preparation 

1.

 



If 

you 


haven't 

done 


so 

yet, 


download 

euroen01.zip 

to 


\DelphiLa. 

Hold your horses! Don't unzip it now: every project will get it's own directory, and it is 

 


best to keep all the original files in \DelphiLa.  

2.

 



Create a new directory:  \DelphiLa\EuroEN01. Of course,  you can choose other names 

for your directories and projects. But if you use the structure that I propose here, it will 

be a lot easier to understand each other.  

3.

 



"Unzip"  euroen01.zip  to  directory  \DelphiLa\EuroEN01.  Check  if  you've  got  the  files 

euroen01.dpr, euroform.dfm and euroform.pas.  



Starting at the end 

As usual, we'll start with the end: how should our simplified EuroCC look like and what should 

it do?  

1.

 



Start  Delphi  and  open  the  project  euroen01.dpr  through  the  File  menu  and  Open 

Project...  

2.

 



Compile the application. If you have forgotten how to do that, have a look at lesson 1.  

3.

 



As  with  every  succesful  compilation,  Delphi  will  start  the  application  in  the 

"environment" 

of 

the 


debugger. 

 

 



4.

 

Just play around with the program. Notice that Delphi gives an error message and that it 



pauses the program,  if  you enter an invalid  value and then  click  a button. To continue, 

click  Run  in  the  Run  menu,  or  press  F9.  In  the  message-box  that  appears,  click  OK

Afterwards  you  do  not  get  a  second  error  message,  because  in  this  simple  version  of 

EuroCC  we  are  not  checking  for  a  valid  input  (compare  this  behaviour  with  lesson  1). 

 

 

5.



 

Stop 


the 

application. 

In the File menu, click Close All. Delete all files from \DelphiLa\EuroEN01, using your 

favorite file manager (Windows Explorer,...).  

1.

 

In the File menu, select New, next select VCL Forms Application - Delphi For Win32



(Note: in Delphi 7 and older, select "New Application")  

2.

 



Delphi will start a new project: 

o

 



Delphi's title bar shows the name of the new project: Project1. This means that 

Delphi created a dpr-file (project file) called Project1.dpr. 

Note: if you start several new projects one after the other during the same session, 

the second project gets the name Project2.dpr, the next one will be Project3,... 

Delphi consequently gives names along these lines to new files and new 

components. 

   

o

 



Notice the window with the title Form1 (in newer versions of Delphi, the Form 

window is completely hidden "behind" the Editor window). 

That window (form) is the basis for your project. Delphi-applications are based on 


FormsEvery application has one or more Forms. A Form is a component in the 

shape of a window. That's why Bill called it "Windows"... 

On the form you put other components, such as buttonsEdit-boxesRadiobuttons

ListBoxesComboBoxes and other well known Windows-creatures. 

   


o

 

We also have the Editor window. That's where the source code can be viewed and 



edited. The source code is the result of the cooperation between Delphi and 

yourself: for every unit, Delphi creates a template that you can complete.  

3.

 

Let's have a look at the Editor window: 



 

 

Unit1 is the name of the Unit that goes with Form1. Delphi created the file Unit1.pas, 

containing the source code for Form1, the main form of the application. 

 

For the moment, all these files only exist in the RAM of your computer. You have to save 



them to disk files.  

4.

 



Open the File menu and click Save All

In the dialog Save Unit1 as, select directory \DelphiLa\EuroEN01. Enter euroform as 

the file name. Delphi saves the text of the unit in file euroform.pas. The other data for 

Form1 are saved automatically in euroform.dfm.  

5.

 

In the next dialog, Save Project1 As, the right directory is already selected. Enter 



euroen01 as file name. Delphi saves the composition-data of the project as euroen01.dpr 

(this file contains the names of the units and the forms that you created, the names of other 

units used by the project, and so on).  

Notice that the title bar of the Editor window has changed to euroform.pas. But where is the 

project file, the dpr-file? It isn't visible at this moment, but that isn't important anyhow. Don't 

change anything in dpr-files if you do not know exactly what you are doing, better leave the 

management of the project to Delphi.  

Files and file naming 

It is best to save all files of a new project as soon as possible. That way, you won't experience 

nasty surprises as lost projects (what was that name again?), or an existing project being 

overwritten by a new one (and that's a lot worse...)  

 

You are completely free in naming the first unit (in our case: the only unit). 



   

 

Of course, all next units have to be given different names. 

   

 

The name of the project file must be different from all unit-names. In our case, the name 



euroen01 is not allowed for both the unit and the project, although the file extensions are 

different! 

   

 

Out of the project name, Delphi creates the name for the compiled executable. In our case 



this will be euroen01.exe. Therefore it is worthwile thinking of a suitable name BEFORE 

starting a project.  



Quick Analysis 

Analyzing before you start is half of the game.  

We know that 1 Euro equals 40.3399 BEF (Belgian frank). BTW: the same is true for the 

Luxemburg frank).  

Clicking a button should convert the value in the edit-box to another currency. Also the name of 

that currency should be shown.  

Components for this application: 1 edit-box for data entry, 2 buttons (one for the conversion Euro 

- Belgian franks, one for the reverse), 6 labels (1 for the name of the starting currency, 1 for the 

text "equals", 2 for the results, 2 for the currency names).  

Adding the Components 

The next steps explain how to add the components to the form:  

1.

 

Bring the form window to the front. Tip: you also can press F 12, which switches between 



the "unit" in the editor and the "form". 

Probably you will see a raster on the form, facilitating the alignment of the components. 

For reasons of clarity I omitted this raster in the pictures. On my PC, I switched off the 

form-raster: about everything can be personalized in Delphi. But don't change too much if 

you only just started with Delphi, because it's quite complicated to restore the original 

settings. In the worst case scenario, you would have to reinstall Delphi :-(((  

2.

 

In the component palette, click the icon of the Edit component. It is on the standard page 



of the palette: 

 

3.



 

Click somewhere in the form. An Edit-box appears on the form, with the text Edit1 (this is 

the name of the component).  

4.

 



Drag component Edit1 to the upper left-hand corner of Form1, right below the title bar. 

 

 



5.

 

Select the Label component from the component palette. 



 

 


6.

 

Move the cursor somewhere right of Edit1 and click: Label1 is placed on the form. 



 

 

7.



 

Once again, select the Label from the component palette. Then, click to the right of 

Label1. Label 2 is born.  

8.

 



Select the Button component from the component palette (also on the standard page). add 

it to the form, somewhere below the Edit-box. A button with the name Button1 appears. 

 

 

9.



 

Below Button1, add a second button to the form.  

10.

 

Add two Labels to the right of Button1 (Label3 and Label4). Add two more labels to the 



right of Button2 (Label5 and Label6). By now, your form should look similar to this: 

 

 



11.

 

Drag the components until Form1 more or less matches the picture shown above. 



 

Probably your form is a lot bigger than the one that I show here. Size the form by 

dragging the bottom left corner of the window's frame, just as you would do with any 

other window.  

12.

 

Time to save your work: in the File menu, select Save All.  



13.

 

Let's test. In the Run menu, select Run. Or press key F9



 

Delphi will compile and run your program. 

The program doesn't seem to do anything, but make no mistake: this is a full blown 

Windows application! You can size and move the window, enter text in the Edit, click the 

buttons,...  

14.


 

Stop the application.  

 

Browse for Folder

 

The following function lets the user browse for a folder in a treeview, that starts at the desktop. 

If a folder is selected, the function returns TRUE and the name of the selected folder is placed in 

variable Foldr; otherwise, it returns FALSE.  

Don't let the code scare you, because it uses some exotic types of variables and the Windows 

API-function SHBrowseForFolder... just copy it and use it. Like all the other code on 

DelphiLand, it has been fully tested :)  

 


function BrowseForFolder(var Foldr: string; Title: string): Boolean; 

var 


  BrowseInfo: TBrowseInfo; 

  ItemIDList: PItemIDList; 

  DisplayName: array[0..MAX_PATH] of Char; 

begin 


  Result := False; 

  FillChar(BrowseInfo, SizeOf(BrowseInfo), #0); 

  with BrowseInfo do begin 

    hwndOwner := Application.Handle; 

    pszDisplayName := @DisplayName[0]; 

    lpszTitle := PChar(Title); 

    ulFlags := BIF_RETURNONLYFSDIRS; 

  end; 


  ItemIDList := SHBrowseForFolder(BrowseInfo); 

  if Assigned(ItemIDList) then 

    if SHGetPathFromIDList(ItemIDList, DisplayName) then begin 

      Foldr := DisplayName; 

      Result := True; 

    end; 

end;  

IMPORTANT: the function above will only work if you add SHLOBJ to the uses-directive in 



your unit. For example, if it is: 

 

   uses Windows, Messages, SysUtils, ..., StdCtrls;  



change it to: 

 

   uses Windows, Messages, SysUtils, ..., StdCtrls, SHLOBJ;  



  

How to use the function? Here's an example: let the user browse for a folder and display it in the 

label Label1; if no folder was selected, we display "Nothing was selected".  

procedure TForm1.Button1Click(Sender: TObject); 

var 

  Foldr: string



begin 

  if BrowseForFolder(Foldr, 'Select a folder') then  

    Label1.Caption := Foldr 

  else  

    Label1.Caption := 'Nothing was selected'; 

end

Date and time of creation/modification of a file 

The function FileAge() returns the date/time stamp of a file. The returned value is an integer 

number; it has to be converted to Delphi's TDateTime format (a floating point number) before you 

can use it. You can use the following code to test the functions involved:  

procedure TForm1.Button1Click(Sender: TObject); 

var 


  File_Name: string; 

  DateTimeStamp: integer; 

  Date_Time: TDateTime; 

begin 


  File_Name := 'c:\mydocuments\test.doc'; 

  DateTimeStamp := FileAge(File_Name); 



  // FileAge returns -1 if file not found 

  if DateTimeStamp < 0 then 

    ShowMessage('File not found') 

  else begin 

    // Convert to TDateTime format 

    Date_Time := FileDateToDateTime(DateTimeStamp); 

    Label1.Caption := DateToStr(Date_Time); 

    Label2.Caption := TimeToStr(Date_Time); 

  end; 

end; 


 

Writing and reading to/from a text file

 

For saving one or more strings to a text file, you code the following steps:  

1.

 

Declare a variable of the type TextFile.  



2.

 

Use the command AssignFile to connect the TextFile variable to a physical file on disk.  



3.

 

"Open" the text file for writing with the command Rewrite. This means, that a new text 



file will be created on the disk. If there is already a file with the same name, it will be 

overwritten.  

4.

 

Write a string to the file with WriteLn. You can repeat this step as many times as you 



want.  

5.

 



Finally, "close" the text file with CloseFile.  

The example below shows how to save the contents of two Edit-components to a text file 

C:\Test\Data.txt  

procedure TForm1.btnWriteClick(Sender: TObject); 

var 

  F: TextFile; 



begin 

  AssignFile(F, 'C:\Test\Data.txt'); 

  Rewrite(F); 

  WriteLn(F, Edit1.Text); 

  WriteLn(F, Edit2.Text); 

  CloseFile(F); 

end; 

Reading strings from a text file is very similar, but in order to be on the safe side, you need an 



extra step. Before trying to read, you have to check if the file exists. You also need an extra 

variable to receive the strings that you read from the file. This are the steps:  

1.

 

Declare two variables, one of the type TextFile and one of the type String.  



2.

 

If the file exists, continue with step 3. If not, it ends here. Optionally, you can show an 



error message to the user.  

3.

 



Use AssignFile to connect the TextFile variable to a physical file.  

4.

 



"Open" the text file for reading with the command Reset.  

5.

 



Read a string from the file into the string variable, with the command ReadLn. Repeat 

this step to read the next line(s) of the file.  

6.

 

Finally, "close" the text file with CloseFile.  



Here's an example that loads the contents of the two Edits from the text file C:\Test\Data.txt  

procedure TForm1.btnReadClick(Sender: TObject); 

var 

  F: TextFile; 



  S: string; 

begin 


  if FileExists('C:\Test\Data.txt') then begin 

    AssignFile(F, 'C:\Test\Data.txt'); 

    Reset(F); 


    ReadLn(F, S); 

    Edit1.Text := S; 

    ReadLn(F, S); 

    Edit2.Text := S; 

    CloseFile(F);     

  end 


  else 

    ShowMessage('File C:\Test\Data.txt not found'); 

end; 

 

Build your own Dialogs in a snap



 

So you want to design your own dialogs, that work just like the Delphi dialog components, such 

as a simplied version of Delphi's OpenDialog. But you don't want to develop a full blown 

component (with published properties and methods for creation and destroying, and more 

complicated stuff...) because that would be a bit overkill. And besides, most beginner and 

intermediate programmers are not to keen on developing components ;) ...hey, isn't Delphi about 

R.A.D., Rapid Application Development ?  

 

Let's suppose that you want a simple dialogbox that looks like the one above. You want to call 



that dialog from your code just like you call Delphi's dialogs, for example call it from clicking 

Button1 on your main form: 

procedure TFormMain.Button1Click(Sender: TObject); 

begin 


  if UserDialog.Execute then 

    Label1.Caption := UserDialog.UserName 

  else 

    Label1.Caption := 'Error: no User ID entered'; 



end; 

Here's a quick and simple solution:  

1.

 

Add a new form to your project and call it UserDialog. Save the new unit as UserDlg (or 



any other name that's not already used in your project).  

2.

 



Set the form's Caption to 'Please enter your username:' and set its BorderStyle property 

to bsDialog.  

3.

 

Add three components: 



- a TEdit with the name edUserName; clear its Text property. 

- a TBitButton with the name btnOK; set its property Kind to bkOK

- a TBitButton with the name btnCancel; set its property Kind to bkCancel.  

4.

 



In the PUBLIC section of your unit, declare a string variable named UserName 

Below that line, declare a function with the name Execute, that returns a Boolean value.  

So far, most of the interface section is written by Delphi itself, you only added the two short lines 

from step 4! (shown in bold): 

unit UserDlg; 

interface 

uses 

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 



  StdCtrls, Buttons; 

type 

  TUserDialog = class(TForm) 

  edUserName: TEdit; 

  btnOK: TBitBtn; 

  btnCancel: TBitBtn; 

  private 

  { Private declarations } 

  public 

  { Public declarations } 

  UserName: string;          // added by you    

  function Execute: Boolean; // added by you 

  end; 

var 


  UserDialog: TUserDialog; 

Next, let's have a look at what you add to the implementation section -- it's surprisingly short: 

function TUserDialog.Execute: Boolean; 

begin 


  Result := (ShowModal = mrOK); 

  UserName := edUserName.Text; 

end; 

That's all! How does Delphi's magic work? 



The function Execute shows the UserDialog form in a "modal" way, that means: the window will 

open on top of the other window(s) and the application does not continue before UserDialog is 

closed.  

If the user clicks the OK button, the dialog is automatically closed and our Execute function 

returns True. When the Cancel button is clicked, the dialog also closes, but we receive False

Also when the UserDialog is closed in another way, we get False

Finally, the text of the edit-box is copied to the public variable UserName, from where other units 

can access it. 

Note: don't forget to add the unit UserDlg to the uses directive in each unit from where you call 

UserDialog, like this: 



  uses ..., ..., UserDlg; 

 

 



СОБӨЖ сабақтарға әдістемелік нұсқаулар  

 

Тақырыбы:   

 INTRODUCTION  

From the programmer's point of view, all members of the РС family consist of а processor, 

memory chips, and several smart,  programmable, circuit chips. All the main circuit components  

make the computer work are located all the system board; other' important parts are located all 

expansion boards,  сап bе plugged into the system board. The system board contains the 

microprocessor, which is tied to at least 64 kb of memory; some built-in ROM programs, such as 

BASIC and the ROM BIOS; and several very important support chips. Some of these chips 

control external devices, such as the disk drive or the display screen, and others help the 

microprocessor perform its tasks. In this section , we discuss each major chip and give а  

!.slli. important technical specifications. These chips are frequently known bу more than оnе 

пате. For example , some peripheral input/output hardware is supervised  а chip known  the 8255. 

This chip is also referred to as the 8255А and the 8255A5. The suffixes А and 5 refer to revision 

numbers and to parts rated for operation at different speeds. For programming purposes, аnу Intel 

chip part number that starts with 8255 is identical to аnу other chip  part number starts with 8255, 

regardless of the suffix. However , when you replace оnе of these chips оn а circuit board, note 

the suffix. If the suffixes are different, the part mау not operate at the proper speed.  



 Әдістемелік нұсқаулар: текстті аударып және мазмұнына талдау жасау 

 


Тақырыбы: DIFFERENT MICROPROCESSORS. BRIEF HISTORY OF ТНЕ INTEL 

ARCHITECTURE. DESIGN PHILOSOPHY OF INTEL  

The 8088 is the 16bit microprocessor  controls the standard IBM personal computers, including 

the original РС, the РС/ХТ, the portable РС, and the PC r. Almost every bit of data that enters or 

leaves the computer passes through the СРО to bе processed. 1nside the 8088, 14 registers 

provide а working area for data transfer and processing. These internal registers, forming аn area 

28 bytes in size, are able to temporarily store data, memory addresses, instruction pointers, and 

status and control flags. Through these registers, the 8088 сап access 1 МВ (megabyte), or   

than ,one million bytes, of memory. Тhе 8086 is used in the PS/2 models 25 and 30 (and also  

in тапу IBM РС clones). The 8086 differs from the 8088 in оn1у оnе minor respect: it uses а full 

16bit data bus instead of the 8bit bus that the 8088 uses. (The difference between 8bi t and 16bi t 

buses is discussed below. ) Virtually anything that you read  the 8086 also applies to the 8088; for 

programming purposes, consider them identical. The 80286 is used in the РС/АТ and in the PS/2 

models 50 and 60. Although fully compatible with the 8086, the 80286 supports extra 

programming features that let it execute programs much more quickly than the 8086. Per  

 the most important enhancement to the 80286 is its support for multitasking.  

Multitasking is the ability of а СРО to perform sever al tasks at а time  such as printing а 

document and calculating а spreadsheet  bу quickly switching its attention among the control1ing 

programs. The 8088 used in а РС or РС/ХТ сап support multitasking with the help of 

sophisticated control software. However , аn 80286 сап do а much better job of multitasking 

because it executes programs more quickly and addresses much more memory than the 8088. 

Moreover, the 80286 was designed to prevent tasks from interfering with each other .  

The 80286 сап run in either of two operating modes: real mode or protected mode. 1n real mode, 

the 80286 is programmed exactly like an 8086. 1t сап access the same 1 МВ range of memory 

addresses as the 8086. 1n protected mode, however, the 80286 reserves а predetermined amount  

of memory for an executing program, preventing that memory from being used bу аnу other 

program. This means that several programs сап execute concurrently without the risk of оnе 

program accidentally changing the contents of another program's memory area. An operating  

system using 80286 protected mode сап allocate memory among several different tasks much 

more effectively than сап аn 8086based operating system. The PS/2 Model 80 uses the 80386, а 

faster, more powerful microprocessor than the 80286. The 80386 supports the same basic 

functions as the 8086 and offers the same protected mode memory management as the 80286.  

However, the 80386 offers two important advantages  its predecessors:  

The 80386 is а 32bit microprocessor with 32bit registers. 1t сап perform computations and 

address memory 32 bits at а time instead of 16 bits at a time. The 80386 offers more flexible 

memory management than I the 80286 and З086. 



Әдістемелік нұсқаулықтар: текстті аударып және мазмұнын талдау 

 


Каталог: dmdocuments
dmdocuments -> Қазақ филологиясы кафедрасы
dmdocuments -> Айса байтабынов эпик жыршы
dmdocuments -> Мүхамбетқалиев С., Ахметов К.Ғ, Ғабдуллин Х. А. Байүлы мен Жетіру және Төре, Төлеңгіт
dmdocuments -> Көпбаева М. Р. ф.ғ. к., М. Әуезов атындағы Оңтүстік
dmdocuments -> Н. С. Тілеуханов
dmdocuments -> Т.ӘЛімқҰловтың «Қараой» ӘҢгімесіндегі махамбет тұЛҒасы акбулатова С. Б
dmdocuments -> Батыс қазақстандық КҮй дәСТҮрін әлемге мойындатқан қҰрманғазы рухы с. Ә. Күзембай
dmdocuments -> Кафедрасының отырысы шешімімен бекітілген. Педагогикалық жоғары оқу орындарының студенттеріне арналған. Орал, 2011

жүктеу 0.61 Mb.

Поделитесь с Вашими друзьями:
1   2   3   4   5   6




©emirb.org 2020
әкімшілігінің қараңыз

    Басты бет