PDA

View Full Version : Translation ???



hammer
22-03-2004, 05:45 PM
hello i am sorry to ask here but i didnt knew where to so here i go:

can someone here translate this c++ code below to pascal(delphi)???





#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_LINKS 100
#define INFINITY 999999
#define EXCLUDED 999998




class cNode
&#123;
public&#58;

char name&#91;10&#93;;
int weight&#91;MAX_LINKS&#93;;

cNode *linked&#91;MAX_LINKS&#93;;
bool visited&#91;MAX_LINKS&#93;;

void init&#40;&#41;
&#123;
for&#40;int i=0;i<MAX_LINKS;i++&#41; &#123; linked&#91;i&#93;=NULL;visited&#91;i&#93;=false;weight&#91;i&#93;=0; &#125;
&#125;

void setname&#40;char *text&#41;
&#123;
strcpy&#40;name,text&#41;;
&#125;

&#125;;

int linker=0;
int lenght=0;
int lenghts&#91;MAX_LINKS&#93;,final_lenghts&#91;MAX_LINKS&#93;,parent s&#91;MAX_LINKS&#93;;

int gpointer=0;
int start=1,finish=gpointer;
cNode *graph&#91;MAX_LINKS&#93;;


void pauz&#40;char *text&#41;
&#123;
cout << "\n" << text << "\n";
char por;
cin >> por;
&#125;

void draw_graph&#40;&#41;
&#123;
cout << "\n************* Drawing graph ********************\n";
for&#40;int X=1;X<=gpointer;X++&#41;
&#123;
for&#40;int Y=0;Y<MAX_LINKS;Y++&#41;
&#123;
if&#40;graph&#91;X&#93;->linked&#91;Y&#93;&#41;
&#123;
cout << "\n" << graph&#91;X&#93;->name << " -> " << graph&#91;X&#93;->linked&#91;Y&#93;->name << " &#58;&#58; weight=" << graph&#91;X&#93;->weight&#91;Y&#93;;
&#125;
&#125;
&#125;

cout << "\n************************************************ \n";
&#125;

bool sysedi&#40;cNode *from,cNode *target&#41;
&#123;
for&#40;int i=0;i<MAX_LINKS;i++&#41;
&#123;
if&#40; &#40;from->linked&#91;i&#93;&#41; && &#40;strcmp&#40;from->linked&#91;i&#93;->name,target->name&#41;==0&#41; &#41;
&#123;
linker=i;
return true;
&#125;
&#125;

return false;
&#125;

int find_smallest&#40;int masiv&#91;MAX_LINKS&#93;&#41;
&#123;
int min = INFINITY, returned=0;


for&#40;int i=1;i<=gpointer;i++&#41;
&#123;
if&#40;masiv&#91;i&#93;<min&#41; &#123; min=masiv&#91;i&#93;;returned=i;&#125;
&#125;

/*- vry6ta NE stoinostta a koe q sydyrja tazi stoinost -*/
return returned;
&#125;

void update_lenghts&#40;int vryh&#41;
&#123;
for&#40;int i=1;i<=gpointer;i++&#41;
&#123;
if&#40;&#40;lenghts&#91;i&#93;>lenghts&#91;vryh&#93;&#41;&&&#40;graph&#91;i&#93;!=graph&#91;vryh&#93;&#41;&#41;
&#123;
if&#40; &#40;lenghts&#91;i&#93;!=EXCLUDED&#41; && &#40;sysedi&#40;graph&#91;vryh&#93;,graph&#91;i&#93;&#41;&#41; && &#40; graph&#91;vryh&#93;->weight&#91;linker&#93; + lenghts&#91;vryh&#93; < lenghts&#91;i&#93;&#41; &#41;
&#123;
lenghts&#91;i&#93; = &#40;graph&#91;vryh&#93;->weight&#91;linker&#93;&#41; + &#40;lenghts&#91;vryh&#93;&#41;;
parents&#91;i&#93; = vryh;
&#125;
&#125;
&#125;

final_lenghts&#91;vryh&#93;=lenghts&#91;vryh&#93;;
lenghts&#91;vryh&#93;=EXCLUDED;
&#125;


bool ima_non_infinit&#40;&#41;
&#123;
for&#40;int i=1;i<=gpointer;i++&#41;
&#123;
if&#40;lenghts&#91;i&#93;<EXCLUDED&#41; return true;
&#125;

return false;
&#125;

void showpath&#40;int to&#41;
&#123;
if&#40;lenghts&#91;to&#93;==INFINITY&#41; &#123; cout << "\nNo path!\n";return; &#125;
cout << graph&#91;to&#93;->name << " <- ";
if&#40;parents&#91;start&#93;==parents&#91;to&#93;&#41; &#123; cout << graph&#91;start&#93;->name; return; &#125;
showpath&#40;parents&#91;to&#93;&#41;;
return;
&#125;


void perform&#40;&#41;
&#123;

for&#40;int k=1;k<=gpointer;k++&#41;
&#123;
if&#40;graph&#91;k&#93;==graph&#91;start&#93;&#41; &#123; lenghts&#91;k&#93;=EXCLUDED; &#125; else
if&#40;sysedi&#40;graph&#91;start&#93;,graph&#91;k&#93;&#41;&#41; &#123; lenghts&#91;k&#93;=graph&#91;start&#93;->weight&#91;linker&#93;; &#125; else
&#123; lenghts&#91;k&#93;=INFINITY; &#125;
parents&#91;k&#93;=start;
&#125;

while&#40;ima_non_infinit&#40;&#41;&#41; &#123; update_lenghts&#40;find_smallest&#40;lenghts&#41;&#41;; &#125;
update_lenghts&#40;find_smallest&#40;lenghts&#41;&#41;;
&#125;

bool seek_path&#40;&#41;
&#123;
cout << "\n************** Seeking path ********************\n";
cout << "Where we start from &#40;1.." << gpointer << "&#41; &#91;0=Exit&#93;&#58; "; cin >> start;

if&#40;start!=0&#41;
&#123;
perform&#40;&#41;;
cout << "Seek path to &#40;1.." << gpointer << "&#41; &#58; "; cin >> finish;
showpath&#40;finish&#41;;
cout << "\nLenght&#58; ";
if&#40;lenghts&#91;finish&#93;==INFINITY&#41; &#123; cout << "INFINITY"; &#125; else
if&#40;final_lenghts&#91;finish&#93;==EXCLUDED&#41; &#123; cout << "0"; &#125; else
&#123; cout << final_lenghts&#91;finish&#93;; &#125;
cout << "\n************************************************ \n";
return true;
&#125;
else
&#123;
if&#40;start==0&#41; &#123; cout << "\n************************************************ \n";return false;&#125;
&#125;

&#125;

int main&#40;&#41;
&#123;
for&#40;int i=0;i<MAX_LINKS;i++&#41; &#123; graph&#91;i&#93;=NULL; &#125;

int number=1; //broi vyzli v grafa
int X=1,Y=0; //rabotni broq4i
int weight=0;

cout << "Number of nodes in the Graph&#58; "; cin >> gpointer;

for&#40;int x=1;x<=gpointer;x++&#41;
&#123;
char temp&#91;10&#93;;
graph&#91;x&#93; = new cNode;
graph&#91;x&#93;->init&#40;&#41;;
graph&#91;x&#93;->setname&#40;itoa&#40;x,temp,10&#41;&#41;;
&#125;

for&#40;X=1;X<=gpointer;X++&#41;
&#123;
cout << "\n";

while&#40;number!=0&#41;
&#123;
cout << " node " << X << " is linked with &#40;'0' exits&#41;&#58; "; cin >> number;
if&#40;number==0&#41; break;
graph&#91;X&#93;->linked&#91;Y&#93;=graph&#91;number&#93;;

cout << " Weight between " << X << " and " << number << " = "; cin >> weight;cout << "\n";
graph&#91;X&#93;->weight&#91;Y&#93;=weight;

Y++;
&#125;

number=1;Y=0;weight=0;
&#125;
draw_graph&#40;&#41;;

while&#40;seek_path&#40;&#41;&#41;;

system&#40;"pause"&#41;;


return 0;
&#125;

Alimonster
23-03-2004, 01:28 PM
Here is a translation. It compiles but I've not run it, so bugs may exist:

program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;


const
MAX_LINKS = 100;
INFINITY = 999999;
EXCLUDED = 999998;

type
TNode = class
private
FName : string;
protected
public
Weight: array[0..MAX_LINKS - 1] of Integer;
Linked : array[0..MAX_LINKS - 1] of TNode;
Visited: array[0..MAX_LINKS - 1] of Boolean;

constructor Create;

property Name: string read FName write FName;
end;

//------------------------------------------------------------------------------

constructor TNode.Create;
var
i: Integer;
begin
inherited;

for i := 0 to MAX_LINKS - 1 do
begin
Linked[i] := nil;
Visited[i] := False;
Weight[i] := 0;
end;
end;

//------------------------------------------------------------------------------

var
Linker : Integer = 0;
Length : Integer = 0;
Lengths: array[0..MAX_LINKS - 1] of Integer;
Final_Lengths: array[0..MAX_LINKS - 1] of Integer;
Parents: array[0..MAX_LINKS - 1] of Integer;
GPointer: Integer = 0;
Start : Integer = 1;
Finish : Integer = 0;

Graph : array[0..MAX_LINKS - 1] of TNode;

//------------------------------------------------------------------------------

procedure Pause(const Msg: string);
begin
WriteLn(#13#10, Msg);
Read;
end;

//------------------------------------------------------------------------------

procedure Draw_Graph;
var
x: Integer;
y: Integer;
begin
WriteLn(#13#10, '************* Drawing graph ********************');

for x := 1 to GPointer do
begin
for y := 1 to MAX_LINKS - 1 do
begin
if Graph[x].Linked[y] <> nil then
begin
WriteLn(#13#10 + Graph[x].Name, ' -> ', Graph[x].Linked[y].Name, ' :: weight=', Graph[x].Weight[y]);
end;
end;
end;

WriteLn(#13#10, '************************************************' , #13#10);
end;

//------------------------------------------------------------------------------

function sysedi(From, Target: TNode): Boolean;
var
i: Integer;
begin
for i := 0 to MAX_LINKS - 1 do
begin
if (From.Linked[i] <> nil) and (From.Linked[i].Name = Target.Name) then
begin
Linker := i;
Result := True;
Exit;
end;
end;

Result := False;
end;

//------------------------------------------------------------------------------

function Find_Smallest(masiv: array of Integer): Integer;
var
Min: Integer;
i : Integer;
begin
Result := 0;
Min := INFINITY;

for i := 1 to GPointer do
begin
if Masiv[i] < Min then
begin
Min := Masiv[i];
Result := i;
end;
end;
end;

//------------------------------------------------------------------------------

procedure Update_Lengths(vryh: Integer);
var
i: Integer;
begin
for i := 1 to GPointer do
begin
if (Lengths[i] > Lengths[vryh]) and (Graph[i] <> Graph[vryh]) then
begin
if (Lengths[i] <> EXCLUDED) and (sysedi(graph[vryh], graph[i])) and (graph[vryh].weight[Linker] + Lengths[vryh] < Lengths[i]) then
begin
Lengths[i] := (Graph[vryh].Weight[Linker]) + Lengths[vryh];
Parents[i] := vryh;
end;
end;
end;

Final_Lengths[vryh] := Lengths[vryh];
Lengths[vryh] := EXCLUDED;
end;

//------------------------------------------------------------------------------

function ima_non_infinit: Boolean;
var
i: Integer;
begin
for i := 1 to GPointer do
begin
if lengths[i] < EXCLUDED then
begin
Result := True;
Exit;
end;
end;

Result := False;
end;

//------------------------------------------------------------------------------

procedure ShowPath(To_: Integer);
begin
if Lengths[To_] = INFINITY then
begin
WriteLn(#13#10, 'No path!');
Exit;
end;

Write(Graph[To_].Name, ' <- ');

if Parents[Start] = Parents[to_] then
begin
Write(Graph[Start].Name);
Exit;
end;

ShowPath(Parents[To_]);
end;

//------------------------------------------------------------------------------

procedure Perform;
var
k: Integer;
begin
for k := 1 to GPointer do
begin
if Graph[k] = Graph[Start] then
begin
Lengths[K] := EXCLUDED;
end
else
if sysedi(Graph[Start], Graph[k]) then
begin
Lengths[K] := Graph[Start].Weight[Linker];
end
else
begin
Lengths[k] := INFINITY;
end;

Parents[k] := Start;
end;

while ima_non_infinit do
begin
Update_Lengths(Find_Smallest(Lengths));
end;

Update_Lengths(Find_Smallest(Lengths));
end;

//------------------------------------------------------------------------------

function Seek_Path: Boolean;
begin
WriteLn(#13#10, '************** Seeking path ********************');

WriteLn('Where we start from (1..', gpointer, ') [0=Exit]: ');
Read(Start);


if Start <> 0 then
begin
Perform;
Write('Seek path to (1..', GPointer, ') : ');
Read(Finish);

ShowPath(Finish);

WriteLn(#13#10, 'Length: ');

if Lengths[Finish] = INFINITY then
Write('INFINITY')
else
if Final_Lengths[Finish] = EXCLUDED then
Write('0')
else
begin
WriteLn(Final_Lengths[Finish]);
WriteLn('***************************************** *******');
end;

Result := True;
end
else
begin
WriteLn(#13#10, '************************************************' );
Result := False;
end;
end;

//------------------------------------------------------------------------------

procedure Main;
var
i: Integer;
Number: Integer; //broi vyzli v grafa
X: Integer; //rabotni broq4i
Y: Integer;
Weight: Integer;
begin
Number := 1;
//Weight := 0;
y := 0;


for i := Low(Graph) to High(Graph) do
Graph[i] := nil;

Write('Number of nodes in the graph: ');
Read(GPointer);

for x := 1 to GPointer do
begin
Graph[x] := TNode.Create;
Graph[x].Name := IntToStr(x);
end;

for x := 1 to GPointer do
begin
WriteLn;

while Number <> 0 do
begin
Write(' node ', x, ' is linked with (''0'' exits): ');
Read(Number);

if Number = 0 then
Break;

Graph[X].Linked[Y] := Graph[Number];

Write(' Weight between ', X, ' and ', number, ' = ');
Read(Weight);

WriteLn;
Graph[x].Weight[Y] := Weight;

Inc(Y);
end;

Number := 1;
Y := 0;
//Weight := 0;
end;

Draw_Graph;

while Seek_Path do
;

for i := Low(Graph) to High(Graph) do
Graph[i].Free;

ReadLn;
end;

//------------------------------------------------------------------------------

begin
Main;
end.

hammer
23-03-2004, 09:16 PM
thanks a million i will try it out