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
{
public:
char name[10];
int weight[MAX_LINKS];
cNode *linked[MAX_LINKS];
bool visited[MAX_LINKS];
void init()
{
for(int i=0;i<MAX_LINKS;i++) { linked[i]=NULL;visited[i]=false;weight[i]=0; }
}
void setname(char *text)
{
strcpy(name,text);
}
};
int linker=0;
int lenght=0;
int lenghts[MAX_LINKS],final_lenghts[MAX_LINKS],parent s[MAX_LINKS];
int gpointer=0;
int start=1,finish=gpointer;
cNode *graph[MAX_LINKS];
void pauz(char *text)
{
cout << "\n" << text << "\n";
char por;
cin >> por;
}
void draw_graph()
{
cout << "\n************* Drawing graph ********************\n";
for(int X=1;X<=gpointer;X++)
{
for(int Y=0;Y<MAX_LINKS;Y++)
{
if(graph[X]->linked[Y])
{
cout << "\n" << graph[X]->name << " -> " << graph[X]->linked[Y]->name << " :: weight=" << graph[X]->weight[Y];
}
}
}
cout << "\n************************************************ \n";
}
bool sysedi(cNode *from,cNode *target)
{
for(int i=0;i<MAX_LINKS;i++)
{
if( (from->linked[i]) && (strcmp(from->linked[i]->name,target->name)==0) )
{
linker=i;
return true;
}
}
return false;
}
int find_smallest(int masiv[MAX_LINKS])
{
int min = INFINITY, returned=0;
for(int i=1;i<=gpointer;i++)
{
if(masiv[i]<min) { min=masiv[i];returned=i;}
}
/*- vry6ta NE stoinostta a koe q sydyrja tazi stoinost -*/
return returned;
}
void update_lenghts(int vryh)
{
for(int i=1;i<=gpointer;i++)
{
if((lenghts[i]>lenghts[vryh])&&(graph[i]!=graph[vryh]))
{
if( (lenghts[i]!=EXCLUDED) && (sysedi(graph[vryh],graph[i])) && ( graph[vryh]->weight[linker] + lenghts[vryh] < lenghts[i]) )
{
lenghts[i] = (graph[vryh]->weight[linker]) + (lenghts[vryh]);
parents[i] = vryh;
}
}
}
final_lenghts[vryh]=lenghts[vryh];
lenghts[vryh]=EXCLUDED;
}
bool ima_non_infinit()
{
for(int i=1;i<=gpointer;i++)
{
if(lenghts[i]<EXCLUDED) return true;
}
return false;
}
void showpath(int to)
{
if(lenghts[to]==INFINITY) { cout << "\nNo path!\n";return; }
cout << graph[to]->name << " <- ";
if(parents[start]==parents[to]) { cout << graph[start]->name; return; }
showpath(parents[to]);
return;
}
void perform()
{
for(int k=1;k<=gpointer;k++)
{
if(graph[k]==graph[start]) { lenghts[k]=EXCLUDED; } else
if(sysedi(graph[start],graph[k])) { lenghts[k]=graph[start]->weight[linker]; } else
{ lenghts[k]=INFINITY; }
parents[k]=start;
}
while(ima_non_infinit()) { update_lenghts(find_smallest(lenghts)); }
update_lenghts(find_smallest(lenghts));
}
bool seek_path()
{
cout << "\n************** Seeking path ********************\n";
cout << "Where we start from (1.." << gpointer << ") [0=Exit]: "; cin >> start;
if(start!=0)
{
perform();
cout << "Seek path to (1.." << gpointer << ") : "; cin >> finish;
showpath(finish);
cout << "\nLenght: ";
if(lenghts[finish]==INFINITY) { cout << "INFINITY"; } else
if(final_lenghts[finish]==EXCLUDED) { cout << "0"; } else
{ cout << final_lenghts[finish]; }
cout << "\n************************************************ \n";
return true;
}
else
{
if(start==0) { cout << "\n************************************************ \n";return false;}
}
}
int main()
{
for(int i=0;i<MAX_LINKS;i++) { graph[i]=NULL; }
int number=1; //broi vyzli v grafa
int X=1,Y=0; //rabotni broq4i
int weight=0;
cout << "Number of nodes in the Graph: "; cin >> gpointer;
for(int x=1;x<=gpointer;x++)
{
char temp[10];
graph[x] = new cNode;
graph[x]->init();
graph[x]->setname(itoa(x,temp,10));
}
for(X=1;X<=gpointer;X++)
{
cout << "\n";
while(number!=0)
{
cout << " node " << X << " is linked with ('0' exits): "; cin >> number;
if(number==0) break;
graph[X]->linked[Y]=graph[number];
cout << " Weight between " << X << " and " << number << " = "; cin >> weight;cout << "\n";
graph[X]->weight[Y]=weight;
Y++;
}
number=1;Y=0;weight=0;
}
draw_graph();
while(seek_path());
system("pause");
return 0;
}
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
{
public:
char name[10];
int weight[MAX_LINKS];
cNode *linked[MAX_LINKS];
bool visited[MAX_LINKS];
void init()
{
for(int i=0;i<MAX_LINKS;i++) { linked[i]=NULL;visited[i]=false;weight[i]=0; }
}
void setname(char *text)
{
strcpy(name,text);
}
};
int linker=0;
int lenght=0;
int lenghts[MAX_LINKS],final_lenghts[MAX_LINKS],parent s[MAX_LINKS];
int gpointer=0;
int start=1,finish=gpointer;
cNode *graph[MAX_LINKS];
void pauz(char *text)
{
cout << "\n" << text << "\n";
char por;
cin >> por;
}
void draw_graph()
{
cout << "\n************* Drawing graph ********************\n";
for(int X=1;X<=gpointer;X++)
{
for(int Y=0;Y<MAX_LINKS;Y++)
{
if(graph[X]->linked[Y])
{
cout << "\n" << graph[X]->name << " -> " << graph[X]->linked[Y]->name << " :: weight=" << graph[X]->weight[Y];
}
}
}
cout << "\n************************************************ \n";
}
bool sysedi(cNode *from,cNode *target)
{
for(int i=0;i<MAX_LINKS;i++)
{
if( (from->linked[i]) && (strcmp(from->linked[i]->name,target->name)==0) )
{
linker=i;
return true;
}
}
return false;
}
int find_smallest(int masiv[MAX_LINKS])
{
int min = INFINITY, returned=0;
for(int i=1;i<=gpointer;i++)
{
if(masiv[i]<min) { min=masiv[i];returned=i;}
}
/*- vry6ta NE stoinostta a koe q sydyrja tazi stoinost -*/
return returned;
}
void update_lenghts(int vryh)
{
for(int i=1;i<=gpointer;i++)
{
if((lenghts[i]>lenghts[vryh])&&(graph[i]!=graph[vryh]))
{
if( (lenghts[i]!=EXCLUDED) && (sysedi(graph[vryh],graph[i])) && ( graph[vryh]->weight[linker] + lenghts[vryh] < lenghts[i]) )
{
lenghts[i] = (graph[vryh]->weight[linker]) + (lenghts[vryh]);
parents[i] = vryh;
}
}
}
final_lenghts[vryh]=lenghts[vryh];
lenghts[vryh]=EXCLUDED;
}
bool ima_non_infinit()
{
for(int i=1;i<=gpointer;i++)
{
if(lenghts[i]<EXCLUDED) return true;
}
return false;
}
void showpath(int to)
{
if(lenghts[to]==INFINITY) { cout << "\nNo path!\n";return; }
cout << graph[to]->name << " <- ";
if(parents[start]==parents[to]) { cout << graph[start]->name; return; }
showpath(parents[to]);
return;
}
void perform()
{
for(int k=1;k<=gpointer;k++)
{
if(graph[k]==graph[start]) { lenghts[k]=EXCLUDED; } else
if(sysedi(graph[start],graph[k])) { lenghts[k]=graph[start]->weight[linker]; } else
{ lenghts[k]=INFINITY; }
parents[k]=start;
}
while(ima_non_infinit()) { update_lenghts(find_smallest(lenghts)); }
update_lenghts(find_smallest(lenghts));
}
bool seek_path()
{
cout << "\n************** Seeking path ********************\n";
cout << "Where we start from (1.." << gpointer << ") [0=Exit]: "; cin >> start;
if(start!=0)
{
perform();
cout << "Seek path to (1.." << gpointer << ") : "; cin >> finish;
showpath(finish);
cout << "\nLenght: ";
if(lenghts[finish]==INFINITY) { cout << "INFINITY"; } else
if(final_lenghts[finish]==EXCLUDED) { cout << "0"; } else
{ cout << final_lenghts[finish]; }
cout << "\n************************************************ \n";
return true;
}
else
{
if(start==0) { cout << "\n************************************************ \n";return false;}
}
}
int main()
{
for(int i=0;i<MAX_LINKS;i++) { graph[i]=NULL; }
int number=1; //broi vyzli v grafa
int X=1,Y=0; //rabotni broq4i
int weight=0;
cout << "Number of nodes in the Graph: "; cin >> gpointer;
for(int x=1;x<=gpointer;x++)
{
char temp[10];
graph[x] = new cNode;
graph[x]->init();
graph[x]->setname(itoa(x,temp,10));
}
for(X=1;X<=gpointer;X++)
{
cout << "\n";
while(number!=0)
{
cout << " node " << X << " is linked with ('0' exits): "; cin >> number;
if(number==0) break;
graph[X]->linked[Y]=graph[number];
cout << " Weight between " << X << " and " << number << " = "; cin >> weight;cout << "\n";
graph[X]->weight[Y]=weight;
Y++;
}
number=1;Y=0;weight=0;
}
draw_graph();
while(seek_path());
system("pause");
return 0;
}