конструкторы + тест работы с файлами

This commit is contained in:
Морозов Андрей 2021-10-21 12:31:08 +00:00
parent 93407fb312
commit 749c181dc8
1 changed files with 87 additions and 23 deletions

View File

@ -14,8 +14,8 @@ class Dot {
String getName() => _name; String getName() => _name;
void setName(String n) => _name = n; void setName(String n) => _name = n;
bool hasConnection(int n) => ln.containsKey(n); bool hasConnection(int n) => ln.containsKey(n);
void addPath(int inp, int length) => ln[inp] = length; void addPath(int inp, int length) => ln[inp] = length; // нужна проверка на ориентированность
void delPath(int n) => ln.removeWhere((key, value) => key == n); void delPath(int n) => ln.removeWhere((key, value) => key == n); // удалить обратный путь если не ориентированный
void printD() { void printD() {
stdout.write("$_name: №$num => "); stdout.write("$_name: №$num => ");
for (var i in ln.keys) { for (var i in ln.keys) {
@ -24,21 +24,28 @@ class Dot {
stdout.write("\n"); stdout.write("\n");
} }
Dot(String name, int n, List<int> num, List<int> length) { Dot() {
_name = "Undefined";
num = -1;
ln = <int, int>{};
}
Dot.fromTwoLists(String name, List<int> num0, List<int> length,
[int n = -1]) {
_name = name; _name = name;
num = n;
Map<int, int> nw = <int, int>{}; Map<int, int> nw = <int, int>{};
if (num.length != length.length) { if (num0.length != length.length) {
print("Error in lists"); print("Error in lists");
} else { } else {
for (var i = 0; i <= num.length; i++) { for (var i = 0; i < num0.length; i++) {
nw[num[i]] = length[i]; nw[num0[i]] = length[i];
ln = nw; ln = nw;
} }
} }
} }
// Привязка к памяти Dot.fromMap(String name, Map<int, int> l, [int n = -1]) {
Dot.fromMap(String name, int n, Map<int, int> l) {
_name = name; _name = name;
num = n; num = n;
ln = l; ln = l;
@ -46,33 +53,83 @@ class Dot {
// Копирование // Копирование
Dot.clone(Dot a) { Dot.clone(Dot a) {
_name = a._name; _name = a.getName();
num = a.num; num = a.num;
ln = a.ln; ln = a.ln;
} }
} }
class Graphs { class Graphs {
String _name = "Undefined";
int _amount = 0; int _amount = 0;
List<Dot> _dots = <Dot>[]; List<Dot> _dots = <Dot>[];
// ignore: prefer_final_fields // ignore: prefer_final_fields
Map<int, String> _nameTable = <int, String>{}; Map<int, String> _nameTable = <int, String>{};
bool useLength = false; bool _useLength = false;
bool doubleSided = false; // ignore: prefer_final_fields
bool _oriented = false;
Graphs(List<Dot> d, bool useL) { Graphs() {
_name = "Udefined";
_dots = <Dot>[];
_useLength = false;
_oriented = false;
_amount = 0;
_nameTable = <int, String>{};
}
Graphs.fromList(String n, List<Dot> d, bool useL) {
_name = n;
_dots = d; _dots = d;
useLength = useL; _useLength = useL;
_amount = _dots.length;
_syncNum();
}
Graphs.fromFile(String path) {
File file = File(path);
List<String> lines = file.readAsLinesSync();
for (var l in lines) {
print(l);
}
}
// Копирование
Graphs.clone(Graphs a) {
_name = a.getName();
_dots = a.getDots();
_oriented = a.getDoubleSided();
_useLength = a.getUseLength();
_amount = _dots.length; _amount = _dots.length;
_syncNameTable(); _syncNameTable();
} }
void toFile(String name) {
var file = File(name);
file.writeAsStringSync("$_name\n");
file.writeAsStringSync("ksdfsdf1\n", mode: FileMode.append);
file.writeAsStringSync("\n", mode: FileMode.append);
file.writeAsStringSync("\n", mode: FileMode.append);
file.writeAsStringSync("ksdfsdf0\n", mode: FileMode.append);
file.writeAsStringSync("ksdfsdf1\n", mode: FileMode.append);
}
void _syncNameTable() { void _syncNameTable() {
_nameTable = <int, String>{};
for (var i in _dots) { for (var i in _dots) {
_nameTable[i.num] = i.getName(); _nameTable[i.num] = i.getName();
} }
} }
void _syncNum() {
_amount = 0;
for (var i in _dots) {
i.num = ++_amount;
}
_syncNameTable();
}
//Для неориентированного
void _fixPathAfterInsert(Dot a) { void _fixPathAfterInsert(Dot a) {
for (var i in a.ln.keys) { for (var i in a.ln.keys) {
if (!_dots[i].ln.containsKey(a.num)) _dots[i].addPath(i, a.ln[i]!); if (!_dots[i].ln.containsKey(a.num)) _dots[i].addPath(i, a.ln[i]!);
@ -88,21 +145,23 @@ class Graphs {
a.num = _amount; a.num = _amount;
_dots.add(a); _dots.add(a);
_syncNameTable(); _syncNameTable();
if (doubleSided) _fixPathAfterInsert(a); if (_oriented) _fixPathAfterInsert(a);
return true; return true;
} }
bool checkDots() { bool checkDots(bool verbose) {
for (var a in _dots) { for (var a in _dots) {
for (var i in a.ln.keys) { for (var i in a.ln.keys) {
try { try {
if (!_dots[i].ln.containsKey(a.num)) { if (!_dots[i - 1].ln.containsKey(a.num)) {
print("Can't find $i"); if (verbose) print("Can't find ${a.num}");
return false; return false;
} }
} catch (e) { } catch (e) {
print( if (verbose) {
"Can't find Dot ${i + 1} for path ${a.num}->${i + 1}. Exception $e"); print("Can't find Dot $i for path ${a.num}->$i. Exception $e");
}
_dots[a.num - 1].ln.remove(i);
return false; return false;
} }
} }
@ -111,6 +170,10 @@ class Graphs {
} }
//Getters //Getters
bool getDoubleSided() => _oriented;
bool getUseLength() => _useLength;
List<Dot> getDots() => _dots;
String getName() => _name;
String? getNameByNum(int n) => _nameTable[n]; String? getNameByNum(int n) => _nameTable[n];
int? getNumByName(String n) { int? getNumByName(String n) {
for (var i in _nameTable.keys) { for (var i in _nameTable.keys) {
@ -121,12 +184,13 @@ class Graphs {
//Print info //Print info
void printG() { void printG() {
if (doubleSided) { stdout.write("$_name: ");
print("Не ориентированный"); if (_oriented) {
stdout.write("Не ориентированный, ");
} else { } else {
print("Ориентированный"); stdout.write("Ориентированный, ");
} }
if (useLength) { if (_useLength) {
print("Взвешенный"); print("Взвешенный");
} else { } else {
print("Не взвешенный"); print("Не взвешенный");