Created
February 5, 2023 23:01
-
-
Save kinjalkishor/5785ca82dfe9876e5592aa991ef94729 to your computer and use it in GitHub Desktop.
pascal_all_tutorials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{$mode objfpc} // directive to be used for defining classes | |
{$m+} // directive to be used for using constructor | |
//{$static on} | |
(* | |
{$MODE OBJFPC} //directive to be used for creating classes | |
{$M+} //directive that allows class constructors and destructors | |
*) | |
program HelloWorld; | |
//uses crt; | |
//uses CalculateArea,crt; | |
uses sysutils,crt; | |
// const | |
// message = ' Welcome to the world of Pascal '; | |
// type | |
// name = string; | |
// var | |
// firstname, surname: name; | |
// type | |
// beverage = (coffee, tea, milk, water, coke, limejuice); | |
// var | |
// drink:beverage; | |
// Subrange Types | |
// var | |
// marks: 1 .. 100; | |
// grade: 'A' .. 'E'; | |
// const | |
// PI = 3.141592654; | |
// var | |
// r, d, c : real; {variable declaration: radius, diameter, circumference} | |
// var | |
// { local variable declaration } | |
// a:integer; | |
// var | |
// { local variable definition } | |
// a, b : integer; | |
// var | |
// a, b, ret : integer; | |
// var | |
// a, b, c, min: integer; | |
// var | |
// grade: char; | |
// var | |
// i, j:integer; | |
//label 1; | |
// var | |
// num, f: integer; | |
// (* function returning the max between two numbers *) | |
// function max_i(num1, num2: integer): integer; | |
// var | |
// (* local variable declaration *) | |
// result: integer; | |
// begin | |
// if (num1 > num2) then | |
// result := num1 | |
// else | |
// result := num2; | |
// max_i := result; | |
// end; | |
procedure findMin(x, y, z: integer; var m: integer); | |
(* Finds the minimum of the 3 values *) | |
begin | |
if x < y then | |
m := x | |
else | |
m := y; | |
//--- | |
if z <m then | |
m := z; | |
end; { end of procedure findMin } | |
function fact(x: integer): integer; (* calculates factorial of x - x! *) | |
begin | |
if x=0 then | |
fact := 1 | |
else | |
fact := x * fact(x-1); (* recursive call *) | |
end; { end of function fact} | |
function fibonacci(n: integer): integer; | |
begin | |
if n=1 then | |
fibonacci := 0 | |
else if n=2 then | |
fibonacci := 1 | |
else | |
fibonacci := fibonacci(n-1) + fibonacci(n-2); | |
end; | |
procedure swap(x, y: integer); | |
var | |
temp: integer; | |
begin | |
temp := x; | |
x:= y; | |
y := temp; | |
end; | |
(* In order to pass the arguments by reference, Pascal allows to define variable parameters. | |
This is done by preceding the formal parameters by the keyword var *) | |
procedure swap_ref(var x, y: integer); | |
var | |
temp: integer; | |
begin | |
temp := x; | |
x:= y; | |
y := temp; | |
end; | |
//------------- | |
// var | |
// greetings: string; | |
// name: packed array [1..10] of char; | |
// organisation: string[10]; | |
// message: pchar; | |
//uses sysutils; | |
// var | |
// str1, str2, str3 : ansistring; | |
// str4: string; | |
// len: integer; | |
// var | |
// exit: boolean; | |
var | |
n: array [1..10] of integer; (* n is an array of 10 integers *) | |
//i, j: integer; | |
// 2D array | |
// var | |
// ar: array [0..3, 0..3] of integer; | |
//i, j : integer; | |
// var | |
// val: integer; | |
// val := a[2, 3]; | |
// 2D dynamic array | |
// var | |
// ar: array of array of integer; (* a 2 dimensional array *) | |
//i, j : integer; | |
var | |
ar: packed array [0..3, 0..3] of integer; | |
//i, j : integer; | |
const | |
size = 5; | |
type | |
aty = array [1..size] of integer; | |
var | |
balance: aty = (1000, 2, 3, 17, 50); | |
average: real; | |
function avg( var arr: aty) : real; | |
var | |
i :1..size; | |
sum: integer; | |
begin | |
sum := 0; | |
for i := 1 to size do | |
sum := sum + arr[i]; | |
avg := sum / size; | |
end; | |
// var | |
// number: integer; | |
// iptr: ^integer; | |
// y: ^word; | |
//const MAX = 3; | |
// var | |
// arr: array [1..MAX] of integer = (10, 100, 200); | |
// i: integer; | |
// iptr: ^integer; | |
// y: ^word; | |
// type | |
// iptr = ^integer; | |
// var | |
// arr: array [1..MAX] of integer = (10, 100, 200); | |
// i: integer; | |
// parray: array[1..MAX] of iptr; | |
// const MAX = 4; | |
// type | |
// sptr = ^ string; | |
// var | |
// i: integer; | |
// names: array [1..4] of string = ('Zara Ali', 'Hina Ali', 'Nuha Ali','Sara Ali') ; | |
// parray: array[1..MAX] of sptr; | |
// type | |
// iptr = ^integer; | |
// pointerptr = ^ iptr; | |
// var | |
// num: integer; | |
// ptr: iptr; | |
// pptr: pointerptr; | |
// x, y : ^word; | |
// type | |
// iptr = ^integer; | |
// var | |
// i: integer; | |
// ptr: iptr; | |
// function getNumber(p: iptr): integer; | |
// var | |
// num: integer; | |
// begin | |
// num:=100; | |
// p:= @num; | |
// getNumber:=p^; | |
// end; | |
// type | |
// ptr = ^integer; | |
// var | |
// i: integer; | |
// iptr: ptr; | |
// function getValue(var num: integer): ptr; | |
// begin | |
// getValue:= @num; | |
// end; | |
// type | |
// Books = record | |
// title: packed array [1..50] of char; | |
// author: packed array [1..50] of char; | |
// subject: packed array [1..100] of char; | |
// book_id: longint; | |
// end; | |
// var | |
// Book1, Book2: Books; (* Declare Book1 and Book2 of type Books *) | |
// procedure printBook( var book: Books ); | |
// begin | |
// (* print Book info *) | |
// writeln ('Book title : ', book.title); | |
// writeln('Book author : ', book.author); | |
// writeln( 'Book subject : ', book.subject); | |
// writeln( 'Book book_id : ', book.book_id); | |
// end; | |
// type | |
// BooksPtr = ^ Books; | |
// Books = record | |
// title: packed array [1..50] of char; | |
// author: packed array [1..50] of char; | |
// subject: packed array [1..100] of char; | |
// book_id: longint; | |
// end; | |
// var | |
// (* Declare Book1 and Book2 of pointer type that refers to Book type *) | |
// Book1, Book2: BooksPtr; | |
//uses variants; | |
// type | |
// color = (red, black, white); | |
// var | |
// v : variant; | |
// i : integer; | |
// r: real; | |
// c : color; | |
// as : ansistring; | |
// type | |
// color = (red, blue, yellow, green, white, black, orange); | |
// colors = set of color; | |
// var | |
// c : colors; | |
// procedure displayColors(c : colors); | |
// const | |
// names : array [color] of String[7] | |
// = ('red', 'blue', 'yellow', 'green', 'white', 'black', 'orange'); | |
// var | |
// cl : color; | |
// s : String; | |
// begin | |
// s:= ' '; | |
// for cl:=red to orange do | |
// if cl in c then | |
// begin | |
// if (s<>' ') then s :=s +' , '; | |
// s:=s+names[cl]; | |
// end; | |
// writeln('[',s,']'); | |
// end; | |
// type | |
// StudentRecord = Record | |
// s_name: String; | |
// s_addr: String; | |
// s_batchcode: String; | |
// end; | |
// var | |
// Student: StudentRecord; | |
// f: file of StudentRecord; | |
// const | |
// MAX = 4; | |
// type | |
// raindata = file of real; | |
// var | |
// rainfile: raindata; | |
// filename: string; | |
// procedure writedata(var f: raindata); | |
// var | |
// data: real; | |
// i: integer; | |
// begin | |
// rewrite(f, sizeof(data)); | |
// for i:=1 to MAX do | |
// begin | |
// writeln('Enter rainfall data: '); | |
// readln(data); | |
// write(f, data); | |
// end; | |
// close(f); | |
// end; | |
// procedure computeAverage(var x: raindata); | |
// var | |
// d, sum: real; | |
// average: real; | |
// begin | |
// reset(x); | |
// sum:= 0.0; | |
// while not eof(x) do | |
// begin | |
// read(x, d); | |
// sum := sum + d; | |
// end; | |
// average := sum/MAX; | |
// close(x); | |
// writeln('Average Rainfall: ', average:7:2); | |
// end; | |
// var | |
// filename, data: string; | |
// myfile: text; | |
// var | |
// myfile: text; | |
// info: string; | |
// var | |
// name: array[1..100] of char; | |
// description: ^string; | |
// var | |
// name: array[1..100] of char; | |
// description: ^string; | |
// desp: string; | |
// var | |
// a, b, c, s, area: real; | |
//uses CalculateArea; | |
// var | |
// l, w, r, a, b, c, area: real; | |
// var | |
// YY,MM,DD : Word; | |
// var | |
// year, month, day, hr, min, sec, ms: Word; | |
(* | |
type | |
Rectangle = object | |
private | |
length, width: integer; | |
public | |
constructor init(l, w: integer); | |
destructor done; | |
procedure setlength(l: integer); | |
function getlength(): integer; | |
procedure setwidth(w: integer); | |
function getwidth(): integer; | |
procedure draw; | |
end; | |
// TableTop is inherited from Rectangle | |
TableTop = object (Rectangle) | |
private | |
material: string; | |
public | |
function getmaterial(): string; | |
procedure setmaterial( m: string); | |
procedure displaydetails; | |
procedure draw; | |
end; | |
constructor Rectangle.init(l, w: integer); | |
begin | |
length := l; | |
width := w; | |
end; | |
destructor Rectangle.done; | |
begin | |
writeln(' Desctructor Called'); | |
end; | |
procedure Rectangle.setlength(l: integer); | |
begin | |
length := l; | |
end; | |
procedure Rectangle.setwidth(w: integer); | |
begin | |
width :=w; | |
end; | |
function Rectangle.getlength(): integer; | |
begin | |
getlength := length; | |
end; | |
function Rectangle.getwidth(): integer; | |
begin | |
getwidth := width; | |
end; | |
procedure Rectangle.draw; | |
var | |
i, j: integer; | |
begin | |
for i:= 1 to length do | |
begin | |
for j:= 1 to width do | |
write(' * '); | |
writeln; | |
end; | |
end; | |
//--- | |
function TableTop.getmaterial(): string; | |
begin | |
getmaterial := material; | |
end; | |
procedure TableTop.setmaterial( m: string); | |
begin | |
material := m; | |
end; | |
procedure TableTop.displaydetails; | |
begin | |
writeln('Table Top: ', self.getlength(), ' by ' , self.getwidth()); | |
writeln('Material: ', self.getmaterial()); | |
end; | |
procedure TableTop.draw(); | |
var | |
i, j: integer; | |
begin | |
for i:= 1 to length do | |
begin | |
for j:= 1 to width do | |
write(' * '); | |
writeln; | |
end; | |
writeln('Material: ', material); | |
end; | |
var | |
r1: Rectangle; | |
pr1: ^Rectangle; | |
var | |
tt1: TableTop; | |
*) | |
type | |
Rectangle = class | |
private | |
length, width: integer; | |
public | |
constructor create(l, w: integer); | |
procedure setlength(l: integer); | |
function getlength(): integer; | |
procedure setwidth(w: integer); | |
function getwidth(): integer; | |
procedure draw; | |
end; | |
var | |
r1: Rectangle; | |
constructor Rectangle.create(l, w: integer); | |
begin | |
length := l; | |
width := w; | |
end; | |
procedure Rectangle.setlength(l: integer); | |
begin | |
length := l; | |
end; | |
procedure Rectangle.setwidth(w: integer); | |
begin | |
width :=w; | |
end; | |
function Rectangle.getlength(): integer; | |
begin | |
getlength := length; | |
end; | |
function Rectangle.getwidth(): integer; | |
begin | |
getwidth := width; | |
end; | |
procedure Rectangle.draw; | |
var | |
i, j: integer; | |
begin | |
for i:= 1 to length do | |
begin | |
for j:= 1 to width do | |
write(' * '); | |
writeln; | |
end; | |
end; | |
//-------------------- | |
type | |
Books = Class | |
protected | |
title : String; | |
price: real; | |
public | |
constructor Create(t : String; p: real); //default constructor | |
procedure setTitle(t : String); //sets title for a book | |
function getTitle() : String; //retrieves title | |
procedure setPrice(p : real); //sets price for a book | |
function getPrice() : real; //retrieves price | |
procedure Display(); virtual; // display details of a book | |
end; | |
(* Creating a derived class *) | |
type | |
Novels = Class(Books) | |
private | |
author: String; | |
public | |
constructor Create(t: String); overload; | |
constructor Create(a: String; t: String; p: real); overload; | |
procedure setAuthor(a: String); // sets author for a book | |
function getAuthor(): String; // retrieves author name | |
procedure Display(); override; | |
end; | |
var | |
n1, n2: Novels; | |
//default constructor | |
constructor Books.Create(t : String; p: real); | |
begin | |
title := t; | |
price := p; | |
end; | |
procedure Books.setTitle(t : String); //sets title for a book | |
begin | |
title := t; | |
end; | |
function Books.getTitle() : String; //retrieves title | |
begin | |
getTitle := title; | |
end; | |
procedure Books.setPrice(p : real); //sets price for a book | |
begin | |
price := p; | |
end; | |
function Books.getPrice() : real; //retrieves price | |
begin | |
getPrice:= price; | |
end; | |
procedure Books.Display(); | |
begin | |
writeln('Title: ', title); | |
writeln('Price: ', price); | |
end; | |
(* Now the derived class methods *) | |
constructor Novels.Create(t: String); | |
begin | |
inherited Create(t, 0.0); | |
author:= ' '; | |
end; | |
constructor Novels.Create(a: String; t: String; p: real); | |
begin | |
inherited Create(t, p); | |
author:= a; | |
end; | |
procedure Novels.setAuthor(a : String); //sets author for a book | |
begin | |
author := a; | |
end; | |
function Novels.getAuthor() : String; //retrieves author | |
begin | |
getAuthor := author; | |
end; | |
procedure Novels.Display(); | |
begin | |
writeln('Title: ', title); | |
writeln('Price: ', price:5:2); | |
writeln('Author: ', author); | |
end; | |
// Interfaces | |
// type | |
// Mail = Interface | |
// Procedure SendMail; | |
// Procedure GetMail; | |
// end; | |
// Report = Class(TInterfacedObject, Mail) | |
// Procedure SendMail; | |
// Procedure GetMail; | |
// end; | |
// type | |
// Shape = ABSTRACT CLASS (Root) | |
// Procedure draw; ABSTRACT; | |
// ... | |
// end; | |
// Static Keyword | |
// type | |
// myclass=class | |
// num : integer;static; | |
// end; | |
// var | |
// h1, h2 : myclass; | |
//------------------------------------------------------ | |
{ This is a single line comment in pascal } | |
(* Here the main program block starts *) | |
begin | |
writeln('Hello, World!'); | |
//readkey; | |
// Integer signed 32-bit, Cardinal unsigned 32-bit, Shortint signed 8-bit, Smallint signed 16-bit | |
// Longint signed 32-bit, Int64 signed 64-bit, Byte unsigned 8-bit, Word unsigned 16-bit, Longword unsigned 32-bit | |
// not Bitwise NOT, and Bitwise AND, or Bitwise OR, xor Bitwise exclusive OR | |
// shl Bitwise shift left, shr Bitwise shift right | |
// writeln('Please enter your first name: '); | |
// readln(firstname); | |
// writeln('Please enter your surname: '); | |
// readln(surname); | |
// writeln; | |
// writeln(message, ' ', firstname, ' ', surname); | |
// writeln('Which drink do you want?'); | |
// drink := limejuice; | |
// writeln('You can drink ', drink); | |
// writeln( 'Enter your marks(1 - 100): '); | |
// readln(marks); | |
// writeln( 'Enter your grade(A - E): '); | |
// readln(grade); | |
// writeln('Marks: ' , marks, ' Grade: ', grade); | |
// writeln('Enter the radius of the circle'); | |
// readln(r); | |
// d := 2 * r; | |
// c := PI * d; | |
// writeln('The circumference of the circle is ',c:7:2); | |
// If statement | |
// a:= 10; | |
// (* check the boolean condition using if statement *) | |
// if( a < 20 ) then | |
// (* if condition is true then print the following *) | |
// writeln('a is less than 20 ' ); | |
// writeln('value of a is : ', a); | |
// a := 100; | |
// (* check the boolean condition *) | |
// if( a < 20 ) then | |
// (* if condition is true then print the following *) | |
// writeln('a is less than 20' ) | |
// else | |
// (* if condition is false then print the following *) | |
// writeln('a is not less than 20' ); | |
// writeln('value of a is : ', a); | |
// a := 100; | |
// b := 200; | |
// (* check the boolean condition *) | |
// if (a = 100) then | |
// (* if condition is true then check the following *) | |
// if ( b = 200 ) then | |
// (* if nested if condition is true then print the following *) | |
// writeln('Value of a is 100 and value of b is 200' ); | |
// writeln('Exact value of a is: ', a ); | |
// writeln('Exact value of b is: ', b ); | |
// case statement | |
// grade := 'A'; | |
// case (grade) of | |
// 'A' : writeln('Excellent!' ); | |
// 'B', 'C': writeln('Well done' ); | |
// 'D' : writeln('You passed' ); | |
// 'F' : writeln('Better try again' ); | |
// end; | |
// writeln('Your grade is ', grade ); | |
// grade := 'F'; | |
// case (grade) of | |
// 'A' : writeln('Excellent!' ); | |
// 'B', 'C': writeln('Well done' ); | |
// 'D' : writeln('You passed' ); | |
// else | |
// writeln('You really did not study right!' ); | |
// end; | |
// writeln('Your grade is ', grade ); | |
// a := 100; | |
// b := 200; | |
// case (a) of | |
// 100: begin | |
// writeln('This is part of outer statement' ); | |
// case (b) of | |
// 200: writeln('This is part of inner statement' ); | |
// end; | |
// end; | |
// end; | |
// writeln('Exact value of a is : ', a ); | |
// writeln('Exact value of b is : ', b ); | |
// Loop | |
// While | |
// a := 10; | |
// while a < 20 do | |
// begin | |
// writeln('value of a: ', a); | |
// a := a + 1; | |
// end; | |
// For | |
// for a := 10 to 20 do | |
// begin | |
// writeln('value of a: ', a); | |
// end; | |
// repeat until | |
// a := 10; | |
// (* repeat until loop execution *) | |
// repeat | |
// writeln('value of a: ', a); | |
// a := a + 1 | |
// until a = 20; | |
// for i := 2 to 50 do | |
// begin | |
// for j := 2 to i do | |
// if (i mod j)=0 then | |
// break; {* if factor found, not prime *} | |
// if(j = i) then | |
// writeln(i , ' is prime' ); | |
// end; | |
// break | |
// a := 10; | |
// (* while loop execution *) | |
// while a < 20 do | |
// begin | |
// writeln('value of a: ', a); | |
// a:=a +1; | |
// if( a > 15) then | |
// (* terminate the loop using break statement *) | |
// break; | |
// end; | |
// continue | |
// a := 10; | |
// (* repeat until loop execution *) | |
// repeat | |
// if( a = 15) then | |
// begin | |
// (* skip the iteration *) | |
// a := a + 1; | |
// continue; | |
// end; | |
// writeln('value of a: ', a); | |
// a := a+1; | |
// until ( a = 20 ); | |
// goto | |
// a := 10; | |
// (* repeat until loop execution *) | |
// 1: repeat | |
// if( a = 15) then | |
// begin | |
// (* skip the iteration *) | |
// a := a + 1; | |
// goto 1; | |
// end; | |
// writeln('value of a: ', a); | |
// a:= a +1; | |
// until a = 20; | |
// function | |
// a := 100; | |
// b := 200; | |
// (* calling a function to get max value *) | |
// ret := max_i(a, b); | |
// writeln( 'Max_i value is : ', ret ); | |
// writeln(' Enter three numbers: '); | |
// readln( a, b, c); | |
// findMin(a, b, c, min); (* Procedure call *) | |
// writeln(' Minimum: ', min); | |
// recursion | |
// writeln(' Enter a number: '); | |
// readln(num); | |
// f := fact(num); | |
// writeln(' Factorial ', num, ' is: ' , f); | |
// for i:= 1 to 10 do | |
// write(fibonacci (i), ' '); | |
// a := 100; | |
// b := 200; | |
// writeln('Before swap, value of a : ', a ); | |
// writeln('Before swap, value of b : ', b ); | |
// (* calling the procedure swap by value *) | |
// //swap(a, b); | |
// swap_ref(a, b); | |
// writeln('After swap, value of a : ', a ); | |
// writeln('After swap, value of b : ', b ); | |
// greetings := 'Hello '; | |
// message := 'Good Day!'; | |
// writeln('Please Enter your Name'); | |
// readln(name); | |
// writeln('Please Enter the name of your Organisation'); | |
// readln(organisation); | |
// writeln(greetings, name, ' from ', organisation); | |
// writeln(message); | |
// str1 := 'Hello '; | |
// str2 := 'There!'; | |
// (* copy str1 into str3 *) | |
// str3 := str1; | |
// writeln('appendstr( str3, str1) : ', str3 ); | |
// (* concatenates str1 and str2 *) | |
// appendstr( str1, str2); | |
// writeln( 'appendstr( str1, str2) ' , str1 ); | |
// str4 := str1 + str2; | |
// writeln('Now str4 is: ', str4); | |
// (* total lenghth of str4 after concatenation *) | |
// len := byte(str4[0]); | |
// writeln('Length of the final string str4: ', len); | |
// bool | |
// exit := true | |
// if (exit) then | |
// writeln(' Good Bye!') | |
// else | |
// writeln('Please Continue'); | |
// (* initialize elements of array n to 0 *) | |
// for i := 1 to 10 do | |
// n[ i ] := i + 100; (* set element at location i to i + 100 *) | |
// (* output each array element's value *) | |
// for j:= 1 to 10 do | |
// writeln('Element[', j, '] = ', n[j] ); | |
// 2D array | |
// for i:=0 to 3 do | |
// for j:=0 to 3 do | |
// ar[i,j]:= i * j; | |
// for i:=0 to 3 do | |
// begin | |
// for j:=0 to 3 do | |
// write(ar[i,j]:2,' '); | |
// writeln; | |
// end; | |
// 2D dynamic array | |
// setlength(ar,5,5); | |
// for i:=0 to 4 do | |
// for j:=0 to 4 do | |
// ar[i,j]:= i * j; | |
// for i:=0 to 4 do | |
// begin | |
// for j:= 0 to 4 do | |
// write(ar[i,j]:2,' '); | |
// writeln; | |
// end; | |
// packed array | |
// These arrays are bit-packed, i.e., each character or truth values are stored in consecutive | |
// bytes instead of using one storage unit, usually a word (4 bytes or more). | |
// for i:=0 to 3 do | |
// for j:=0 to 3 do | |
// ar[i,j]:= i * j; | |
// for i:=0 to 3 do | |
// begin | |
// for j:=0 to 3 do | |
// write(ar[i,j]:2,' '); | |
// writeln; | |
// end; | |
// (* Passing the array to the function *) | |
// average := avg( balance ) ; | |
// (* output the returned value *) | |
// writeln( 'Average value is: ', average:7:2); | |
// pointers | |
// iptr := nil; | |
// number := 100; | |
// writeln('Number is: ', number); | |
// iptr := @number; | |
// writeln('iptr points to a value: ', iptr^); | |
// iptr^ := 200; | |
// writeln('Number is: ', number); | |
// writeln('iptr points to a value: ', iptr^); | |
// y := addr(iptr); | |
// writeln(y^); | |
// Incrementing a Pointer | |
// (* let us have array address in pointer *) | |
// iptr := @arr[1]; | |
// for i := 1 to MAX do | |
// begin | |
// y:= addr(iptr); | |
// writeln('Address of arr[', i, '] = ' , y^ ); | |
// writeln(' Value of arr[', i, '] = ' , iptr^ ); | |
// (* move to the next location *) | |
// inc(iptr); | |
// end; | |
// Decrementing a Pointer | |
// (* let us have array address in pointer *) | |
// iptr := @arr[MAX]; | |
// for i := MAX downto 1 do | |
// begin | |
// y:= addr(iptr); | |
// writeln('Address of arr[', i, '] = ' , y^ ); | |
// writeln(' Value of arr[', i, '] = ' , iptr^ ); | |
// (* move to the next location *) | |
// dec(iptr); | |
// end; | |
// Pointer Comparisons | |
// i:=1; | |
// (* let us have array address in pointer *) | |
// iptr := @arr[1]; | |
// while (iptr <= @arr[MAX]) do | |
// begin | |
// y:= addr(iptr); | |
// writeln('Address of arr[', i, '] = ' , y^ ); | |
// writeln(' Value of arr[', i, '] = ' , iptr^ ); | |
// (* move to the next location *) | |
// inc(iptr); | |
// i := i+1; | |
// end; | |
// Array of ptr | |
// (* let us assign the addresses to parray *) | |
// for i:= 1 to MAX do | |
// parray[i] := @arr[i]; | |
// (* let us print the values using the pointer array *) | |
// for i:=1 to MAX do | |
// writeln(' Value of arr[', i, '] = ' , parray[i]^ ); | |
// String List | |
// for i := 1 to MAX do | |
// parray[i] := @names[i]; | |
// for i:= 1 to MAX do | |
// writeln('Value of names[', i, '] = ' , parray[i]^ ); | |
// Pointer to pointer | |
// num := 3000; | |
// (* take the address of var *) | |
// ptr := @num; | |
// (* take the address of ptr using address of operator @ *) | |
// pptr := @ptr; | |
// (* let us see the value and the adresses *) | |
// x:= addr(ptr); | |
// y := addr(pptr); | |
// writeln('Value of num = ', num ); | |
// writeln('Value available at ptr^ = ', ptr^ ); | |
// writeln('Value available at pptr^^ = ', pptr^^); | |
// writeln('Address at ptr = ', x^); | |
// writeln('Address at pptr = ', y^); | |
// Passing pointers to function | |
// i := getNumber(ptr); | |
// writeln(' Here the pointer brings the value ', i); | |
// Return pointer from function | |
// i := 100; | |
// iptr := getValue(i); | |
// writeln('Value deferenced: ', iptr^); | |
// records | |
// (* book 1 specification *) | |
// Book1.title := 'C Programming'; | |
// Book1.author := 'Nuha Ali '; | |
// Book1.subject := 'C Programming Tutorial'; | |
// Book1.book_id := 6495407; | |
// (* book 2 specification *) | |
// Book2.title := 'Telecom Billing'; | |
// Book2.author := 'Zara Ali'; | |
// Book2.subject := 'Telecom Billing Tutorial'; | |
// Book2.book_id := 6495700; | |
// (* print Book1 info *) | |
// writeln ('Book 1 title : ', Book1.title); | |
// writeln('Book 1 author : ', Book1.author); | |
// writeln( 'Book 1 subject : ', Book1.subject); | |
// writeln( 'Book 1 book_id : ', Book1.book_id); | |
// writeln; | |
// (* print Book2 info *) | |
// writeln ('Book 2 title : ', Book2.title); | |
// writeln('Book 2 author : ', Book2.author); | |
// writeln( 'Book 2 subject : ', Book2.subject); | |
// writeln( 'Book 2 book_id : ', Book2.book_id); | |
//--- | |
// (* print Book1 info *) | |
// printbook(Book1); | |
// writeln; | |
// (* print Book2 info *) | |
// printbook(Book2); | |
// Pointers to Records | |
// new(Book1); | |
// new(book2); | |
// (* book 1 specification *) | |
// Book1^.title := 'C Programming'; | |
// Book1^.author := 'Nuha Ali '; | |
// Book1^.subject := 'C Programming Tutorial'; | |
// Book1^.book_id := 6495407; | |
// (* book 2 specification *) | |
// Book2^.title := 'Telecom Billing'; | |
// Book2^.author := 'Zara Ali'; | |
// Book2^.subject := 'Telecom Billing Tutorial'; | |
// Book2^.book_id := 6495700; | |
// (* print Book1 info *) | |
// writeln ('Book 1 title : ', Book1^.title); | |
// writeln('Book 1 author : ', Book1^.author); | |
// writeln( 'Book 1 subject : ', Book1^.subject); | |
// writeln( 'Book 1 book_id : ', Book1^.book_id); | |
// (* print Book2 info *) | |
// writeln ('Book 2 title : ', Book2^.title); | |
// writeln('Book 2 author : ', Book2^.author); | |
// writeln( 'Book 2 subject : ', Book2^.subject); | |
// writeln( 'Book 2 book_id : ', Book2^.book_id); | |
// dispose(Book1); | |
// dispose(Book2); | |
// With Statement | |
// (* book 1 specification *) | |
// With Book1 do | |
// begin | |
// title := 'C Programming'; | |
// author := 'Nuha Ali '; | |
// subject := 'C Programming Tutorial'; | |
// book_id := 6495407; | |
// end; | |
// Variant (Union) | |
// i := 100; | |
// v:= i; | |
// writeln('Variant as Integer: ', v); | |
// r:= 234.345; | |
// v:= r; | |
// writeln('Variant as real: ', v); | |
// c := red; | |
// v := c; | |
// writeln('Variant as Enumerated data: ', v); | |
// as:= ' I am an AnsiString'; | |
// v:= as; | |
// writeln('Variant as AnsiString: ', v); | |
// Set | |
// c:= [red, blue, yellow, green, white, black, orange]; | |
// displayColors(c); | |
// c:=[red, blue]+[yellow, green]; | |
// displayColors(c); | |
// c:=[red, blue, yellow, green, white, black, orange] - [green, white]; | |
// displayColors(c); | |
// c:= [red, blue, yellow, green, white, black, orange]*[green, white]; | |
// displayColors(c); | |
// c:= [red, blue, yellow, green]><[yellow, green, white, black]; | |
// displayColors(c); | |
// file handling | |
// write | |
// Assign(f,'students.dat'); | |
// Rewrite(f); | |
// Student.s_name := 'John Smith'; | |
// Student.s_addr := 'United States of America'; | |
// Student.s_batchcode := 'Computer Science'; | |
// Write(f,Student); | |
// Close(f); | |
// // read | |
// assign(f, 'students.dat'); | |
// reset(f); | |
// while not eof(f) do | |
// begin | |
// read(f,Student); | |
// writeln('Name: ',Student.s_name); | |
// writeln('Address: ',Student.s_addr); | |
// writeln('Batch Code: ', Student.s_batchcode); | |
// end; | |
// close(f); | |
// writeln('Enter the File Name: '); | |
// readln(filename); | |
// assign(rainfile, filename); | |
// writedata(rainfile); | |
// computeAverage(rainfile); | |
// writeln('Enter the file name: '); | |
// readln(filename); | |
// assign(myfile, filename); | |
// rewrite(myfile); | |
// writeln(myfile, 'Note to Students: '); | |
// writeln(myfile, 'For details information on Pascal Programming'); | |
// writeln(myfile, 'Contact: Tutorials Point'); | |
// writeln('Completed writing'); | |
// close(myfile); | |
// assign(myfile, 'contact.txt'); | |
// append(myfile); | |
// writeln('Contact Details'); | |
// writeln('webmaster@tutorialspoint.com'); | |
// close(myfile); | |
// (* let us read from this file *) | |
// assign(myfile, 'contact.txt'); | |
// reset(myfile); | |
// while not eof(myfile) do | |
// begin | |
// readln(myfile, info); | |
// writeln(info); | |
// end; | |
// close(myfile); | |
// memory allocation | |
// name:= 'Zara Ali'; | |
// new(description); | |
// if not assigned(description) then | |
// writeln(' Error - unable to allocate required memory') | |
// else | |
// description^ := 'Zara ali a DPS student in class 10th'; | |
// writeln('Name = ', name ); | |
// writeln('Description: ', description^ ); | |
// name:= 'Zara Ali'; | |
// description := getmem(200); | |
// if not assigned(description) then | |
// writeln(' Error - unable to allocate required memory') | |
// else | |
// description^ := 'Zara ali a DPS student in class 10th'; | |
// writeln('Name = ', name ); | |
// writeln('Description: ', description^ ); | |
// freemem(description); | |
// name:= 'Zara Ali'; | |
// desp := 'Zara ali a DPS student.'; | |
// description := getmem(30); | |
// if not assigned(description) then | |
// writeln('Error - unable to allocate required memory') | |
// else | |
// description^ := desp; | |
// (* Suppose you want to store bigger description *) | |
// description := reallocmem(description, 100); | |
// desp := desp + ' She is in class 10th.'; | |
// description^:= desp; | |
// writeln('Name = ', name ); | |
// writeln('Description: ', description^ ); | |
// freemem(description); | |
// unit (modules) | |
// textbackground(white); (* gives a white background *) | |
// clrscr; (*clears the screen *) | |
// textcolor(green); (* text color is green *) | |
// gotoxy(30, 4); (* takes the pointer to the 4th line and 30th column*) | |
// writeln('This program calculates area of a triangle:'); | |
// writeln('Area = area = sqrt(s(s-a)(s-b)(s-c))'); | |
// writeln('S stands for semi-perimeter'); | |
// writeln('a, b, c are sides of the triangle'); | |
// writeln('Press any key when you are ready'); | |
// readkey; | |
// clrscr; | |
// gotoxy(20,3); | |
// write('Enter a: '); | |
// readln(a); | |
// gotoxy(20,5); | |
// write('Enter b:'); | |
// readln(b); | |
// gotoxy(20, 7); | |
// write('Enter c: '); | |
// readln(c); | |
// s := (a + b + c)/2.0; | |
// area := sqrt(s * (s - a)*(s-b)*(s-c)); | |
// gotoxy(20, 9); | |
// writeln('Area: ',area:10:3); | |
// readkey; | |
// clrscr; | |
// l := 5.4; | |
// w := 4.7; | |
// area := RectangleArea(l, w); | |
// writeln('Area of Rectangle 5.4 x 4.7 is: ', area:7:3); | |
// r:= 7.0; | |
// area:= CircleArea(r); | |
// writeln('Area of Circle with radius 7.0 is: ', area:7:3); | |
// a := 3.0; | |
// b:= 4.0; | |
// c:= 5.0; | |
// area:= TriangleArea(a, b, c); | |
// writeln('Area of Triangle 3.0 by 4.0 by 5.0 is: ', area:7:3); | |
// Date time | |
// writeln ('Current time : ',TimeToStr(Time)); | |
// writeln ('Date : ',Date); | |
// DeCodeDate (Date,YY,MM,DD); | |
// writeln (format ('Today is (DD/MM/YY): %d/%d/%d ',[dd,mm,yy])); | |
// writeln ('Date and Time at the time of writing : ',DateTimeToStr(Now)); | |
//--- | |
// writeln ('Date and Time at the time of writing : ',DateTimeToStr(Now)); | |
// writeln('Today is ',LongDayNames[DayOfWeek(Date)]); | |
// writeln; | |
// writeln('Details of Date: '); | |
// DecodeDate(Date,year,month,day); | |
// writeln (Format ('Day: %d',[day])); | |
// writeln (Format ('Month: %d',[month])); | |
// writeln (Format ('Year: %d',[year])); | |
// writeln; | |
// writeln('Details of Time: '); | |
// DecodeTime(Time,hr, min, sec, ms); | |
// writeln (format('Hour: %d:',[hr])); | |
// writeln (format('Minutes: %d:',[min])); | |
// writeln (format('Seconds: %d:',[sec])); | |
// writeln (format('Milliseconds: %d:',[hr])); | |
// Object (on stack) | |
// r1.setlength(3); | |
// r1.setwidth(7); | |
// writeln('Draw a rectangle:', r1.getlength(), ' by ' , r1.getwidth()); | |
// r1.draw; | |
// new(pr1); | |
// pr1^.setlength(5); | |
// pr1^.setwidth(4); | |
// writeln('Draw a rectangle:', pr1^.getlength(), ' by ' ,pr1^.getwidth()); | |
// pr1^.draw; | |
// dispose(pr1); | |
// cosntructor destructor | |
// r1.init(3, 7); | |
// writeln('Draw a rectangle:', r1.getlength(), ' by ' , r1.getwidth()); | |
// r1.draw; | |
// new(pr1, init(5, 4)); | |
// writeln('Draw a rectangle:', pr1^.getlength(), ' by ',pr1^.getwidth()); | |
// pr1^.draw; | |
// pr1^.init(7, 9); | |
// writeln('Draw a rectangle:', pr1^.getlength(), ' by ' ,pr1^.getwidth()); | |
// pr1^.draw; | |
// dispose(pr1); | |
// r1.done; | |
// Inheritance Object | |
// tt1.setlength(3); | |
// tt1.setwidth(7); | |
// tt1.setmaterial('Wood'); | |
// tt1.displaydetails(); | |
// writeln; | |
// writeln('Calling the Draw method'); | |
// tt1.draw(); | |
// Class (on heap) | |
// r1:= Rectangle.create(3, 7); | |
// writeln(' Draw Rectangle: ', r1.getlength(), ' by ' , r1.getwidth()); | |
// r1.draw; | |
// r1.setlength(4); | |
// r1.setwidth(6); | |
// writeln(' Draw Rectangle: ', r1.getlength(), ' by ' , r1.getwidth()); | |
// r1.draw; | |
// Inheritance Class | |
// n1 := Novels.Create('Gone with the Wind'); | |
// n2 := Novels.Create('Ayn Rand','Atlas Shrugged', 467.75); | |
// n1.setAuthor('Margaret Mitchell'); | |
// n1.setPrice(375.99); | |
// n1.Display; | |
// n2.Display; | |
// Static Keyword | |
// h1:= myclass.create; | |
// h2:= myclass.create; | |
// h1.num := 12; | |
// writeln(n2.num); | |
// h2.num := 31; | |
// writeln(h1.num); | |
// writeln(myclass.num); | |
// myclass.num := myclass.num + 20; | |
// writeln(h1.num); | |
// writeln(h2.num); | |
//-------------------------------------------------------------- | |
end. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment