Sunday, September 02, 2007

How to validate an IBAN

IBAN stands for International Bank Account Number
Example : CH10002300A1023502601

Here is a method how to check if an IBAN is valid:

function ChangeAlpha(input: string): string;
// A -> 10, B -> 11, C -> 12 ...
var
a: Char;
begin
Result := input;
for a := 'A' to 'Z' do
begin
Result := StringReplace(Result, a, IntToStr(Ord(a) - 55), [rfReplaceAll]);
end;
end;

function CalculateDigits(iban: string): Integer;
var
v, l: Integer;
alpha: string;
number: Longint;
rest: Integer;
begin
iban := UpperCase(iban);
if Pos('IBAN', iban) > 0 then
Delete(iban, Pos('IBAN', iban), 4);
iban := iban + Copy(iban, 1, 4);
Delete(iban, 1, 4);
iban := ChangeAlpha(iban);
v := 1;
l := 9;
rest := 0;
alpha := '';
try
while
v <= Length(iban) do
begin
if
l > Length(iban) then
l := Length(iban);
alpha := alpha + Copy(iban, v, l);
number := StrToInt(alpha);
rest := number mod 97;
v := v + l;
alpha := IntToStr(rest);
l := 9 - Length(alpha);
end;
except
rest := 0;
end;
Result := rest;
end;

function CheckIBAN(iban: string): Boolean;
begin
iban := StringReplace(iban, ' ', '', [rfReplaceAll]);
if CalculateDigits(iban) = 1 then
Result := True
else
Result := False;
end;


Implementing Floyd-Warshall algorithm in Delphi

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Floyd-Warshall algorithm - shortest path problem - Graph Theory
//
// http://de.wikipedia.org/wiki/Algorithmus_von_Floyd_und_Warshall
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, ExtCtrls;

type
typ = array [1..50,1..50] of Integer;
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
sg1: TStringGrid;
Button2: TButton;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button3: TButton;
i1: TImage;
sg2: TStringGrid;
Edit4: TEdit;
sg3: TStringGrid;
Label5: TLabel;
Label6: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private

public
procedure
floyd2(n: Integer; w: typ; var d: typ; var p: typ);
procedure path(q: Integer; r: Integer);
procedure laa(teta: Integer; r: Integer; x: Integer; y: Integer; i1: TImage);
end;

var
Form1: TForm1;
w: typ;
d: typ;
p: typ;
n, cont: Integer;
v: array of Integer;
X, y: array of Integer;

implementation

procedure
tform1.path(q: Integer; r: Integer);
begin
if not
(p[q, r] = 0) then
begin
path(q, p[q, r]);
label4.Caption := label4.Caption + IntToStr(p[q, r]) + ',';
path(p[q, r], r);
end;
end;

procedure tform1.floyd2(n: Integer; w: typ; var d: typ; var p: typ);
var
i, j, k: Integer;
begin
for
i := 1 to n do
for
j := 1 to n do
p[i, j] := 0;
d := w;
for k := 1 to n do
for
i := 1 to n do
for
j := 1 to n do
begin
if
(d[i, k] + d[k, j] <>then
begin
p[i, j] := k;
d[i, j] := d[i][k] + d[k][j];
end;
end;
end;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i, j: Integer;
s: string;
e: TEdit;
begin
Button3Click(Sender);
n := StrToInt(edit1.Text);
setlength(v, n);
for i := 1 to n do
for
j := 1 to n do
w[i, j] := StrToInt(sg1.Cells[i, j]);
floyd2(n, w, d, p);
label4.Caption := edit2.Text + ',';
path(StrToInt(edit2.Text), StrToInt(edit3.Text));
Button3Click(Sender);
label4.Caption := label4.Caption + edit3.Text + '.';
s := label4.Caption;
i := 1;
label3.Caption := '';
cont := 0;
while not (s[i] = '.') do
begin
label3.Caption := s[i] + label3.Caption;
if s[i] = ',' then i := i + 1
else
begin
if
cont <> 0 then
begin
i1.Canvas.MoveTo(x[cont], y[cont]);
i1.Canvas.LineTo(x[StrToInt(s[i])], y[StrToInt(s[i])]);
end;
cont := StrToInt(s[i]);
i := i + 1;
end;
end;

for i := 1 to n do
for
j := 1 to n do
sg2.Cells[i, j] := IntToStr(p[i, j]);
for i := 1 to n do
for
j := 1 to n do
sg3.Cells[i, j] := IntToStr(d[i, j]);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i, j: Integer;
begin
Button3Click(Sender);
sg1.Visible := True;
sg1.Cells[0,0] := 'W matris:';
sg1.RowCount := StrToInt(edit1.Text) + 1;
sg1.ColCount := StrToInt(edit1.Text) + 1;
sg2.Visible := True;
sg2.Cells[0,0] := 'Paths:';
sg2.RowCount := StrToInt(edit1.Text) + 1;
sg2.ColCount := StrToInt(edit1.Text) + 1;
sg3.Visible := True;
sg3.Cells[0,0] := 'D Matris:';
sg3.RowCount := StrToInt(edit1.Text) + 1;
sg3.ColCount := StrToInt(edit1.Text) + 1;
for i := 1 to StrToInt(edit1.Text) + 1 do
begin
sg1.Cells[0,i] := IntToStr(i);
sg1.Cells[i, 0] := IntToStr(i);
sg2.Cells[0,i] := IntToStr(i);
sg2.Cells[i, 0] := IntToStr(i);
sg3.Cells[0,i] := IntToStr(i);
sg3.Cells[i, 0] := IntToStr(i);
end;
for i := 1 to StrToInt(edit1.Text) + 1 do
begin
for
j := 1 to StrToInt(edit1.Text) + 1 do
begin
sg1.Cells[i, j] := IntToStr(Random(19) + 1);
if i = j then sg1.Cells[i, j] := '0';
end;
end;
//sg1.Width:=(strtoint(edit1.Text)+3)*sg1.ColWidths[0];
//sg1.Height:=(strtoint(edit1.Text)+3)*sg1.RowHeights[0];
end;

procedure TForm1.Button3Click(Sender: TObject);
var
i, j, k, l, r, rt: Integer;
centerx, centery: Integer;
rad, teta, alfax: Integer;
alfa: Extended;
a, b: TPoint;
begin
i1.Canvas.Brush.Style := bsSolid;
n := StrToInt(edit1.Text);
setlength(x, n + 1);
setlength(y, n + 1);
centery := i1.Width div 2;
centerx := i1.Height div 2;
rad := centerx - 20;
teta := 360 div n;
rt := 10;//pointer
i1.Canvas.Rectangle(0,0,i1.Width, i1.Height);
i1.Canvas.Pen.Color := clgreen;
i1.Canvas.Pen.Width := 3;
for i := 1 to n do
begin
Y[i] := centerx + trunc(rad * sin(teta * i * ((2 * 3.14) / 360)));
X[i] := centery + trunc(rad * cos(teta * i * ((2 * 3.14) / 360)));
l := y[i];
k := x[i];
r := 3;
i1.Canvas.Pie(k - r, l - r, k + r, l + r, 1,1,1,1);
end;
i1.Canvas.Pen.Width := 1;

for i := 1 to n do
for
j := 1 to n do
begin
if not
(w[i, j] = 0) then
begin
if
i = j then
begin
i1.Canvas.Pen.Color := clred;
i1.Canvas.Brush.Style := bsClear;
l := y[i];
k := x[i];
i1.Canvas.Pie(k, l, k + 6 * r, l + 6 * r, 1,1,1,1);
//loop
end;

if (i <> j) and (w[i, j] <> StrToInt(edit4.Text)) then
begin
i1.Canvas.Pen.Color := clblue;
i1.Canvas.Pen.Width := 1;
i1.Canvas.MoveTo(x[i], y[i]);
i1.Canvas.LineTo(x[j], y[j]);
// i1.Canvas.Chord();
end;
i1.Canvas.Pen.Width := 2;

{ if i if (y[i]-y[j])<>0 then alfa:=ArcTan((X[i]-x[j])/(y[j]-y[i])) else alfa:=pi/2;
if x[i]>x[j] then alfax:=round((180/Pi)*alfa+90);
if (x[i] if (x[i]y[j]) then alfax:=270+round((180/Pi)*alfa);
l:=x[j];k:=y[j];
laa(alfax,10,l,k,i1);
end;
if i>j then begin
if (y[i]-y[j])<>0 then alfa:=ArcTan((X[i]-x[j])/(y[j]-y[i])) else alfa:=pi/2;
if x[i]>x[j] then alfax:=round((180/Pi)*alfa+90);
if (x[i] if (x[i]y[j]) then alfax:=270+round((180/Pi)*alfa);
l:=x[i];k:=y[i];
laa(alfax,10,l,k,i1);
end;}
end;
end;
end;

procedure tform1.laa(teta: Integer; r: Integer; x: Integer; y: Integer; i1: TImage);
var
tetap: Extended;
begin
teta := teta mod 360;
tetap := (pi / 180) * (teta);
tetap := (pi / 180) * (teta - 30);
i1.Canvas.MoveTo(x - round(r * sin(tetap)), y - round(r * cos(tetap)));
i1.Canvas.LineTo(x, y);
tetap := (pi / 180) * (teta + 30);
i1.Canvas.MoveTo(x - round(r * sin(tetap)), y - round(r * cos(tetap)));
i1.Canvas.LineTo(x, y);
{end;
if (teta<=180) and (teta>=90) then begin
tetap:=(pi/180)*(teta-30);
i1.Canvas.MoveTo(x-round(r*cos(tetap)),y-round(r*sin(tetap)));
i1.Canvas.LineTo(x,y);
tetap:=(pi/180)*(teta+30);
i1.Canvas.MoveTo(x-round(r*cos(tetap)),y-round(r*sin(tetap)));
i1.Canvas.LineTo(x,y);
end;
if (teta<=270) and (teta>=180) then begin
tetap:=(pi/180)*(teta-30);
i1.Canvas.MoveTo(x+round(r*sin(tetap)),y+round(r*cos(tetap)));
i1.Canvas.LineTo(x,y);
tetap:=(pi/180)*(teta+30);
i1.Canvas.MoveTo(x+round(r*sin(tetap)),y+round(r*cos(tetap)));
i1.Canvas.LineTo(x,y);
end;
if (teta<=360) and (teta>=270) then begin
tetap:=(pi/180)*(teta-30);
i1.Canvas.MoveTo(x+round(r*cos(tetap)),y+round(r*sin(tetap)));
i1.Canvas.LineTo(x,y);
tetap:=(pi/180)*(teta+30);
i1.Canvas.MoveTo(x+round(r*cos(tetap)),y+round(r*sin(tetap)));
i1.Canvas.LineTo(x,y);
end;
}
end;

procedure TForm1.Button4Click(Sender: TObject);
var
i: Integer;
begin
for
i := 1 to 360 do
begin
laa(i, 10,100,100,i1);
ShowMessage(IntToStr(i));
end;
end;

end.

Saturday, September 01, 2007

How To Use MySQL With Delphi

For using this tutorial you need some stuff that you can get them all from respective vendor. (Almost all of them are free). you should install them all before you start working by this tutorial

First you need Delphi 6 (I did this pages by Enterprise edition but probably you can use professional or personal edition) for more information about Delphi refer to www.Borland.com

Then you need My SQL that you can get from www.MySQL.com

And also Myodbc which is available at WWW.MySQL.com

And finally you need to have Mycc which is available at www.MySQL.com

For this tutorial you need to make tutorial database which contain 4 different table

I did this one by means of Mycc.

At first you should register a server, to register a server follow the instruction mentioned below

1-Run Mycc

2-By default register server button is the first button on the left side of toolbar.

3-click on register server button and fill the form as I did. (figure Mycc1).

Note:

if you r using a network joined computer you should fill the host name field by your computer name or IP

Now push the test button if you did all jobs without mistake you will get no message else

You will get an error message which means you didn’t fill it right

Warning: Database name is mini_employee which defined as a normal database.

I set admin as user and password is 123.

So if you get error message check for following items

1-check if MySQL server is running (By default after you install MySQL it will run automatically)

2-check if you fill host name as mentioned?

3-check if you fill server name as you should do?

After you make testing sequence error free you can click add button to add the server to your server list.

After you register the server as mentioned you will see ‘Test server for my tutorial ‘ in the MySQL servers tree view

There is a nice icon for servers in this application, isn’t it?

To connect to the server double click on the Test server for my tutorial or click on connect button from toolbar

Now you are connected to a fresh MySQL server.

figure:Mycc1

To create database :

1-right clicks on databases and choose new database

2-fill database name by mini_employee and then choose okay.

Congratulation you made your database for this tutorial.

Okay now we assume you register your server and create your database its now I’ll teach you how to make table.

There is some differences between MySQL and Interbase you can learn the MySQL scripting in the MySQL manual wich is downloadable from http://www.MySQL.com

To make tables

1-double click on databases to make it open

2-double click on the mini_employee database.[you can click on connect button from tool bar instead]

Now you can see the tables item below of mini_employee

Creating tables is too easy by using Mycc

Just right click on the tables and select create new table

Now fill the field names and field type for each table as I mentioned in the tables

After you fill all field of each table click the save button and type table name for that as I write under each table.

For example I’ll fill one field of employee

1-type “emp_no” (without double quote) for field name.

2-select smallint from the combo box at the end of cell.

3-fill the length edit box by 6.

4-fill the default value by 0.

Note

Allow null means that if the field can left empty in data entry ?

If you don’t check allow null check box and put the field empty in data entry time you will get

a database level exception

Now create tables as u see in the tables below.

(My best wishes are with you to create tables successfully J )

Tables are:

1- Employee:

DDL for employee is

CREATE TABLE `employee` (

`emp_no` smallint(6) NOT NULL default '0',

`first_name` varchar(15) default NULL,

`last_name` varchar(20) default NULL,

`phone` varchar(15) default NULL,

`dept_no` smallint(6) default NULL,

`jobe_code` tinyint(4) default NULL,

`job_grade` tinyint(4) default NULL,

`salary` int(11) default NULL,

`hire_date` date default NULL,

`Full_name` tinyint(4) default NULL

) TYPE=MyISAM

You should fill the create table form like this:

Field name Field kind

emp_no

smallint(6) NOT NULL default '0'

first_name

varchar(15) default NULL

last_name

varchar(20) default NULL

Phone

varchar(15) default NULL

dept_no

smallint(6) default NULL

jobe_code

tinyint(4) default NULL

job_grade

tinyint(4) default NULL

salary

int(11) default NULL

hire_date

date default NULL

Full_name

tinyint(4) default NULL

2- department:

DDL for department is

CREATE TABLE `department` (

`dep_no` smallint(4) default NULL,

`department` varchar(30) default NULL,

`manager_no` smallint(6) default NULL,

`location` varchar(30) default NULL,

`phone` varchar(10) default NULL

) TYPE=MyISAM

You should fill the create table form like this:

Field name

Field kind

Dep_no

smallint(4) default NULL


Department

varchar(30) default NULL


Manager_no

smallint(6) default NULL


Location

varchar(30) default NULL


phone

varchar(10) default NULL


3-jobs:

DDL for job is

CREATE TABLE `jobs` (

`jobe_id` smallint(6) default NULL,

`job_grade` smallint(6) default NULL,

`job_title` varchar(10) default NULL

) TYPE=MyISAM

You should fill the create table form like this:

Field name

Field kind


jobe_id

smallint(6) default NULL



job_grade

smallint(6) default NULL



job_title

varchar(10) default NULL






4-projects:

DDL for projects is

CREATE TABLE `projects` (

`emp_no` smallint(4) default NULL,

`proj_name` varchar(15) default NULL,

`team_leader` smallint(6) default NULL,

`product` set('software','hardware','n/a','other') default 'software'

) TYPE=MyISAM

You should fill the create table form like this:

Field name

Field kind

Emp_no

smallint(4) default NULL


Proj_name

varchar(15) default NULL


Team_leader

smallint(6) default NULL


Product

set('software','hardware','n/a','other') default 'software'


After you make database and tables you need to make an odbc connection to your database

For this item you need to install Myodbc which is downloadable from www.MySQL.com

Its know we assume you install the driver now I’ll tell you how to make odbc connection for your database

1-Goto control panel

Note:

if you are in win2k or winxp goto Administrative Tools then select

2-open data sources (odbc)

Now you will see a window like this

now you should make a new connection by clicking on add button

its now select MySQL odbc 3.51 driver from the listbox (its probably at the end of list)

after you select the item and click on the finish button you need to fill some field of data to make your connection:

do it as I did

Warning:

Beware that the password field is 123 .

Now push the Test Data Source button if you did all job as yu should you will face with a successfully connected to … MessageJ.

Congratulation you did the first phase of learning MySQL J

If you get any fail message test if MySQL service is running?

if you fill database name correctly?

If you fill password and user name correctly?

Okay we assume you connected to database successfully

Now you should copy some files from MySQL dir\ lib\opt to your windows directory\system\

Note:

If you r using any NT edition of Microsoft windows you should copy the files into

Windows directory\system32 \

It’s now our programming by Delphi started

1-Start Delphi

The data access component which we use are BDE component tab which contain several component (listed below) but we will not use them all in this part of my tutorial probably you will learn more

About them in next part tutorial, we will just use

1-Table 2- Query 3- Database

2-Open a new project

3-add a datamodule to the new project and change name property to employee_DtMdl.

4-add a form to your project and change the name to details_frm.

5-add employee_DtMdl to use cluse of the form and frame ;)

6-rename the form1 to master_frm

7-save the (u can choose your desire name for the project and units ;))

Drop the component which listed below on the datamodule which you add to new project

Component

Name

Related properties

Database

Mini_emp_dtb

AliasName = mini_employee

DatabaseName= mini_employee

Name=mini_employee_db

Donot change the driver name property

Left it blank

Params:

USERNAME

Admin

PASSWORD

Sysdba

Query

Employee_qry

Database=mini_emp_dtb

Table

projects _tlb

Databasename= Mini_emp_dtb

TableName =projects

Mastersource= Employee_dtsr

Masterfield=emp_no

Indexfieldname= employee_no

Table

Jobs_tlb

Databasename= Mini_emp_dtb

TableName =jobs

Mastersource= Employee_dtsr

Masterfield=jobe_code

Indexfieldname=jobe_id

Table

department_tlb

Databasename= Mini_emp_dtb

TableName =employee_proj

Mastersource= Employee_dtsr

Masterfield=dept_no

Indexfieldname=dep_no

Datasource

projects _dtsr

Dataset= projects _tlb

Datasource

Jobs_dtsr

Dataset= Jobs_tlb

Datasource

department_dtsr

Dataset= department_tlb

Datasource

Employee_dtsr

Dataset=employee_qry


Now we did anything require for our data access part.
Lets
do some data control job and finish our first MySQL learning paper
Drop the components listed below on the form and make changes with their properties as listed in the Table

Component

Name

Properties

DbGride

Employee_grd

Datasource= employee_DtMdl. employee_qry

Dbgride

Projects_grd

Datasource= employee_DtMdl. projects _tlb

Statusbar

Main_stbar

Add 1 new panel and make it a text panel

Button

Exit_btn

Caption= cancel

button

Clear_btn

Caption=Clear

Bottom

Execute_btn

Caption = Execute

Memo

SQL_mem

Lines:select * from employee where emp_no = 2

Label

No matter

Caption=Enter your SQL command here

Label

No matter

Caption=Result of your SQL command

Label

No matter

Caption=Related project for selected employee

We need frame to show some other details about our database so we need another frame or form (we add frame because it dosnt need as resources as form need)

Component

Name

Properties

Dbgrid

Jobs_grd

Datasource= employee_DtMdl. Jobs_dtsr

Dbgrid

Dep_grid

Datasource= employee_DtMdl.department_drst

Label

No matter

Caption= Selected employee job details

Label

Nomatter

Caption=selected employee department details

Button

Close_btn

Cancel=true

Now we can start coding in delph to make our test application for MySQL accessing J

We will made a master details application to show some preadvanced topic of database programming

And a SQL input box will help us to execute our query by typing and clicking on execute button.J

So design form and frame as you can see below on figures: detail_frm and main_frm
After you designed the interface you need to write some code to make application working
So we will write code for the execute button to make our query execute what we will write
In the SQL_mem .

Note:

MySQL language has some difference with ISQL (Interbase SQL) PL SQL (Oracle SQL) and T-SQL

(Ms SQL server SQL). So you need to have a look at MySQL manual which is available in www.MySQL.com

We will write one SQL sample of them together.

Figure:details_frm

Figure:master_frm

Now make following changes in the components events

For details_frm:

procedure Tdetails_frm.Button1Click(Sender: TObject);

begin

self.Close;

end;

for master_frm:

procedure Tmaster_frm.Button3Click(Sender: TObject);

begin

application.Terminate;

end;

procedure Tmaster_frm.Button1Click(Sender: TObject);

begin

SQL_mem.Lines.Clear;

end;

procedure Tmaster_frm.Button2Click(Sender: TObject);

begin

employee_dtmdl.employee_qry.close;

employee_dtmdl.employee_qry.SQL:=SQL_mem.Lines;

try

employee_dtmdl.employee_qry.Open;

details_frm.Show;

except

application.MessageBox ('There is an error in your type SQL command',' Error');

end;

Now save the project and run it J.

Amazing hum? You did your first MySQL database application using odbc driver J

Congratulation.