This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cpp
217 lines (173 loc) · 4.43 KB
/
Program.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// Created by barto on 19.05.18.
//
#include <iostream>
#include "Program.h"
using namespace std;
// public
void Program::start() {
char option;
char option2;
bool breakMainMenu;
string file;
int numberOfVerticles;
int density;
string matrix;
string list;
char index;
string result;
int range;
do {
printGraphTypeSelect();
cin >> option;
cout << endl;
breakMainMenu = false;
switch (option) {
case '1':
graph = new UndirectedGraph();
break;
case '2':
graph = new DirectedGraph();
break;
case '0':
breakMainMenu = true;
break;
default:
cerr << "Nie ma takiej opcji, wybierz jeszcze raz." << endl;
breakMainMenu = true;
break;
}
if (breakMainMenu)
continue;
do {
printGraphMenu();
cin >> option2;
cout << endl;
switch (option2) {
case '1': // wczytaj z pliku
cout << "Podaj nazwe pliku: ";
cin >> file;
try {
graph->loadDataFrom(file);
} catch (const char* e) {
cerr << e << endl;
}
break;
case '2': // generuj losowo
cout << "Podaj ilosc wierzcholkow: ";
while (!(cin >> numberOfVerticles)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Bledna wartosc! Podaj ilosc wierzcholkow: ";
}
cout << "Podaj gestosc grafu: ";
while (!(cin >> density)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Bledna wartosc! Podaj gestosc grafu: ";
}
cout << "Wagi krawedzi beda generowane z zakresu [1, max_value]. Podaj maksymalna wartosc: ";
while (!(cin >> range)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Bledna wartosc! Podaj maksymalna wartosc: : ";
}
graph->generate(numberOfVerticles, density, range);
break;
case '3': // wyswietl macierz
matrix = graph->printIncidenceMatrix();
cout << endl;
cout << matrix;
cout << endl;
break;
case '4': // wyswietl liste
list = graph->printAdjacencyList();
cout << endl;
cout << list;
cout << endl;
break;
case '5':
graph->test();
break;
case '6':
case '7':
case '8':
case '9':
index = option2 - 53;
try {
result = runAlgorithm(index);
cout << result << endl;
} catch (const char* e) {
cerr << e << endl;
}
case '0':
break;
default:
cerr << "Nie ma takiej opcji, wybierz jeszcze raz." << endl;
break;
}
} while (option2 != '0');
delete graph;
} while (option != '0');
}
// private
void Program::printGraphTypeSelect() {
cout << endl;
cout << "==== MENU GLOWNE ===" << endl;
cout << "1. Graf nieskierowany" << endl;
cout << "2. Graf skierowany" << endl;
cout << "0. Wyjscie" << endl;
cout << "Podaj opcje: ";
}
void Program::printGraphMenu() {
cout << endl;
string name = graph->getName();
for (auto &c: name) c = toupper(c); //convert to uppercase
cout << "==== " << name << " ===" << endl;
cout << "1. Wczytaj z pliku" << endl;
cout << "2. Generuj losowo" << endl;
cout << "3. Wyswietl graf jako macierz" << endl;
cout << "4. Wyswietl graf jako lista" << endl;
cout << "5. Wykonaj testy" << endl;
string availableAlgorithms = graph->getAvailableAlgorithms();
cout << availableAlgorithms;
cout << "0. Wyjscie" << endl;
cout << "Podaj opcje: ";
}
string Program::runAlgorithm(char index) {
string arg1s;
char arg1;
int arg2;
int arg3;
string output;
if (graph->getNumberOfAvailableAlgorithms() > index - 1) {
cout << "Podaj argument 1: ";
cin >> arg1s;
while (arg1s != "m" && arg1s != "M" && arg1s != "l" && arg1s != "L") {
cerr << "Bledna wartosc! Podaj argument 1: ";
cin >> arg1s;
}
if (arg1s == "m" || arg1s == "M")
arg1 = 0; // reprezentacja macierzowa
else if (arg1s == "l" || arg1s == "L")
arg1 = 1; // reprezentacja listowa
else
throw "Nieznany blad!"; // should never be thrown
cout << "Podaj argument 2: ";
while (!(cin >> arg2)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Bledna wartosc! Podaj argument 2: ";
}
cout << "Podaj argument 3: ";
while (!(cin >> arg3)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Bledna wartosc! Podaj argument 3: ";
}
output = graph->runAlgorithm(index, arg1, arg2, arg3);
} else {
cerr << "Nie ma takiej opcji, wybierz jeszcze raz." << endl;
}
return output;
}