Построение компьютерной сети - C#
Формулировка задачи:
Здравствуйте!
Вопрос, возможно, покажется глуповатым, но суть состоит вот в чем. Нужно было написать программу, которая рисует на экране заданную компьютерную сеть и определяет, с какой максимальной скоростью можно передавать между компьютерами с номерами 0 и 7, с помощью Windows Forms. Программа написана полностью в одном месте, стоит задача - разбить на классы (функции, вычисляющие; функции, рисующие). И вот здесь тупик, потому что как-то ничего не получается..подскажите, как лучше сделать? Код и входной файл вот:
8
100 200
250 300
400 350
550 300
700 200
550 100
400 50
250 100
16
0 1 32
0 2 95
0 3 75
0 4 57
1 2 5
1 4 23
1 7 16
2 3 18
2 5 6
3 4 24
3 5 9
4 6 20
4 7 94
5 4 11
5 6 7
6 7 81
Надеюсь, понятно объяснила
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Drawing.Drawing2D; namespace MaxStream { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static int FindPath(int[,] cap, bool[] vis, int u, int t, int f) { if (u == t) return f; vis[u] = true; for (int v = 0; v < cap.GetLength(0); v++) if (!vis[v] && cap[u, v] > 0) { int df = FindPath(cap, vis, v, t, Math.Min(f, cap[u, v])); if (df > 0) { cap[u, v] -= df; cap[v, u] += df; return df; } } return 0; } public static int MaxFlow(int[,] cap, int s, int t) { for (int flow = 0; ; ) { int df = FindPath(cap, new bool[cap.Length], s, t, Int32.MaxValue); if (df == 0) return flow; flow += df; } } private void BuildMap(object sender, EventArgs e) { panel1.Visible = true; } private void Map(object sender, PaintEventArgs e) { Graphics g = panel1.CreateGraphics(); Brush b = new SolidBrush(Color.Purple); Pen Pen = new Pen(b, 3); string file = File.ReadAllText("input.txt"); int[] info = file.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(n => int.Parse(n)).ToArray(); string file1 = File.ReadAllText("input.txt"); string[] info1 = file1.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries); int q = info[0]; int qLink = info[q * 2 + 1]; int[,] coordinates = new int[q, 2]; int k = 1; for (int i = 0; i < q; i++) { for (int j = 0; j < 2; j++) { coordinates[i, j] = info[k]; k++; } } for (int i = q * 2 + 2; i <= q * 2 + qLink * 3 - 1; i += 3) { Pen pen = new Pen(Color.DarkBlue, 8); pen.StartCap = LineCap.ArrowAnchor; e.Graphics.DrawLine(pen, coordinates[info[i+1], 0], coordinates[info[i+1], 1], coordinates[info[i], 0], coordinates[info[i], 1]); } } private void Calculate(object sender, EventArgs e) { string file = File.ReadAllText("input.txt"); int[] info = file.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(n => int.Parse(n)).ToArray(); string file1 = File.ReadAllText("input.txt"); string[] info1 = file1.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries); int q = info[0]; int[,] MyArray = new int[q, q]; Dictionary<string, int> dic = new Dictionary<string, int>(); int qLink = info[q * 2 + 1]; for (int i = q * 2 + 2; i <= q * 2 + qLink * 3 - 1; i += 3) { string vsp = info1[i] + ' ' + info1[i + 1]; dic.Add(vsp, info[i + 2]); } foreach (KeyValuePair<string, int> kvp in dic) { string key = kvp.Key; string[] mas = key.Split(' '); int stock = int.Parse(mas[0]); int istock = int.Parse(mas[1]); MyArray[stock, istock] = kvp.Value; } int strange = MaxFlow(MyArray, 0, MyArray.GetLength(0) - 1); label1.Text = String.Format("= {0}", strange); } } }
Решение задачи: «Построение компьютерной сети»
textual
Листинг программы
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Drawing.Drawing2D; namespace MaxStream { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void BuildMap(object sender, EventArgs e) { MyNetwork.BuildMap(); } public void Map(object sender, PaintEventArgs e) { MyNetwork.Map(sender,e); } public void Calculate(object sender, EventArgs e) { MyNetwork.Calculate(); } } public class MyNetwork { Form1 form1; MyNetwork(Form1 reference) { form1 = reference; } public static void BuildMap() { form1.panel1.Visible=true; } public static void Map(object sender, PaintEventArgs e) { Graphics g = form1.panel1.CreateGraphics(); Brush b = new SolidBrush(Color.Purple); Pen Pen = new Pen(b, 3); string file = File.ReadAllText("input.txt"); int[] info = file.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(n => int.Parse(n)).ToArray(); string file1 = File.ReadAllText("input.txt"); string[] info1 = file1.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries); int q = info[0]; int qLink = info[q * 2 + 1]; int[,] coordinates = new int[q, 2]; int k = 1; for (int i = 0; i < q; i++) { for (int j = 0; j < 2; j++) { coordinates[i, j] = info[k]; k++; } } for (int i = q * 2 + 2; i <= q * 2 + qLink * 3 - 1; i += 3) { Pen pen = new Pen(Color.DarkBlue, 8); pen.StartCap = LineCap.ArrowAnchor; e.Graphics.DrawLine(pen, coordinates[info[i + 1], 0], coordinates[info[i + 1], 1], coordinates[info[i], 0], coordinates[info[i], 1]); } } public static void Calculate() { string file = File.ReadAllText("input.txt"); int[] info = file.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(n => int.Parse(n)).ToArray(); string file1 = File.ReadAllText("input.txt"); string[] info1 = file1.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries); int q = info[0]; int[,] MyArray = new int[q, q]; Dictionary<string, int> dic = new Dictionary<string, int>(); int qLink = info[q * 2 + 1]; for (int i = q * 2 + 2; i <= q * 2 + qLink * 3 - 1; i += 3) { string vsp = info1[i] + ' ' + info1[i + 1]; dic.Add(vsp, info[i + 2]); } foreach (KeyValuePair<string, int> kvp in dic) { string key = kvp.Key; string[] mas = key.Split(' '); int stock = int.Parse(mas[0]); int istock = int.Parse(mas[1]); MyArray[stock, istock] = kvp.Value; } int strange = MaxFlow(MyArray, 0, MyArray.GetLength(0) - 1); Form1.label1.Text = String.Format("= {0}", strange); } private static int MaxFlow(int[,] cap, int s, int t) { for (int flow = 0; ; ) { int df = FindPath(cap, new bool[cap.Length], s, t, Int32.MaxValue); if (df == 0) return flow; flow += df; } } private static int FindPath(int[,] cap, bool[] vis, int u, int t, int f) { if (u == t) return f; vis[u] = true; for (int v = 0; v < cap.GetLength(0); v++) if (!vis[v] && cap[u, v] > 0) { int df = FindPath(cap, vis, v, t, Math.Min(f, cap[u, v])); if (df > 0) { cap[u, v] -= df; cap[v, u] += df; return df; } } return 0; } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д