матрица смежности в переполнении стека

Мне тоже нужно создать матрицу смежности в C ++ для данных, читаемых из файла, подобного этому

ABC CDE 100
ZXY ABC  25
TER ZXY  11
POP ABC  66ABC CDE POP TER ZXY
ABC     100
CDE
POP 66
TER                  11
ZXY 25

#include <fstream>   // for std::ifstream
#include <sstream>   // for std::istringstream
#include <cstring>    // for std::string and std::getline
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include<algorithm>
#include <string.h>

using namespace std;

#define MAX 30
#define WORD 3
string* currentArray;

typedef struct node{
int nodeId;
string destCity[MAX];
string arrCity[MAX];
int time;
}NODE;

typedef struct edge{
int adjoin;
int distance;
}EDGE;

typedef struct graph{
NODE cityNode[MAX];
EDGE e[MAX][MAX];
}GRAPH;

GRAPH graf;bool removeDuplicates(int count,string* tempArray){
for (int i = 0; i <= count; i++)
{
int n=0;
bool matching = false;
for (int j = 0; (j < i) && (matching == false); j++){
if (currentArray[i] == currentArray[j]) matching = true;
}
//if (!matching) tempArray[n] = currentArray[i];
}
return false;
}
void MergeA(int low ,int mid , int high)
{
int i = low, j = mid+1 , k = low;
string Temp[MAX];

while(i <= mid && j <= high)
{
if( currentArray[i] <= currentArray[j] )
{

Temp[k].assign(currentArray[i]);
i++;
}
else
{
Temp[k].assign(currentArray[j]);
j++;
}
k++;
}
if(i > mid )
{
for(int h = j ;h <= high ; h++ )
{

Temp[k].assign(currentArray[h]);
k++;

}
}
else
for(int h = i; h<= mid ; h++ )
{

Temp[k].assign(currentArray[h]);
k++;

}
for(int i = low; i <= high ; i++)
{          currentArray[i].assign(Temp[i]);
}
}
void MergeSortA(int low , int high)
{

int mid = 0;
if(low < high)
{
mid = low + (high-low)/2;
MergeSortA(low , mid);
MergeSortA(mid+1,high);
MergeA(low,mid,high);

}
}

int main()
{std::ifstream infile("theWords.txt");
std::string line;
string departureCity[MAX],arrivalCity[MAX];
int time[MAX];
int count = 0;
while (std::getline(infile,(line)) && count<30){
std::istringstream iss(line);
if ((iss) >> departureCity[count] >> arrivalCity[count] >> time[count]){
//departureCityCopy[i]
std::cout << "From : " << departureCity[count] << " To : " << arrivalCity[count] << " Duration " << time[count] << "\n";
graf.cityNode[count].destCity = departureCity[count];

count++;
}else{
// error processing that line
}}

std::string cpyDepartureCity[MAX],cpyArrivalCity[MAX];
int cpyTime[MAX];
std::copy(departureCity,departureCity+count,cpyDepartureCity);
std::copy(arrivalCity,arrivalCity+count,cpyArrivalCity);
std::copy(time,time+count,cpyTime);
currentArray= cpyDepartureCity;
MergeSortA(0,count);
cout<<"before dup"<<endl;
//removeDuplicates(count,&currentArray[1]);

for(int i = 0; i <= count ; i++){
cout << cpyDepartureCity[i] <<endl;
}
currentArray= cpyArrivalCity;
MergeSortA(0,count);

/*for(int i = 0; i <= count ; i++){
cout << cpyArrivalCity[i] <<endl;
}*/

}

Я читаю в массив, а затем использую сортировку слиянием для сортировки по алфавиту
Я создал узел, но я не уверен, как идти отсюда
Я был на нем часами безуспешно
Пожалуйста помоги

1

Решение

Я не собираюсь писать вам полный код, вы должны изучить его для себя.

Здесь описан алгоритм:

1) Create 2D matrix (int type)

2) Create look-up table (hash map <String, int>)

3) Fill look-up table with your 3-letter strings and assign each of them ID (0 ... max count of strings)

4) Filling matrix - use look-up table int id = [XYZ] ... and then in your matrix, use this id as key to col / row -> put value into matrix
0

Другие решения

Других решений пока нет …