Advertisement
Guest User

Untitled

a guest
May 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.57 KB | None | 0 0
  1. Program:
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8.  
  9. namespace LR08
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Application.EnableVisualStyles();//включает визуальные стили
  16. Application.Run(new Form1());
  17. }
  18. }
  19.  
  20.  
  21. }
  22. ArrayVector:
  23. using System;
  24. using System.Collections.Generic;
  25. using System.Linq;
  26. using System.Text;
  27. using System.Threading.Tasks;
  28.  
  29. namespace LR08
  30. {
  31. class ArrayVector: Vector, IComparable, ICloneable
  32. {
  33. double[] mass;
  34. public ArrayVector (int l)
  35. {
  36. mass = new double[l];
  37. }
  38. public ArrayVector ()
  39. {
  40. mass = new double[5];
  41. }
  42.  
  43. public double this[int i] //ИНДЕКСАТОР
  44. {
  45. set { if ((i>=mass.Length)||(i<0)) throw new ArgumentOutOfRangeException(); else mass[i] = value; }
  46.  
  47. get { if ((i >= mass.Length) || (i < 0)) throw new ArgumentOutOfRangeException(); else return mass[i]; }
  48. }
  49.  
  50. public int Length
  51. {
  52. get { return mass.Length; }
  53. }
  54.  
  55. public double GetNorm()
  56. {
  57. double norm = 0;
  58. for (int i = 0; i < mass.Length; i++)
  59. {
  60. norm = norm + mass[i] * mass[i];
  61.  
  62. }
  63. norm = Math.Sqrt(norm); return norm;
  64. }
  65.  
  66. public override string ToString()
  67. {
  68. string res = "";
  69. for (int i = 0; i < Length; i++)
  70. res = res+ this[i].ToString() + " ";
  71. return res;
  72. }
  73. public override bool Equals(Object obj)
  74. {
  75. Vector vec = (Vector)obj;
  76.  
  77. if (Length == vec.Length)
  78. {
  79. int k = 0;
  80. for (int i = 0; i < Length; i++)
  81. {
  82. if (vec[i] == this[i]) k++;
  83. }
  84. if (k == Length)
  85. {
  86. return true;
  87. }
  88. else return false;
  89. }
  90. else return false;
  91. }
  92. public int CompareTo(object obj)
  93. {
  94. Vector vec = (Vector)obj;
  95. if (this.Length.CompareTo(vec.Length) > 0) return 1;
  96. if (this.Length.CompareTo(vec.Length) < 0) return -1;
  97. return 0;
  98. }
  99. public object Clone()
  100. {
  101. ArrayVector vec = new ArrayVector(Length);
  102. for (int i = 0; i < Length; i++)
  103. vec[i] = mass[i] + 1;
  104. return vec;
  105. /*new ArrayVector(Length)
  106. {
  107. mass = vec
  108. }; */
  109. }
  110.  
  111.  
  112. }
  113. }
  114. Form1.Designer:
  115. namespace LR08
  116. {
  117. partial class Form1
  118. {
  119. /// <summary>
  120. /// Обязательная переменная конструктора.
  121. /// </summary>
  122. private System.ComponentModel.IContainer components = null;
  123.  
  124. /// <summary>
  125. /// Освободить все используемые ресурсы.
  126. /// </summary>
  127. /// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
  128. protected override void Dispose(bool disposing)
  129. {
  130. if (disposing && (components != null))
  131. {
  132. components.Dispose();
  133. }
  134. base.Dispose(disposing);
  135. }
  136.  
  137. #region Код, автоматически созданный конструктором форм Windows
  138.  
  139. /// <summary>
  140. /// Требуемый метод для поддержки конструктора — не изменяйте
  141. /// содержимое этого метода с помощью редактора кода.
  142. /// </summary>
  143. private void InitializeComponent()
  144. {
  145. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
  146. this.buttonAddArrVec = new System.Windows.Forms.Button();
  147. this.listBoxForVectors = new System.Windows.Forms.ListBox();
  148. this.buttonAddLLVec = new System.Windows.Forms.Button();
  149. this.buttonDel = new System.Windows.Forms.Button();
  150. this.listBoxForGetNorm = new System.Windows.Forms.ListBox();
  151. this.buttonGetNorm = new System.Windows.Forms.Button();
  152. this.buttonSortU = new System.Windows.Forms.Button();
  153. this.buttonSortD = new System.Windows.Forms.Button();
  154. this.SuspendLayout();
  155. //
  156. // buttonAddArrVec
  157. //
  158. this.buttonAddArrVec.AutoSize = true;
  159. this.buttonAddArrVec.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonAddArrVec.BackgroundImage")));
  160. this.buttonAddArrVec.ForeColor = System.Drawing.Color.Gold;
  161. this.buttonAddArrVec.Location = new System.Drawing.Point(12, 12);
  162. this.buttonAddArrVec.Name = "buttonAddArrVec";
  163. this.buttonAddArrVec.Size = new System.Drawing.Size(167, 26);
  164. this.buttonAddArrVec.TabIndex = 0;
  165. this.buttonAddArrVec.Text = "Добавить ArrayVector";
  166. this.buttonAddArrVec.UseVisualStyleBackColor = true;
  167. this.buttonAddArrVec.Click += new System.EventHandler(this.AddArrayVector);
  168. //
  169. // listBoxForVectors
  170. //
  171. this.listBoxForVectors.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  172. | System.Windows.Forms.AnchorStyles.Left)
  173. | System.Windows.Forms.AnchorStyles.Right)));
  174. this.listBoxForVectors.FormattingEnabled = true;
  175. this.listBoxForVectors.Location = new System.Drawing.Point(204, 12);
  176. this.listBoxForVectors.Name = "listBoxForVectors";
  177. this.listBoxForVectors.Size = new System.Drawing.Size(272, 238);
  178. this.listBoxForVectors.TabIndex = 1;
  179. //
  180. // buttonAddLLVec
  181. //
  182. this.buttonAddLLVec.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonAddLLVec.BackgroundImage")));
  183. this.buttonAddLLVec.ForeColor = System.Drawing.Color.Gold;
  184. this.buttonAddLLVec.Location = new System.Drawing.Point(12, 44);
  185. this.buttonAddLLVec.Name = "buttonAddLLVec";
  186. this.buttonAddLLVec.Size = new System.Drawing.Size(167, 25);
  187. this.buttonAddLLVec.TabIndex = 2;
  188. this.buttonAddLLVec.Text = "Добавить LinkedListVector";
  189. this.buttonAddLLVec.UseVisualStyleBackColor = true;
  190. this.buttonAddLLVec.Click += new System.EventHandler(this.AddLinkedListVector);
  191. //
  192. // buttonDel
  193. //
  194. this.buttonDel.ForeColor = System.Drawing.Color.Yellow;
  195. this.buttonDel.Image = ((System.Drawing.Image)(resources.GetObject("buttonDel.Image")));
  196. this.buttonDel.Location = new System.Drawing.Point(12, 263);
  197. this.buttonDel.Name = "buttonDel";
  198. this.buttonDel.Size = new System.Drawing.Size(167, 42);
  199. this.buttonDel.TabIndex = 3;
  200. this.buttonDel.Text = "Удалить все массивы";
  201. this.buttonDel.UseVisualStyleBackColor = true;
  202. this.buttonDel.Click += new System.EventHandler(this.CleanVectors);
  203. //
  204. // listBoxForGetNorm
  205. //
  206. this.listBoxForGetNorm.FormattingEnabled = true;
  207. this.listBoxForGetNorm.Location = new System.Drawing.Point(204, 263);
  208. this.listBoxForGetNorm.Name = "listBoxForGetNorm";
  209. this.listBoxForGetNorm.Size = new System.Drawing.Size(272, 43);
  210. this.listBoxForGetNorm.TabIndex = 4;
  211. //
  212. // buttonGetNorm
  213. //
  214. this.buttonGetNorm.ForeColor = System.Drawing.Color.Yellow;
  215. this.buttonGetNorm.Image = ((System.Drawing.Image)(resources.GetObject("buttonGetNorm.Image")));
  216. this.buttonGetNorm.Location = new System.Drawing.Point(12, 75);
  217. this.buttonGetNorm.Name = "buttonGetNorm";
  218. this.buttonGetNorm.Size = new System.Drawing.Size(167, 26);
  219. this.buttonGetNorm.TabIndex = 5;
  220. this.buttonGetNorm.Text = "Модуль вектора";
  221. this.buttonGetNorm.UseVisualStyleBackColor = true;
  222. this.buttonGetNorm.Click += new System.EventHandler(this.GetNorm);
  223. //
  224. // buttonSortU
  225. //
  226. this.buttonSortU.ForeColor = System.Drawing.Color.Yellow;
  227. this.buttonSortU.Image = ((System.Drawing.Image)(resources.GetObject("buttonSortU.Image")));
  228. this.buttonSortU.Location = new System.Drawing.Point(12, 107);
  229. this.buttonSortU.Name = "buttonSortU";
  230. this.buttonSortU.Size = new System.Drawing.Size(167, 44);
  231. this.buttonSortU.TabIndex = 6;
  232. this.buttonSortU.Text = "Сортировка по кол-ву элементов (возрастание)";
  233. this.buttonSortU.UseVisualStyleBackColor = true;
  234. this.buttonSortU.Click += new System.EventHandler(this.SortUp);
  235. //
  236. // buttonSortD
  237. //
  238. this.buttonSortD.ForeColor = System.Drawing.Color.Yellow;
  239. this.buttonSortD.Image = ((System.Drawing.Image)(resources.GetObject("buttonSortD.Image")));
  240. this.buttonSortD.Location = new System.Drawing.Point(12, 157);
  241. this.buttonSortD.Name = "buttonSortD";
  242. this.buttonSortD.Size = new System.Drawing.Size(167, 44);
  243. this.buttonSortD.TabIndex = 7;
  244. this.buttonSortD.Text = "Сортировка по кол-ву элементов (убывание)";
  245. this.buttonSortD.UseVisualStyleBackColor = true;
  246. this.buttonSortD.Click += new System.EventHandler(this.SortDown);
  247. //
  248. // Form1
  249. //
  250. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  251. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  252. this.BackColor = System.Drawing.SystemColors.Menu;
  253. this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
  254. this.ClientSize = new System.Drawing.Size(488, 317);
  255. this.Controls.Add(this.buttonSortD);
  256. this.Controls.Add(this.buttonSortU);
  257. this.Controls.Add(this.buttonGetNorm);
  258. this.Controls.Add(this.listBoxForGetNorm);
  259. this.Controls.Add(this.buttonDel);
  260. this.Controls.Add(this.buttonAddLLVec);
  261. this.Controls.Add(this.listBoxForVectors);
  262. this.Controls.Add(this.buttonAddArrVec);
  263. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  264. this.Name = "Form1";
  265. this.Text = "Лабораторная работа №8";
  266. this.Load += new System.EventHandler(this.Form1_Load);
  267. this.ResumeLayout(false);
  268. this.PerformLayout();
  269.  
  270. }
  271.  
  272. #endregion
  273.  
  274. private System.Windows.Forms.Button buttonAddArrVec;
  275. private System.Windows.Forms.ListBox listBoxForVectors;
  276. private System.Windows.Forms.Button buttonAddLLVec;
  277. private System.Windows.Forms.Button buttonDel;
  278. private System.Windows.Forms.ListBox listBoxForGetNorm;
  279. private System.Windows.Forms.Button buttonGetNorm;
  280. private System.Windows.Forms.Button buttonSortU;
  281. private System.Windows.Forms.Button buttonSortD;
  282. }
  283. }
  284.  
  285. Form2:
  286. using System;
  287. using System.Collections.Generic;
  288. using System.Linq;
  289. using System.Text;
  290. using System.Threading.Tasks;
  291. using System.Windows.Forms;
  292.  
  293. namespace LR08
  294. {
  295. public static class Form2
  296. {
  297. public static string ShowDialog(string message, string name)
  298. {
  299. Form form2 = new Form()
  300. {
  301. Width = 350,
  302. Height = 140,
  303. FormBorderStyle = FormBorderStyle.FixedDialog,
  304. Text = name,
  305. StartPosition = FormStartPosition.CenterParent
  306. };
  307. Label textLabel = new Label() { Left = 49, Top = 10, Width = 230, Height = 30, Text = message };
  308. TextBox textBox = new TextBox() { Left = 50, Top = 39, Width = 230 };
  309. Button confirmation = new Button() { Text = "Ок", Left = 49, Width = 50, Top = 63 };
  310. confirmation.Click += (sender, e) => { form2.DialogResult = DialogResult.OK; form2.Close(); };
  311. Button cancel = new Button() { Text = "Отмена", Left = 200, Width = 81, Top = 63 };
  312. cancel.Click += (sender, e) => { form2.DialogResult = DialogResult.Cancel; form2.Close(); };
  313. form2.Controls.Add(textBox);
  314. form2.Controls.Add(confirmation);
  315. form2.Controls.Add(cancel);
  316. form2.Controls.Add(textLabel);
  317. form2.AcceptButton = confirmation;
  318. form2.CancelButton = cancel;
  319. form2.MinimizeBox = false;
  320. form2.MaximizeBox = false;
  321. form2.FormBorderStyle = FormBorderStyle.FixedDialog;
  322. return form2.ShowDialog() == DialogResult.OK ? textBox.Text : null;
  323. }
  324. }
  325. }
  326. LinkedListVector:
  327. using System;
  328. using System.Collections.Generic;
  329. using System.Linq;
  330. using System.Text;
  331. using System.Threading.Tasks;
  332.  
  333. namespace LR08
  334. {
  335. class LinkedListVector : Vector, IComparable, ICloneable
  336. {
  337. public class Node
  338. {
  339. public double el = 0;
  340. public Node next;
  341. public Node(double elem)
  342. {
  343. this.el = elem;
  344. this.next = null;
  345. }
  346. public Node()
  347. {
  348. el = 0;
  349. next = null;
  350. }
  351. }
  352. Node headNode;
  353. Node lastNode;
  354. int len;
  355.  
  356. public LinkedListVector() //Создание пустого списка
  357. {
  358. this.len = 0;
  359. }
  360. /* public LinkedListVector(int n)
  361. {
  362. if (n < 1) throw new FormatException("Длина односвязного списка не может быть меньше единицы");
  363. Node lastNode = headNode;
  364.  
  365. for (int i = 2; i <= n; i++)
  366. {
  367. Node newNode = new Node();
  368. lastNode = newNode;
  369. }
  370. } */
  371.  
  372.  
  373. public int Length
  374. {
  375. get
  376. {
  377. int result = 1;
  378. Node lastNode = headNode;
  379. while (lastNode.next != null)
  380. {
  381. result++;
  382. lastNode = lastNode.next;
  383. }
  384. return result;
  385. }
  386. }
  387.  
  388.  
  389.  
  390. public void Push(double element)
  391. {
  392. if (headNode == null)
  393. {
  394. // создать узел, сделать его головным
  395. this.headNode = new Node();
  396. this.headNode.el = element;
  397. // этот же узел и последний
  398. this.lastNode = this.headNode;
  399. // следующего узла нет
  400. this.headNode.next = null;
  401. len += 1;
  402. }
  403. else
  404. {
  405. // создать временный узел
  406. Node newNode = new Node();
  407. // следующий за предыдущим последним узлом - новый узел
  408. this.lastNode.next = newNode;
  409. // он становится новым последним
  410. this.lastNode = newNode;
  411. this.lastNode.el = element;
  412. // следующего узла пока нет
  413. this.lastNode.next = null;
  414. len += 1;
  415. }
  416. }
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428. public double this[int i]
  429. {
  430. get
  431. {
  432. if (i >= 0 && i < len)
  433. {
  434. Node newnode = new Node();
  435. newnode = this.headNode;
  436. for (int k = 0; k < i; k++)
  437. {
  438. newnode = newnode.next;
  439. }
  440. return newnode.el;
  441. }
  442. else
  443. throw new ArgumentOutOfRangeException();
  444. }
  445. set
  446. {
  447. if (i >= 0 && i < len)
  448. {
  449. Node newnode = new Node();
  450. newnode = this.headNode;
  451. for (int k = 0; k < i; k++)
  452. {
  453. newnode = newnode.next;
  454. }
  455. newnode.el = value;
  456. }
  457. else throw new ArgumentOutOfRangeException();
  458. }
  459. }
  460.  
  461. public double GetNorm()
  462. {
  463. double result = 0;
  464. Node lastNode = headNode;
  465. for (int i = 1; i <= len; i++)
  466. {
  467. result = result + lastNode.el * lastNode.el;
  468. lastNode = lastNode.next;
  469.  
  470. }
  471. return Math.Sqrt(result);
  472. }
  473. public override string ToString()
  474. {
  475. string res="";
  476. for (int i = 0; i < len; i++)
  477. res = res + this[i].ToString() + " ";
  478. return res;
  479. }
  480.  
  481. public override bool Equals(Object obj)
  482. {
  483. Vector vec = (Vector)obj;
  484. if (Length == vec.Length)
  485. {
  486. int k = 0;
  487. for (int i = 0; i < Length; i++)
  488. {
  489. if (vec[i] == this[i]) k++;
  490. }
  491. if (k == Length)
  492. {
  493. return true;
  494. }
  495. else return false;
  496. }
  497. else return false;
  498. }
  499. public int CompareTo(object obj)
  500. {
  501. Vector vec = (Vector)obj;
  502. if (this.Length.CompareTo(vec.Length) > 0) return 1;
  503. if (this.Length.CompareTo(vec.Length) < 0) return -1;
  504. return 0;
  505. }
  506.  
  507. public object Clone()
  508. {
  509. LinkedListVector vec = new LinkedListVector();
  510. for (int i = 0; i < Length; i++)
  511. vec.Push(this[i] + 1);
  512. return vec;
  513. }
  514. }
  515. }
  516. Vector:
  517. using System;
  518. using System.Collections.Generic;
  519. using System.Linq;
  520. using System.Text;
  521. using System.Threading.Tasks;
  522.  
  523. namespace LR08
  524. {
  525. interface Vector : ICloneable
  526. {
  527. int Length
  528. {
  529. get;
  530. }
  531.  
  532. double GetNorm();
  533. double this[int i]
  534. {
  535. set;
  536. get;
  537. }
  538.  
  539.  
  540. }
  541. }
  542. VectorComparer:
  543. using System;
  544. using System.Collections.Generic;
  545. using System.Linq;
  546. using System.Text;
  547. using System.Threading.Tasks;
  548.  
  549. namespace LR08
  550. {
  551. class VectorComparer: IComparer<Vector>
  552. {
  553. public int Compare(Vector obj, Vector obj1)
  554. {
  555. if (obj.GetNorm() > obj1.GetNorm()) return 1;
  556. if (obj.GetNorm() < obj1.GetNorm()) return -1;
  557. return 0;
  558. }
  559. }
  560. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement