在C相邻矩阵++

问题描述 投票:1回答:1

我需要建立在C ++邻接矩阵从这样的文件中读取数据

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


     ABC 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;
}*/






}

我读入一个数组,然后使用合并排序它们按字母顺序排序我创建了一个节点,但我不知道如何去从这里我一直盯着它几个小时,但没有成功,请帮忙

c++ adjacency-matrix
1个回答
-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
© www.soinside.com 2019 - 2024. All rights reserved.