-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
101 lines (82 loc) · 2.02 KB
/
graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
class graph_matrix{
public:
int dist[20][20],num,x,conn_name,weight;
char a[7][50],ch;
graph_matrix(){
for(int i=0;i<7;i++){
for(int j=0;j<7;j++){
a[i][j]=0;
}
}
}
void add(){
cout<<"\n \t enter number of cities : ";
cin>>num;
for(int i=1;i<=num;i++){
cout<<"\n\t enter name of city "<<i<<" : ";
cin>>a[i];
}
}
void route(){
for(int i=0;i<=num;i++){
for(int j=i;j<=num;j++){
if(i==j){
dist[i][j]=0;
}
else{
cout<<"\n is there a flight between cities "<<i<<" and "<<j<<" ? (y/n)";
cin>>ch;
if(ch=='y' || ch=='Y'){
cout<<"enter the distance between the city airports(kms) : ";
cin>>dist[i][j];
dist[i][j]=dist[j][i];
}
else{
dist[i][j]=dist[j][i]=-1;
}
}
}
}
}
void display(){
cout<<"\n distance matrix : ";
for(int i=0;i<=num;i++){
cout<<"\t";
cout<<a[i];
}
cout<<endl;
for(int i=0;i<=num;i++){
cout<<a[i];
for(int j=1;j<=num;j++){
cout<<"\t"<<dist[i][j];
}
cout<<endl;
}
}
}m;
int main(){
int n;
while(true){
cout<<"choose option : ";
cout<<"1. enter data \n 2. display routes \n 3. exit \n option : ";
cin>>n;
switch(n){
case 1:
m.add();
m.route();
break;
case 2:
m.display();
break;
case 3:
exit(0);
default:
cout<<"invalid choice";
}
}
return 0;
}