Базовые классы

This commit is contained in:
Морозов Андрей 2021-10-21 02:31:33 +04:00
parent 3d8e0f3775
commit 93407fb312
8 changed files with 221 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
# Files and directories created by pub.
.dart_tool/
.packages
# Conventional directory for build output.
build/

3
CHANGELOG.md Normal file
View File

@ -0,0 +1,3 @@
## 1.0.0
- Initial version.

1
README.md Normal file
View File

@ -0,0 +1 @@
A simple command-line application.

30
analysis_options.yaml Normal file
View File

@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.
include: package:lints/recommended.yaml
# Uncomment the following section to specify additional rules.
# linter:
# rules:
# - camel_case_types
# analyzer:
# exclude:
# - path/to/excluded/files/**
# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints
# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options

17
bin/graphs0.dart Normal file
View File

@ -0,0 +1,17 @@
import 'src/graph.dart';
void main(List<String> arguments) {
Map<int, int> x = <int, int>{};
x[1] = 5;
x[2] = 10;
var p = Dot.fromMap("Точка 1", 1, x);
p.addPath(3, 20);
var x1 = Graphs([p], true);
x1.printG();
p.delPath(4);
x1.printG();
/*var p2 = Dot("Точка 2", 2, x);
x1.addDot(p2);
x1.printG();
x1.checkDots();*/
}

138
bin/src/graph.dart Normal file
View File

@ -0,0 +1,138 @@
import 'dart:io';
class Point {
int i = -1;
int length = 0;
}
class Dot {
// ignore: prefer_final_fields
String _name = "";
int num = -1;
Map<int, int> ln = <int, int>{};
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 printD() {
stdout.write("$_name: №$num => ");
for (var i in ln.keys) {
stdout.write("$i|${ln[i]} ");
}
stdout.write("\n");
}
Dot(String name, int n, List<int> num, List<int> length) {
_name = name;
Map<int, int> nw = <int, int>{};
if (num.length != length.length) {
print("Error in lists");
} else {
for (var i = 0; i <= num.length; i++) {
nw[num[i]] = length[i];
ln = nw;
}
}
}
// Привязка к памяти
Dot.fromMap(String name, int n, Map<int, int> l) {
_name = name;
num = n;
ln = l;
}
// Копирование
Dot.clone(Dot a) {
_name = a._name;
num = a.num;
ln = a.ln;
}
}
class Graphs {
int _amount = 0;
List<Dot> _dots = <Dot>[];
// ignore: prefer_final_fields
Map<int, String> _nameTable = <int, String>{};
bool useLength = false;
bool doubleSided = false;
Graphs(List<Dot> d, bool useL) {
_dots = d;
useLength = useL;
_amount = _dots.length;
_syncNameTable();
}
void _syncNameTable() {
for (var i in _dots) {
_nameTable[i.num] = i.getName();
}
}
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]!);
}
}
bool addDot(Dot a) {
if (getNumByName(a.getName()) != null) {
print("Dot name already in use. Change name or use addPath");
return false;
}
_amount++;
a.num = _amount;
_dots.add(a);
_syncNameTable();
if (doubleSided) _fixPathAfterInsert(a);
return true;
}
bool checkDots() {
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");
return false;
}
} catch (e) {
print(
"Can't find Dot ${i + 1} for path ${a.num}->${i + 1}. Exception $e");
return false;
}
}
}
return true;
}
//Getters
String? getNameByNum(int n) => _nameTable[n];
int? getNumByName(String n) {
for (var i in _nameTable.keys) {
if (_nameTable[i] == n) return i;
}
return null;
}
//Print info
void printG() {
if (doubleSided) {
print("Не ориентированный");
} else {
print("Ориентированный");
}
if (useLength) {
print("Взвешенный");
} else {
print("Не взвешенный");
}
for (var i in _dots) {
i.printD();
}
}
}

12
pubspec.lock Normal file
View File

@ -0,0 +1,12 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
lints:
dependency: "direct dev"
description:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
sdks:
dart: ">=2.15.0-178.0.dev <3.0.0"

14
pubspec.yaml Normal file
View File

@ -0,0 +1,14 @@
name: graphs0
description: A simple command-line application.
version: 1.0.0
# homepage: https://www.example.com
environment:
sdk: '>=2.15.0-178.0.dev <3.0.0'
# dependencies:
# path: ^1.8.0
dev_dependencies:
lints: ^1.0.0