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

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