Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

lec

.pdf
Скачиваний:
51
Добавлен:
24.03.2015
Размер:
3 Мб
Скачать
☆

catch

{

MessageBox.Show("Заполнены не все поля " +

"или введен неверный тип данных!");

return;

}

obj.Add(printer);

label7.Text = obj.Count.ToString();

}

private void button2_Click(object sender, EventArgs e) // в таблицу

{

dataGridView1.Rows.Clear();

foreach (object prn in obj)

{

dataGridView1.Rows.Add ( ((Printer)prn).Show() );

}

}

private void выходToolStripMenuItem1_Click(object sender, EventArgs e)

{

Close();

}

 

private void button3_Click(object sender, EventArgs e)

// очистка базы

{

 

obj.Clear();

 

dataGridView1.Rows.Clear();

 

label7.Text = obj.Count.ToString();

 

}

 

private void очиститьПоляToolStripMenuItem_Click(object sender, EventArgs e)

{

textBoxMark.Clear();

textBoxModel.Clear();

textBoxColors.Clear();

}

private void оПрограммеToolStripMenuItem_Click(object sender, EventArgs e)

{

MessageBox.Show("Программа 'База данных принтеров'.\n" + "Авторы: Горелов, Мошаров.");

}

351

private void помощьToolStripMenuItem1_Click(object sender, EventArgs e)

{

MessageBox.Show("Программа 'База данных принтеров'.\n" + "Ввод данных производится в специально отведенные поля.\n" + "Типы данных ввода:\n Марка - строка\n Модель - строка\n" + " Количество цветов - число.\n" +

"После ввода информации о принтере в БД, выводите ее в таблицу.");

}

}

}

-- Файл Form1.Designer.cs --

namespace Printer_Virtual

{

partial class Form1

{

private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

private void InitializeComponent()

{

this.textBoxMark = new System.Windows.Forms.TextBox(); this.textBoxModel = new System.Windows.Forms.TextBox(); this.textBoxColors = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button();

this.domainUpDownType = new System.Windows.Forms.DomainUpDown(); this.button2 = new System.Windows.Forms.Button(); this.domainUpDownUSB = new System.Windows.Forms.DomainUpDown(); this.button3 = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.label6 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.label8 = new System.Windows.Forms.Label(); this.menuStrip1 = new System.Windows.Forms.MenuStrip();

this.выходToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.очиститьПоляToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.выходToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();

352

this.помощьToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.помощьToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();

this.оПрограммеToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.Марка = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Модель = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Тип = new System.Windows.Forms.DataGridViewTextBoxColumn();

this.ПоддержкаUSB = new System.Windows.Forms.DataGridViewCheckBoxColumn();

this.Количество_цветов = new System.Windows.Forms.DataGridViewTextBoxColumn();

this.groupBox1.SuspendLayout();

this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout();

//

//textBoxMark

this.textBoxMark.Location = new System.Drawing.Point(10, 43); this.textBoxMark.Name = "textBoxMark";

this.textBoxMark.Size = new System.Drawing.Size(100, 20); this.textBoxMark.TabIndex = 1;

//textBoxModel

//

this.textBoxModel.Location = new System.Drawing.Point(10, 86); this.textBoxModel.Name = "textBoxModel"; this.textBoxModel.Size = new System.Drawing.Size(100, 20); this.textBoxModel.TabIndex = 2;

//

//textBoxColors

this.textBoxColors.Location = new System.Drawing.Point(10, 130); this.textBoxColors.Name = "textBoxColors";

this.textBoxColors.Size = new System.Drawing.Size(100, 20); this.textBoxColors.TabIndex = 5;

//button1

//

this.button1.Location = new System.Drawing.Point(408, 68); this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(99, 23); this.button1.TabIndex = 6;

this.button1.Text = "Ввести в базу"; this.button1.UseVisualStyleBackColor = true;

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// domainUpDownType

//

this.domainUpDownType.BackColor = System.Drawing.SystemColors.ActiveCaptionText;

353

this.domainUpDownType.Items.Add("Струйный"); this.domainUpDownType.Items.Add("Лазерный"); this.domainUpDownType.Location = new System.Drawing.Point(148, 87); this.domainUpDownType.Name = "domainUpDownType"; this.domainUpDownType.ReadOnly = true; this.domainUpDownType.Size = new System.Drawing.Size(73, 20); this.domainUpDownType.TabIndex = 8;

this.domainUpDownType.Text = "Струйный";

//

//button2

this.button2.Location = new System.Drawing.Point(408, 111); this.button2.Name = "button2";

this.button2.Size = new System.Drawing.Size(99, 23); this.button2.TabIndex = 9;

this.button2.Text = "Вывести базу"; this.button2.UseVisualStyleBackColor = true;

this.button2.Click += new System.EventHandler(this.button2_Click);

//domainUpDownUSB

//

this.domainUpDownUSB.BackColor = System.Drawing.SystemColors.ActiveCaptionText;

this.domainUpDownUSB.Items.Add("Да"); this.domainUpDownUSB.Items.Add("Нет"); this.domainUpDownUSB.Location = new System.Drawing.Point(148, 44); this.domainUpDownUSB.Name = "domainUpDownUSB"; this.domainUpDownUSB.ReadOnly = true; this.domainUpDownUSB.Size = new System.Drawing.Size(73, 20); this.domainUpDownUSB.TabIndex = 10;

this.domainUpDownUSB.Text = "Да";

//

//button3

this.button3.Location = new System.Drawing.Point(408, 155); this.button3.Name = "button3";

this.button3.Size = new System.Drawing.Size(99, 23); this.button3.TabIndex = 11;

this.button3.Text = "Очистить базу"; this.button3.UseVisualStyleBackColor = true;

this.button3.Click += new System.EventHandler(this.button3_Click);

//label1

//

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(10, 27); this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(43, 13); this.label1.TabIndex = 12;

this.label1.Text = "Марка:";

//

// label2

//

this.label2.AutoSize = true;

354

this.label2.Location = new System.Drawing.Point(10, 70); this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(49, 13); this.label2.TabIndex = 13;

this.label2.Text = "Модель:";

//

//label3

this.label3.AutoSize = true;

this.label3.Location = new System.Drawing.Point(10, 117); this.label3.Name = "label3";

this.label3.Size = new System.Drawing.Size(82, 13); this.label3.TabIndex = 14;

this.label3.Text = "Кол-во цветов:";

//label4

//

this.label4.AutoSize = true;

this.label4.Location = new System.Drawing.Point(128, 27); this.label4.Name = "label4";

this.label4.Size = new System.Drawing.Size(93, 13); this.label4.TabIndex = 15;

this.label4.Text = "Поддержка USB:";

//

//label5

this.label5.AutoSize = true;

this.label5.Location = new System.Drawing.Point(128, 71); this.label5.Name = "label5";

this.label5.Size = new System.Drawing.Size(29, 13); this.label5.TabIndex = 16;

this.label5.Text = "Тип:";

//groupBox1

//

this.groupBox1.Controls.Add(this.label5);

this.groupBox1.Controls.Add(this.label4);

this.groupBox1.Controls.Add(this.label3);

this.groupBox1.Controls.Add(this.label2);

this.groupBox1.Controls.Add(this.label1);

this.groupBox1.Controls.Add(this.domainUpDownUSB);

this.groupBox1.Controls.Add(this.domainUpDownType);

this.groupBox1.Controls.Add(this.textBoxColors);

this.groupBox1.Controls.Add(this.textBoxModel);

this.groupBox1.Controls.Add(this.textBoxMark); this.groupBox1.Location = new System.Drawing.Point(22, 27); this.groupBox1.Name = "groupBox1";

this.groupBox1.Size = new System.Drawing.Size(233, 193); this.groupBox1.TabIndex = 17;

this.groupBox1.TabStop = false; this.groupBox1.Text = "Ввод данных:";

//

// label6

//

355

this.label6.AutoSize = true;

this.label6.Location = new System.Drawing.Point(411, 207); this.label6.Name = "label6";

this.label6.Size = new System.Drawing.Size(96, 13); this.label6.TabIndex = 18;

this.label6.Text = "Объектов в базе:";

//

//label7

this.label7.AutoSize = true;

this.label7.Location = new System.Drawing.Point(507, 207); this.label7.Name = "label7";

this.label7.Size = new System.Drawing.Size(13, 13); this.label7.TabIndex = 19;

this.label7.Text = "0";

//label8

//

this.label8.AutoSize = true;

this.label8.Location = new System.Drawing.Point(19, 239); this.label8.Name = "label8";

this.label8.Size = new System.Drawing.Size(83, 13); this.label8.TabIndex = 20;

this.label8.Text = "Вывод данных:";

//

// menuStrip1

//

this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.выходToolStripMenuItem,

this.помощьToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1";

this.menuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;

this.menuStrip1.Size = new System.Drawing.Size(568, 24); this.menuStrip1.TabIndex = 21;

this.menuStrip1.Text = "menuStrip1";

//

// выходToolStripMenuItem

//

this.выходToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.очиститьПоляToolStripMenuItem, this.выходToolStripMenuItem1});

this.выходToolStripMenuItem.Name = "выходToolStripMenuItem"; this.выходToolStripMenuItem.Size = new System.Drawing.Size(84, 20); this.выходToolStripMenuItem.Text = "Программа";

//

// очиститьПоляToolStripMenuItem

//

this.очиститьПоляToolStripMenuItem.Name = "очиститьПоляToolStripMenuItem";

this.очиститьПоляToolStripMenuItem.Size = new System.Drawing.Size(176, 22); this.очиститьПоляToolStripMenuItem.Text = "Очистить поля";

356

this.очиститьПоляToolStripMenuItem.ToolTipText = "Очистить поля ввода"; this.очиститьПоляToolStripMenuItem.Click += new

System.EventHandler(this.очиститьПоляToolStripMenuItem_Click);

//

// выходToolStripMenuItem1

//

this.выходToolStripMenuItem1.Name = "выходToolStripMenuItem1"; this.выходToolStripMenuItem1.Size = new System.Drawing.Size(176, 22); this.выходToolStripMenuItem1.Text = "Выход"; this.выходToolStripMenuItem1.Click += new

System.EventHandler(this.выходToolStripMenuItem1_Click);

//

// помощьToolStripMenuItem

//

this.помощьToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.помощьToolStripMenuItem1, this.оПрограммеToolStripMenuItem});

this.помощьToolStripMenuItem.Name = "помощьToolStripMenuItem"; this.помощьToolStripMenuItem.Size = new System.Drawing.Size(52, 20); this.помощьToolStripMenuItem.Text = "Инфо";

//

// помощьToolStripMenuItem1

//

this.помощьToolStripMenuItem1.Name = "помощьToolStripMenuItem1"; this.помощьToolStripMenuItem1.Size = new System.Drawing.Size(165, 22); this.помощьToolStripMenuItem1.Text = "Помощь"; this.помощьToolStripMenuItem1.Click += new

System.EventHandler(this.помощьToolStripMenuItem1_Click);

//

// оПрограммеToolStripMenuItem

//

this.оПрограммеToolStripMenuItem.Name = "оПрограммеToolStripMenuItem"; this.оПрограммеToolStripMenuItem.Size = new System.Drawing.Size(165, 22); this.оПрограммеToolStripMenuItem.Text = "О программе"; this.оПрограммеToolStripMenuItem.Click += new

System.EventHandler(this.оПрограммеToolStripMenuItem_Click);

//

// dataGridView1

//

this.dataGridView1.AllowUserToAddRows = false; this.dataGridView1.AllowUserToDeleteRows = false; this.dataGridView1.AllowUserToResizeColumns = false; this.dataGridView1.AllowUserToResizeRows = false; this.dataGridView1.BackgroundColor = System.Drawing.Color.White; this.dataGridView1.ColumnHeadersHeightSizeMode =

DataGridViewColumnHeadersHeight-

SizeMode.AutoSize;

this.dataGridView1.Columns.AddRange(new DataGridViewColumn[] { this.Марка,

357

this.Модель, this.Тип, this.ПоддержкаUSB,

this.Количество_цветов});

this.dataGridView1.Location = new System.Drawing.Point(12, 265); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Size = new System.Drawing.Size(543, 239); this.dataGridView1.TabIndex = 22;

//

//Марка

this.Марка.HeaderText = "Марка"; this.Марка.Name = "Марка";

this.Марка.ReadOnly = true;

//Модель

//

this.Модель.HeaderText = "Модель"; this.Модель.Name = "Модель"; this.Модель.ReadOnly = true;

//

//Тип

this.Тип.HeaderText = "Тип"; this.Тип.Name = "Тип";

this.Тип.ReadOnly = true;

//ПоддержкаUSB

//

this.ПоддержкаUSB.HeaderText = "Поддержка USB"; this.ПоддержкаUSB.Name = "ПоддержкаUSB"; this.ПоддержкаUSB.ReadOnly = true;

this.ПоддержкаUSB.Resizable = System.Windows.Forms.DataGridViewTriState.True;

this.ПоддержкаUSB.SortMode = DataGridViewColumnSortMode.Automatic;

//

//Количество_цветов

this.Количество_цветов.HeaderText = "Количество Цветов"; this.Количество_цветов.Name = "Количество_цветов";

this.Количество_цветов.ReadOnly = true;

//Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(568, 516); this.Controls.Add(this.dataGridView1); this.Controls.Add(this.label8);

this.Controls.Add(this.label7);

this.Controls.Add(this.label6);

this.Controls.Add(this.groupBox1);

this.Controls.Add(this.button3);

this.Controls.Add(this.button2);

358

this.Controls.Add(this.button1);

this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "Form1";

this.Text = "База данных \"Принтеры\""; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout();

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false);

this.PerformLayout();

}

private System.Windows.Forms.TextBox textBoxMark; private System.Windows.Forms.TextBox textBoxModel; private System.Windows.Forms.TextBox textBoxColors; private System.Windows.Forms.Button button1;

private System.Windows.Forms.DomainUpDown domainUpDownType; private System.Windows.Forms.Button button2;

private System.Windows.Forms.DomainUpDown domainUpDownUSB; private System.Windows.Forms.Button button3;

private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5;

private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label6;

private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label8;

private System.Windows.Forms.MenuStrip menuStrip1;

private System.Windows.Forms.ToolStripMenuItem выходToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem выходToolStripMenuItem1; private System.Windows.Forms.ToolStripMenuItem очиститьПоляToolStrip-

MenuItem;

private System.Windows.Forms.ToolStripMenuItem помощьToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem помощьToolStripMenuItem1; private System.Windows.Forms.ToolStripMenuItem оПрограммеToolStripMenuItem; private System.Windows.Forms.DataGridView dataGridView1;

private System.Windows.Forms.DataGridViewTextBoxColumn Марка; private System.Windows.Forms.DataGridViewTextBoxColumn Модель; private System.Windows.Forms.DataGridViewTextBoxColumn Тип;

private System.Windows.Forms.DataGridViewCheckBoxColumn ПоддержкаUSB; private System.Windows.Forms.DataGridViewTextBoxColumn Количество_цветов;

}

}

Пример 2

Предметная область - сеть аэропортов, связанных авиатрассами Определить длину кратчайшего по количеству трасс маршрута

359

от заданного аэропорта до всех остальных аэропортов. Представление сети аэропортов - граф, заданный матрицей смежно-

сти.

Вершина графа - азропорт, ребро - авиатрасса.

Редактирование матрицы смежности - визуально щелчком мыши на соответствующей ячейке. Назначение аэропорта вылета - щелчком мыши на имени аэропорта в левой колонке таблицы.

Решение задачи основано на обходе графа в ширину. Управление обходом через очередь.

Роль окраски вершин при обходе выполняет массив расстояний. Отрицательное значение расстояния эквивалентно не пройденной

вершине.

рис.10

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data;

using System.Drawing; using System.Text;

using System.Windows.Forms; using System.Collections;

namespace EX1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

 

 

string[] name;

//имена аэропортов

int

n,

//Количество аэропортов

 

старт; //порядковый номер аэропорта вылета

int[]

d;

//Расстояние от аэропорта вылета до всех аэропортов

360

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]