Загрузил(а) файлы в 'bin/src'

This commit is contained in:
Морозов Андрей 2021-10-22 12:50:29 +00:00
parent 8df7c6cf34
commit 6b3b499695
1 changed files with 204 additions and 89 deletions

View File

@ -1,21 +1,48 @@
import 'dart:io'; import 'dart:io';
class Point { class Separators {
int i = -1; final String dotToConnections = ": ";
int length = 0; final String dotToLength = "|";
final String hasLength = "Вес\n";
final String hasNoLength = "НетВеса\n";
final String isOriented = "Ориентированный\n";
final String isNotOriented = "НеОриентированный\n";
final String nL = "\n";
final String end = "END";
Separators();
} }
class Dot { class Dot {
//Data
// ignore: prefer_final_fields // ignore: prefer_final_fields
String _name = ""; String _name = "";
int num = -1; int num = -1;
Map<int, int> ln = <int, int>{}; Map<int, int> ln = <int, int>{};
//****Get****
String getName() => _name; String getName() => _name;
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; // нужна проверка на ориентированность int getLength(int x) {
void delPath(int n) => ln.removeWhere((key, value) => key == n); // удалить обратный путь если не ориентированный if (hasConnection(x)) {
return ln[x]!;
}
return -1;
}
//****Get****
//Set
void setName(String n) => _name = n;
//Add
void addPath(int inp, int length) =>
ln[inp] = length; // нужна проверка на ориентированность
//Del
void delPath(int n) => ln.removeWhere((key, value) =>
key == n); // удалить обратный путь если не ориентированный
//Print
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,12 +51,12 @@ class Dot {
stdout.write("\n"); stdout.write("\n");
} }
//******Constructor******
Dot() { Dot() {
_name = "Undefined"; _name = "Undefined";
num = -1; num = -1;
ln = <int, int>{}; ln = <int, int>{};
} }
Dot.fromTwoLists(String name, List<int> num0, List<int> length, Dot.fromTwoLists(String name, List<int> num0, List<int> length,
[int n = -1]) { [int n = -1]) {
_name = name; _name = name;
@ -44,14 +71,14 @@ class Dot {
} }
} }
} }
Dot.fromMap(String name, Map<int, int> l, [int n = -1]) { Dot.fromMap(String name, Map<int, int> l, [int n = -1]) {
_name = name; _name = name;
num = n; num = n;
ln = l; ln = l;
} }
//******Constructor******
// Копирование //Copy
Dot.clone(Dot a) { Dot.clone(Dot a) {
_name = a.getName(); _name = a.getName();
num = a.num; num = a.num;
@ -60,96 +87,69 @@ class Dot {
} }
class Graphs { class Graphs {
String _name = "Undefined"; //Data
int _amount = 0; String _name = "Undefined"; //Имя
List<Dot> _dots = <Dot>[]; int _amount = 0; //Количество вершин
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; //Взвешенность
// ignore: prefer_final_fields // ignore: prefer_final_fields
bool _oriented = false; bool _oriented = false; //Ориентированность
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;
_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]!);
}
}
//*********************Add************************
bool addDot(Dot a) { bool addDot(Dot a) {
if (getNumByName(a.getName()) != null) { if (getNumByName(a.getName()) != null) {
print("Dot name already in use. Change name or use addPath"); print(
"Dot name ${a.getName()} already in use. Change name or use addPath");
return false; return false;
} }
_amount++; _amount++;
a.num = _amount; a.num = _amount;
_dots.add(a); _dots.add(a);
_syncNameTable(); _syncNameTable();
if (_oriented) _fixPathAfterInsert(a); checkDots(false);
if (!_oriented) _fullFix();
return true; return true;
} }
bool checkDots(bool verbose) { bool addDotFromToLists(String name, List<int> num0, List<int> length,
[int n = -1]) {
var a = Dot.fromTwoLists(name, num0, length, n);
if (getNumByName(a.getName()) != null) {
print(
"Dot name ${a.getName()} already in use. Change name or use addPath");
return false;
}
_amount++;
a.num = _amount;
_dots.add(a);
_syncNameTable();
checkDots(false);
if (!_oriented) _fixPathAfterInsert(a);
return true;
}
bool addIsolated(String name) {
var o = addDot(Dot.fromTwoLists(name, [], []));
_syncNameTable();
return o;
}
bool addPath(int from, int to, [int len = 0]) {
if (from <= 0 || from > _amount || to <= 0 && to > _amount) {
return false;
}
_dots[from - 1].addPath(to, len);
if (!_oriented) {
_dots[to - 1].addPath(from, len);
}
return true;
}
//*********************Add************************
//******Helper*******
bool checkDots([bool verbose = false]) {
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 {
@ -169,7 +169,38 @@ class Graphs {
return true; return true;
} }
//Getters void _fixPathAfterInsert(Dot a) {
//Для неориентированного
for (var i in a.ln.keys) {
if (!_dots[i - 1].ln.containsKey(a.num)) {
_dots[i - 1].addPath(i, a.ln[i]!);
}
}
}
void _fullFix() {
for (var i in _dots) {
_fixPathAfterInsert(i);
}
}
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();
}
//******Helper*******
//*****Getters*******
bool getDoubleSided() => _oriented; bool getDoubleSided() => _oriented;
bool getUseLength() => _useLength; bool getUseLength() => _useLength;
List<Dot> getDots() => _dots; List<Dot> getDots() => _dots;
@ -182,13 +213,26 @@ class Graphs {
return null; return null;
} }
//Print info List<List<int>>? getTable() {
List<List<int>>? out = <List<int>>[];
for (int i = 0; i < _amount; i++) {
List<int> xx = <int>[];
for (int j = 1; j <= _amount; j++) {
xx.add(_dots[i].getLength(j));
}
out.add(xx);
}
return out;
}
//*****Getters*******
//******Print******
void printG() { void printG() {
stdout.write("$_name: "); stdout.write("$_name: ");
if (_oriented) { if (_oriented) {
stdout.write("Не ориентированный, ");
} else {
stdout.write("Ориентированный, "); stdout.write("Ориентированный, ");
} else {
stdout.write("Не ориентированный, ");
} }
if (_useLength) { if (_useLength) {
print("Взвешенный"); print("Взвешенный");
@ -199,4 +243,75 @@ class Graphs {
i.printD(); i.printD();
} }
} }
void toFile(String name) {
Separators sep = Separators();
var file = File(name);
file.writeAsStringSync("$_name\n");
if (_oriented) {
file.writeAsStringSync(sep.isOriented, mode: FileMode.append);
} else {
file.writeAsStringSync(sep.isNotOriented, mode: FileMode.append);
}
if (_useLength) {
file.writeAsStringSync(sep.hasLength, mode: FileMode.append);
} else {
file.writeAsStringSync(sep.hasNoLength, mode: FileMode.append);
}
for (int i = 0; i < _amount; i++) {
file.writeAsStringSync((i + 1).toString() + sep.dotToConnections,
mode: FileMode.append);
var d = _dots[i].ln;
for (var j in d.keys) {
file.writeAsStringSync(
j.toString() + sep.dotToLength + d[j].toString() + " ",
mode: FileMode.append);
}
file.writeAsStringSync(sep.nL, mode: FileMode.append);
}
file.writeAsStringSync(sep.end, mode: FileMode.append);
}
//******Print******
//*******Constructor********
Graphs() {
_name = "Udefined";
_dots = <Dot>[];
_useLength = false;
_oriented = false;
_amount = 0;
_nameTable = <int, String>{};
}
Graphs.fromList(String n, List<Dot> d, bool useL, bool oriented) {
_name = n;
_dots = d;
_useLength = useL;
_amount = _dots.length;
_oriented = oriented;
_syncNum();
if (!_oriented) {
for (int i = 0; i < _amount; i++) {
_fixPathAfterInsert(_dots[i]);
}
}
if (!_oriented) _fullFix();
}
Graphs.fromFile(String path) {
File file = File(path);
List<String> lines = file.readAsLinesSync();
for (var l in lines) {
print(l);
}
}
//*******Constructor********
//Copy
Graphs.clone(Graphs a) {
_name = a.getName();
_dots = a.getDots();
_oriented = a.getDoubleSided();
_useLength = a.getUseLength();
_amount = _dots.length;
_syncNameTable();
}
} }