Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace LR08
- {
- class Program
- {
- static void Main(string[] args)
- {
- Application.EnableVisualStyles();//включает визуальные стили
- Application.Run(new Form1());
- }
- }
- }
- ArrayVector:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LR08
- {
- class ArrayVector: Vector, IComparable, ICloneable
- {
- double[] mass;
- public ArrayVector (int l)
- {
- mass = new double[l];
- }
- public ArrayVector ()
- {
- mass = new double[5];
- }
- public double this[int i] //ИНДЕКСАТОР
- {
- set { if ((i>=mass.Length)||(i<0)) throw new ArgumentOutOfRangeException(); else mass[i] = value; }
- get { if ((i >= mass.Length) || (i < 0)) throw new ArgumentOutOfRangeException(); else return mass[i]; }
- }
- public int Length
- {
- get { return mass.Length; }
- }
- public double GetNorm()
- {
- double norm = 0;
- for (int i = 0; i < mass.Length; i++)
- {
- norm = norm + mass[i] * mass[i];
- }
- norm = Math.Sqrt(norm); return norm;
- }
- public override string ToString()
- {
- string res = "";
- for (int i = 0; i < Length; i++)
- res = res+ this[i].ToString() + " ";
- return res;
- }
- public override bool Equals(Object obj)
- {
- Vector vec = (Vector)obj;
- if (Length == vec.Length)
- {
- int k = 0;
- for (int i = 0; i < Length; i++)
- {
- if (vec[i] == this[i]) k++;
- }
- if (k == Length)
- {
- return true;
- }
- else return false;
- }
- else return false;
- }
- public int CompareTo(object obj)
- {
- Vector vec = (Vector)obj;
- if (this.Length.CompareTo(vec.Length) > 0) return 1;
- if (this.Length.CompareTo(vec.Length) < 0) return -1;
- return 0;
- }
- public object Clone()
- {
- ArrayVector vec = new ArrayVector(Length);
- for (int i = 0; i < Length; i++)
- vec[i] = mass[i] + 1;
- return vec;
- /*new ArrayVector(Length)
- {
- mass = vec
- }; */
- }
- }
- }
- Form1.Designer:
- namespace LR08
- {
- partial class Form1
- {
- /// <summary>
- /// Обязательная переменная конструктора.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Освободить все используемые ресурсы.
- /// </summary>
- /// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Код, автоматически созданный конструктором форм Windows
- /// <summary>
- /// Требуемый метод для поддержки конструктора — не изменяйте
- /// содержимое этого метода с помощью редактора кода.
- /// </summary>
- private void InitializeComponent()
- {
- System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
- this.buttonAddArrVec = new System.Windows.Forms.Button();
- this.listBoxForVectors = new System.Windows.Forms.ListBox();
- this.buttonAddLLVec = new System.Windows.Forms.Button();
- this.buttonDel = new System.Windows.Forms.Button();
- this.listBoxForGetNorm = new System.Windows.Forms.ListBox();
- this.buttonGetNorm = new System.Windows.Forms.Button();
- this.buttonSortU = new System.Windows.Forms.Button();
- this.buttonSortD = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // buttonAddArrVec
- //
- this.buttonAddArrVec.AutoSize = true;
- this.buttonAddArrVec.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonAddArrVec.BackgroundImage")));
- this.buttonAddArrVec.ForeColor = System.Drawing.Color.Gold;
- this.buttonAddArrVec.Location = new System.Drawing.Point(12, 12);
- this.buttonAddArrVec.Name = "buttonAddArrVec";
- this.buttonAddArrVec.Size = new System.Drawing.Size(167, 26);
- this.buttonAddArrVec.TabIndex = 0;
- this.buttonAddArrVec.Text = "Добавить ArrayVector";
- this.buttonAddArrVec.UseVisualStyleBackColor = true;
- this.buttonAddArrVec.Click += new System.EventHandler(this.AddArrayVector);
- //
- // listBoxForVectors
- //
- this.listBoxForVectors.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.listBoxForVectors.FormattingEnabled = true;
- this.listBoxForVectors.Location = new System.Drawing.Point(204, 12);
- this.listBoxForVectors.Name = "listBoxForVectors";
- this.listBoxForVectors.Size = new System.Drawing.Size(272, 238);
- this.listBoxForVectors.TabIndex = 1;
- //
- // buttonAddLLVec
- //
- this.buttonAddLLVec.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonAddLLVec.BackgroundImage")));
- this.buttonAddLLVec.ForeColor = System.Drawing.Color.Gold;
- this.buttonAddLLVec.Location = new System.Drawing.Point(12, 44);
- this.buttonAddLLVec.Name = "buttonAddLLVec";
- this.buttonAddLLVec.Size = new System.Drawing.Size(167, 25);
- this.buttonAddLLVec.TabIndex = 2;
- this.buttonAddLLVec.Text = "Добавить LinkedListVector";
- this.buttonAddLLVec.UseVisualStyleBackColor = true;
- this.buttonAddLLVec.Click += new System.EventHandler(this.AddLinkedListVector);
- //
- // buttonDel
- //
- this.buttonDel.ForeColor = System.Drawing.Color.Yellow;
- this.buttonDel.Image = ((System.Drawing.Image)(resources.GetObject("buttonDel.Image")));
- this.buttonDel.Location = new System.Drawing.Point(12, 263);
- this.buttonDel.Name = "buttonDel";
- this.buttonDel.Size = new System.Drawing.Size(167, 42);
- this.buttonDel.TabIndex = 3;
- this.buttonDel.Text = "Удалить все массивы";
- this.buttonDel.UseVisualStyleBackColor = true;
- this.buttonDel.Click += new System.EventHandler(this.CleanVectors);
- //
- // listBoxForGetNorm
- //
- this.listBoxForGetNorm.FormattingEnabled = true;
- this.listBoxForGetNorm.Location = new System.Drawing.Point(204, 263);
- this.listBoxForGetNorm.Name = "listBoxForGetNorm";
- this.listBoxForGetNorm.Size = new System.Drawing.Size(272, 43);
- this.listBoxForGetNorm.TabIndex = 4;
- //
- // buttonGetNorm
- //
- this.buttonGetNorm.ForeColor = System.Drawing.Color.Yellow;
- this.buttonGetNorm.Image = ((System.Drawing.Image)(resources.GetObject("buttonGetNorm.Image")));
- this.buttonGetNorm.Location = new System.Drawing.Point(12, 75);
- this.buttonGetNorm.Name = "buttonGetNorm";
- this.buttonGetNorm.Size = new System.Drawing.Size(167, 26);
- this.buttonGetNorm.TabIndex = 5;
- this.buttonGetNorm.Text = "Модуль вектора";
- this.buttonGetNorm.UseVisualStyleBackColor = true;
- this.buttonGetNorm.Click += new System.EventHandler(this.GetNorm);
- //
- // buttonSortU
- //
- this.buttonSortU.ForeColor = System.Drawing.Color.Yellow;
- this.buttonSortU.Image = ((System.Drawing.Image)(resources.GetObject("buttonSortU.Image")));
- this.buttonSortU.Location = new System.Drawing.Point(12, 107);
- this.buttonSortU.Name = "buttonSortU";
- this.buttonSortU.Size = new System.Drawing.Size(167, 44);
- this.buttonSortU.TabIndex = 6;
- this.buttonSortU.Text = "Сортировка по кол-ву элементов (возрастание)";
- this.buttonSortU.UseVisualStyleBackColor = true;
- this.buttonSortU.Click += new System.EventHandler(this.SortUp);
- //
- // buttonSortD
- //
- this.buttonSortD.ForeColor = System.Drawing.Color.Yellow;
- this.buttonSortD.Image = ((System.Drawing.Image)(resources.GetObject("buttonSortD.Image")));
- this.buttonSortD.Location = new System.Drawing.Point(12, 157);
- this.buttonSortD.Name = "buttonSortD";
- this.buttonSortD.Size = new System.Drawing.Size(167, 44);
- this.buttonSortD.TabIndex = 7;
- this.buttonSortD.Text = "Сортировка по кол-ву элементов (убывание)";
- this.buttonSortD.UseVisualStyleBackColor = true;
- this.buttonSortD.Click += new System.EventHandler(this.SortDown);
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.BackColor = System.Drawing.SystemColors.Menu;
- this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
- this.ClientSize = new System.Drawing.Size(488, 317);
- this.Controls.Add(this.buttonSortD);
- this.Controls.Add(this.buttonSortU);
- this.Controls.Add(this.buttonGetNorm);
- this.Controls.Add(this.listBoxForGetNorm);
- this.Controls.Add(this.buttonDel);
- this.Controls.Add(this.buttonAddLLVec);
- this.Controls.Add(this.listBoxForVectors);
- this.Controls.Add(this.buttonAddArrVec);
- this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
- this.Name = "Form1";
- this.Text = "Лабораторная работа №8";
- this.Load += new System.EventHandler(this.Form1_Load);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.Button buttonAddArrVec;
- private System.Windows.Forms.ListBox listBoxForVectors;
- private System.Windows.Forms.Button buttonAddLLVec;
- private System.Windows.Forms.Button buttonDel;
- private System.Windows.Forms.ListBox listBoxForGetNorm;
- private System.Windows.Forms.Button buttonGetNorm;
- private System.Windows.Forms.Button buttonSortU;
- private System.Windows.Forms.Button buttonSortD;
- }
- }
- Form2:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace LR08
- {
- public static class Form2
- {
- public static string ShowDialog(string message, string name)
- {
- Form form2 = new Form()
- {
- Width = 350,
- Height = 140,
- FormBorderStyle = FormBorderStyle.FixedDialog,
- Text = name,
- StartPosition = FormStartPosition.CenterParent
- };
- Label textLabel = new Label() { Left = 49, Top = 10, Width = 230, Height = 30, Text = message };
- TextBox textBox = new TextBox() { Left = 50, Top = 39, Width = 230 };
- Button confirmation = new Button() { Text = "Ок", Left = 49, Width = 50, Top = 63 };
- confirmation.Click += (sender, e) => { form2.DialogResult = DialogResult.OK; form2.Close(); };
- Button cancel = new Button() { Text = "Отмена", Left = 200, Width = 81, Top = 63 };
- cancel.Click += (sender, e) => { form2.DialogResult = DialogResult.Cancel; form2.Close(); };
- form2.Controls.Add(textBox);
- form2.Controls.Add(confirmation);
- form2.Controls.Add(cancel);
- form2.Controls.Add(textLabel);
- form2.AcceptButton = confirmation;
- form2.CancelButton = cancel;
- form2.MinimizeBox = false;
- form2.MaximizeBox = false;
- form2.FormBorderStyle = FormBorderStyle.FixedDialog;
- return form2.ShowDialog() == DialogResult.OK ? textBox.Text : null;
- }
- }
- }
- LinkedListVector:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LR08
- {
- class LinkedListVector : Vector, IComparable, ICloneable
- {
- public class Node
- {
- public double el = 0;
- public Node next;
- public Node(double elem)
- {
- this.el = elem;
- this.next = null;
- }
- public Node()
- {
- el = 0;
- next = null;
- }
- }
- Node headNode;
- Node lastNode;
- int len;
- public LinkedListVector() //Создание пустого списка
- {
- this.len = 0;
- }
- /* public LinkedListVector(int n)
- {
- if (n < 1) throw new FormatException("Длина односвязного списка не может быть меньше единицы");
- Node lastNode = headNode;
- for (int i = 2; i <= n; i++)
- {
- Node newNode = new Node();
- lastNode = newNode;
- }
- } */
- public int Length
- {
- get
- {
- int result = 1;
- Node lastNode = headNode;
- while (lastNode.next != null)
- {
- result++;
- lastNode = lastNode.next;
- }
- return result;
- }
- }
- public void Push(double element)
- {
- if (headNode == null)
- {
- // создать узел, сделать его головным
- this.headNode = new Node();
- this.headNode.el = element;
- // этот же узел и последний
- this.lastNode = this.headNode;
- // следующего узла нет
- this.headNode.next = null;
- len += 1;
- }
- else
- {
- // создать временный узел
- Node newNode = new Node();
- // следующий за предыдущим последним узлом - новый узел
- this.lastNode.next = newNode;
- // он становится новым последним
- this.lastNode = newNode;
- this.lastNode.el = element;
- // следующего узла пока нет
- this.lastNode.next = null;
- len += 1;
- }
- }
- public double this[int i]
- {
- get
- {
- if (i >= 0 && i < len)
- {
- Node newnode = new Node();
- newnode = this.headNode;
- for (int k = 0; k < i; k++)
- {
- newnode = newnode.next;
- }
- return newnode.el;
- }
- else
- throw new ArgumentOutOfRangeException();
- }
- set
- {
- if (i >= 0 && i < len)
- {
- Node newnode = new Node();
- newnode = this.headNode;
- for (int k = 0; k < i; k++)
- {
- newnode = newnode.next;
- }
- newnode.el = value;
- }
- else throw new ArgumentOutOfRangeException();
- }
- }
- public double GetNorm()
- {
- double result = 0;
- Node lastNode = headNode;
- for (int i = 1; i <= len; i++)
- {
- result = result + lastNode.el * lastNode.el;
- lastNode = lastNode.next;
- }
- return Math.Sqrt(result);
- }
- public override string ToString()
- {
- string res="";
- for (int i = 0; i < len; i++)
- res = res + this[i].ToString() + " ";
- return res;
- }
- public override bool Equals(Object obj)
- {
- Vector vec = (Vector)obj;
- if (Length == vec.Length)
- {
- int k = 0;
- for (int i = 0; i < Length; i++)
- {
- if (vec[i] == this[i]) k++;
- }
- if (k == Length)
- {
- return true;
- }
- else return false;
- }
- else return false;
- }
- public int CompareTo(object obj)
- {
- Vector vec = (Vector)obj;
- if (this.Length.CompareTo(vec.Length) > 0) return 1;
- if (this.Length.CompareTo(vec.Length) < 0) return -1;
- return 0;
- }
- public object Clone()
- {
- LinkedListVector vec = new LinkedListVector();
- for (int i = 0; i < Length; i++)
- vec.Push(this[i] + 1);
- return vec;
- }
- }
- }
- Vector:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LR08
- {
- interface Vector : ICloneable
- {
- int Length
- {
- get;
- }
- double GetNorm();
- double this[int i]
- {
- set;
- get;
- }
- }
- }
- VectorComparer:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LR08
- {
- class VectorComparer: IComparer<Vector>
- {
- public int Compare(Vector obj, Vector obj1)
- {
- if (obj.GetNorm() > obj1.GetNorm()) return 1;
- if (obj.GetNorm() < obj1.GetNorm()) return -1;
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement