Помощь в написании студенческих работ
Антистрессовый сервис

Реализация программного модуля

РефератПомощь в написанииУзнать стоимостьмоей работы

Private void button3_Click (object sender, EventArgs e). Private void button2_Click (object sender, EventArgs e). Private void button1_Click (object sender, EventArgs e). DataGridView1.Rows. Cells. Value = Matrix. ToString (); Private void find_star_in_row (int row, ref int col). Private void Form1_Load (object sender, EventArgs e). For (int j = 0; j < dataGridView1. ColumnCount; j++). For (int j… Читать ещё >

Реализация программного модуля (реферат, курсовая, диплом, контрольная)

Код программы

using System;

using System. IO;

using System.Collections.Generic;

using System. ComponentModel;

using System. Data;

using System. Drawing;

using System. Linq;

using System. Text;

using System.Windows.Forms;

namespace WindowsFormsApplication1.

{.

public partial class Form1: Form.

{.

public static int[,] Matrix = new int[10, 10];

public static int[,] M = new int[10, 10];

public static int[,] path = new int[21, 2];

public static int[] RowCover = new int[10];

public static int[] ColCover = new int[10];

public static int nrow;

public static int ncol;

public static int path_count = 0;

public static int path_row0;

public static int path_col0;

public static int asgn = 0;

public static int step;

public Form1().

{.

InitializeComponent ();

}.

private void Form1_Load (object sender, EventArgs e).

{.

dataGridView1.Rows.Clear ();

dataGridView2.Rows.Clear ();

textBox1.Clear ();

textBox2.Clear ();

}.

private void button1_Click (object sender, EventArgs e).

{.

dataGridView1.Rows.Clear ();

dataGridView2.Rows.Clear ();

textBox2.Clear ();

string fname, text;

OpenFileDialog Fd = new OpenFileDialog ();

Fd.Title = «Выберитефайл» ;

Fd.InitialDirectory = @" L:" ;

Fd.Filter = «текстовые.файлы (*.txt)|*.txt;|Всефайлы|*.*» ;

bool flag = false;

while (flag == false).

{.

if (Fd.ShowDialog () == DialogResult. OK).

{.

fname = Fd. FileName;

int n;

StreamReader reader = new StreamReader (fname);

if (!Int32.TryParse (reader.ReadLine (), out n)).

{.

MessageBox.Show («Некорректно записана размерность матрицы в файле!»);

flag = false;

}.

else.

{.

dataGridView1.RowCount = n;

dataGridView1.ColumnCount = n;

dataGridView2.RowCount = n;

dataGridView2.ColumnCount = n;

flag = true;

}.

if (flag == true).

{.

nrow = 0;

do.

{.

text = reader. ReadLine ();

if (text ≠ null).

{.

ncol = 0;

foreach (string subString in text. Split (' ')).

{.

if (subString.Length > 0).

{.

if (!Int32.TryParse (subString, out Matrix[nrow, ncol])).

{.

MessageBox.Show («Некорректно записан элемент матрицы в файле!»);

flag = false;

break;

}.

else.

{.

Matrix[nrow, ncol] = Int32. Parse (subString);

dataGridView1.Columns[ncol]. Width = 15;

dataGridView1[ncol, nrow]. Value = Matrix[nrow, ncol];

ncol += 1;

flag = true;

}.

}.

}.

if (flag == false) break;

nrow += 1;

}.

} while (text ≠ null);

reader.Close ();

break;

}.

}.

else break;

}.

}.

private void button2_Click (object sender, EventArgs e).

{.

dataGridView1.Rows.Clear ();

dataGridView2.Rows.Clear ();

textBox2.Clear ();

if (!string.IsNullOrEmpty (textBox1.Text)).

{.

nrow = int. Parse (textBox1.Text);

ncol = int. Parse (textBox1.Text);

dataGridView1.RowCount = nrow;

dataGridView1.ColumnCount = ncol;

dataGridView2.RowCount = nrow;

dataGridView2.ColumnCount = ncol;

Random r = new Random (10);

for (int i = 0; i < dataGridView1. RowCount; i++).

{.

for (int j = 0; j < dataGridView1. ColumnCount; j++).

{.

Matrix[i, j] = r. Next (10);

dataGridView1.Columns[j]. Width = 15;

dataGridView1.Rows[i]. Cells[j].Value = Matrix[i, j]. ToString ();

}.

}.

}.

}.

private void button5_Click (object sender, EventArgs e).

{.

for (int i = 0; i < dataGridView1. RowCount; i++).

{.

for (int j = 0; j < dataGridView1. ColumnCount; j++).

{.

dataGridView1[j, i]. Style. ForeColor = Color. Black;

Matrix[i, j] = Convert. ToInt32(dataGridView1.Rows[i]. Cells[j].Value);

}.

}.

resetMaskandCovers ();

step = 1;

HungarianMethod ();

}.

private void HungarianMethod ().

{.

bool done = false;

while (!done).

{.

switch (step).

{.

case 1:

step_one (ref step);

break;

case 2:

step_two (ref step);

break;

case 3:

step_three (ref step);

break;

case 4:

step_four (ref step);

break;

case 5:

step_five (ref step);

break;

case 6:

step_six (ref step);

break;

case 7:

step_seven (ref step);

done = true;

break;

}.

}.

}.

private void resetMaskandCovers ().

{.

for (int r = 0; r < nrow; r++).

{.

RowCover[r] = 0;

for (int c = 0; c < ncol; c++).

{.

M[r, c] = 0;

}.

}.

for (int c = 0; c < ncol; c++).

ColCover[c] = 0;

}.

private void step_one (ref int step).

{.

int min_in_row, max_in_col, min_in_col;

if ((checkBox1.Checked == true) && (checkBox2.Checked == false)).

{.

for (int c = 0; c < ncol; c++).

{.

max_in_col = Matrix[0, c];

for (int r = 0; r < nrow; r++).

if (Matrix[r, c] > max_in_col).

max_in_col = Matrix[r, c];

for (int r = 0; r < nrow; r++).

Matrix[r, c] = max_in_col — Matrix[r, c];

}.

for (int r = 0; r < nrow; r++).

{.

min_in_row = Matrix[r, 0];

for (int c = 0; c < ncol; c++).

if (Matrix[r, c] < min_in_row).

min_in_row = Matrix[r, c];

for (int c = 0; c < ncol; c++).

Matrix[r, c] -= min_in_row;

}.

}.

else if ((checkBox2.Checked == true) && (checkBox1.Checked == false)).

{.

for (int c = 0; c < ncol; c++).

{.

min_in_col = Matrix[0, c];

for (int r = 0; r < nrow; r++).

if (Matrix[r, c] < min_in_col).

min_in_col = Matrix[r, c];

for (int r = 0; r < nrow; r++).

Matrix[r, c] -= min_in_col;

}.

for (int r = 0; r < nrow; r++).

{.

min_in_row = Matrix[r, 0];

for (int c = 0; c < ncol; c++).

if (Matrix[r, c] < min_in_row).

min_in_row = Matrix[r, c];

for (int c = 0; c < ncol; c++).

Matrix[r, c] -= min_in_row;

}.

}.

else if ((checkBox1.Checked == false) && (checkBox2.Checked == false)).

{.

checkBox1.Checked = true;

for (int c = 0; c < ncol; c++).

{.

max_in_col = Matrix[0, c];

for (int r = 0; r < nrow; r++).

if (Matrix[r, c] > max_in_col).

max_in_col = Matrix[r, c];

for (int r = 0; r < nrow; r++).

Matrix[r, c] = max_in_col — Matrix[r, c];

}.

for (int r = 0; r < nrow; r++).

{.

min_in_row = Matrix[r, 0];

for (int c = 0; c < ncol; c++).

if (Matrix[r, c] < min_in_row).

min_in_row = Matrix[r, c];

for (int c = 0; c < ncol; c++).

Matrix[r, c] -= min_in_row;

}.

}.

step = 2;

}.

private void step_two (ref int step).

{.

for (int r = 0; r < nrow; r++).

for (int c = 0; c < ncol; c++).

{.

if (Matrix[r, c] == 0 && RowCover[r] == 0 && ColCover[c] == 0).

{.

M[r, c] = 1;

RowCover[r] = 1;

ColCover[c] = 1;

}.

}.

for (int r = 0; r < nrow; r++).

RowCover[r] = 0;

for (int c = 0; c < ncol; c++).

ColCover[c] = 0;

step = 3;

}.

private void step_three (ref int step).

{.

int colcount;

for (int r = 0; r < nrow; r++).

for (int c = 0; c < ncol; c++).

if (M[r, c] == 1).

ColCover[c] = 1;

colcount = 0;

for (int c = 0; c < ncol; c++).

if (ColCover[c] == 1).

colcount += 1;

if (colcount >= ncol || colcount >= nrow).

step = 7;

else.

step = 4;

}.

private void find_a_zero (ref int row, ref int col).

{.

int r = 0;

int c;

bool done;

row = -1;

col = -1;

done = false;

while (!done).

{.

c = 0;

while (true).

{.

if (Matrix[r, c] == 0 && RowCover[r] == 0 && ColCover[c] == 0).

{.

row = r;

col = c;

done = true;

}.

c += 1;

if (c >= ncol || done).

break;

}.

r += 1;

if (r >= nrow).

done = true;

}.

}.

private bool star_in_row (int row).

{.

bool tmp = false;

for (int c = 0; c < ncol; c++).

if (M[row, c] == 1).

tmp = true;

return tmp;

}.

private void find_star_in_row (int row, ref int col).

{.

col = -1;

for (int c = 0; c < ncol; c++).

if (M[row, c] == 1).

col = c;

}.

private void step_four (ref int step).

{.

int row = -1;

int col = -1;

bool done;

done = false;

while (!done).

{.

find_a_zero (ref row, ref col);

if (row == -1).

{.

done = true;

step = 6;

}.

else.

{.

M[row, col] = 2;

if (star_in_row (row)).

{.

find_star_in_row (row, ref col);

RowCover[row] = 1;

ColCover[col] = 0;

}.

else.

{.

done = true;

step = 5;

path_row0 = row;

path_col0 = col;

}.

}.

}.

}.

private void find_star_in_col (int c, ref int r).

{.

r = -1;

for (int i = 0; i < nrow; i++).

if (M[i, c] == 1).

r = i;

}.

private void find_prime_in_row (int r, ref int c).

{.

for (int j = 0; j < ncol; j++).

if (M[r, j] == 2).

c = j;

}.

private void augment_path ().

{.

for (int p = 0; p < path_count; p++).

if (M[path[p, 0], path[p, 1]] == 1).

M[path[p, 0], path[p, 1]] = 0;

else.

M[path[p, 0], path[p, 1]] = 1;

}.

private void clear_covers ().

{.

for (int r = 0; r < nrow; r++).

RowCover[r] = 0;

for (int c = 0; c < ncol; c++).

ColCover[c] = 0;

}.

private void erase_primes ().

{.

for (int r = 0; r < nrow; r++).

for (int c = 0; c < ncol; c++).

if (M[r, c] == 2).

M[r, c] = 0;

}.

private void step_five (ref int step).

{.

bool done;

int r = -1;

int c = -1;

path_count = 1;

path[path_count — 1, 0] = path_row0;

path[path_count — 1, 1] = path_col0;

done = false;

while (!done).

{.

find_star_in_col (path[path_count — 1, 1], ref r);

if (r > -1).

{.

path_count += 1;

path[path_count — 1, 0] = r;

path[path_count — 1, 1] = path[path_count — 2, 1];

}.

else.

done = true;

if (!done).

{.

find_prime_in_row (path[path_count — 1, 0], ref c);

path_count += 1;

path[path_count — 1, 0] = path[path_count — 2, 0];

path[path_count — 1, 1] = c;

}.

}.

augment_path ();

clear_covers ();

erase_primes ();

step = 3;

}.

private void find_smallest (ref int minval).

{.

for (int r = 0; r < nrow; r++).

for (int c = 0; c < ncol; c++).

if (RowCover[r] == 0 && ColCover[c] == 0).

if (minval > Matrix[r, c]).

minval = Matrix[r, c];

}.

private void step_six (ref int step).

{.

int minval = int. MaxValue;

find_smallest (ref minval);

for (int r = 0; r < nrow; r++).

for (int c = 0; c < ncol; c++).

{.

if (RowCover[r] == 1).

Matrix[r, c] += minval;

if (ColCover[c] == 0).

Matrix[r, c] -= minval;

}.

step = 4;

}.

private void step_seven (ref int step).

{.

int F = 0;

for (int i = 0; i < dataGridView2. ColumnCount; i++).

{.

for (int j = 0; j < dataGridView2. RowCount; j++).

{.

dataGridView2.Columns[i]. Width = 15;

dataGridView2.Rows[i]. Cells[j].Value = Matrix[i, j];

if (M[i, j] == 1).

{.

dataGridView1[j, i]. Style. ForeColor = Color. Red;

F += Convert. ToInt32(dataGridView1[j, i]. Value);

}.

}.

}.

textBox2.Text = F. ToString ();

}.

private void textBox1_KeyPress (object sender, KeyPressEventArgs e).

{.

if (!(Char.IsDigit (e.KeyChar))).

{.

if (e.KeyChar ≠ (char)Keys.Back).

{.

e.Handled = true;

}.

}.

}.

private void textBox1_TextChanged (object sender, EventArgs e).

{.

if (textBox1.Text == «»).

return;

try.

{.

int number = System.Convert.ToInt32(textBox1.Text);

if (number 10).

{.

MessageBox.Show («Введенноечислонекорректно! Введите число от 1 до 10!»);

textBox1.Text = «» ;

}.

}.

catch (Exceptionex).

{.

MessageBox.Show («Введенное число некорректно! Введитечислоот 1 до 10!»);

}.

}.

private void button3_Click (object sender, EventArgs e).

{.

Application.Exit ();

}.

private void button4_Click (object sender, EventArgs e).

{.

dataGridView1.Rows.Clear ();

dataGridView1.Columns.Clear ();

dataGridView2.Rows.Clear ();

dataGridView2.Columns.Clear ();

textBox1.Clear ();

textBox2.Clear ();

checkBox1.Checked = false;

checkBox2.Checked = false;

}.

private void checkBox1_CheckedChanged (object sender, EventArgs e).

{.

if (checkBox1.Checked == true) checkBox2. Checked = false;

if (checkBox1.Checked == false) checkBox2. Checked = true;

}.

private void checkBox2_CheckedChanged (object sender, EventArgs e).

{.

if (checkBox2.Checked == true) checkBox1. Checked = false;

if (checkBox2.Checked == false) checkBox1. Checked = true;

}.

}.

}.

Показать весь текст
Заполнить форму текущей работой