Pārlūkot izejas kodu

Добавлена 6 лабораторная

Вадим Королёв 2 gadi atpakaļ
vecāks
revīzija
78763e2f19

+ 25 - 0
6 Arrays/ArrayForms.sln

@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.32802.440
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArrayForms", "ArrayForms\ArrayForms.csproj", "{AB7AD051-50B5-4996-ABA1-996C7C928A87}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{AB7AD051-50B5-4996-ABA1-996C7C928A87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{AB7AD051-50B5-4996-ABA1-996C7C928A87}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{AB7AD051-50B5-4996-ABA1-996C7C928A87}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{AB7AD051-50B5-4996-ABA1-996C7C928A87}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {9AAB4B7F-67B1-48B8-A83B-02A23352024A}
+	EndGlobalSection
+EndGlobal

+ 9 - 0
6 Arrays/ArrayForms/ArrayForms.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>WinExe</OutputType>
+    <TargetFramework>net5.0-windows</TargetFramework>
+    <UseWindowsForms>true</UseWindowsForms>
+  </PropertyGroup>
+
+</Project>

+ 124 - 0
6 Arrays/ArrayForms/MainForm.Designer.cs

@@ -0,0 +1,124 @@
+
+namespace ArrayForms
+{
+    partial class MainForm
+    {
+        /// <summary>
+        ///  Required designer variable.
+        /// </summary>
+        private System.ComponentModel.IContainer components = null;
+
+        /// <summary>
+        ///  Clean up any resources being used.
+        /// </summary>
+        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing && (components != null))
+            {
+                components.Dispose();
+            }
+            base.Dispose(disposing);
+        }
+
+        #region Windows Form Designer generated code
+
+        /// <summary>
+        ///  Required method for Designer support - do not modify
+        ///  the contents of this method with the code editor.
+        /// </summary>
+        private void InitializeComponent()
+        {
+            this.lblBefore = new System.Windows.Forms.Label();
+            this.lbBefore = new System.Windows.Forms.ListBox();
+            this.lbAfter = new System.Windows.Forms.ListBox();
+            this.lblAfter = new System.Windows.Forms.Label();
+            this.btnFill = new System.Windows.Forms.Button();
+            this.btnMove = new System.Windows.Forms.Button();
+            this.SuspendLayout();
+            // 
+            // lblBefore
+            // 
+            this.lblBefore.AutoSize = true;
+            this.lblBefore.Location = new System.Drawing.Point(13, 13);
+            this.lblBefore.Name = "lblBefore";
+            this.lblBefore.Size = new System.Drawing.Size(110, 15);
+            this.lblBefore.TabIndex = 0;
+            this.lblBefore.Text = "Исходный массив:";
+            // 
+            // lbBefore
+            // 
+            this.lbBefore.FormattingEnabled = true;
+            this.lbBefore.ItemHeight = 15;
+            this.lbBefore.Location = new System.Drawing.Point(13, 32);
+            this.lbBefore.Name = "lbBefore";
+            this.lbBefore.Size = new System.Drawing.Size(203, 469);
+            this.lbBefore.TabIndex = 1;
+            // 
+            // lbAfter
+            // 
+            this.lbAfter.FormattingEnabled = true;
+            this.lbAfter.ItemHeight = 15;
+            this.lbAfter.Location = new System.Drawing.Point(249, 32);
+            this.lbAfter.Name = "lbAfter";
+            this.lbAfter.Size = new System.Drawing.Size(203, 469);
+            this.lbAfter.TabIndex = 2;
+            // 
+            // lblAfter
+            // 
+            this.lblAfter.AutoSize = true;
+            this.lblAfter.Location = new System.Drawing.Point(249, 13);
+            this.lblAfter.Name = "lblAfter";
+            this.lblAfter.Size = new System.Drawing.Size(125, 15);
+            this.lblAfter.TabIndex = 3;
+            this.lblAfter.Text = "Полученный массив:";
+            // 
+            // btnFill
+            // 
+            this.btnFill.Location = new System.Drawing.Point(13, 508);
+            this.btnFill.Name = "btnFill";
+            this.btnFill.Size = new System.Drawing.Size(203, 33);
+            this.btnFill.TabIndex = 4;
+            this.btnFill.Text = "Заполнить";
+            this.btnFill.UseVisualStyleBackColor = true;
+            this.btnFill.Click += new System.EventHandler(this.btnFill_Click);
+            // 
+            // btnMove
+            // 
+            this.btnMove.Location = new System.Drawing.Point(249, 511);
+            this.btnMove.Name = "btnMove";
+            this.btnMove.Size = new System.Drawing.Size(203, 33);
+            this.btnMove.TabIndex = 5;
+            this.btnMove.Text = "Перестановка";
+            this.btnMove.UseVisualStyleBackColor = true;
+            this.btnMove.Click += new System.EventHandler(this.btnMove_Click);
+            // 
+            // MainForm
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
+            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.ClientSize = new System.Drawing.Size(464, 556);
+            this.Controls.Add(this.btnMove);
+            this.Controls.Add(this.btnFill);
+            this.Controls.Add(this.lblAfter);
+            this.Controls.Add(this.lbAfter);
+            this.Controls.Add(this.lbBefore);
+            this.Controls.Add(this.lblBefore);
+            this.Name = "MainForm";
+            this.Text = "Одномерные массивы";
+            this.ResumeLayout(false);
+            this.PerformLayout();
+
+        }
+
+        #endregion
+
+        private System.Windows.Forms.Label lblBefore;
+        private System.Windows.Forms.ListBox lbBefore;
+        private System.Windows.Forms.ListBox lbAfter;
+        private System.Windows.Forms.Label lblAfter;
+        private System.Windows.Forms.Button btnFill;
+        private System.Windows.Forms.Button btnMove;
+    }
+}
+

+ 92 - 0
6 Arrays/ArrayForms/MainForm.cs

@@ -0,0 +1,92 @@
+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;
+
+namespace ArrayForms
+{
+    public partial class MainForm : Form
+    {
+        private int[] Numbers = new int[15];
+
+        public MainForm()
+        {
+            InitializeComponent();
+        }
+
+        private void btnFill_Click(object sender, EventArgs e)
+        {
+            var Rng = new Random();
+            
+            // Заполняем массив случайными числами, заодно добавляем их в lbBefore
+            lbBefore.Items.Clear();
+            for (int i = 0; i < 15; i++)
+            {
+                Numbers[i] = Rng.Next(-50, 50);
+                lbBefore.Items.Add($"Numbers[{i}] = {Numbers[i]}");
+            }
+        }
+
+        // Вставляет значение на определённый индекс в массиве. Самое последнее значение массива будет удалено
+        // Принимает массив, значение чтобы вставить и индекс на который нужно ставить значение
+        private int[] InsertValueToArray(int[] SourceArray, int Value, int InsertIndex)
+        {
+            // Копируем массив
+            var NewArray = new int[15];
+            Array.Copy(SourceArray, NewArray, 15);
+            
+            // Сместить все элементы массива на их индекс + 1 чтобы освободить место
+            for (int i = NewArray.Length - 1; i >= InsertIndex; i--)
+            {
+                NewArray[i] = NewArray[i - 1];
+            }
+            // Заменить освободившийся индекс
+            NewArray[InsertIndex] = Value;
+            return NewArray;
+        }
+
+        private void btnMove_Click(object sender, EventArgs e)
+        {
+            if (lbBefore.Items.Count == 0)
+            {
+                MessageBox.Show("Сначала заполните массив!");
+                return;
+            }
+            
+            // Находим последний положительный элемент
+            int LastPositive = -1;
+            for (int i = 14; i >= 0; i--)
+            {
+                if (Numbers[i] > 0)
+                {
+                    LastPositive = Numbers[i];
+                    break;
+                }      
+            }
+
+            /* Если положительный элемент был найден, то и LastPositive должен стать положительным
+            * Поэтому, если LastPositive отрицательный, положительных элементов в массиве не было найдено, т.к.
+            * LastPositive по умолчанию отрицательный и не был заменён положительным */
+            if (LastPositive < 0)
+            {
+                MessageBox.Show("В массиве не обнаружено положительных чисел");
+                return;
+            }
+
+            // Вставляем LastPositive перед 6-ым элементом
+            var NewArray = InsertValueToArray(Numbers, LastPositive, 6);
+
+            // Заполняем lbAfter
+            lbAfter.Items.Clear();
+            for (int i = 0; i < 15; i++)
+            {
+                lbAfter.Items.Add($"NewArray[{i}] = {NewArray[i]}");
+            }
+        }
+    }
+}

+ 60 - 0
6 Arrays/ArrayForms/MainForm.resx

@@ -0,0 +1,60 @@
+<root>
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+</root>

+ 23 - 0
6 Arrays/ArrayForms/Program.cs

@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace ArrayForms
+{
+    static class Program
+    {
+        /// <summary>
+        ///  The main entry point for the application.
+        /// </summary>
+        [STAThread]
+        static void Main()
+        {
+            Application.SetHighDpiMode(HighDpiMode.SystemAware);
+            Application.EnableVisualStyles();
+            Application.SetCompatibleTextRenderingDefault(false);
+            Application.Run(new MainForm());
+        }
+    }
+}