اربد
هل تريد التفاعل مع هذه المساهمة؟ كل ما عليك هو إنشاء حساب جديد ببضع خطوات أو تسجيل الدخول للمتابعة.

اربد

منتدى معلومات عامة
 
صفحة الاعلاناتالمنشوراتالرئيسيةأحدث الصورالتسجيلدخول
دخول
اسم العضو:
كلمة السر:
ادخلني بشكل آلي عند زيارتي مرة اخرى: 
:: لقد نسيت كلمة السر

 

 أوبجكت باسكال (Object Oriented Programming)

اذهب الى الأسفل 
2 مشترك
كاتب الموضوعرسالة
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge24/2/2011, 00:33



أول ظهور للغة باسكال تدعم البرمجة الكائنية الموجهة (Object Oriented Programming) كان في عام 1983 في شركة أبل للكمبيوتر. بعد ذلك تلتها تيربو باسكال 5.5 الشهيرة التابعة لسلسة تيربو باسكال التي كانت تنتجتها شركة بورلاند، وقد قام المبرمج أندرس هجلسبيرغ بإضافة البرمجة الكائنية لخط تيربو باسكال في عام 1989.

لغة الباسكال الكائنية هي لغة تدعم البرمجة الهيكلية (Structured Programming) كما وتدعم البرمجة الكائنية(Object Oriented Programming). لغة الباسكال هي لغة متعددة الأغراض تصلح لكافة أنواع التطبيقات والإحتياجات، فبدايةً من تعلُم البرمجة لسهولتها ووضوحها، مروراً بالألعاب ، والبرامج الحسابية، والبرامج التعليمية، وتطبيقات الإنترنت، وبرامج الإتصالات ، ولغات البرمجة، فمثلاً بيئة تطوير دلفي مطورة بالدلفي نفسها، كذلك فري باسكال ولازاراس تم تطويرهما بلغة الباسكال، وإنتهاءً بنظم التشغيل مثل اﻹصدارات اﻷولى من نظام تشغيل ماكنتوش كان يستخدم فيها لغة باسكال الكائنية و كذلك نظامي التشغيل StreamOS وToro OS المستخدم فيه مترجم Free Pascal.

بعد النجاح التي حققته أداة التطوير تيربو باسكال التي كانت تُستخدم لإنتاج برامج تعمل في نظام الدوز، قامت شركة بورلانت بإنتاج أداة التطوير دلفي في عام 1995 لتستهدف نظام التشغيل وندوز 16 بت ثم وندوز 32 بت في إصدارة دلفي 2 ليعمل مع وندوز 95. وقد لاقت دلفي نجاح كبير حيث أنها كانت تنتج برامج أسرع بأضعاف المرات التي كانت تنتجها نظيرتها الفيجوال بيسك، وكان البرنامج الذي ينتج عن الدلفي لايحتاج لمكتبات إضافية أو مايعرف بالـ Run-time libraries حين إنزال البرامج في أجهزة أخرى لاتحتوي على دلفي.

يوجد مترجم حر ومجاني للغة أوبجكت باسكال يمكن إنزاله في عدة منصات تشغيلية كنظام لينكس ووندوز وماكنتوش، وهو مترجم فري باسكال Free Pascal، وأداة التطوير التي تسخدم فري باسكال كمترجم لها هي بيئة التطوير لازاراس Lazarus

الرجوع الى أعلى الصفحة اذهب الى الأسفل
nermeen

nermeen



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge24/2/2011, 01:26

يسلموا
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge24/2/2011, 01:34

يسعدك ربي ام ادم
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:35


Article #5: Objected Oriented Programming - Part 1
Object oriented programming is another programming paradigm which is an extension to the ordinary Pascal programming language. Object pascal helps programmers to approach a problem in a different way from the ordinary procedural programming paradigm. When programming using procedural approaches to a problem, one can only use calls to procedures and functions in order to perform some operation. In object oriented programming, one can define a particular object.

When we watch around us, we all see objects of different types: your computer that you are using right now, televisions, mobiles, cup, persons, cars, animals etc... Objects may be categorized into two parts: moving and non-moving. Moving objects can move on their own whereas non-moving objects can't move or they require a force in order to be moved. All objects have attributes which uniquely identify them from other objects. Such attributes may be the size, weight, colour, dimensions etc... Apart from attributes, objects may also have their own behavious which seperate their unique identity from other objects as well; (such as a person walks, sits on a chair, eats, runs etc.., an aeroplane takes off, flies, auto-pilots, start engines, drive on the runway etc...). Although there are many objects out there that are distinct from each other, some may exhibit similar behaviour and share similar characteristics. This can be seen in humans and monkeys.

Classification of Objects
When coming to allocate objects into groups using a tree hierarchy, one can start from the very top - i.e. the most general, to the very bottom of the tree - i.e. the most specific. Let's take for example a vehicle object. When someone talks with you about vehicles, it may come to your mind a car, or maybe a motorcycle. A truck, trailer and a bus are also vehicles. They are all objects having wheels that can be driven by one driver and transport some type of medium around the land - a bus carries people whereas a truck carries goods. These objects can all be defined using object orientation by declaring their properties (using variables) and behaviours (using methods - ie. procedures and functions).

Objects that more or less have similar characterstics can be categorized into a tree hierarchy that are most general at the top and are specified further down the tree. To simplify what is being explained, let's take a shape as an example. A shape can be any of rectangle, circle, triangle, cube, sphere and so on. But you cannot specify that a shape is a rectangle. So we have got an abstract idea of a shape - something that has got an enclosed body. A shape always has got points of origin, corners or curves. For example, a rectangle has got 4 vertices, and a circle has got a point of origin around which the line of circle is drawn.

Not all shapes have got the same characteristics. As you know, the more we go down the tree hierarchy the more we specify.


[ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذه الصورة]

The class hierarchy above shows how the very general shape object is classified at every stage by other different shapes. The more we go down from the top, the more we specify the object. If we take the path of the rectangle, it is divided into three specific objects - rounded rectangle, square and a cuboid.

In this context, it is important to introduce the concept of Inheritance. In the diagram above, inheritance is shown as one goes down the classification tree - hence an object becomes more specific. The rectangle shape inherits methods (e.g. draw())and properties (e.g. a record structure variable that stores x and y i.e. Point(x,y)) from the shape object. The rounded rectangle, square and cuboid inherit methods and properties of the rectangle. It is implicit that these three specific shapes automatically inherit methods and properties of the object that was inherited by the rectangle i.e. the shape object. The same applies to the other group of shapes of the same level.

It is possible that the shape object may include methods (i.e. functions and procedures) that need not necessarily be implemented but only declared. This concept is known as Polyphormism. For a clear and simple example of polyphormism is the following: we assume that all shapes need to be drawn. The shape object need only to declare a method called draw(). But this is not implemented since we don't know which type of shape we should draw. However, whichever object inherits the shape object, needs to explicitly implement the draw() function. For example, the rectangle object inherits the shape object, therefore it must implement the draw() function which draws a rectangle on the screen - otherwise a compiler error results.

Hopefully, I have introduced you to some important concepts of object orientism. You will meet them and use them inevitably when you'll be doing object oriented programming.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:36


Article #1: Sorting
Sorting is a useful technique which is used vastly from program to program. Sorting is a programming technique which is used to sort a list of pre-stored data list in an ascending or descending order according to a preset criterion.

There are several types of sorting, and one must choose a sorting method which best suites its application. One sorting method may be faster than another but slower in some other cases depending on the current situation. Thus, one must make sure the proper characteristics of a particular data items list, such as the number of items and the data type of the subject to be sorted.

Sorting Methods and the Big-O Notation
There are various sorting methods which are used according to a particular situation. All of the sorting methods are compared with one another using a time unit related measure called the big-O notation. The big-O notation is used to measure the efficiency of an algorithm which performs a particular function over a collection of items of size n. For example, the big-O complexity of both the Insertion Sort and the Bubble Sort is O(n2). Although both have got the same time complexity, Bubble Sort is slower.

Now let's just go straight to the point of our subject i.e. sorting. There are lots of useful sorting methods, though I will be implementing and discussing only three sorting algorithms. For the rest, I will leave them for you to discover and implement yourself. Users may feel free to note down and discuss some other interesting sorting algorithms (also on the context that is being discussed) in the user comments below.



Sorting Method #1: Bubble Sort
The Bubble Sort algorithm is simple, inefficient sorting algorithm. It is not recommended for use, since its performance at sorting a list of items is terribly slow. It is best at sorting a small list of items, but not for large ones.

The sorting time complexity of the Bubble Sort is O(n2).

Implementation


Procedure BubbleSort(numbers : Array of Integer; size : Integer);
Var i, j, temp : Integer;


Begin
For i := size-1 DownTo 1 do
For j := 2 to i do
If (numbers[j-1] > numbers[j]) then
Begin
temp := numbers[j-1];
numbers[j-1] := numbers[j];
numbers[j] := temp;
End;

End.
This may be freely used in your programs and start off learning sorting algorithms using this simple method.



Sorting Method #2: Insertion Sort
Insertion Sort algorithm is a bit more efficient sorting algorithm than Bubble Sort. As it name implies, the insertion sort algorithm inserts an unsorted item in an already sorted item list. This makes you think of the use of two seperated arrays - one unsorted and the other sorted. However, to save space one uses the same array and uses a pointer to separate the sorted and unsorted elements of the list.

The sorting time complexity of the Insertion Sort is O(n2). Although this exactly the same to Bubble Sort's, the Insertion Sort algorithm is twice more efficient, yet inefficient for large lists.

Implementation


Procedure InsertionSort(numbers : Array of Integer; size : Integer);
Var i, j, index : Integer


Begin
For i := 2 to size-1 do
Begin
index := numbers[i];
j := i;
While ((j > 1) AND (numbers[j-1] > index)) do
Begin
numbers[j] := numbers[j-1];
j := j - 1;
End;
numbers[j] := index;
End;

End.


Sorting Method #3: Quick Sort
Quick Sort is not a joke. This algorithm seems pretty fast in performance as its name suggests, though it's not easy to implement even if getting the gist of how the sorting algorithm works is not that difficult.

This sorting algorithm uses recursion extensively, so make sure you are quite familiar with recursion, and have used it a lot before trying to understand the algorithm. The quick sort works by using a "pivot". The pivot is an index pointer just like the ones used in previous sorting algorithms. The purpose of the pivot is to divide the list in two halves, one with elements greater than the pivot and the other with elements smaller than the pivot. The pivot is usually chosen to be the left-most element of the list, however it is not necessary and one may choose any random element from the list to be the pivot. Up till now, we have got the array list divided into two halves. Now, we do the same procedure over this two halves just like we did to the whole list - and this is what we call recursion. The longer the list, the more recursion there will be - thus more resources are requested i.e. memory space.

Quick-sort's worst case is when the list is already sorted and choosing the left-most element as the pivot - this will obviously be a very lengthy process which turns out to be inefficient for sorting an already sorted list using a quick sort. One may think of keeping a state variable which keeps track whether a list is already sorted or not and avoid using quick sort to check if an algorithm is sorted or not. Also, if the list to be sorted has got only 1 or less elements, the function returns.

To summurize, the following steps are fundamental in doing a quick sort:

If array has got 1 or less elements, then return.
Choose a pivot from the list.
The array is divided into two sections - one with elements smaller than the pivot, the other with larger elements than the pivot
Use recursion to do the first three steps again, operating on the two divided halves
Implementation


Procedure QSort(numbers : Array of Integer;
left : Integer; right : Integer);
Var pivot, l_ptr, r_ptr : Integer;


Begin
l_ptr := left;
r_ptr := right;
pivot := numbers[left];
While (left < right) do
Begin
While ((numbers[right] >= pivot) AND (left < right)) do
right := right - 1;
If (left <> right) then
Begin
numbers[left] := numbers[right];
left := left + 1;
End;
While ((numbers[left] <= pivot) AND (left < right)) do
left := left + 1;
If (left <> right) then
Begin
numbers[right] := numbers[left];
right := right - 1;
End;
End;
numbers[left] := pivot;
pivot := left;
left := l_ptr;
right := r_ptr;
If (left < pivot) then
QSort(numbers, left, pivot-1);
If (right > pivot) then
QSort(numbers, pivot+1, right);
End;
Procedure QuickSort(numbers : Array of Integer; size : Integer);
Begin
QSort(numbers, 0, size-1);
End;
So far so good, we've completed discussing three sorting algorithms. There are a number of sorting algorithms that were left out of context including Selection Sort, Heap Sort and Merge Sort. These are three other important sorting algorithms which more or less work in the same way like the algorithms we have just discussed. Like for example, the Merge Sort algorithm uses division of the list and recurssion as much as Quick Sort do.

Readers may add their opinions and other amendments concerning the subject of Sorting below.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:37


Article #2: The Stack Data Structure
An array is a data structure which stores data items in a pre-allocated space. It allows you to store data anywhere within its range. On the other hand, the Stack Data Structure does not allow you to store data where you want i.e. order is important. It is based on a LIFO basis - the last to arrive, the first to be served.

The stack is a simple array which stores data items using an index which points to the last element that has been inserted. If data is requested from the stack, the last element that has been stored is 'poped' out of the array and returned. When popping occurs, the last element is returned and discarded from the stack with the top pointer being decremented by 1. Obviously, if the stack is empty, nothing is returned and the top pointer will remain untouched.

One can always add elements to the stack by pushin data items at the end of the stack indicated by the top pointer. After each added item, the top pointer is increased by 1, however items won't be added and top pointer won't be increased by 1 if the stack is already full. The stack rejects any more items to be added when full until at least 1 item is poped.

Functions involved in operating the Stack Data Structure
The fundamental operations performed when working with stacks are listed below.

[ Assuming DataItem is a pre-declared user data type. It maybe anything like an integer, a string or any other data object. ]

Function Description
Procedure InitStack; Initialises the stack data structure.
Function IsEmpty : Boolean; Returns true if the stack is empty.
Function IsFull : Boolean; Returns true if the stack is full.
Function Pop : DataItem; If list is empty nothing is returned and top pointer remains the same. Otherwise, it acquires the last data item from the list and returns it. The top pointer is decreased by 1.
Procedure Push(item : DataItem); If the stack is not full, the item passed as argument is added to the end of the list indicated by the top pointer and latter is increased by 1.
Function GetSize : Integer; Returns the top pointer.


Implementation
An implementation of the fundamental functions are demonstrated below.

First I'll show you how to declare a stack data structure using an array and its top pointer. These will be global variables in your program as long as you decide to use them specifically in some procedure or function.


Const
STACK_SIZE = 100;


Var
myStack : Array[1..STACK_SIZE] of DataItem;
topPointer : Integer;
The initialisation is to be called before any stack operation.


Procedure InitStack;
Begin
topPointer := 0;
End;
We now implemement the IsEmpty() and IsFull() functions.


Function IsEmpty : Boolean;
Begin
IsEmpty := false;
If (topPointer = 0) then
IsEmpty := true;
End;


Function IsFull : Boolean;
Begin
IsFull := false;
If ((topPointer + 1) = STACK_SIZE) then
IsFull := true;
End;
Here are the implementations of the Pop() and Push() functions and making use of the functions that we have just implemented.


Function Pop : DataItem;

Begin
Pop := nil;

If not IsEmpty then
Begin
Pop := myStack[topPointer];
topPointer := topPointer - 1;
End;
End;


Procedure Push(item : DataItem);
Begin
If not IsFull then
Begin
myStack[topPointer+1] := item;
topPointer := topPointer + 1;
End;
End;
Finally, we implement the utility function GetSize(). Although one can access the current size of the stack using the global variable topPointer, it is of good practice to make use of functions instead of global variables.


Function GetSize : Integer;
Begin
GetSize := topPointer;
End;
That's the story of implementing a Stack Data Structure. It is a very useful utility that is not provided ready made with a programming language. A stack has wide range of uses such as in parsing a command line calculator - matching brackets. The stack is also used inevitably by your operating system in executing programs. When a function is called, the location in memory of the embedding function is pushed onto the stack for later reference, so that the operating system 'remembers' where it has broken execution of the previous function. When the function continues execution, it is poped off the stack.




Article # Article Title
1. Sorting
2. The Stack Data Structure
3. Pointers
4. Linked Lists
5. Recursion
6. Object Oriented Programming - Part 1

User Comments

Post By: Belstaff Jackets Posted On: 2011-01-26
There is no doubt that the women Belstaff jackets are your best choice .

Post By: gucci handbags outlet Posted On: 2011-01-26
There are many people who want to buy gucci handbags outlet.

Post By: Herve Leger Dresses Posted On: 2011-01-26
Most of the other Herve Leger Dresses as well as jackets.

Post By: PATRICK Posted On: 2010-11-15
HELP ME GET SOMETHING ON 2,3-DIMENSIONAL ARRAY.I LIKE THE LESSON

Post By: Kenrazy Posted On: 2010-10-12
On 18hth Nov this year am doing KNEC(Kenya National Examination Council)exams (Programming 1) which is extremely hard. am double sure your notes will help me alot to prepare for this exam. Keep up!

Post By: said Posted On: 2010-08-11
iwant aprogram about array racord which make studentname index

Post By: said Posted On: 2010-07-29
i want to follow your lesson

Post By: dherlyn Posted On: 2010-07-07
Your site about Pascal Programming is very helpul and easy to understand. I am teaching Data Structures and Algoritms using Pascal and I hope that this site will help me a lot.

Post By: Paul(M) Posted On: 2010-05-28
Thank you very much for your helpful materials!!! keep in touch and don't give up!.I am very interested with your material keep it up again we have no enough material in our developing countries particularly in Tanzania

Post By: silver Posted On: 2010-05-27
Am very interested with your shortly and clear notes you put in this page Thank you very much You have also helped me in answering and doing well my assignments in Pascal Let you carry in adding some more notes!!!we have no books so we depend much on you!

Post By: Johann Posted On: 2009-12-24
Thanks a lot for creating this website on Pascal programming because in Mauritius there is no book on Pascal programming for more than 10 years!!!

Post By: ray meth Posted On: 2009-05-07
quite helpful

Post By: blaze Posted On: 2006-11-20
that's some good work
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:38



Lesson 1 - The First Few steps in Pascal Programming

Programming Basics
In a program, you must always obey the rules of the language, in our case, the Pascal language. A natural language has its own grammar rules, spelling and sentence construction. The Pascal programming language is a high level language that has its own syntax rules and grammar rules. As you go along with the lessons, you must note what you can do and what you cannot do in writing a Pascal program. A very simple program is shown below:

Program Lesson1_Program1;
Begin
Write('Hello World. Prepare to learn PASCAL!!');
Readln;
End.
The program is written only to display the message : 'Hello World. Prepare to learn PASCAL!!' - an introductory message that is displayed to you whenever you are going to learn a new programming language. This is simply shown on the screen. So, to display any message on the screen, you should use 'write' (or 'writeln'). The 'readln' statement, here is used as to 'stop' the program and wait until the user presses enter. If the 'readln' statement is missing in this program, then the message is displayed on the screen without giving any chance for the user to read it and obviously halts! Try running this program with and without the 'readln' statement and notice the difference. I suggest you see it!! Now, look at this:

Program Lesson1_Program2;begin
Write('Hello World. Prepare to learn PASCAL!!');Readln;End.
This program also runs perfectly as the previous one. The only difference is: neatness and friendliness.

This first program is, what is commonly referred to in programming, as 'indented'. Indentation is a must in writing programs as it aids in the way the code is written ie. neater. Indentation also helps with debugging and code presentation. You will note how I indent programs.

A program in Pascal always starts by the reserved word 'Program' following the title of the program. There are various restrictions on how to write this statement. Below is a simple example of a small program. (Remember: you can copy and paste the program in a text file, save the text file as filename.pas and open it with Turbo Pascal. The .pas extension is required.)

In the following program, the computer must prompt the user to enter a number, then the latter is added to the second number input by the user.

Program Lesson1_Program3;
Var
Num1, Num2, Sum : Integer;

Begin {no semicolon}
Write('Input number 1:');
Readln(Num1);
Writeln('Input number 2:');
Readln(Num2);
Sum := Num1 + Num2; {addition}
Writeln(Sum);
Readln;
End.
Now we must take a look at the program. A program in Pascal starts with the reserved word 'Program' (although it is not explicitly required) and ends with 'End', following a full stop (this is required though). A full-stop is never used within the program, except when dealing with records (later topics) and at the end of the program as seen in the example above.

The 'Var' statement, is used to introduce any suitable variables which will be used later in the program. These variables are non-constant terms so that they are used in the program for storing values. The terms 'Num1', 'Num2' and 'Sum' in the program are the variables which store any numbers, except those which are real (in fact, during the execution of the program, a runtime error may occur if a decimal number is input). As you can see in the example above, these variables are assigned to as integers. The term 'integer' means any whole number, i.e. a number which is not a decimal number but a positive or negative number. The integer type ranges from -32768 to 32767. So values which are not within the specified range cannot be stored by an integer type. There are other types which are wider in range, but for now the integer type is enough to hold up our values. The variables 'Num1', 'Num2' and 'Sum' are terms which are not reserved words, but can be used as variables in the program to store data in them. They could be changed more than once. Moreover, I could have used 'number1', 'number2' and 'totalsum' (note that there must be no spaces within the variables), instead of 'Num1', 'Num2' and 'Sum', respectively. As you can see, it is much better to shorten the variables than writing long words, such as 'variable_number1'.

After declaring all the variables which are required to be used later in the program, the main program always starts with the reserved word 'Begin'. Without this word, the compiler will display a diagnostic (error message). In the program above, both of the two types of 'write' are used. These are 'write' and 'writeln'. Both has the same function, except that the 'write' function, does not proceed to the following line when writing a statement. If you run this program, you will notice the difference between them. When using these two terms, any message that will be typed in between the brackets and the inverted commas '(' ')', is displayed on the screen. However, if a variable is used instead of a message, without using the inverted commas, the CPU will display the stored variable in the memory, on the screen. In line 9, the CPU will not display 'Sum' on the screen, but the stored number in the memory. Another important thing which must be noticed is the semi-colon (;). The semicolon is used after each statement in the program, except those that you will learn later. However, in the example above, there isn't a semicolon after a 'begin' statement. This is because the flow of the program has just started and must not be stopped by a ';'.

The messages in between the braces ({ }) are called comments or in-line documentation. I guess you consider the comments to be 'extra'. Very long programs which include thousands of lines, have already been felt in need of describing certain functions or even complicated functions. In my experiences, I have already met many problems, when refusing to write a program for a long time, and then resuming again writing it! I've made a long time trying to understand what I have done. You must keep it into your mind that comments within the braces are not read or compiled by the compiler/interpreter.

The 'readln' statement is another reserved word for input purposes, which enables the user to input a number or text only i.e.: using the keyboard. But in our case the 'readln' statement is used to input numbers only (letters are accepted but will cause a run-time error because it is not the input we want) and store them in the variables 'Num1' and 'Num2'. This is because both variables are assigned to as integers, and integer variables do not store strings. A run-time error is detected by the OS (Operating System; ex. Windows or Linux) if something goes wrong with the input. Later in the course, you will also learn how to control input and output exceptions - unexpected runtime errors. One last thing on errors is this: there are 2 major error types which are - Runtime Errors and Compilation Errors. Runtime errors are those which occur unexpectedly during the execution of the program, whereas a Compilation error is one which is detected during the compilation process. Note that a decimal number is also considered as a wrong input; a decimal number must not be input, since it is a real number (more on this later).

After the prompts and inputs by the user, follows the addition. i.e.

Sum := Num1 + Num2;

The result of the above statement is the addition of the values stored in variables 'Num1' and 'Num2'. The important thing that you should know is that one cannot make the same statement as follows:

Num1 + Num2 := Sum;

This is another syntax error. It is the fact that transfer of information is from left to right and not from right to left. So, mind not to make this error. The ':=' is called the assignment statement, and should be discussed later on.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:38


Lesson 2 - Variables, Constants and the Assignment Statement


This lesson will cover:

String Variables
Constants and the Assignment Statement


String Variables
Soon, you should learn how to input text by the user using 'string variables'. The following program is written showing an example of a string variable, prompting the user to input his name, whatsoever:

Program Lesson2_Program1;
Var name, surname: String;

Begin
Write('Enter your name:');
readln(name);
Write('Enter your surname:');
readln(surname);
writeln;{new line}
writeln;{new line}
Writeln('Your full name is: ',name,' ',surname);
Readln;
End.
If we take a look at this program, we notice a new variable type : 'String'. Both the name and surname variables are of type string. When the program is run and prompts the user to input his name, the name which is keyed in by the user goes directly to its place in the memory called 'name'. Same occurs to surname. *I'd like to remind you that the variables 'name' and 'surname' are not reserved words, but are used by the programmer as his variables. I could have used 'n' instead of 'name' and similarly 'sname' instead of 'surname'. The two 'idle' writeln's in lines 9 and 10 are used to move on for a new line. In this case, 2 lines are cleared. The next message displays the full name of the user using the above format. If a string variable is required to be displayed on screen, it should be put in between inverted commas and commas if it is concatenated with another message; example:



It could be in the form of: (please note where you must or you must not put the inverted commas following the commas)

*Writeln('Your name is: ',name);

or:

Writeln('Your name is:',name,'. Your surname is ',surname,'.');

*Note that you can even make it this way:

Writeln('Your name is: ',name,'');

BUT you should put the inverted commas properly (underlined) to close the message.



Constants and the Assignment Statement
Apart from variables, there are also items in the program which are referred to as 'constants'. Unlike variables, constants keep their value or string unchanged for the whole program. Here I have made a program, not so much different from the previous one:

Program Lesson2_Program2;
Var
surname: String;
Const {the reserved word 'const'
is used to initialize constants}
name = 'Victor';

Begin
Write('Enter your surname:');
readln(surname);
writeln;
writeln;
Writeln('Your full name is: ',name,' ',surname);
Readln;
End.
In the above program, the constant 'name' is assigned to as 'Victor' and is of type string. However, in other cases, you might have used integer constants (whole numbers), i.e.:

Const

age = 15;

The constant 'age' is a value that could be used whever it is required in a program. Example:

age2 := 15;

age2 := age + 15;

The above example shows an addition of the value of the variable 'age' with the value 15. The value of the constant 'age' remains 15, but the value of the variable 'age2' becomes 30. The assignment statement is not only used for additions, but is also used to assign a variable: text if it is a string variable or a numeric value if it is an integer variable.

Try examine this yourself:

name := 'victor';
age := 15; {also: "age:='15';" BUT in this case, 'age' is an integer variable}
writeln('Name:',name,'. Age:',age,'.');

I conclude lesson 2 with another simple program for you to read and think about:

Program lesson2_Program3;
Var PD, Dname, Cmodel : String;
TotalKM, CostPD, TCostPD, Distance : Real;
{real is a decimal (described later)}

begin
TCostPD := 0;
{note that this is called an 'initialisation'.
It is important to initialise variables to 0
so that it is 'refreshed' from the previous
'rubbish' value in the memory.}
Writeln('This program prompts you to '+
+'input the cost per litre of');
Writeln('the petrol/diesel you spend '+
+'in and the average distance you travel');
Writeln('with your car every week. Then '+
+'the computer calculates the total cost');
Writeln('you spend in fuel every week.');
Readln;
Write('Diesel or Petrol?: ');
Readln(PD);
Write('Name Of Driver: ');
Readln(Dname);
Write('Car Model: ');
Readln(Cmodel);
Write('Cost of Diesel/Petrol: (£) ');
Readln(CostPD);
Writeln('Average distance you travel '+
+'with your car every week: (kilometres) ');
Readln(Distance);
Writeln;
Writeln;
Writeln('Name of Driver:',Dname);
Writeln('Car Model:',Cmodel);
Writeln('Diesel/Petrol:',PD);
Writeln('Average distance covered '+
+'every week: ',Distance:1:2,'Km');
Writeln('Cost of ',PD,' per liter: £',CostPD:1:2,'/litre');
Writeln;
Writeln;
TCostPD := Distance * CostPD;
Writeln('Total cost of ',PD,' per week:'+
+'£',TCostPD:1:2); {note this,}
TCostPD := 0;
Writeln('Total cost of ',PD,' per week:'+
+'£',(Distance * CostPD):1:2); {this}
Writeln('Total cost of ',PD,' per week:'+
+'£',Distance * CostPD); {and this - without ':1:2'}
readln;
End.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:39


































Add this site to your Favourites












Lesson 3 - Special Reserved Words of the CRT Unit: ClrScr(), GotoXy(), etc...


This lesson will cover :
Use of the reserved words
Description of the reserved words
Examples :
Clrscr;
GotoXy(_,_);
Textbackground();
Textcolor();
Readkey;
Delay();
Halt; / Halt()


Use of the Reserved Words

Before I teach you the if statements and for loops, I would like to give you an idea of some pascal functions which are quite useful. This would require your program to include the library 'crt.tpu'. To include a library in the program, one should use the reserved word 'uses', because it is used to call a library of functions and procedures. Here is the program of lesson 2 (program 3) which is better handled and more user-friendly:

Program lesson3_Program1;

Uses Crt; {We will make use of the crt library}

Var PD, Dname, Cmodel : String;
CostPD, TCostPD, Distance : Real;
{real is a decimal (described later)}

Begin
textbackground(brown); {background colour}
ClrScr; {Clear screen with a brown colour.
Try run the program without this!!!}
TextColor(lightgreen); {text colour}
TCostPD := 0;
Writeln('This program prompts you to '+
+'input the cost per litre of');
Writeln('the petrol/diesel you spend in and '+
+'the average distance you travel');
Writeln('with your car every week. Then, '+
+'the computer calculates the total cost');
Writeln('you spend in fuel every week.');
Readkey; {program move on as soon as a key is pressed}
ClrScr;{short for clear screen}
GotoXy(28,3);
{^move to a position on the screen:
x (horizontal), y (vertical)}
Write('Diesel or Petrol? Type p or d: ');
PD := Readkey;
{^as soon as a key is pressed,
it is stored in the variable 'PD'}
GotoXy(30,4);
Write('Name Of Driver: ');
Readln(Dname);
GotoXy(30,5);
Write('Car Model: ');
Readln(Cmodel);
GotoXy(29,6);
Write('Cost of Diesel/Petrol: (£) ');
Readln(CostPD);
GotoXy(8,7);
Writeln('Average distance you travel with '+
+'your car every week: (kilometres) ');
Readln(Distance);
ClrScr;
GotoXy(28,3);
Writeln('Name of Driver:',Dname);
GotoXy(31,4); Delay(500);
Writeln('Car Model:',Cmodel);
GotoXy(32,5); Delay(500);
Writeln('Diesel/Petrol:',PD);
GotoXy(8,6); Delay(500);
Writeln('Average distance covered '+
+'every week: ',Distance:1:2,'Km');
GotoXy(25,7); Delay(500);
Writeln('Cost of ',PD,' per litre: £',CostPD:1:2,'/litre');
Writeln; Delay(500);
Writeln;
TCostPD := Distance * CostPD;
GotoXy(21,10);
Writeln('Total cost of ',PD,' per week:£',TCostPD:1:2);
TCostPD := 0;
GotoXy(21,12);
Writeln('Total cost of ',PD,' per week:'+
+'£',(Distance * CostPD):1:2);
GotoXy(18,14);
Writeln('Total cost of ',PD,' per week:£',Distance * CostPD);
readln;
End.
(if you want to see the difference of the 2 programs then you should run them) What is the difference between this program and the program which is program 3 in lesson 2?

The 'Crt' (short for cathode-ray tube) library has a wide range of functions and procedures that you will use so frequently. Some of them are listed in the table below. There are many similar libraries, such as 'Strings' (you will be learning something on this later) and 'Dos'.



Description of the following Reserved Words
Below is a table of the new words:

Reserved Word

Crt: Yes/No

Description

Clrscr Yes Clears screen
Gotoxy(int,int) Yes Takes the cursor to the pre-defined position
Textbackground(word/int) Yes Background colour
Textcolor(word/int) Yes Colour of text
Readkey Yes Reads a key; Could be assigned to a variable
Delay(int) Yes Waits for the included time(milliseconds)
Halt(parameter) No Program terminates
key (variable type) :

int - integer (-32768 to 32767), word - 0 to 65535.

Example of each


Clrscr: (clear screen)
writeln('When you press enter, the screen would be cleared!');
readln;
clrscr;


Gotoxy(int,int): (Go to position x and y);
gotoxy(10,10);
Writeln('The position is 10 pixels from the left of the screen, and ten pixels');
Writeln('from the top of the screen.');
readln;


Textbackground(word/int): (Background colour);
Textbackground(red); {word - red}
Writeln('Note the difference');
Textbackground(5); {integer - 5}
ClrScr;
Writeln('Note the difference'); Readln;


Textcolor(word/int): (Text colour);
Textcolor(red); {word - red}
Writeln('Text colour');
Textcolor(5); {integer - 5}
Writeln('Text colour'); Readln;


Readkey: (Reads a key-press);
Example 1:

Writeln('Press ANY key!!!');
Readkey;

Example 2:

Writeln('Press ANY key');
Keypress := readkey; {keypress is a DECLARED string variable(can be an integer variable)}
Writeln(Keypress);


Delay(int): (Waits for some time);
Writeln('1');
Delay(1000);{1000 milliseconds}
Writeln('2');
Delay(1000);
Writeln('3');
Readln;


Halt(int): (Program terminates with an exit code);
writeln('Press enter and the program terminates!);
Readln;
Halt(0);

Note that instructions following 'halt' are not executed since the program terminates when halt is encountered.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:40



Lesson 4 - Program Control


This lesson will cover:

The If Statement
If..Then..Else
Nested If Statements
The Repeat-Until Loop
The For Loop
Nested For loops
While-Do Loop

Now, it is time to learn the most important rules of programming: the if statements - decision making, for loops and the repeat-until loop. Almost, these 3 general programming constructs are common in every programming language and you have to make sure that when you have finished reading this lesson, make sure that you have practiced them enough before continuing with learning pascal because they are of outmost importance. If you fall in doubt about these programming constructs, then try to use the forums, describing your problem.

The If Statement

The 'if statement' executes a the proceeding statement(s) conditionally. This means that if an action comes to be true, then the statement(s) proceeding the if statement are executed, else these statements are skipped. It works like this:

If this happens(action), then do this(reaction, if action is true).

OR:

If this happens(action), then do this(reaction, if action is true), else do this(reaction, if action is false).

In Pascal, the 'if statement' should be written as follows:

If conditional expression then code ... ;{if one action}

OR:

If conditional expression then Begin instructions ... End; {if more than one action is required}

Note that you should not use an assignment statement in the 'if' construct, otherwise the compiler will raise a syntax error. I.e.:

Wrong:

If x := 20 then x := x + 1; {the underlined character must be excluded}

Correct:

If x = 20 then x := x + 1; {only an equal sign is used for comparison}

A program is shown below as an example of how the 'if statement' works:

Program lesson4_Program1;
Uses Crt;
Label 1; {this is used with a goto statement}
Var Sel: String;
N1,N2, Total : Real;
YN : Char; {this is a character variable type,
which holds single characters ONLY}
Begin
1:Clrscr;
Total := 0; {always initialise integer/real variables}
GotoXy(4,3);
Writeln('1.Addition');
GotoXy(4,4);
Writeln('2.Subtraction');
GotoXy(4,5);
Writeln('3.Exit');
GotoXy(6,8);
Write('Select: ');
Sel := Readkey;
If Sel = '1' {action} then
Begin {more than one statement}
ClrScr;
Write('Input No.1:');
Readln(N1);
Write('Input No.2:');
Readln(N2);
Total := N1 + N2;
Writeln('Addition: ',N1:2:3,' + ',N2:2:3,' = ',Total:2:3);
Write('Press any key to continue...');
Readkey;
Goto 1;{this leads back to the beginning of the program,
otherwise the program terminates}
End; {Closing the if statement(begin)}
If Sel = '2' then
{note that the assignment statement
is not used within an if statement}
Begin
ClrScr;
Write('Input No.1:');
Readln(N1);
Write('Input No.2:');
Readln(N2);
Total := N1 - N2;
Write('Subtraction: ');
Write(N1:2:3,' - ',N2:2:3,' = ',Total:2:3);
Write('Press any key to continue...');
Readkey;
Goto 1;
End; {Closing the if statement}
If Sel = '3' then
Begin
ClrScr;
Write('Are you sure?(Y/N)');
YN := Readkey;
If YN = 'y' then Halt; {1 action, so no need of Begin..End}
If YN = 'n' then Goto 1; {the goto statement is not
recommended for excessive use}
End;
End.
In the above program, the 'goto' statement is used. So far, it has been a real damage to programs and it has produced unwanted confusions. I strongly suggest you not to use it repeatedly.

-> If..Then..Else

In a normal if statement, the 'reaction' cannot be performed if the condition is not true. But in an if..then..else statement, there is at least one set of statements to be performed. Let's take a look at the example below:

writeln('Who has discovered the land of America?');
Readln(ans);
If (ans = 'Christopher Colombus') then
score := score + 1 {if this is false,}
ELSE
writeln('sorry, you''ve got it wrong!'); {then this is true}
Note that if the 'else' term is included with an if statement, then there should be no semi-colon before the 'else' term; just as seen in the above example.

-> Nested If statements

The previous program have already shown an example of nested if statements. I.e.:

If Sel = '3' then
Begin
ClrScr;
Write('Are you sure?(Y/N)');
YN := Readkey;
If YN = 'y' then HALT; {Nested if statement}
If YN = 'n' then Goto 1;{Another Nested if statement}
End;
A nested if statement, is in the form:

If (this happens) then {if 1}
If (this happens) then {if 2}
(do this) etc...
Else (do this) {if 2}
Else (do this) etc... {if 1}

A nested if statement is an if statement within another if statement, as shown above.

The Repeat-Until Loop
This loop is used to repeat the execution of a set of instructions for at least one time. It is repeated until the conditional expression is obeyed. The following example, shows the model of the 'repeat-until' loop:

Repeat

..(code)

..(code)

..(code)

Until conditional statement;

Here's an example:

Uses Crt;
Var YN : String;

Begin
Writeln('Y(YES) or N(NO)?');
Repeat {repeat the code for at least one time}
YN := Readkey ;
If YN = 'y' then Halt; {Halt - exit}
If YN = 'n' then Writeln('Why not? Exiting...');
Delay(1800); { wait a second plus 800 milliseconds }
Until (YN = 'y') OR (YN = 'n');
End.
See? It's very simple! In the above program, there is a Boolean expression in the 10th line (or). This will be described later on.

The For Loop

The for loop is a sort of repeat-until loop. The for loop, repeats a set of instructions for a number of times. The for loop is in the form:

- If used for only one action:

for {variable}* := {original value} to/downto {final value} do

{code...(for one action)}

- If used for more than one action:

for {variable}* := {original value} to/downto {final value} do Begin

{code...}

{code...}

End;

*Generally, this variable is called the 'loop counter'.

Now, an example of the for loop is shown below, but firstly, you should have an idea of the usefulness of the for loop. Consider the following example:

Without for loop:

Program lesson4_Program2a;
Uses Crt;

Begin
Writeln('for loop'); {somewhat boring writing all this!!!}
Writeln('for loop');
Writeln('for loop');
Writeln('for loop');
Writeln('for loop');
Writeln('for loop');
Writeln('for loop');
Readln;
End.
With for loop:

Program lesson4_Program2b;
Uses Crt;

Var Counter : Integer; {loop counter declared as integer}

Begin
For Counter := 1 to 7 do {it's easy and fast!}
writeln('for loop');
Readln;
End.
Note that the two programs above perform the same function, but which programming style is more useful?

Suppose we have to make a program which designs a small box with some of the characters of the ASCII, obviously the characters which are most likely to make up a simple box.

Without the for loop:

Program Program3a_lesson4;
Uses Crt;

Begin
Gotoxy(25,5);Writeln('+');
Gotoxy(25,6);Writeln('I');
GotoXy(25,7);Writeln('I');
GotoXy(25,8);Writeln('I');
GotoXy(25,9);Writeln('I');
GotoXy(25,10);Writeln('I');
GotoXy(25,11);Writeln('+');
GotoXy(26,11);Writeln('-');
GotoXy(27,11);Writeln('-');
GotoXy(28,11);Writeln('-');
GotoXy(29,11);Writeln('-');
GotoXy(30,11);Writeln('-');
GotoXy(31,11);Writeln('-');
GotoXy(32,5);Writeln('+');
Gotoxy(32,6);writeln('I');
GotoXy(32,7);Writeln('I');
GotoXy(32,8);Writeln('I');
GotoXy(32,9);Writeln('I');
GotoXy(32,10);Writeln('I');
GotoXy(32,5);Writeln('+');
GotoXy(26,5);Writeln('-');
GotoXy(27,5);Writeln('-');
GotoXy(28,5);Writeln('-');
GotoXy(29,5);Writeln('-');
GotoXy(30,5);Writeln('-');
GotoXy(31,5);Writeln('-'); {oh my God!!! Phew!}
Readln; { wait for user to read }
End.
With for loop:

Program Program3b_lesson4;
Uses Crt;

Var Counter : Integer; {loop counter}

Begin
For Counter := 1 to 5 do
Begin
gotoxy(25, 5 + Counter);
Writeln('I');
End;
For Counter := 5 Downto 1 do
Begin {an example of 'downto' instead of
'to', note the 'gotoxy(_,_)'}
gotoxy(32, 11 - Counter);
Writeln('I');
End;
For Counter := 1 to 6 do
Begin
gotoxy(25 + Counter, 11);
Writeln('-');
End;
For Counter := 6 Downto 1 do
Begin
gotoxy(32 - Counter, 5);
Writeln('-');
End;
{--------------The Corners(+)---------------}
Gotoxy(25,5);
Writeln('+');
GotoXy(25,11);
Writeln('+');
GotoXy(32,5);
Writeln('+');
GotoXy(32,11);
Writeln('+');
GotoXy(45,7);
Writeln('Just as simple as the for loop!!!');
Readln;
End.
Again, the two programs above perform the same function.

-> Nested for loops
A nested for loop is similar to that of the nested if statements. A nested for loop is in the form:

for {loop counter} := {original value} to {final value} do {Begin-if required}

{code if any..begin should be included (i.e more than one action)}

for {loop counter} := {original value} to {final value} do {Begin-if required}

{code..if more than one action, include begin in the second for loop}

{End; - if begin is included in the second for loop)}

{code if any..begin should be included in the first for loop}

{End; - if begin is included in the first for loop)}

The nested for loop is rarely used and it may cause problems.

While-Do Loop
This type of loop is executed while the condition is true. It is different from the 'Repeat-Until' loop since the loop might not be executed for at least one time. The code works like this:

While do the following:

instruction 1;

instruction 2;

instruction 3;

etc...

End; {If while-do loop starts with a begin statement}

Example Program on the While-Do loop:

Program Lesson4_Program4;
Uses Crt;

Var Ch : Char;
Begin
Writeln('Press ''q'' to exit...');
Ch := Readkey;
While Ch <> 'q' do
Begin
Writeln('I told you press ''q'' to exit!!');
Ch := Readkey;
End;
End.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:40


Lesson 5 - The CASE-OF Statement
*You should have an idea of the 'if statement' before you proceed with this lesson.

The Simple Case Statement

So far, you have learned how to use an 'if statement'. But in some cases the 'case statement' is preferred to the if statement because it reduces some unnecessary code but the same meaning is retained. The case statement is very similar to the if statement, except in that the it does not accept literal conditional expressions (i.e.: strings) but surprisingly enough, it allows single character conditional expressions. Here is how it works:

Case {variable of type: integer or character ONLY} of

{input statement- within inverted commas if of type char} : {code..}

{input statement- within inverted commas if of type char} : {code..}

...

End; {End Case}

Now you should note the difference and the intelligent use of the case statement over the if statement.
The Program is written using the if statement:

Program Program1a_Lesson5;
Uses Crt;
Label Return; {used respectively with the
goto statement; beware of it}

Var SEL : Integer;
YN : Char;

Begin
Return: Clrscr;
Writeln('[1].PLAY GAME');
WRITELN('[2].LOAD GAME');
WRITELN('[3].MULTIPLAYER');
WRITELN('[4].EXIT GAME');
Writeln('note: Do note press anything except');
Writeln('numbers; otherwise an error occurs!');
Readln(SEL);
If SEL = 1 then
Begin
Writeln('Are you able to create a game');
Writeln('of yourself using pascal??');
Delay(2000);
Goto Return;
End;
If SEL = 2 then
Begin
Writeln('Ahhh... no saved games');
Delay(2000);
Goto Return;
End;
If SEL = 3 then
Begin
Writeln('networking or 2 players?');
Delay(2000);
Goto Return;
End;
If SEL = 4 then
Begin
Writeln('Exit?');
YN := Readkey;
If YN = 'y' then
Begin
Writeln('Nooooooooooooo...');
Delay(1000);
Halt; {EXIT PROGRAM}
End;
If YN = 'n' then
Goto Return;
End;
End.
Now, the next program is written using the case statement and the output
is almost the same.

Program Program1b_Lesson5;
Uses Crt;
Label Return; {use of the goto statement
is not recommended..avoid it}
Var SEL : Integer;
YN : Char;

Begin
Return:Clrscr;
Writeln('[1].PLAY GAME');
WRITELN('[2].LOAD GAME');
WRITELN('[3].MULTIPLAYER');
WRITELN('[4].EXIT GAME');
Writeln('note: Do note press anything except');
Writeln('numbers; otherwise an error occurs!');
Readln(SEL);
Case SEL of
1 : Begin
Writeln('Are you able to create');
Writeln('a game of yourself using pascal??');
Delay(2000);
Goto Return;
End;
2 : Begin
Writeln('Ahhh... no saved games');
Delay(2000);
Goto Return;
End;
3 : Begin
Writeln('networking or 2 players?');
Delay(2000);
Goto Return;
End;
4 : Begin
Writeln('Exit?');
YN := Readkey;
Case YN of {a sort of a nested case statement}
'y' : Begin
Writeln('Nooooooooooooo...');
Delay(1000);
Halt;
End;
'n' : Goto Return;
End;{End Case2}
End;{Close Conditional Expression 4}
End; {End Case1}
End.
This source code had a syntax error and is now corrected (Vaar changed to Var)


The Case-Else Statement
Again this is similar to the if..then..else statement. Study the program below to learn how to use the 'else' term following the 'case statement':

Program Program2_Lesson5;
Uses Crt;
Label Return; { avoid it }
Var YN : Char;

Begin
Return:ClrScr;
Writeln('Exiting?');
YN := Readkey;
Case YN of
'y' : Halt;
'n' : Begin
Writeln('What are you going to do here, anyway?');
Delay(2000);
Halt;
End;
Else
Begin
Writeln('Either press ''y'' for yes');
Writeln('or ''n'' for no.. please try again..');
Delay(3500);
ClrScr;
Goto Return;
End;
End; {CASE}
End. {PROGRAM}
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:41







Lesson 6 - Logical Operators and Boolean Expressions


This lesson will cover:
What are logical operators?
The Logical Operators
AND
OR
NOT
Example of each logical operator:
AND
OR
NOT
The 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...



And
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.



OR
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
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


Example of the AND Operator

Program Lesson6_Program1;
Uses Crt;
Var n1, n2 : string;

Begin
Writeln('Enter two numbers: (''0'' & ''0'' to exit)');
Repeat
Write('No.1: ');
Readln(n1);
Write('No.2: ');
Readln(n2);
If (n1 = '0') AND (n2 = '0') then Halt(0);
Until (n1 = '0') AND (n2 = '0');
End.


Example of the OR Operator

Program Lesson6_Program2;
Uses Crt;
Var n1, n2 : String;

Begin
Writeln('Enter two numbers: (''1'' & ''2'' to exit)');
Repeat
Write('No.1: ');
Readln(n1);
Write('No.2: ');
Readln(n2);
If (n1 = '1') OR (n2 = '2') then Halt;
Until (n1 = '1') OR (n2 = '2');
End.


Example of the NOT Operator
Program Lesson6_Program3;
Uses Crt;
Var n1 : String;

Begin
Writeln('Enter two numbers: (any number except 0 to exit)');
Repeat
Write('No.1: ');
Readln(n1);
If not(n1 = '0') then Halt;
Until not(n1 = '0');
End.


The Boolean Expressions

The boolean expressions are the terms 'true' and 'false'. These are simply similar to 1's (for true) and 0's(for false). They describe an expression whether it is false or true. The variable types over boolean expressions is the 'boolean' type. Example:

Var bool : Boolean;

Example Program:

Program Lesson6_Program4;
Var quit : Boolean;
a : String;

Begin
Repeat
Write('Type ''exit'' to quit:');
Readln(a);
If a = 'exit' then quit := True else quit := False;
If quit = True then Halt;
Until quit = True;
End.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:41



Lesson 7 - 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

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:

Program Lesson7_Program1a;
Uses Crt;
Var Counter : Integer;
Begin
textcolor(green);
GotoXy(10,5);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
GotoXy(10,6);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
GotoXy(10,7);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
GotoXy(10,10);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
Readkey;
End.
Now have a look at the next program which uses a procedure:

Program Lesson7_Program1;
Uses Crt;
Procedure DrawLine;
{This procedure helps me to
avoid the repetition of steps [1]..[3]}
Var Counter : Integer;

Begin
textcolor(green);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
End;
Begin
GotoXy(10,5);
DrawLine;
GotoXy(10,6);
DrawLine;
GotoXy(10,7);
DrawLine;
GotoXy(10,10);
DrawLine;
Readkey;
End.
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 developement process. The whole Software Developement 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. The new program is as follows:

Program Lesson7_Program2;
Uses Crt;
Procedure DrawLine(X : Integer; Y : Integer);
{the decleration of the variables in brackets are called
parameters or arguments}
Var Counter : Integer;
{normally this is called a local variable}
Begin
GotoXy(X,Y); {here I use the parameters}
textcolor(green);
For Counter := 1 to 10 do
Begin
write(chr(196));
End;
End;
Begin
DrawLine(10,5);
DrawLine(10,6);
DrawLine(10,7);
DrawLine(10,10);
Readkey;
End.
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.

Program Lesson7_Program3;
Uses Crt;
Var
UName, USurn, UCoun, UMail : String[50];
{These var's are global because
they are used by more than one procedure}
TxtB, TxtC, i : Integer;
InfoCor : Boolean;
Procedure EnterUserInfo(TxtCol : SmallInt; TxtBck : SmallInt);
Begin
textcolor(TxtCol);
textbackground(TxtBck);
ClrScr;
Write('Your Name: ');
Readln(UName);
Write('Your Surname : ');
Readln(USurn);
Write('Country : ');
Readln(UCoun);
Write('E-Mail Address: ');
Readln(UMail);
Write(' Thank you for entering your personal information!!');
Readkey;
End;
Procedure ConfirmationField(TxtCol : SmallInt;
TxtBck : SmallInt);
Var
YN : Char; { a local variable }

Begin
textcolor(TxtCol);
textbackground(TxtBck);
ClrScr;
Writeln('Your Name: ',UName);
Writeln('Your Surname : ',USurn);
Writeln('Country : ',UCoun);
Writeln('E-Mail Address: ',UMail);
Writeln;
Writeln;
Writeln('This is a confirmation field. Please verify that');
Writeln('your information is correct!');
Writeln;
Write('Is your personal information all correct? [Y/N] ');
Repeat
YN := Readkey;
Case YN Of
'N' : InfoCor := False;
'Y' : InfoCor := True;
End;
Until (YN = 'N') or (YN = 'Y');
End;
Begin { main program }
InfoCor := True;
ClrScr;
TextBackground(cyan);
TextColor(green);
Write('A list of colours is being displayed...');
For i := 1 to 16 do
Begin
Case i Of
16 : Begin
TextBackGround(white);
End;
End;
textcolor(i);
Writeln(i,': This is Colour No.',i);
End;
TextBackGround(black);
TextColor(white);
Write('Please, put into your mind your favourite colour. ');
Write('When you are ready press any key...');
Readkey;
ClrScr;
Write('Enter your favourite text colour: (only numbers) ');
Readln(TxtC);
Write('Enter your favourite background colour : __');
Readln(TxtB);
Writeln;
Writeln;
Write('Now, you must enter your personal information. ');
Write('Hit any key to continue...');
ClrScr;
EnterUserInfo(TxtC,TxtB);
ConfirmationField(TxtC,TxtB);
If InfoCor = False then
Repeat
Writeln;
Writeln('You verified that your information is, '+
+'for some reason, incorrect.');
Writeln('You are now going to re-enter your '+
+'correct information. Hit any key..');
Readkey;
EnterUserInfo(TxtC,TxtB);
ClrScr;
ConfirmationField(TxtC,TxtB);
Until InfoCor = True;
End.


The Variable Parameter

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

Here is an example of how to use a varaible parameter and what's its purpose:

Program VAR_PARAM_EXAMPLE;

Procedure Square(Index : Integer; Var Result : Integer);
Begin
Result := Index * Index;
End;

Var
Res : Integer;

Begin
Writeln('The square of 5 is: ');
Square(5, Res);
Writeln(Res);
End.


Functions

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.

Example of a program using a function is seen below:

Program Lesson7_Program4;
Uses Crt;

Var SizeA, sizeB : Real;
YN : Char;
unitS : String[2];
Function PythagorasFunc(A:Real; B:Real) : Real;
{The pythagoras theorem}
Begin
PythagorasFunc := SQRT(A*A + B*B);
{Output: Assign the procedure name to the value.
If you forget to assign the function to the value,
you will get a trash value from the memory}
End;
Begin
Repeat
Writeln;
Write('Enter the size of side A : ');
Readln(sizeA);
Write('Enter the size of side B : ');
Readln(sizeB);
Repeat
Write('metres or centimetres? Enter : [m or cm] ');
Readln(unitS);
Until (unitS = 'm') or (unitS = 'cm');
Writeln(PythagorasFunc(sizeA,sizeB),' ',unitS);
Writeln;
Write('Repeat? ');
YN := Readkey;
Until (YN in ['N','n']);
End.
Program code is updated (24th August 2005)

Details: the function returns a real instead an integer; if..then..halt removed; repeat..until condition is also changed

Program Errors corrected thanks to Harold Smith
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:42








Lesson 8 - BGI Graphics

If you feel that Graphics is not important for you than skip to lesson 9 (File Handling). BGI Graphics is an advanced topic and need not be explicitly covered. It depends on your preference. Graphics lovers will surely be interested in this topic.


This lesson will cover:

Introduction to Graphics

Initialising Graphics

Graphics Statements

Advanced Graphics Statements

One might require the file egavga.bgi during this graphics tutorial. Click here if you don't have this file (or it is corrupt) and place it in the BGI directory in the Turbo Pascal installation folder.

COPYRIGHT NOTICE: It is assumed that you have bought Turbo Pascal once you have downloaded this file. This file is owned by its respective owners (Borland Company).

IMPORTANT DISCLAIMER: You will download and use this file at your own risk. We don't take any responsability for any damage whatsoever.

Introduction to Graphics

Graphics is a method of representing output material in a more complex, user-friendlier way than the crt. The crt is one of the few turbo pascal units or libraries. Turbo Pascal has a graphics library with which you can represent output material. Before you can call turbo pascal's graphics procedures and functions, you have to include the Graph unit in the Uses section (Eg. Uses Crt, Graph, Overlay, Printers etc...).



Initialising Graphics

Unlike the crt, graphics should be initialised, and it should be initialised at the beginning of the program or just before you start using graphics procedures and functions. This will help the compiler to link to the graphics file. Usually, the default graphics routines used by the Turbo Pascal's compiler is the EGAVGA.BGI. When you initialise the graphics, automatically your computer will use this file to output graphics material.

The famous piece of code, used to initialise the graphics in Turbo Pascal (NOT Dev-PASCAL! Mind you! Dev-Pascal does not accept the graph driver and graph mode be declared as integer values. If you have Dev-Pascal then change all the variable types of the 'gd,gm' to smallint! 'Gd, Gm : Smallint;') is shown below:

Program Lesson8_Program1;
Uses Crt,Graph;
Var GraphicsDriver, GraphicsMode,
ErrCode : Integer;
{two var's are needed for initialisation}
Begin
Writeln('Initialising Graphics, please wait...');
GraphicsDriver := Detect;
InitGraph(GraphicsDriver, GraphicsMode,'');
{IMPORTANT, read the following or
otherwise graphics will not work!! ;)}
(*between the inverted commas,
type in the path of the graphics BGI file
(usually 'C:\TP\BGI'),
OR
change the dir in the file menu (PRESS Alt+F)
and roll down your mouse pointer to the 'change dir'
menu; then either type the path to the BGI file,
or go to C: -> TP -> BGI*)
ErrCode := GraphResult;
If GraphResult <> grOK then { <> means 'not equal to' }
Begin
ClrScr;
Writeln('Graphics error occured: ',
GraphErrorMsg(ErrCode));
Writeln('If a file not found error is displayed above');
Writeln('then, change the dir from the current');
Writeln('location to C:\ -> TP -> BGI, '+
+'from the file menu!');
Readln;
Halt(1);
End Else
Begin
Randomize;
SetColor(Random(15) + 1); {Set text colour}
{Output text at 20 pixels from the top of the screen,
and 20 other from the left side of the screen.}
OutTextXY(20,20,'Welcome to the new generation
of Pascal Programming:');
OutTextXY(20,30,'Pascal Graphics!!');
OutTextXY(25,70,'You will learn more
graphics procedures and');
OutTextXY(25,80,'functions, later in this lesson :-)');
Readln;
End;
CloseGraph;
End.
The program above uses the statement:

InitGraph(GraphicsDriver,GraphicsMode,'');

You may find it strange what does the two inverted commas (' ') mean, in the statement shown above. It is the path which redirects the turbo pascal linker to the graphics BGI file. So, in between the inverted commas, you have to enter the path to the graphics driver, or otherwise you will receive an error message. You may leave it blank and let the compiler himself find the BGI file automatically (in the current directory), but when I did this, it never worked!! :-) If you did not change the folder address (C:\TP) after the installation, you may use this path: 'C:\TP\BGI', like this:

InitGraph(GraphicsDriver,GraphicsMode,'C:\TP\BGI');

After the 'initgraph()' statement, you should inform the user with the problem concerning a graphics error, if the EGAVGA.BGI file is not found. Hope it does not happen to you :-) !! Then, if the user is error-free, then you can start calling graphics procedures and functions from the graphics library (graph). In the example program above, I used only the 'OutTextXY()' statement and the 'SetColor()' statement. However there are many more functions other than these!

At the end of each graphic statments, you have to close the graphics section, by using the 'CloseGraph' statement.

USEFUL INFORMATION:

Note that the screen resolution is 640 by 480 pixels (compare this with that of the CRT! - the CRT has a screen resolution of 80 by 25 'pixels' only!)



Graphics Statements
Learning graphics, is very simple - all you have to learn is how to initialise graphics and then use your graphics knowledge to display whatever you want! Now, the following statements are found in the graph unit which are used to output graphically. Then I will show you an example program using the graph unit, to demonstrate how the statments below work:

Statement
Description
Example
SetBkColor(colour/colour code);
This will set the background colour of the screen to the colour indicated within the parenthesis.
SetBkColor(Brown); or

SetBkColor(6);

SetColor(colour/colour code);
This will set the text colour to the colour indicated within the parenthesis.
SetColor(Green); or

SetColor(2);

OutText('');
This statement will output the text indicated within the inverted commas.
OutText('BGI Graphics');
OutTextXY(X,Y,'');
This is very similar to the one before this statement, except in that it move the cursor position indicated as shown
OutTextXY(300,100,'BGI Graphics');
MoveTo(X,Y);
Takes the cursor position to that indicated in the brackets.
MoveTo(GetMaxX,GetMaxY);
PutPixel(X,Y,colour/colour code);
Places a small pixel at X pixels left and Y pixels down.
PutPixel(50,128,Cyan); or

PutPixel(50,128,5);

Line(X,Y);
Draws a line from the position of the cursor to X, Y.
Line(30,111);
LineTo(X1,Y1,X2,Y2);
Draws a line from X1, Y1 to X2, Y2.
LineTo(0,0, GetMaxX,GetMaxY);
Rectangle(X1,Y1,X2,Y2);
Draws a rectangle from point X1, Y1 to point X2, Y2.
Rectangle(30,50,25,45);
Circle(X,Y,Radius);
Draws a circle from point X, Y with a radius length of Radius.
Circle(GetMaxX Div 2, GetMaxY Div 2, 100);
Ellipse(X,Y,P,Q,HorR,VerR);
Draws an ellipse with centre point X,Y starting from P degrees to Q degrees, with a horizontal radius of HorR and a vertical radius of VerR.
Ellipse(300,160, 0,360,60,40);
Arc(X,Y,P,Q,Radius);
Draws an arc starting from X,Y starting with an angle of Pdegrees and ending with an angle of Q degrees, with radius Radius.
Arc(100,200,0,90,70);
PieSlice(X,Y,P,Q,Radius);
Similar to the arc();, except in that it is a pie.
PieSlice(90,150,20,95,100);
Bar(X1,Y1,X2,Y2);
Draws a bar filled with the current colour, starting from X1, X2 to Y1, Y2.
Bar(50,100,150,500);
Bar3D(X1,Y1,X2,Y2, Depth,ThreeD_Top);
Similar to the one above, except in that it is 3D. The last parameter indicates the 3D top of the 3D Bar.
Bar3D(120,100,250, 150,50,TopOn);
Below is a really fantastic graphics program, which uses most of the graphics statements above. If you could not run the program because of a graphics error, then send me an e-mail and I will help you how to do so. But first of all, try the path of the BGI file as shown above. You can download or copy this program:

Program Lesson8_Program2;
{Author: Victor J. Saliba}
{victorsaliba@hotmail.com}
{Website: [ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذا الرابط]
Uses Crt,Graph;

Var Gd, Gm,
Radius, Grow, IncP, IncQ : Integer;
DecrP, DecrQ : Boolean;
Begin
Gd := Detect;
InitGraph(Gd, Gm, '');
{Do not forget to change the dir path}
{Try C:\TP\BGI}
if GraphResult <> grOk then
Halt(1);
Randomize;
SetColor(Random(15)+1);
{In the following loop, 600 circles:
circles with different radii are
drawn. Everytime the loop is repeated,
the radius increases by
one, and thus the circle becomes +1
larger than the previous one.}
For Radius := 1 to 600 do
Begin
Circle(GetMaxX Div 2, GetMaxY Div 2, Radius);
Delay(1);
End;
ClearViewPort;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,'Prepare for another one...');
Delay(1000);
ClearViewPort;
Grow := 0;
{The ellipse loop, is similar to the one
above except in that the vertical
radius increases by 1}
For Radius := 1 to 600 do
Begin
Inc(Grow);
Ellipse(GetMaxX Div 2,
GetMaxY Div 2, 0, 360,
GetMaxX Div 2, Radius + Grow);
Delay(1);
End;
ClearViewPort;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,'Now what?'); Delay(1000);
ClearViewPort;
{Here's another graphics invention of mine! Similar
to the first one, but using two circles positioned
on both edges of the screen,
continously increasing in size}
For Radius := 1 to 600 do
Begin
Circle(GetMaxX Div 2, 0, Radius);
Circle(GetMaxX Div 2, GetMaxY, Radius);
Delay(2);
End;
Delay(1000);
ClearViewPort;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,'What are you lookin'' at?');
Delay(1000);
ClearViewPort;
Grow := 0;
{Below is a simple rectangle loop,
which inreases its size by 1
(increment x1,y1 and decrement x2,y2)}
For Radius := 1 to 600 do
Begin
Inc(Grow);
Rectangle(((GetMaxX Div 2) - Radius) - Grow,
((GetMaxY Div 2) - Radius)- Grow,
((GetMaxX Div 2) + Radius) + Grow,
((GetMaxY Div 2) + Radius) + Grow);
Delay(2);
End;
ClearDevice;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,
'Rotor of a helicopter thorn apart..');
Delay(2000);
ClearDevice;
{The demonstration below shows 4 rotating sectors,
moving horizontally,
and 4 other sectors in the opposite direction}
IncP := 0; IncQ := 0;
For Radius := 1 to 320 do
Begin
Inc(IncQ, 1);
Inc(IncP, (IncQ Div 2));
PieSlice(GetMaxX Div 2 + IncP,
GetMaxY Div 2, 0 + IncP, 2 + IncP, 240);
PieSlice(GetMaxX Div 2 + IncP,
GetMaxY Div 2, 180 + IncP, 182 + IncP, 240);
PieSlice(GetMaxX Div 2 + IncP,
GetMaxY Div 2, 88 + IncP, 90 + IncP, 240);
PieSlice(GetMaxX Div 2 + IncP,
GetMaxY Div 2, 268 + IncP, 270 + IncP, 240);
PieSlice(GetMaxX Div 2 - IncP,
GetMaxY Div 2, 45 + IncP, 47 + IncP, 240);
PieSlice(GetMaxX Div 2 - IncP,
GetMaxY Div 2, 135 + IncP, 137 + IncP, 240);
PieSlice(GetMaxX Div 2 - IncP,
GetMaxY Div 2, 225 + IncP, 227 + IncP, 240);
PieSlice(GetMaxX Div 2 - IncP,
GetMaxY Div 2, 315 + IncP, 317 + IncP, 240);
Delay(10);
ClearDevice;
If IncP >= 220 then
Dec(IncQ, 4);
End;
ClearViewPort;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,'Ah, now it is repaired..');
Delay(2000);
ClearDevice;
{The next demonstration shows a fantastic
rotor-like movement animation}
{Do not be amazed by the coding below - it's very simple}
{To make such animation, all you
have to do is to make a sector rotating
by simply increasing its degrees and clear the old one}
IncP := 0; IncQ := 0;
For Radius := 1 to 320 do
Begin
Inc(IncQ, 1);
Inc(IncP, (IncQ Div 4));
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 0 + IncP, 2 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 180 + IncP, 182 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 90 + IncP, 92 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 270 + IncP, 272 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 45 + IncP, 47 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 135 + IncP, 137 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 225 + IncP, 227 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 315 + IncP, 317 + IncP, 240);
Delay(10);
ClearDevice;
End;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(220,GetMaxY Div 2,'The chop is on its way!');
Delay(1000);
ClearDevice;
Randomize;
IncP := Random(GetMaxX) + 1;
IncQ := 0;
DecrP := False;
DecrQ := False;
For Radius := 1 to MaxInt do
Begin
If not(DecrP) then Inc(IncP,3) Else
If DecrP then Dec(IncP,3);
If not(DecrQ) then Inc(IncQ,3) Else
If DecrQ then Dec(IncQ,3);
SetColor(LightCyan);
{Now FillCircle! - same radii (20)}
FillEllipse(IncP,IncQ, 20, 20);
Circle(IncP, IncQ, 40);
PieSlice(IncP, IncQ, 0 + (IncP * 80),
6 + (IncP * 80), 100);
PieSlice(IncP, IncQ, 180 + (IncP * 80),
186 + (IncP * 80), 100);
PieSlice(IncP, IncQ, 90 + (IncP * 80),
96 + (IncP * 80), 100);
PieSlice(IncP, IncQ, 270 + (IncP * 80),
276 + (IncP * 80), 100);
If IncP > GetmaxX then DecrP := True Else
If IncP < 0 then DecrP := False;
If IncQ > GetmaxY then DecrQ := True Else
If IncQ < 0 then DecrQ := False;
Delay(10);
ClearDevice; {Try to remove this :-)}
If KeyPressed then Break;
End;
ReadKey;
SetTextStyle(SansSerifFont,HorizDir,1);
SetUserCharSize(2,1,5,1);
OutTextXy(12,20,'Was it fun? Ehh???');
SetUserCharSize(1,1,2,1);
SetColor(LightCyan);
OutTextXY(35,190,'Now you can try to do it yourself.');
OutTextXY(20,240,'If you have any comments then send');
OutTextXY(180,300,'me an e-mail to');
OutTextXY(120,400,'victorsaliba@hotmail.com');
Readln;
CloseGraph;
End.
Have you successfully ran the program above? How's it? There are many different animations! I used some statements which you are'nt still familiar with. But, sooner or later, you will get to know how to use them and know their function.



Advanced Graphics Statments

There are many other graphics procedures and functions, which do not draw graphic objects, but manipulate the format of the graphic objects you draw. For example, if I want to fill a circle with a colour, it's like this:

Circle(GetMaxX Div 2, GetMaxY Div 2, 60);
FillEllipse(GetMaxX Div 2, GetMaxY Div 2, 60, 60); {Note the radii}

The command 'FillEllipse' will fill the circle with the current colour. Take a look at the table below:

Statement
Description
Example
SetFillStyle(Pattern, Colour);
Changes the style of the filling of an object. It can only be used with bar/bar3d and pieslice. It has 13 different fill styles.
SetFillStyle(XHatchFill,LightCyan);

OR

SetFillStyle(10,3);

SetUserCharSize(MultX, DivX, MultY, DivY);
Display the text in a larger or smaller size.
SetUserCharSize(5,1,3,1);
SetTextStyle(Font, Direction, Size);
Display text using the font style, orientation and size indicated as shown.
SetTextStyle(TriplexFont, HorizDir,2);
ClearViewPort;
Clears the active screen page.
ClearViewPort;
GetDriverName
Get the driver name being currently used.
OutText('Using driver:' + GetDriverName);
It is useful to try to run the program below yourself so as to understand the meaning of several reserved words included in certain functions, such as:

SetTextStyle(SansSerifFont,VertDir,2);

OR

Bar3D(40,50,60,100,50,TopOn);

Program Lesson8_Program3;
Uses Graph;
Var Gd, Gm: Integer;

Begin
Gd := Detect;
InitGraph(Gd, Gm, ' ');
if GraphResult <> grOk then Halt(1);
SetTextStyle(TriplexFont, HorizDir, 1);
SetUserCharSize(2,1,10,1);
SetTextJustify(CenterText, CenterText);
OutTextXY(GetMaxX Div 2,200,'Is it big enough?');
readln;
ClearViewPort;
SetTextStyle(SmallFont, VertDir, 5);
SetTextJustify(LeftText,CenterText);
OutTextXY(200,250,'VertDir (vertical) Direction.
Do you understand now??');
Readln;
ClearViewPort;
Bar3d(150,80,450,330,50,TopOn);
SetTextStyle(SansSerifFont, HorizDir, 2);
SetColor(LightRed);
OutTextXY(130,410,'BAR3D(150,80,350,330,50,TopOn);');
Readln;
ClearViewPort;
Bar3d(150,80,450,330,50,TopOff);
SetColor(LightGreen);
OutTextXY(130,410,'BAR3D(150,80,450,330,50,TopOff);');
Readln;
ClearViewPort;
OutTextXY(130,380,'SetFillStyle(BkSlashFill,7);');
SetFillStyle(BkSlashFill,7);
Bar3d(150,80,450,330,50,TopOn);
OutTextXY(130,410,'BAR3D(150,80,450,330,50,TopOn);');
Readln;
ClearViewPort;
OutTextXY(130,380,'SetFillStyle(XHatchFill,6);');
SetFillStyle(XHatchFill,6);
Bar3d(150,80,450,330,50,TopOn);
OutTextXY(130,410,'BAR3D(150,80,450,330,50,TopOn);');
Readln;
CloseGraph;
End.
This is just the beginning of the graphics adventure. If you would like to be a guru in graphics, just keep on practicing and produce only graphical programs.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:42













Lesson 9 - File Handling


This topic deals on how to:

Read from a File
Create and Write to a File
Append data to an existing File
Delete Files
Use the '{$I-},{$I+}' compiler directive
Create and Remove Sub-Directories
FileSize() - return the size of the file in bytes





Intro

A file contains data which is saved in the hard disk. You can only view a file from the hard disk by using an operating system. A can contain text data which is used by word processors. Many text files are saved in the hard disk with the extensions: *.txt and *.doc. The file with extension of *.txt, means that it is created by the Microsoft Notepad. Whenever you use the Microsoft Notepad and save a text file, this saved file is created and written on to the hard disk. However, some programs have various formats on how to maintain the text - such as justification, font, font colour. So many files contain more data other than text data! The program itself uses various techniques to create a file in such a way that, when it is being read again it can read (using programming skills, etc..) if it is justified, its font style etc..

The program that is able to read the file of extension *.xxx, is called the viewer of that file!

Before you go on with files, please make sure that you have enough hard disk space for our experiments, since I am going to demonstrate to save a file to the hard disk!! :) Only a few kb, however..



Read from a File (file input)

Reading a file in pascal is very easy. Note that there are no reserved words in Pascal that are used to to read or write to a file. We used the 'usual words': readln() and writeln(); Here's the technique of how to read a text file (only):

Program Lesson9_Program1;
Var UserFile : Text;
FileName, TFile : String;
Begin
Writeln('Enter the file name '+
+'(with its full path) of the text file:');
readln(FileName);
{A .txt file will be assigned to a text variable}
Assign(UserFile, FileName + '.txt');
Reset(UserFile); {'Reset(x)' - means open the file x}
Repeat
Readln(UserFile,TFile);
Writeln(TFile);
Until Eof(UserFile);
Close(UserFile);
Readln;
End.
Program code is updated (13th June 2006)

Details: the Assign()function on line 10 has been changed from Assign(UserFile, FileName, + '.txt') to Assign(UserFile, FileName + '.txt') because the comma ',' after 'Filename' is a syntax error.

Program Errors corrected thanks to James McNamara

I think that it is worth to take a look at various important lines of the program. A new variable of type: 'Text' is new to you, and this should be used whenever you are going to edit a text file! The variable 'FileName' is required to link to the file indicated by the user. The 'assign' statement is used to declare 'FileName' + '.txt' to a text file, so that the file could be opened, using - 'Reset()'. To read from the first line to the very last line of the file, you should use the repeat-until loop, ending the loop with : 'Until Eof(textfile);', which means: 'Until the End Of File [eof]'.



Create and Write to a File (file output)

The following program is an example of how to create-and-write or overwrite a file:

Program Lesson9_Program2;
Var FName, Txt : String[10];
UserFile : Text;
Begin
FName := 'Textfile';
Assign(UserFile,'C:\'+FName+'.txt'); {assign a text file}
Rewrite(UserFile); {open the file 'fname' for writing}
Writeln(UserFile,'PASCAL PROGRAMMING');
Writeln(UserFile,'if you did not understand something,');
Writeln(UserFile,'please send me an email to:');
Writeln(UserFile,'victorsaliba@hotmail.com');
Writeln('Write some text to the file:');
Readln(Txt);
Writeln(UserFile,'');
Writeln(UserFile,'The user entered this text:');
Writeln(UserFile,Txt);
Close(UserFile);
End.
In the above program, I am using the 'writeln()' statement so that I write to the file I have previously assigned to. Note that, since I am using writeln(), there is no output to the screen, it goes to the file I initialised.

To check exactly what has just been written to this file, go to C:\, and see if there is a file named:

Textfile.txt. Open it and see what does it contain!! :-). If you had problems, or if you did not have a way out, then in the start menu go to:

start -> find -> files or folders => look in drive C: and enter 'textfile.txt' in the text box; double click on it.



Append text to an existing File
Writing to an existing file, means, open a file and add extra data, but not overwrite the file. Some beginner programmers do not actually understand how to, not overwrite a file with data they would like to input. This is the common problem:

Var UFile : Text;
Begin
Assign(UFile,'C:\ADDTEXT.TXT');
ReWrite(UFile);
Writeln(UFile,'How many sentences, '+
+'are present in this file?');
Close(UFile);
End.
Copy the program above in a text file editor and save it in the turbo pascal bin folder as 'example.pas'. Open Turbo Pascal, and run this program for two times or more. Close Turbo Pascal and find the file named 'addtext.txt'. Is there more than one sentence?

A new reserved word which works with files, can complete our problem. The new reserved word is 'append(f)', where f is a variable of type text. This can be done by simply change the 'Rewrite(UFile)' to 'append(UFile)', and the text file is not overwritten, but appended! The above program changes to the following:

Var UFile : Text;
Begin
Assign(UFile,'C:\ADDTEXT.TXT');
Append(UFile);
Writeln(UFile,'How many sentences, '+
+'are present in this file?');
Close(UFile);
End.
Run the program two times or more to see the change...



Delete Files

In Pascal, the reserved word used to delete files from the hard this is the 'Erase(f)' where f is a variable of any data type. This means that 'f' could be both file and text variable type. Note, that the file you are about to delete is not taken in the recycle bin, but it is directly kicked off the hard disk!!

Unlike any other file functions, the erase() function does not open the file to delete it, so you don't need to apply a 'close(...)' after erase().

Example Program:

Var UFile : Text; { or it could be of 'file' type}
Begin
Assign(UFile,'C:\ADDTEXT.TXT');
Erase (UFile);
End.


The '{$I-},{$I+}' compiler directives
Compiler directives are used to adjust the compiler settings during the compilation process, so that programmers can control their program in their preferrable way. Compiler directives are declared using the symbols '{', '$' and '}'. Note that the braces are used for comments, but still, '{$something}' is taken to be a directive. Instead of 'something' the programmer should replace it by a known directive (a letter). Now, the '{$I}' is used to tell the compiler not to take into consideration I/O errors including file operations. Say, for example, if a file is trying to be opened, but it does not exist, the program does not stop from executing and halts with a runtime error, saying: 'File not found' or whatever boring error! Make sense? So, if you apply this compiler directive before you open the file, either for writing or reading, I/O errors will not cause the program to raise a runtime error forced halt.

Now, I must teach you how to use this compiler directive by letting you know where to put it and how to cater with the error that might occur. It must be placed where you are going to open or delete a file. ie.:

{$I-}
action on file...
{$I+} { enable the i/o error check again }
It is important to enable again the i/o error check, after you have disabled it, so that any unknown future errors would be automatically encountered. However, if you think that it would be better to disable i/o error checking for the entire program, you can! Just apply the directive at the beginning and that's it but it is not recommended since during the runtime of your program you will not be notfied of IO errors that might occur!! After you control a file action, you must check whether an error has occured or not by using the system function 'IOResult', which returns IO error information. This is a typical traditional exception handling. You should test for errors using the statement:

If (IOResult <> 0) then ...

The IOResult function should be explicitly used after an IO error check so that it will automatically clear the error flag of the system otherwise, the IO error cause a 'mutation' to other IO processes resulting into a runtime error. You are not required to grasp each word perfectly from what I said. The most important thing is that you include that statement if the IO directives are to be used and everything would be fine. Now I should show you how to use it by an example:

Program Lesson9_Program3;
Uses Crt;
Var t : Text;
s : String;

Begin
Assign(t,'C:\ABC.DEF');
{$I-} { disable i/o error checking }
Reset(t);
{$I+} { enable again i/o error checking - important }
If (IOResult <> 0) then
Begin
Writeln('The file required to be opened is not found!');
Readln;
End Else
Begin
readln(t,s);
Writeln('The first line of the file reads: ',s);
Close(t);
End;
End.
If the required file is found the IOResult returns a 0 value, meaning no errors; ELSE if not found (IOResult returns a non-0 value) display an error message!

IMPORTANT: if the file is successfuly found, the file is opened and you should close it as shown in the program above. However, if it is not found, the common sense says that it couldn't be opened because it is not found!?! So, you should not include a 'close(..)' after it is not opened and this is done conditionally as shown in the example. Study carefully my program and run it several times with good paths and non-good ones to see well the difference and how does the compiler directive works!!

Hopefully, the compiler directive could be applied to different functions, similarly as in the program above. You can use it with 'rewrite()', 'append()', 'erase()', and also 'FSearch()'. You can try as many programs as you wish and hopefully practicing the IO directive. I know, that this directive is somewhat complex and hard to be understood but undoubtedly useful! I am there waiting for your e-mails, if you think that your program does not work properly when using this directive.



Create and Remove Sub-Directories
In Pascal, there are functions with which you can either create or remove a directory from the hard disk. To create a directoy, we use the function 'createdir(c)' where 'c' is of type PChar. Ohh, that's sound kinky... What the heck does PChar means? Hehe, PChar is a pointer variable which holds the address of a dynamic variable of a specified type. It is sort of a pointer for characters. Before you create the directory, however, you have to check if the directory exists, else a runtime error shows up fiddling in front of your monitor!! :-) The directory is created as follows:

NewDir := FSearch('C:\Pascal Programming',GetEnv(''));
if NewDir = '' then CreateDir('C:\Pascal Programming');
From only two lines of code, we have been able to create a dir. But, before you go experimenting on your own, you should strictly read the following documented example: (I will explain in detail the 'FSearch()' function)

Program Lesson9_Program4;
Uses WinDos, Dos;
{ note the inclusion of the 'windos.tpu' library }

Var NewDir : PathStr;
{ for searching the dir and create a new one,
if it does not exist }
F : Text;

Begin
{ search for the dir }
NewDir := FSearch('C:\Pascal Programming', GetEnv(''));
{ create a new one, if it does not exist }
if NewDir = '' then CreateDir('C:\Pascal Programming');
Assign(F,'C:\Pascal Programming\pprogramming.txt');
{$I+} ReWrite(F); {$I-} { disable and enable back again
i/o error checking }
{ write to text file }
Writeln(F,'http://pascal-programming.info/');
{$I+} Close(F); {$I-}
End.
The variable type, 'PathStr', is new to you, and this is a variable defined in the 'dos.tpu' library. So, you have to include the 'dos' library at the beginning of your program. The 'FSearch()' function is implemented also in the windos library to search in a dir list. So, it was useful in our program and any other program to search if the dir exists or not. It's not important to know exactly what does 'GetEnv()' mean, which is found in the second parameter of FSearch(). The Borland Turbo Pascal reference says that its function is to return the value of the specified function. Note that 'FSearch()' is implemented in the 'dos.tpu' library, while CreateDir()' is implemented in the 'windos.tpu' library.

To remove a directory, it is quite simple. Just add 'remove()' at the end of your program! :-) Your operating system will automatically erase the dir if it exists. It doesn't matter if you 'remove' a dir which does not exist. Hopefully, no runtime errors, will show up! The 'remove(Pch)' function is also implemented in the windos library, where 'Pch' is a variable of type PChar, again.



FileSize() - Return the value of a file in bytes
Now, for the fun stuff!! To return the size of a file, simply declare a variable of 'longint' type, and assign it to the filesize of the file. A file variable of type byte, should be assigned to the file which you would like to return its size, using 'assign()'.

Program Lesson9_Program4;
Var f : file of byte; { file var of type byte }
sz : longint; { var for the size }

Begin
Assign(f,'C:\anyfile.txt');
{$I-} Reset(f); {$I+}
f (IOResult <> 0) then
Begin { file found? }
Writeln('File not found.. exiting');
Readln;
End Else
Begin
{ Return the file size in Kilobytes }
sz := round(FileSize(f)/1024);
Writeln('Size of the file in Kilobytes: ',sz,' Kb');
Readln;
Close(f);
End;
End.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:43








Lesson 9 - File Handling


This topic deals on how to:

Read from a File
Create and Write to a File
Append data to an existing File
Delete Files
Use the '{$I-},{$I+}' compiler directive
Create and Remove Sub-Directories
FileSize() - return the size of the file in bytes





Intro

A file contains data which is saved in the hard disk. You can only view a file from the hard disk by using an operating system. A can contain text data which is used by word processors. Many text files are saved in the hard disk with the extensions: *.txt and *.doc. The file with extension of *.txt, means that it is created by the Microsoft Notepad. Whenever you use the Microsoft Notepad and save a text file, this saved file is created and written on to the hard disk. However, some programs have various formats on how to maintain the text - such as justification, font, font colour. So many files contain more data other than text data! The program itself uses various techniques to create a file in such a way that, when it is being read again it can read (using programming skills, etc..) if it is justified, its font style etc..

The program that is able to read the file of extension *.xxx, is called the viewer of that file!

Before you go on with files, please make sure that you have enough hard disk space for our experiments, since I am going to demonstrate to save a file to the hard disk!! :) Only a few kb, however..



Read from a File (file input)

Reading a file in pascal is very easy. Note that there are no reserved words in Pascal that are used to to read or write to a file. We used the 'usual words': readln() and writeln(); Here's the technique of how to read a text file (only):

Program Lesson9_Program1;
Var UserFile : Text;
FileName, TFile : String;
Begin
Writeln('Enter the file name '+
+'(with its full path) of the text file:');
readln(FileName);
{A .txt file will be assigned to a text variable}
Assign(UserFile, FileName + '.txt');
Reset(UserFile); {'Reset(x)' - means open the file x}
Repeat
Readln(UserFile,TFile);
Writeln(TFile);
Until Eof(UserFile);
Close(UserFile);
Readln;
End.
Program code is updated (13th June 2006)

Details: the Assign()function on line 10 has been changed from Assign(UserFile, FileName, + '.txt') to Assign(UserFile, FileName + '.txt') because the comma ',' after 'Filename' is a syntax error.

Program Errors corrected thanks to James McNamara

I think that it is worth to take a look at various important lines of the program. A new variable of type: 'Text' is new to you, and this should be used whenever you are going to edit a text file! The variable 'FileName' is required to link to the file indicated by the user. The 'assign' statement is used to declare 'FileName' + '.txt' to a text file, so that the file could be opened, using - 'Reset()'. To read from the first line to the very last line of the file, you should use the repeat-until loop, ending the loop with : 'Until Eof(textfile);', which means: 'Until the End Of File [eof]'.



Create and Write to a File (file output)

The following program is an example of how to create-and-write or overwrite a file:

Program Lesson9_Program2;
Var FName, Txt : String[10];
UserFile : Text;
Begin
FName := 'Textfile';
Assign(UserFile,'C:\'+FName+'.txt'); {assign a text file}
Rewrite(UserFile); {open the file 'fname' for writing}
Writeln(UserFile,'PASCAL PROGRAMMING');
Writeln(UserFile,'if you did not understand something,');
Writeln(UserFile,'please send me an email to:');
Writeln(UserFile,'victorsaliba@hotmail.com');
Writeln('Write some text to the file:');
Readln(Txt);
Writeln(UserFile,'');
Writeln(UserFile,'The user entered this text:');
Writeln(UserFile,Txt);
Close(UserFile);
End.
In the above program, I am using the 'writeln()' statement so that I write to the file I have previously assigned to. Note that, since I am using writeln(), there is no output to the screen, it goes to the file I initialised.

To check exactly what has just been written to this file, go to C:\, and see if there is a file named:

Textfile.txt. Open it and see what does it contain!! :-). If you had problems, or if you did not have a way out, then in the start menu go to:

start -> find -> files or folders => look in drive C: and enter 'textfile.txt' in the text box; double click on it.



Append text to an existing File
Writing to an existing file, means, open a file and add extra data, but not overwrite the file. Some beginner programmers do not actually understand how to, not overwrite a file with data they would like to input. This is the common problem:

Var UFile : Text;
Begin
Assign(UFile,'C:\ADDTEXT.TXT');
ReWrite(UFile);
Writeln(UFile,'How many sentences, '+
+'are present in this file?');
Close(UFile);
End.
Copy the program above in a text file editor and save it in the turbo pascal bin folder as 'example.pas'. Open Turbo Pascal, and run this program for two times or more. Close Turbo Pascal and find the file named 'addtext.txt'. Is there more than one sentence?

A new reserved word which works with files, can complete our problem. The new reserved word is 'append(f)', where f is a variable of type text. This can be done by simply change the 'Rewrite(UFile)' to 'append(UFile)', and the text file is not overwritten, but appended! The above program changes to the following:

Var UFile : Text;
Begin
Assign(UFile,'C:\ADDTEXT.TXT');
Append(UFile);
Writeln(UFile,'How many sentences, '+
+'are present in this file?');
Close(UFile);
End.
Run the program two times or more to see the change...



Delete Files

In Pascal, the reserved word used to delete files from the hard this is the 'Erase(f)' where f is a variable of any data type. This means that 'f' could be both file and text variable type. Note, that the file you are about to delete is not taken in the recycle bin, but it is directly kicked off the hard disk!!

Unlike any other file functions, the erase() function does not open the file to delete it, so you don't need to apply a 'close(...)' after erase().

Example Program:

Var UFile : Text; { or it could be of 'file' type}
Begin
Assign(UFile,'C:\ADDTEXT.TXT');
Erase (UFile);
End.


The '{$I-},{$I+}' compiler directives
Compiler directives are used to adjust the compiler settings during the compilation process, so that programmers can control their program in their preferrable way. Compiler directives are declared using the symbols '{', '$' and '}'. Note that the braces are used for comments, but still, '{$something}' is taken to be a directive. Instead of 'something' the programmer should replace it by a known directive (a letter). Now, the '{$I}' is used to tell the compiler not to take into consideration I/O errors including file operations. Say, for example, if a file is trying to be opened, but it does not exist, the program does not stop from executing and halts with a runtime error, saying: 'File not found' or whatever boring error! Make sense? So, if you apply this compiler directive before you open the file, either for writing or reading, I/O errors will not cause the program to raise a runtime error forced halt.

Now, I must teach you how to use this compiler directive by letting you know where to put it and how to cater with the error that might occur. It must be placed where you are going to open or delete a file. ie.:

{$I-}
action on file...
{$I+} { enable the i/o error check again }
It is important to enable again the i/o error check, after you have disabled it, so that any unknown future errors would be automatically encountered. However, if you think that it would be better to disable i/o error checking for the entire program, you can! Just apply the directive at the beginning and that's it but it is not recommended since during the runtime of your program you will not be notfied of IO errors that might occur!! After you control a file action, you must check whether an error has occured or not by using the system function 'IOResult', which returns IO error information. This is a typical traditional exception handling. You should test for errors using the statement:

If (IOResult <> 0) then ...

The IOResult function should be explicitly used after an IO error check so that it will automatically clear the error flag of the system otherwise, the IO error cause a 'mutation' to other IO processes resulting into a runtime error. You are not required to grasp each word perfectly from what I said. The most important thing is that you include that statement if the IO directives are to be used and everything would be fine. Now I should show you how to use it by an example:

Program Lesson9_Program3;
Uses Crt;
Var t : Text;
s : String;

Begin
Assign(t,'C:\ABC.DEF');
{$I-} { disable i/o error checking }
Reset(t);
{$I+} { enable again i/o error checking - important }
If (IOResult <> 0) then
Begin
Writeln('The file required to be opened is not found!');
Readln;
End Else
Begin
readln(t,s);
Writeln('The first line of the file reads: ',s);
Close(t);
End;
End.
If the required file is found the IOResult returns a 0 value, meaning no errors; ELSE if not found (IOResult returns a non-0 value) display an error message!

IMPORTANT: if the file is successfuly found, the file is opened and you should close it as shown in the program above. However, if it is not found, the common sense says that it couldn't be opened because it is not found!?! So, you should not include a 'close(..)' after it is not opened and this is done conditionally as shown in the example. Study carefully my program and run it several times with good paths and non-good ones to see well the difference and how does the compiler directive works!!

Hopefully, the compiler directive could be applied to different functions, similarly as in the program above. You can use it with 'rewrite()', 'append()', 'erase()', and also 'FSearch()'. You can try as many programs as you wish and hopefully practicing the IO directive. I know, that this directive is somewhat complex and hard to be understood but undoubtedly useful! I am there waiting for your e-mails, if you think that your program does not work properly when using this directive.



Create and Remove Sub-Directories
In Pascal, there are functions with which you can either create or remove a directory from the hard disk. To create a directoy, we use the function 'createdir(c)' where 'c' is of type PChar. Ohh, that's sound kinky... What the heck does PChar means? Hehe, PChar is a pointer variable which holds the address of a dynamic variable of a specified type. It is sort of a pointer for characters. Before you create the directory, however, you have to check if the directory exists, else a runtime error shows up fiddling in front of your monitor!! :-) The directory is created as follows:

NewDir := FSearch('C:\Pascal Programming',GetEnv(''));
if NewDir = '' then CreateDir('C:\Pascal Programming');
From only two lines of code, we have been able to create a dir. But, before you go experimenting on your own, you should strictly read the following documented example: (I will explain in detail the 'FSearch()' function)

Program Lesson9_Program4;
Uses WinDos, Dos;
{ note the inclusion of the 'windos.tpu' library }

Var NewDir : PathStr;
{ for searching the dir and create a new one,
if it does not exist }
F : Text;

Begin
{ search for the dir }
NewDir := FSearch('C:\Pascal Programming', GetEnv(''));
{ create a new one, if it does not exist }
if NewDir = '' then CreateDir('C:\Pascal Programming');
Assign(F,'C:\Pascal Programming\pprogramming.txt');
{$I+} ReWrite(F); {$I-} { disable and enable back again
i/o error checking }
{ write to text file }
Writeln(F,'http://pascal-programming.info/');
{$I+} Close(F); {$I-}
End.
The variable type, 'PathStr', is new to you, and this is a variable defined in the 'dos.tpu' library. So, you have to include the 'dos' library at the beginning of your program. The 'FSearch()' function is implemented also in the windos library to search in a dir list. So, it was useful in our program and any other program to search if the dir exists or not. It's not important to know exactly what does 'GetEnv()' mean, which is found in the second parameter of FSearch(). The Borland Turbo Pascal reference says that its function is to return the value of the specified function. Note that 'FSearch()' is implemented in the 'dos.tpu' library, while CreateDir()' is implemented in the 'windos.tpu' library.

To remove a directory, it is quite simple. Just add 'remove()' at the end of your program! :-) Your operating system will automatically erase the dir if it exists. It doesn't matter if you 'remove' a dir which does not exist. Hopefully, no runtime errors, will show up! The 'remove(Pch)' function is also implemented in the windos library, where 'Pch' is a variable of type PChar, again.



FileSize() - Return the value of a file in bytes
Now, for the fun stuff!! To return the size of a file, simply declare a variable of 'longint' type, and assign it to the filesize of the file. A file variable of type byte, should be assigned to the file which you would like to return its size, using 'assign()'.

Program Lesson9_Program4;
Var f : file of byte; { file var of type byte }
sz : longint; { var for the size }

Begin
Assign(f,'C:\anyfile.txt');
{$I-} Reset(f); {$I+}
f (IOResult <> 0) then
Begin { file found? }
Writeln('File not found.. exiting');
Readln;
End Else
Begin
{ Return the file size in Kilobytes }
sz := round(FileSize(f)/1024);
Writeln('Size of the file in Kilobytes: ',sz,' Kb');
Readln;
Close(f);
End;
End.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:44



Lesson 10 - Arrays


This lesson will cover:

What are Arrays?
Introducing User-Defined Data Types
2 Dimensional and multi-Dimensional Arrays





What are 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 Integer, Boolean 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:

Type
i = 1..5;
myArrayDataType = Array[1..5] of Byte;
byteFile = File of Byte; { binary file }

Var
myArrayVar : myArrayDataType;
myFile : byteFile;

Begin
Writeln('Please enter 5 number from (0..255): ');
For i := 1 to 5 do
Readln(myArrayVar[i]);
Writeln('You have entered the following numbers: ');
For i := 1 to 5 do
Writeln('Number ',i,': ',myArrayVar[i]);

Writeln('Now writing them to file...');
{store the numbers in a file}
Assign(myFile, 'example.dat');
ReWrite(byteFile);
Write(myFile, myArrayVar[i]);
Close(myFile);
Writeln('Done, you may exit..');
Readln;
End.
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 and Multi-Dimensional Arrays

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:

1 2 3 4 5
2
3 3,4
4
5 5,3
An example of a 5 by 5 2D array illustrated on a grid

Let us declare an array having 3 by 5 dimensions, assign a value to a particular variable in the array and illustrate this on a grid just like the one above:

Var
my2DArray : Array[1..3][1..5] of Byte;
Begin
my2DArray[2][4] := 10;
End.
Having the vertical axis as the 1st dimension and the horizontal one as the 2nd dimension, the above example is illustrated as follows:

1 2 3 4 5
2
10

3
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.

Uses Crt;
Type
myRange = 1..5;
arrayIntType = Array[myRange] of Integer;
myFileType = File of arrayIntType;
Var
i : myRange;
myFile : myFileType;
{ the next array is 2 dimensional }
arrayInt : Array[1..2] of arrayIntType;
Begin
Clrscr;
Randomize;
For i := 1 to 5 do
Begin
arrayInt[1][i] := Random(1000);
Writeln('rand num: ',arrayInt[1][i]);
End;
Assign(myFile, 'test.dat');
ReWrite(myFile);
Write(myFile, arrayInt[1]);
Close(myFile);
ReSet(myFile);
Read(myFile, arrayInt[2]);
Close(myFile);

For i := 1 to 5 do
Writeln(i,': ', arrayInt[2][i]);
Readln;
End.
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.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:45

Record Data Structure


This lesson will cover:

What are Records?
The With Keyword
Passing Records as Arguments
Arrays of Records
Binary Files and Records





What are Records?

A record is a special type of data structure that, unlike arrays, collects different data types that define a particular structure such a book, product, person and many others. The programmer defines the data structure under the Type user definition.

Let us take the case of defining a book using a record data structure. The main entities of a book are its title, author, unique ISBN number and its price. This is how it is defined in Pascal and in most other programming languages:

Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;
Var
myBookRec : TBookRec;
That's it! Note that I could have used this structure as a variable by declaring it under Var. When type-defining it, you can declare as many TBookRec variables as you wish.The entities of the record (title, author...) are called fields and these will be accessed through the instance of myBookRec variable. As you might have noticed, the declaration of typical record data structure starts with the keyword Record and always ends with the end keyword.

Now, you will see how we access the fields of the record by assigning them values and retrieve them back later in the following small program.

Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;
Var
myBookRec : TBookRec;

Begin
myBookRec.Title := 'Some Book';
myBookRec.Author := 'Victor John Saliba';
myBookRec.ISBN := '0-12-345678-9';
myBookRec.Price := 25.5;

Writeln('Here are the book details:');
Writeln;
Writeln('Title: ', myBookRec.Title);
Writeln('Author: ', myBookRec.Author);
Writeln('ISBN: ', myBookRec.ISBN);
Writeln('Price: ', myBookRec.Price);
Readln;
End.


The With Keyword

The with keyword is a quick way in accessing fields of a record. Although it helps with quick field evaluation, it is rarely used since it has a lack of readability and increases ambiguity when coming to distinguish between a variable having the same name as one of the field names of a record within a with statement. Taking a snippet from the previous example, this is how the code above transforms when incorprating the with keyword.

With myBookRec do
Begin
Title := 'Some Book';
Author := 'Victor John Saliba';
ISBN := '0-12-345678-9';
Price := 25.5;
End;


Passing Records as Arguments

It may become very useful when records are required to be passed through arguments and this will be demonstrated shortly. I will use the same data structure, pass it by reference as a parameter and return the value back through the parameter also.

Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;

Procedure EnterNewBook(var newBook : TBookRec);
Begin
Writeln('Please enter the book details: ');
Write('Book Name: ');
Readln(newBook.Title);
Write('Author: ');
Readln(newBook.Author);
Write('ISBN: ');
Readln(newBook.ISBN);
Write('Price: ');
Readln(newBook.Price);
End;

Procedure DisplayBookDetails(myBookRec : TBookRec);
Begin
Writeln('Here are the book details:');
Writeln;
Writeln('Title: ', myBookRec.Title);
Writeln('Author: ', myBookRec.Author);
Writeln('ISBN: ', myBookRec.ISBN);
Writeln('Price: ', myBookRec.Price);
End;

Var
bookRec : TBookRec;

Begin
EnterNewBook(bookRec);
Writeln('Thanks for entering the book details');
DisplayBookDetails(bookRec);
Readln;
End.


Arrays of Records
Records may be stored in arrays and this will become very useful and it is not that difficult to manage. We will use an array of records to store a number of different books and by using this example, it will be immensely indicative to learn how to use them.

In the following example I will use the procedures above to store 10 different books from input and then output only one chosen record to display it back to screen in order to demonstrate how to access a record from an array.

Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;

Procedure EnterNewBook(var newBook : TBookRec);
Begin
Writeln('Please enter the book details: ');
Write('Book Name: ');
Readln(newBook.Title);
Write('Author: ');
Readln(newBook.Author);
Write('ISBN: ');
Readln(newBook.ISBN);
Write('Price: ');
Readln(newBook.Price);
End;

Var
bookRecArray : Array[1..10] of TBookRec;
i : 1..10;

Begin
For i := 1 to 10 do
EnterNewBook(bookRecArray[i]);
Writeln('Thanks for entering the book details');
Write('Now choose a record to display from 1 to 10: ');
Readln(i);
Writeln('Here are the book details of record #',i,':');
Writeln;
Writeln('Title: ', bookRecArray[i].Title);
Writeln('Author: ', bookRecArray[i].Author);
Writeln('ISBN: ', bookRecArray[i].ISBN);
Writeln('Price: ', bookRecArray[i].Price);
Readln;
End.
Note that you can also use arrays within records and this time the square brackets go on with the field name instead of the record. Also you can embed records within records. More dots will be required to access deeper records.



Binary Files and Records
Records can also be stored into files and this could be done by using binary files. I will demonstrate storing records into files by continuing from the previous example. Using binary files could be very handy, fast and more reliable over text files. You can't afford storing hundreths of files by using text files since it becomes confusing and even slower for the computer to process and read/write from/to the file.

In the following example I will use a file of the book record I have created and then store as many books as I want in the file using the binary file system. Watch carefully how I will create the file of record and how I will perform the file I/O for the binary file system. Also, I will make use of special built in functions that help me position the file pointer to the record I want.

Note that with binary files, only Read and Write are allowed to read/write fro/to a file.

Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;

Procedure EnterNewBook(var newBook : TBookRec);
Begin
Writeln('Please enter the book details: ');
Write('Book Name: ');
Readln(newBook.Title);
Write('Author: ');
Readln(newBook.Author);
Write('ISBN: ');
Readln(newBook.ISBN);
Write('Price: ');
Readln(newBook.Price);
End;

Var
bookRecArray : Array[1..10] of TBookRec;
tempBookRec : TBookRec;
bookRecFile : File of TBookRec;
i : 1..10;

Begin
Assign(bookRecFile, 'bookrec.dat');
ReWrite(bookRecFile);
For i := 1 to 10 do
Begin
EnterNewBook(bookRecArray[i]);
{ bookRecArray[i] now contains the book details }
Write(bookRecFile, bookRecArray[i]);
End;
Close(bookRecFile);
Writeln('Thanks for entering the book details.');
Writeln('They are saved in a file!');
Write('Now choose a record to display from 1 to 10: ');
Readln(i);
ReSet(bookRecFile);
Seek(bookRecFile, i-1);
Read(bookRecFile, tempBookRec);
Close(bookRecFile);
Writeln('Here are the book details of record #',i,':');
Writeln;
Writeln('Title: ', tempBookRec.Title);
Writeln('Author: ', tempBookRec.Author);
Writeln('ISBN: ', tempBookRec.ISBN);
Writeln('Price: ', tempBookRec.Price);
Readln;
End.
The example program above demonstrated the use of the seek function. It's role is to place the file pointer to the desired position. The first component of the file is marked as 0. So you have to keep in mind that if you have a counter starting from 1, you have to decrement it by 1 to obtain the actual record you want.

The seek function is very important and has an important role in binary file system. Here are some uses of the function and how it can be used effectively to obtain a particular position of the file.

Special Uses of the Seek Function
Seek the first record of the file

Seek(myFile, 0);
Seek the last record of the file

Seek(myFile, FileSize(myFile)-1);
Seek one position beyond the last record

Seek(myFile, FileSize(myFile));
Seek to the next position of the file from the current

Seek(myFile, FilePos(myFile)+1);
When trying to access from a file position that is beyonf the file limits, a runtime error is automatically raised. Try to avoid this type of error. This may be caused because you might have looped through the file and kept on looping beyond its limits. Note that Seek(myFile, -1) is a typical runtime error becuase -1 position does not exist. 0 is the least and the first record in the file. Note that FilePos is also very useful and it returns the current positon of the file. Please note that FileSize returns the number of components in the specified file and not the size in Bytes. If the file is empty, 0 is the returned value. On the other hand, if the file contains 5 records (ie. 0 to 4), 5 is returned.

The structure of a binary file is just like blocks being stored contiguosly in a line. Think of boxes being placed one adjacent the other and each one of them has data. There is a space between this boxes that indicates the file positon and we can easily depict this fact below.

0
BookRec0
1
BookRec1
2
BookRec2
3
BookRec3
4
BookRec4
^
The first row is the actual file showing the indexes of each record block and the second row shows the file pointer ie. the file position. The current file position shown in the illustration above is relevant to Seek(myFile, 1). Now you have been assured that the number 1 record of a file is not the first record of the file. After the last record, there is an EOF marker that indicates the end of the file and it is not legal to go beyond this point except for only one position to allow appending ie. Seek(myFile, FileSize(myFile)).

This concludes another lesson from the pascal programming tutorial. See you in the next lesson!
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:46

Strings


This lesson will cover:

Introduction to Strings
Some Useful String Functions
Pos
Copy
Delete
Insert
Concat
UpCase
Str
Val





Introduction to Strings

Maybe you already know what is a string variable from previous program examples that you have tried, but you don't know what operations and what functions one can apply to them and how can they be manipulated in order to obtain another form of string whatsoever.

In this lesson we will cover some important functions that the Pascal programming language has for us in order to cater for string operations far more easier to use than you think.

Let's jump into the strings staff straight away. In order to understand strings, one has to keep in mind that a string is made up of an array of characters. The string data type is an in-built data type that is an array of 256 characters (Type String = Packed Array[0..255] of Char). When stored in memory, the processor should know where the string starts and where it finishes. In order to know where the string finishes, in Pascal, the 0th element of a string is defined as the length of the string. So, if you try to access character 0 of a string, the number of characters stored in that array is returned, thus letting the processor to know where the string finishes.

The following example shows some simple string operations.

Var
myString : String;

Begin
myString := 'Hey! How are you?';
Writeln('The length of the string is ',byte(myString[0]));
Write(myString[byte(myString[0])]);
Write(' is the last character.');
End.
Note that in the previous program I have used an automatic data-type conversion, normally referred to as data type-casting. When type-casting from one data type to another, all that is happening is simply a conversion from one data type to another based on the data type in subject and the wrapping data type to which the old data type is being converted. In our case, the array variable myString has a special 0th element storing the number of characters in that array in string format. This value is an ascii value, so trying to display it without converting it into a number, the alternative ascii character is displayed. So you have to change the character value to an ordinary number by wrapping the variable by another data type, in our case a byte data type (why use integer? - its all waste of memory and memory consumption).

Note that myString[myString[0]] without having data-type casting around myString[0] will lead to a compiler error because myString[0] is a character and not an integer.

Note that to retrieve the length of a string (ie. the number of characters in a string) there is no need to use the method I showed to you because its too complicated and unfriendly. Instead, there is the simpler function, length() which will return the number of characters of the string in subject. Eg. numOfChars := length(myString);



Some Useful String Functions

There are some basic Pascal functions that has to do with string operations and so there is no need to write them yourself, and they are of course quite useful. I will explain all the string functions by describing what they do and a simple example of each.




Function Pos(SubString : String; S : String) : Byte;

Description

This function will search for the string SubString within the string S. If the sub-string is not found, then the function would return 0. If on the other hand the sub-string is found, then the index integer value of the first character of the main string that matches the character of the sub-string is returned.

Var
S : String;

Begin
S := 'Hey there! How are you?';
Write('The word "How" is found at char index ');
Writeln(Pos('How',S));
If Pos('Why',S) <= 0 then
Writeln('"Why" is not found.');
End.


Function Copy(S : String; Index : Integer; Count : Integer ) : String;

Description

This function will copy some characters from string S starting from character index Index and copies as much as Count. The copied string is then returned.

Var
S : String;

Begin
S := 'Hey there! How are you?';
S := Copy(S, 5, 6); { 'there!' }
Write(S);
End.


Procedure Delete(var S : String; Index : Integer; Count : Integer );

Description

Deletes a specified number of characters from the string S. The starting position of deletion is from character index Index. The number of characters that will be deleted is specified through Count. The new string is passed back through the variable parameter S.

Var
S : String;

Begin
S := 'Hey Max! How are you?';
Delete(S, 4, 4); { 'Hey! How are you?' }
Write(S);
End.



Procedure Insert(Source : String; var S : String; Index : Integer);

Description

This function will insert a string of any length into a source string starting from an index character. The string S will be inserted into string Source starting from the index character Index. No characters will be deleted from the front except if the resulting string is longer than 255 characters. In this case, the front characters will be truncated as to fit a 255-character string.

Var
S : String;

Begin
S := 'Hey! How are you?';
Insert(S, ' Max', 4);
Write(S);
{ 'Hey Max! How are you?' }
End.


Function Concat(s1 [, s2, s3...sn] : String) : String;

Description

Concatenates 2 or more strings depending how long is the argument expression. Try to make sure not to exceed the limit of 255 characters when concatening strings as it will result in truncation. This function can also be obtained by using the plus (+) operator between strings that need to be concatenated.

Var
S1, S2 : String;

Begin
S1 := 'Hey!'
S2 := ' How are you?';
Write(Concat(S1, S2)); { 'Hey! How are you?' }
End.
is the same as

Var
S1, S2 : String;

Begin
S1 := 'Hey!'
S2 := ' How are you?';
Write(S1 + S2); { 'Hey! How are you?' }
End.



Function UpCase(C : Char) : Char;

Description

Converts the character C to uppercase and returned. If the character is already in uppercase form or the character is not within the range of the lower case alphabet, then it is left as is.

Var
S : String;
i : Integer;

Begin
S := 'Hey! How are you?';
For i := 1 to length(S) do
S[i] := UpCase(S[i]);
Write(S); { 'HEY! HOW ARE YOU?' }
End.



Procedure Str(Val : Integer / LongInt / Real; var S : String);

Description

Converts an integer or a decimal value to a string. The value parameter Val is converted into a string and passed through the variable paramter S.

Var
S : String;
i : Real;

Begin
i := -0.563;
Str(i, S);
Write(S);
End.


Procedure Val(S : String; var Val; Code : Integer);

Description

Converts a string to its corresponding numeric value. The string paramater S is converted into a numeric value and passed back through the variable paramter Val. If the string to be converted is not a correct numeric value, an error occurs and is returned via Code. If the conversion is correct then Code is 0.

Var
S : String;
error : Integer;
R : Real;

Begin
S := '-0.563';
Val(S, R, error);
If error > 0 then
Write('Error in conversion.')
Else
Write(R);
End.
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:46


The idea of this section is to provide you with various articles concerning subjects that haven't been covered in the lessons. The articles sections is intended for those who have the required basic knowledge of programming in Pascal.

For instance, sorting is a common technique used inevitably in many programs which would like to sort a list of data items in either ascending or descending order. Also, linked lists and stacks and similar data structures are of outmost importance and is necessary to cover them out since they are used quite often. Such data structures are very useful utilities which enlighten your programming skills. These will be covered for you in as general application as possible, so that you may use them in any of your programs and practice them for your applications.

Feel free to go through the following articles and try them yourself and manipulate them in your own way.

Want to add your article?
You may wish to add your own article about any programming concept you want in whichever language you like. Yes! You may also choose the language of your choice. It does'nt have to explicitly be an article using the pascal language. You may like for example write an article on dynamic memory using the c language.

Feel free to send any articles on my email address and make sure you've got these important features in your article:

- Title of your article

- Author of article (so that the article will be credited to the author of the article)

- [optional] any links to your homepage that has relevant content with the article

Note that readers of the article will be able to send comments regarding the content of the article.

Important: We will not tolerate any submitted material that is copyrighted!
الرجوع الى أعلى الصفحة اذهب الى الأسفل
Jasmine collar

Jasmine collar



أوبجكت باسكال  (Object Oriented Programming) Empty
مُساهمةموضوع: رد: أوبجكت باسكال (Object Oriented Programming)   أوبجكت باسكال  (Object Oriented Programming) Icon-new-badge22/7/2011, 02:50


Recent Forum Pascal Programming Topic Posts
Topic Posted By Date Posted
How to resize window? joeandrews 2011-07-08
TubroVision - TListBox - TCollection HansGerber 2011-07-08
help with a command irhebi 2011-06-16
[Emergency Help] Something like chess! geryeo 2011-06-13
2D arrays in pascal harrison38 2011-05-23

Go to the Forums Now
Intro
Welcome to Pascal Programming Website - a website which is particularly designed to help you learn the pascal programming language very quickly and interactively! Enjoy surfing through the site and reading the lessons built purposely for those who would like to get themselves familiar and to get capable of writing structured Pascal programs.


The Pascal Programming Language - Is it for me?
If you stepped into the site to start learning Pascal, you are on the right track! There are'nt any special pre-requisites to learn Pascal. Thus, you can start learning it straight away! Pascal is a very easy language, and helps beginners to introduce them to other languages, before they can start to learn C or C++ or any other advanced language (such as JAVA).



What is PASCAL?
Pascal is an ordinary high-level language which is widely used as a starting or as a teaching language. However, nowadays Pascal is sometimes preferred to other languages, and could be useful to solve technical problems. It is not necessary to learn any other easier language to start learning Pascal, or any of that sort. It is a very easy programming language and helps you to understand the basics of the world of programming. Also, it greatly helps you in order to start learning the C programming language. I had different experiences, one of which I learned Pascal, and then moved to C programming very easily. The Pascal programming language has its structure and syntax very similar to that of the C programming language.



A General Overview of Low-Level and High-Level Languages
There are two main categories of programming levels; these are low-level and high-level languages. The terms 'low' and 'high' does not mean that a group of 'low-level' languages are inferior to the 'high' ones. A low-level language consists of machine-oriented programming. For example, machine code is the only language with which the CPU understands. The other most-common low-level language, is the Assembly language. The Assembly language has a structure of machine-oriented programming. The Assembly Language, in other words, has a major control of the computer hardware. The Assembly Language makes use of mnemonics and has a better relationship with the memory and the CPU unit far more than that of other programming languages. The Assembly language is sometimes used as part of the source code written in Pascal.

A high-level language consists of all those languages which are problem-oriented. A low-level language is not user-friendly, that is, makes it hard for the user to understand the structure of the program. Pascal, C/C++, BASIC, COBOL, Fortran, etc.. are examples of high-level programming languages. High-level languages represent their source code in such a way that it is more understandable to the programmer.
[img][ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذا الرابط]



Machine code is a language which uses binary coding in its source codes. What do you understand by 0001001011 010101 1001110? It's machine code - the only language which is understandable by the CPU.



Interpreters and Compilers
Every high-level language has its own source which needs to be translated into the CPU's language - machine code. These translators are referred to as interpreters and compilers. A high-level language package contains either an interpreter, or a compiler, with which a high-level language uses to translate the source code into object code.

An interpreter is more slower than a compiler, the fact that it has to be loaded into memory till the end of the translation of the program. It is less practical than the compiler. However, the interpreter can be helpful for correcting out errors in programs i.e. it is ideal for debugging. A compiler is faster due to its single, complete translation of the program into object code. A compiler, compiles the source code (with no syntax errors) and makes a copy of it. This copy is called the 'object code'. After it has been compiled, it does not need a recompilation (unless the source code is changed and a recompilation is required to affect the change). The object code used by the OS in order to execute the compiled program.



Where Can I Get A Compiler?
For any student who tends to learn Pascal, starting from today, the best thing he should do is to search for a Pascal compiler, before he start learning Pascal. If you would like a compiler which runs under windows environment and GUI, then you can click here. The most famous compiler is the latest Borland Turbo Pascal 7.0, and is highly difficult to find it out on the market. However, the 'museum of traditional pascal' of Borland is giving out (pascal) compilers for free because of their old age. [ Note: It is important to use the Borland's Pascal Compiler because it is widely used ]

Turbo Pascal 5.5 is free: CLICK HERE TO DOWNLOAD



Ready to start?
When you are ready to start, choose lesson 1 from the navigation bar menu or click here.
Articles
Are you looking for some more advanced and applied concepts of the pascal programming language such as sorting, pointers and linked lists? Then articles is just the section for you. Articles provide discussions about advanced concepts of the language which you can integrate into your programs and use them wisely.

[ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذه الصورة]


Why don't you have a look at the articles section by clicking here or choose an article from the table below:

Article # Article Title
1. Sorting
2. The Stack Data Structure
3. Pointers
4. Linked Lists
5. Recursion
6. Object Oriented Programming - Part 1
Errors in Programs
Mistakes can be found in some of my source codes around this site. Should this happen, please send me an e-mail and inform me immediately, and obviously state where should this mistake be found. You will be doing a favour for yourself and other students. Anyways, thank you if you spot and obviously report any errors in my pascal programs.

[PLEASE REPORT ANY DEAD LINKS HERE]



Any Problems?
If you would like to discuss any problems with me and other users, then don't hesitate to register with our forums and posting your problem using the POST NEW TOPIC feature. If you are not registered, the register by clicking the link below:

[ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذا الرابط]

I will be very glad and looking forward to help you through our forums!



Link To Us
We greatly appreciate your help to get this site more useful and let others benefit from the best pascal programming resource site on the net. Great effort was done to greatly improve this site's environment for every visitor to this site to be comfortable in.

For those who would like to include a link to our site, they might use a cute logo like the one provided to you below linking to (right-click on the image and choose save as to save the image to your computer):

[ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذا الرابط]


[ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذه الصورة]



One may hot link to the image using:

[ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذا الرابط]

News
4 November 2006
This site has been awarded the Alcazaren award.


[ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذه الصورة]



11 October 2006
The registration form had a minor glitch in its javascript coding and because of this technical problem, one could not submit his/her details in order to register. Now the problem is solved and the registration form is once again working normally.

15 September 2006
Articles just keep adding in numbers ;). Another one is added, and is about Recursion.

14 September 2006
Two more articles are added ;). The new articles are about Pointers and Linked Lists.

11 September 2006
Another article is added. The new article is about the Stack Data Structure.

9 September 2006
The first article is out for you to read! Its about Sorting. Check it out right now by clicking here or choose the articles button from the navigational menu on the left.

7 September 2006
The new and freshly re-designed website is uploaded back for you to enjoy reading it more comfortably.

[ندعوك للتسجيل في المنتدى أو التعريف بنفسك لمعاينة هذه الصورة]


As you may have noticed, a new section has been added to the lessons on the left navigation menu. Articles consist of a number of discussions about chosen subjects and which won't be available in the lessons. The articles are still under construction, but you will be informed here when articles will be added.

Help yourself surfing this site and reading lessons in a better environment.

5 September 2006
A complete website re-design is in progress. The new layout is user friendlier which of course gives a boost to the necessary comfort for a user to read the text. Unfortunately, because of the major website upgrade, the site would be unable to be up and running and would need some amount of downtime. Hope that it would not be long and be up again for you to enjoy learning in a better environment that you deserve.

27 June 2006
NEW! Users can freely submit their own comments regarding a particular lesson in the end of a lesson.

26 June 2006
HOT NEWS: FORUM Ver 2 is here! Just take advantage of this cutting edge forum, ready to help the community about its programming needs!

What's new in Programming Forums Ver 2:

> Nice New Look ;)

> Topic/Reply is far more flexible

> New Thread: Off-Topic so you can post on anything you like :)

الرجوع الى أعلى الصفحة اذهب الى الأسفل
 
أوبجكت باسكال (Object Oriented Programming)
الرجوع الى أعلى الصفحة 
صفحة 1 من اصل 1

صلاحيات هذا المنتدى:لاتستطيع الرد على المواضيع في هذا المنتدى
اربد :: تكنولوجيا :: كمبيوتر و برامج-
انتقل الى: