-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (143 loc) · 5.34 KB
/
main.cpp
File metadata and controls
159 lines (143 loc) · 5.34 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "header/CompleteGraph.h"
#include "header/LinearGraph.h"
#include "header/GeneralGraph.h"
#include "header/DAG.h"
#include "header/Tree.h"
#include <new>
void printMenu(){
cout<<"Menu: \n";
cout<<"* create-region (create new region)\n";
cout<<"* add-road (add road between two existing cities)\n";
cout<<"* get-distance (returns distance between two existing cities)\n";
cout<<"* STOP (ends program)";
enter; enter; enter;
}
class CustomException{
string message;
public:
CustomException(string msg):message(msg){};
string getMessage(){ return message; }
};
int main()
{
ifstream f("harti.in");
printMenu();
vector< Graph* > Regions;
unordered_map<string, int> poz_regions; //pozitia orasului in Regions
string graph_type, command, region_name, city_name, tree_root, city1, city2;
double x, y, distance; //coordonatele orasului
int nr_cities;
do
{
cout<<"Command type: ";
cin>>command; enter;
if(command == "create-region")
{
cout<<"Region name: "; cin>>region_name; enter;
if(poz_regions.find(region_name)!=poz_regions.end())
{
cout<<"Region already exists!\n\n";
continue;
}
cout<<"Region type: "; cin>>graph_type; enter;
Graph *new_region;
try{
if(graph_type == "complete")
new_region = new CompleteGraph(region_name);
else if(graph_type == "linear")
new_region = new LinearGraph(region_name);
else if(graph_type == "general")
new_region = new GeneralGraph(region_name);
else if(graph_type == "dag")
new_region = new DAG(region_name);
else if(graph_type == "tree")
new_region = new Tree(region_name);
else throw new CustomException("Region type not defined! \n\n");
}
catch(bad_alloc& ex){
cout<<"Bad memory allocation exception caught!\n\n";
}
catch(CustomException* exc){
cout<<exc->getMessage();
continue;
}
Regions.push_back(new_region);
poz_regions[region_name] = Regions.size()-1;
int i=poz_regions[region_name];
cout<<"Number of cities: "; cin>>nr_cities; enter;
while(!nr_cities){
cout<<"Region can not be empty!\n\n";
cout<<"Number of cities: "; cin>>nr_cities; enter;
}
while(nr_cities--)
{
cout<<"City name: "; cin>>city_name; enter;
if(Regions[i]->checkCity(city_name)){
cout<<city_name<<" is already inserted in "<<region_name, enter;
continue;
}
cout<<"City cordinates: "; cin>>x>>y; enter;
City<double> oras(city_name, x, y);
Regions[i]->addCity(oras);
}
if(graph_type == "tree"){
cout<<"Root will be randomly selected: "; enter; enter;
dynamic_cast<Tree*>(Regions[i])->setRoot();
}
cout<<(string)(*Regions[i])<<" - region added\n\n";
}
else if(command == "add-road")
{
cout<<"Region name: "; cin>>region_name; enter;
if(poz_regions.find(region_name)==poz_regions.end())
{
cout<<"Region does not exist!\n\n";
continue;
}
int i=poz_regions[region_name];
cout<<"Road start: "; cin>>city1; enter;
if(!Regions[i]->checkCity(city1)){
cout<<"Error: city "<<city1<<" is not in this region\n\n";
continue;
}
cout<<"Road end: "; cin>>city2; enter;
if(!Regions[i]->checkCity(city2)){
cout<<"Error: city "<<city2<<" is not in this region\n\n";
continue;
}
Regions[i]->addRoad(city1, city2);
cout<<"Road added between "<<city1<<" and "<<city2; enter; enter;
}
else if(command == "get-distance")
{
cout<<"Region name: ";
cin>>region_name;
enter;
if(poz_regions.find(region_name)==poz_regions.end())
{
cout<<"Region does not exist!\n\n";
continue;
}
int i=poz_regions[region_name];
cout<<"First city: "; cin>>city1; enter;
if(!Regions[i]->checkCity(city1)){
cout<<city1<<" is not in region "<<region_name; enter; enter;
continue;
}
cout<<"Second city: "; cin>>city2; enter;
if(!Regions[i]->checkCity(city2)){
cout<<city2<<" is not in region "<<region_name; enter; enter;
continue;
}
distance = Regions[i]->getDistance(city1, city2);
if(distance != Graph::INF)
cout<<"Minimum distance between "<<city1<<" and "<<city2<<" is "<<distance, enter, enter;
else
cout<<city2<<" is not reachable from "<<city1, enter, enter;
}
else
if(command != "STOP") cout<<"Error: undefined action!\n\n";
}
while(command != "STOP");
return 0;
}