数据结构课程设计成果╰( ̄▽ ̄)╮
//从文件读入到内存
//从文件读入到内存
void Save(ALGraph *G)
{
string n =" ";
int i = 0,stah = 0,stam = 0,arrh = 0,arrm = 0,k;
double money; char * j;
char w = ' ';
ArcNode * p;
//从"City.txt"读入到内存
ifstream fin("City.txt"); //打开
while(!fin.eof()) //直至结束
{
getline(fin,n,'\n'); //从fin获取以\n结束的字符串放到n中
j = &n[0]; //从字符串转化为字符数组指针
strcpy(G->vertices[i].cityname,j); //从字符指针到字符数组,实现转化
G->vertices[i].trainarc = NULL;
G->vertices[i].planearc = NULL;
G->vexnum++; i++; }
fin.close(); //关闭文件
//从"Train.txt"到内存
ifstream fin2("Train.txt");
while(!fin2.eof()) { getline(fin2,n,' ');
j = &n[0];
k = LocateVex(G,j);
p = (ArcNode*)malloc(sizeof(ArcNode)); //头插
p->nextarc = G->vertices[k].trainarc;
G->vertices[k].trainarc = p;
getline(fin2,n,' ');
j = &n[0];
p->adjvex = LocateVex(G,j); //得到后续数据,更新
fin2 >> money >> stah >> stam >> arrh >> arrm; //读取数据
p->price = money;
p->statime.hour = stah;
p->statime.min = stam;
p->arrtime.hour = arrh;
p->arrtime.min = arrm;
} fin2.close(); }
void Output(ALGraph *G) {
int i;
ArcNode * p;
//从内存到"Train.txt"
ofstream fout2("Train.txt",ios::trunc);//打开文件并清空原来的数据
for(i = 0;i < G->vexnum - 1;i++) {
p = G->vertices[i].trainarc;
while(p) { //直至最后,更新文件
fout2 << G->vertices[i].cityname << " " << G->vertices[p->adjvex].cityname << " " << p->price << " ";//写入数据
fout2 << p->statime.hour << " " << p->statime.min << " " << p->arrtime.hour << " " << p->arrtime.min;
p = p->nextarc;
if(p)
fout2 << endl;
}
}
fout2.close();
}