第一天写了400行代码,没写完
#include<stdio.h>
#include<stdlib.h>
#include <string>
#include <iostream>
//需要包含cstring的字符串
#include <cstring>
#define Max 100
#define wuqiong 0
//清空文件要包含的函数
#include <fcntl.h>
#include <unistd.h>
using namespace std;
typedef struct{
int vexnum;
int arcnum;
int list[Max][Max];
int list2[Max][Max];
string vex[Max];
}amgraph;
//邻接矩阵的dfs算法
typedef int Boolean;
Boolean visited[Max];
//函数的声明
void init(amgraph &G);
void ma(void);
void guanliyuan(void);
int locate(amgraph G,char a);
void search(amgraph G);
void addcity(amgraph &G);
void delectcity(amgraph G);
void user(void);
void dij(amgraph &G);
void dij2(amgraph &G);
void creat(amgraph &G,int v);
void addroad(amgraph G);
void delectroad(amgraph G);
void init(amgraph &G){
FILE *fpdot;
FILE *fpxian;
FILE *fpcity;
FILE *fp3;
//"C:\Users\蒋雨铭\Desktop\数据结构\1.txt"
int i;
string a[Max];
int info1;
int info2;
int q;
int w;
int j;
int re;
string b[Max];
int inf1[Max];
int inf2[Max];
int num;
if((fpdot=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\dot.txt","r"))==NULL){
printf("error\n");
exit(0);
}
if((fpxian=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\xian.txt","r"))==NULL){
printf("error\n");
exit(0);
}
if((fpcity=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\city.txt","r"))==NULL){
printf("error\n");
exit(0);
}
if((fp3=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\map.txt","r"))==NULL){
printf("error\n");
exit(0);
}
fscanf(fpdot,"%d",&G.vexnum);
fscanf(fpxian,"%d",&G.arcnum);
//printf("%d",G.vexnum);
for(i=0;i<G.vexnum;i++){
fscanf(fpcity,"%s",G.vex[i].c_str());
}
// for(i=0;i<G.vexnum;i++){
// printf("%s ",G.vex[i].c_str());
// }
for(i=0;i<G.vexnum;i++){
for(j=0;j<G.vexnum;j++){
G.list[i][j]=wuqiong;
}
}
for(i=0;i<G.arcnum;i++){
fscanf(fp3,"%s%s%d%d",a[i].c_str(),b[i].c_str(),&inf1[i],&inf2[i]);
}
for(i=0;i<G.arcnum;i++){
for(j=0;j<G.vexnum;j++){
re=strcmp(a[i].c_str(),G.vex[j].c_str());//不知道为啥作比较只能用这个方式做比较
if(re==0) q=j;
}
//printf("%d\n",q);
for(j=0;j<G.vexnum;j++){
re=strcmp(b[i].c_str(),G.vex[j].c_str());
if(re==0) w=j;
}
//printf("%d\n",q);
G.list[q][w]=inf1[i];
G.list[w][q]=inf1[i];
G.list2[q][w]=inf2[i];
G.list2[w][q]=inf2[i];
}
//printf("%d",G.list[1][1]);
//printf("%d",G.vexnum);
// for(i=0;i<G.vexnum;i++){
// for(j=0;j<G.vexnum;j++){
// printf("%d ",G.list[i][j]);
// }
// printf("\n");
// }
fclose(fpdot);
fclose(fpcity);
fclose(fpxian);
fclose(fp3);
}
void addcity(amgraph &G){
// int a;
FILE *fpdot;
FILE *fpcity;
char string[16] = {0};
char string2[16]={0};
if((fpdot=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\dot.txt","r+"))==NULL){
printf("error\n");
exit(0);
}
if((fpcity=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\city.txt","a"))==NULL){//追加的方式
printf("error\n");
exit(0);
}
printf("请输入你想添加的城市\n");
scanf("%s",&string2);
fscanf(fpdot,"%d",&G.vexnum);
fseek(fpdot,0, SEEK_SET);
G.vexnum+=1;
fputs(" ",fpcity);
fputs(string2,fpcity);
itoa(G.vexnum,string,10);
fputs(string, fpdot);
// puts(string);
fclose(fpdot);
fclose(fpcity);
}
void delectcity(amgraph G){
int i;
int re;
int flag=1;
int j;
int k=0;
int num;
FILE *fpdot;
FILE *fpcity1;
FILE *fpcity;
char string[16] = {0};
char string2[16]={0};
if((fpdot=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\dot.txt","r+"))==NULL){
printf("error\n");
exit(0);
}
if((fpcity1=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\city.txt","r+"))==NULL){
printf("error\n");
exit(0);
}
if((fpcity=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\city.txt","w+"))==NULL){
printf("error\n");
exit(0);
}
printf("请输入你要删除的城市\n");
scanf("%s",&string2);
//读取
fscanf(fpdot,"%d",&G.vexnum);
fseek(fpdot,0,SEEK_SET);
for(i=0;i<G.vexnum;i++){
fscanf(fpcity1,"%s",G.vex[i].c_str());
}
// for(i=0;i<G.vexnum;i++){
// printf("%s ",G.vex[i].c_str());
// }用city1才能正确读出
//判断
for(i=0;i<G.vexnum;i++){
re=strcmp(string2,G.vex[i].c_str());
if(re==0){
flag=1;
num=i;
}
}
if(flag==1){
printf("删除成功\n");
//对dot文件进行操作
G.vexnum=G.vexnum-1;
printf("%d\n",G.vexnum);
itoa(G.vexnum,string,10);
//fputs(string,fpdot);
//对city文件进行操作
for(j=0;j<num;j++) {
G.vex[k++]=G.vex[j];
}
for(j=num+1;j<G.vexnum+1;j++){
G.vex[k++]=G.vex[j];
}
// for(i=0;i<G.vexnum;i++){
// printf("%s ",G.vex[i].c_str());
// }
//先清空文件
// for(i=0;i<G.vexnum;i++){
// printf("%s ",G.vex[i].c_str());
// }
//然后再打印内容
// for(i=0;i<G.vexnum-1;i++){
// fputs(G.vex[i].c_str(),fpcity);
// fputs(" ",fpcity);
// }
// fputs(G.vex[G.vexnum].c_str(),fpcity);
fclose(fpcity);
fclose(fpcity1);
}
else{
printf("删除失败\n");
}
fclose(fpdot);
}
void addroad(amgraph G){
FILE *fpmap;
FILE *fpxian;
char string[16]={0};
char city1[16]={0};
char city2[16]={0};
int info1;
int info2;
char in1[16]={0};
char in2[16]={0};
if((fpmap=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\map.txt","a"))==NULL){
printf("error\n");
exit(0);
}
if((fpxian=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\xian.txt","r+"))==NULL){
printf("error\n");
exit(0);
}
//对xian文本进行修改
fscanf(fpxian,"%d",&G.arcnum) ;
fseek(fpxian,0,SEEK_SET);
G.arcnum+=1;
itoa(G.arcnum,string,10);
fputs(string,fpxian);
//对map文本进行修改
printf("请输入第一个城市结点\n");
scanf("%s",&city1);
printf("请输入第二个城市结点\n");
scanf("%s",&city2);
printf("请输入路程权值\n");
scanf("%d",&info1);
printf("请输入费用权值\n");
scanf("%d",&info1);
itoa(info1,in1,10);
itoa(info2,in2,10);
fputs("\n",fpmap);
fputs(city1,fpmap);
fputs(" ",fpmap);
fputs(city2,fpmap);
fputs(" ",fpmap);
fputs(in1,fpmap);
fputs(" ",fpmap);
fputs(in2,fpmap);
fclose(fpmap);
fclose(fpxian);
}
void creat(amgraph &G,int v){
int j;
visited[i]=TRUE;
printf("%s->",G.vex[i]) ;
for(j=0;j<G.vexnum;j++){
if(G.list[i][j]==1&&!visited[j])
creat(G,j);
}
}
void search(amgraph G){
FILE *fpcity;
FILE *fpdot;
FILE *fpxian;
// FILE *fp3;
//"C:\Users\蒋雨铭\Desktop\数据结构\1.txt"
int i;
int num;
if((fpcity=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\city.txt","r"))==NULL){
printf("error\n");
exit(0);
}
if((fpdot=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\dot.txt","r"))==NULL){
printf("error\n");
exit(0);
}
if((fpxian=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\txt\\xian.txt","r"))==NULL){
printf("error\n");
exit(0);
}
fscanf(fpdot,"%d",&G.vexnum);
fscanf(fpxian,"%d",&G.arcnum);
//printf("%d",G.vexnum);
for(i=0;i<G.vexnum;i++){
fscanf(fpcity,"%s",G.vex[i].c_str());
}
for(i=0;i<G.vexnum;i++){
printf("%s ",G.vex[i].c_str());
}
printf("\n");
fclose(fpdot);
fclose(fpxian);
fclose(fpcity);
}
void dij(amgraph G){
}
void dij2(amgraph G){
}
void addroad(){
FILE *fp;
char a[10];
char b[10];
int c;
int d;
gets(a);
if((fp=fopen("C:\\Users\\蒋雨铭\\Desktop\\数据结构\\map2.txt","r+"))==NULL){
printf("error\n");
exit(0);
}
fputs(a, stdout);
fclose(fp);
}
void ma(){
int choose=-1;
while(choose!=0){
printf("****************welcome to 全国交通管理系统***********************\n");
printf("****************1.用户******2.管理员*****3.退出*******************\n");
printf("******************************************************************\n");
scanf("%d",&choose);
switch(choose){
case 1:user();
break;
case 2:guanliyuan(); break;
}
}
}
void guanliyuan(){
amgraph G;
//int v=0;
char a[10];
int choose=-1;
while(choose!=0){
printf("******************************************************************\n");
printf("请输入你的操作指令:1.创建交通网络 2.查看当前网络已有的城市 3.添加城市 4.删除城市 5.添加指定交通路线 6.删除指定交通路线 7.返回主界面*******************\n");
printf("******************************************************************\n");
scanf("%d",&choose);
switch(choose){
case 1:init(G);
break;
case 2:search(G);
//printf("hh");
break;
case 3:
addcity(G);
break;
case 4:
delectcity(G);
break;
case 5:
addroad(G);
break;
case 6:
//delectroad(G);
break;
case 7:ma(); break;
}
}
}
void user(){
amgraph G;
int v;
int choose=-1;
while(choose!=0){
printf("******************************************************************\n");
printf("请输入你的操作指令:1.加载交通网络 2.查询距离最短路线 3.费用最少路线 4.中转次数最少路线 5.返回主界面*******************\n");
printf("******************************************************************\n");
scanf("%d",&choose);
switch(choose){
case 1:
v=1;
printf("请输入起始点\n");
printf("请输入结束点\n");
creat(G,v);
break;
case 2:
break;
case 3:
//dij(G);
break;
case 4:
// dij2(G);
break;
case 5:ma();
break;
}
}
}
int main(){
ma();
}