我也需要在 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,¤tArray[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;
}*/
}
我正在读入一个数组,然后使用合并排序按字母顺序对它们进行排序我已经创建了一个节点,但我不知道如何从这里开始我已经在这几个小时没有成功请帮助