Praktika_Pibd-14/Praktika/Form1.cs
2024-06-29 17:25:47 +04:00

821 lines
26 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Praktika
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[,] array2D;
int[] array;
public static int flag = 0;
string result;
int len;
private void ButtonOne_Click(object sender, EventArgs e)
{
//ââîä äëÿ ñòðîê
if (arrayTextBox.Text.Length > 0 && textBoxLen.Text.Length >5 )
{
Vvod();
return;
}
if (textBoxLen.Text == null)
{
MessageBox.Show("Íå ââåäåíà äëèíà ìàññèâà");
return;
}
if (string.IsNullOrEmpty(textBoxMin.Text) || string.IsNullOrEmpty(textBoxMax.Text))
{
MessageBox.Show("Ïîæàëóéñòà, ââåäèòå âñå íåîáõîäèìûå äàííûå äëÿ ñîçäàíèÿ ìàññèâà.");
return;
}
//âûâîäèò ìàññèâ ñ áîëüøèì ðàçìåðîì
len = Convert.ToInt32(textBoxLen.Text);
array = new int[len * 2];
Random random = new Random();
for (int i = 0; i < len; i++)
{
array[i] = random.Next(Convert.ToInt32(textBoxMin.Text), Convert.ToInt32(textBoxMax.Text));
}
//âûâîäèò ìàññèâ èçáàâëÿþ îò 0
string str = ""; for (int i = 0; i < len; i++)
{
str += array[i].ToString();
str += " ";
}
arrayTextBox.Text = str;
//âûâîäèò îáû÷íûé ìàññèâ
/*len = Convert.ToInt32(textBoxLen.Text);
array = new int[len ];
Random random = new Random();
for (int i = 0; i < len; i++)
{
array[i] = random.Next(Convert.ToInt32(textBoxMin.Text), Convert.ToInt32(textBoxMax.Text));
}*/
//arrayTextBox.Text = string.Join(", ", array);
}
private void Vvod()
{
string[] mas = arrayTextBox.Text.Split(' ');
len = mas.Length;
array = new int[len * 2];
for (int i = 0; i < len; i++)
{
array[i] = Convert.ToInt32(mas[i]);
}
}
private void ButtonTwo_Click(object sender, EventArgs e)
{
if (arrayTextBox.Text.Length > 0 && textBoxLen.Text.Length > 5)
{
Vvod2();
return;
}
if (textBoxLen.Text == null)
{
MessageBox.Show("Íå ââåäåíà äëèíà ìàññèâà");
return;
}
if (string.IsNullOrEmpty(textBoxMin.Text) || string.IsNullOrEmpty(textBoxMax.Text))
{
MessageBox.Show("Ïîæàëóéñòà, ââåäèòå âñå íåîáõîäèìûå äàííûå äëÿ ñîçäàíèÿ ìàññèâà.");
return;
}
len = Convert.ToInt32(textBoxLen.Text);
array2D = new int[len * 2, len * 2];
Random random = new Random();
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
array2D[i, j] = random.Next(Convert.ToInt32(textBoxMin.Text), Convert.ToInt32(textBoxMax.Text));
}
}
// Âûâîä äâóìåðíîãî ìàññèâà â òåêñòîâîå ïîëå
string str = "";
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
str += array2D[i, j].ToString();
str += " ";
}
str += "\n";
}
arrayTextBox.Text = str;
/*StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
sb.Append(array2D[i, j] + " ");
}
sb.AppendLine();
}
arrayTextBox.Text = sb.ToString();*/
}
private void Vvod2()
{
string[] mas = arrayTextBox.Text.Split(' ', '\n');
len = Convert.ToInt32(Math.Sqrt(mas.Length));
array2D = new int[len * 2, len * 2];
int c = 0;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
array2D[i, j] = Convert.ToInt32(mas[c++]);
}
}
}
private void Button1_Click(object sender, EventArgs e)
{
//Çíàê çíà÷åíèé âñåõ ýëåìåíòîâ ñåðèè îäèíàêîâû. ìèíèìàëüíàÿ ñåðèÿ
int[] array = Array.ConvertAll(arrayTextBox.Text.Split(','), int.Parse);
int minLength = 999;
int seriesStartIndex = 0;
int seriesLength = 0;
int i = 1;
int currentLength = 1;
for (; i < array.Length; i++)
{
if (array[i] < 0 && array[i - 1] < 0 || array[i] >= 0 && array[i - 1] >= 0)
{
currentLength++;
}
else
{
// Åñëè ñåðèÿ çàêîí÷èëàñü
if (currentLength < minLength && currentLength >= 2)
{
minLength = currentLength;
seriesStartIndex = i - currentLength; // Íà÷èíàþ ñ ïðåäûäóùåãî ýëåìåíòà
seriesLength = currentLength;
}
currentLength = 1;
}
}
//ïîñëåäíÿÿ ñåðèÿ
if (currentLength < minLength && currentLength >= 2)
{
minLength = currentLength;
seriesStartIndex = i - currentLength; // Íà÷èíàþ ñ ïðåäûäóùåãî ýëåìåíòà
seriesLength = currentLength;
}
MessageBox.Show($"Ñåðèÿ ñ ìèíèìàëüíîé äëèíîé íà÷èíàåòñÿ ñ ïîçèöèè {seriesStartIndex} è èìååò äëèíó {seriesLength}");
flag = 1;
result = "Ñåðèÿ ñ ìèíèìàëüíîé äëèíîé íà÷èíàåòñÿ ñ ïîçèöèè " + seriesStartIndex + " è èìååò äëèíó " + seriesLength;
}
private void Button9_Click(object sender, EventArgs e)
{
if (flag == 1)
{
richTextBox1.Text = "Äàí öåëî÷èñëåííûé îäíîìåðíûé ìàññèâ ðàçìåðà N. Ñåðèÿ ýòî" +
"ïîñëåäîâàòåëüíîñòü ýëåìåíòîâ ìàññèâà, èäóùèõ äðóã çà äðóãîì. Çíàêè" +
"çíà÷åíèé âñåõ ýëåìåíòîâ ñåðèè îäèíàêîâû(âñå ïîëîæèòåëüíûå èëè âñå" +
"îòðèöàòåëüíûå). Ñåðèÿ äîëæíà ñîäåðæàòü ìèíèìóì 2 ýëåìåíòà.Äëèíà" +
"ñåðèè êîëè÷åñòâî ýëåìåíòîâ â ñåðèè. Íàéòè ñåðèþ ñ ìèíèìàëüíîé" +
"äëèíîé.Âûâåñòè ñ êàêîé ïîçèöèè íà÷èíàåòñÿ ñåðèÿ è åå äëèíó.";
}
else if (flag == 2)
{
richTextBox1.Text = "7. Äàí öåëî÷èñëåííûé îäíîìåðíûé ìàññèâ ðàçìåðà N. Ïåðåñòàâèòü â\r\nîáðàòíîì ïîðÿäêå ýëåìåíòû ìàññèâà, ðàñïîëîæåííûå ìåæäó\r\níàèáîëüøèì ÷åòíûì è íàèìåíüøèì íå÷åòíûì";
}
else if (flag == 3)
{
richTextBox1.Text = "8. Äàí öåëî÷èñëåííûé îäíîìåðíûé ìàññèâ ðàçìåðà N. Âñòàâèòü 3 ïîñëå\r\nêàæäîãî íå÷åòíîãî ýëåìåíòà ìàññèâà (íå÷åòíûé ïî çíà÷åíèþ, à íå ïî\r\nïîðÿäêîâîìó íîìåðó).\r\n\r\n";
}
else if (flag == 4)
{
richTextBox1.Text = "8. Äàí öåëî÷èñëåííûé îäíîìåðíûé ìàññèâ ðàçìåðà N. Óäàëèòü èç ìàññèâà\r\nâñå íå÷åòíûå ýëåìåíòû (íå÷åòíûé ïî çíà÷åíèþ, à íå ïî ïîðÿäêîâîìó\r\níîìåðó).\r\n";
}
else if (flag == 5)
{
richTextBox1.Text = "8. Äàí öåëî÷èñëåííûé äâóìåðíûé ìàññèâ ðàçìåðà N*N. Äîáàâèòü â ìàññèâ\r\nñòðîêó 0 ïîñëå êàæäîé ñòðîêè, â êîòîðîé åñòü õîòÿ áû îäèí ýëåìåíò,\r\nìåíüøèé ïî ìîäóëþ ñðåäíåãî àðèôìåòè÷åñêîãî ýëåìåíòîâ ìàññèâà.";
}
else if (flag == 6)
{
richTextBox1.Text = "8. Äàí öåëî÷èñëåííûé äâóìåðíûé ìàññèâ ðàçìåðà N*N. Óïîðÿäî÷èòü\r\nýëåìåíòû â ìàòðèöå ïî âîçðàñòàíèþ (ñïðàâà íàëåâî, ñíèçó-ââåðõ)";
}
else
{
MessageBox.Show("Ïîæàëóéñòà, âûáåðèòå çàäàíèå");
}
}
/// <summary>
/// ðåçóëüòàò
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button10_Click(object sender, EventArgs e)
{
richTextBox1.Text = result;
}
private void Button2_Click(object sender, EventArgs e)
{
//Ïåðåñòàâèòü â îáðàòíîì ïîðÿäêå ýëåìåíòû ìåæäó íàèáîëüøèì ÷åòíûì è íàèìåíüøèì íå÷åòíûì
int[] array = Array.ConvertAll(arrayTextBox.Text.Split(','), int.Parse);
int Min = int.MaxValue, minIndex = -1, Max = int.MinValue, maxIndex = -1;
for (int i = 0; i < array.Length; i++)
{
if (array[i] > Max && array[i] % 2 == 0)
{
Max = array[i];
maxIndex = i;
}
if (array[i] < Min && array[i] % 2 != 0)
{
Min = array[i];
minIndex = i;
}
}
if (maxIndex != -1 && minIndex != -1)
{
int begin = Math.Min(minIndex, maxIndex) + 1, end = Math.Max(minIndex, maxIndex) - 1;
for (; begin < end; begin++, end--)
{
int tmp = array[begin];
array[begin] = array[end];
array[end] = tmp;
}
}
else
{
MessageBox.Show("Íåò ÷åòíîãî èëè íå÷åòíîãî ýëåìåíòà");
return;
}
/*int begin = Math.Min(minIndex, maxIndex) + 1, end = Math.Max(minIndex, maxIndex) - 1;
for (; begin < end; begin++, end--)
{
int tmp = array[begin];
array[begin] = array[end];
array[end] = tmp;
}*/
result = string.Join(", ", array);
flag = 2;
}
private void Button3_Click(object sender, EventArgs e)
{
// Âñòàâèòü 1 ïîñëå êàæäîãî ïîëîæèòåëüíîãî ýëåìåíòà ìàññèâà.
//int[] array = Array.ConvertAll(arrayTextBox.Text.Split(','), int.Parse); // íå äëÿ ñòðîê
/*for (int i = 0; i < len; i++)
{
if (array[i] >= 0 )
{
//Array.Resize(ref array, array.Length + 1); // Óâåëè÷èâàåì ðàçìåð ìàññèâà íà 1
//len++;
for (int j = len - 1; j > i + 1; j--) // Ñäâèãàåì âñå ýëåìåíòû ïîñëå òåêóùåãî íà 1 ïîçèöèþ âïðàâî
{
array[j] = array[j - 1];
}
array[i + 1] = 1; // Âñòàâëÿåì ÷èñëî 1 ïîñëå òåêóùåãî ýëåìåíòà
i++; // Ïðîïóñêàåì ñëåäóþùóþ èòåðàöèþ öèêëà, ÷òîáû íå îáðàáàòûâàòü âñòàâëåííîå ÷èñëî 2
}
}*/
//int n = array.Length;
int count = len;
int i = 0;
for (; i < count; i++)
{
if (array[i] % 2 != 0)
{
for (int j = count; j > i; j--)
{
array[j] = array[j - 1];
}
array[i + 1] = 3;
count++;
i++;
}
}
string str = ""; for (i = 0; i < count; i++)
{
str += array[i].ToString();
str += " ";
}
result = str;
//result = string.Join(", ", array);
/*for (int i = n - 1; i >= 0; i--)
{
if (array[i] > 0)
{
for (int j = n - 1; j > i + 1; j--)
{
array[j] = array[j - 1];
}
array[i + 1] = 1;
len++;
}
}*/
/*int k = 0;
for(int i = 0; i < array.Length; i++)
{
if (array[i] >= 0)
{
k++;
}
}*/ //ïîñ÷èòàòü ñêîëüêî ðåàëüíûõ ýëåìåíòîâ
// Ñîçäàþ íîâûé ìàññèâ ñ óâåëè÷åííûì ðàçìåðîì
/*int[] newArray = new int[array.Length ];
// Ñ÷åò÷èê äëÿ íîâîãî ìàññèâà
int j = 0;
// Ïðîõîæó ïî èñõîäíîìó ìàññèâó
for (int i = 0; i < array.Length; i++)
{
// Êîïèðóþ ýëåìåíò
newArray[j] = array[i];
j++;
// Åñëè ýëåìåíò íåîòðèöàòåëüíûé, âñòàâëÿþ 1
if (array[i] >= 0)
{
newArray[j] = 1; // Âñòàâëÿþ çíà÷åíèå
j++;
}
}
// Îáíîâëÿþ èñõîäíûé ìàññèâ
array = newArray;*/
//result = string.Join(", ", array);
flag = 3;
}
private void Button4_Click(object sender, EventArgs e)
{
//Óäàëèòü èç ìàññèâà âñå ïîëîæèòåëüíûå ýëåìåíòû.
//int[] array = Array.ConvertAll(arrayTextBox.Text.Split(','), int.Parse);
/*for (int i = 0; i < array.Length; i++)
{
if (array[i] > 0)
{
// Óâåëè÷èâàåì ðàçìåð ìàññèâà íà 1
for (int j = i; j < array.Length - 1; j++) // Ñäâèãàåì âñå ýëåìåíòû ïîñëå òåêóùåãî íà 1 ïîçèöèþ âïðàâî
{
array[j] = array[j + 1];
}
Array.Resize(ref array, array.Length - 1);
i--;
}
}
result = string.Join(", ", array);*/
int count = len;
int i = 0;
for (; i < count; i++)
{
if (array[i] % 2 != 0)
{
for (int j = i; j < count ; j++)
{
array[j] = array[j + 1];
}
count--;
i--;
if (count == 0)
{
result = "Íåò ýëåìåíòîâ";
return;
}
}
}
string str = " "; for (i = 0; i < count; i++)
{
str += array[i].ToString();
str += " ";
}
result = str;
flag = 4;
}
private void Button5_Click(object sender, EventArgs e)
{
/*int s = 0;
float ar = 0;
for (int j = 0; j < columns; j++)
{
for (int i = 0; i < rows; i++)
{
if (array2D[i, j] >= 0)
{
s += array2D[i, j];
}
}
}
ar = s / rows;
for (int j = 0; j < columns; j++)
{
for (int i = 0; i < rows; i++)
{
if (array2D[i,j] > ar)
{
for (int k = columns; k > j; k--)
{
for (int p = 0; p < rows; p++)
{
array2D[p, k] = 0;
}
}
columns++;
j++;
break;
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
sb.Append(array2D[i, j] + " ");
}
sb.AppendLine();
}
richTextBox1.Text = sb.ToString();
//richTextBox1.Text = array2D.ToString();
//result = string.Join(" ", array2D);*/
int s = 0;
double ar = 0;
int len2 = len;
for (int j = 0; j < len2; j++)
{
for (int i = 0; i < len; i++)
{
if (array2D[i, j] < 0 )
{
s += array2D[i, j];
}
}
}
ar = (double)s / (len * len2);
//çàäàíèå 5
for (int j = 0; j < len2; j++)
{
for (int i = 0; i < len; i++)
{
if (Math.Abs(array2D[i, j]) > ar && array2D[i,j] != 0)
{
for (int k = len2; k > j; k--)
{
for (int p = 0; p < len; p++)
{
array2D[p, k] = array2D[p, k - 1];
}
}
for (int p = 0; p < len; p++)
{
array2D[p, j + 1] = 0;
}
len2++;
j++;
}
}
}
// Âûâîä ðåçóëüòàòà
string str = "";
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len2; j++)
{
str += array2D[i, j].ToString();
str += " ";
}
str += "\n";
}
result = str;
flag = 5;
//çàäàíèå 6
/*int s =0;
double ar ;
int len2 = len;
for (int j = 0; j < len2; j++)
{
for (int i = 0; i < len; i++)
{
s += array2D[i, j];
}
}
ar = (double)s / (len * len2);
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len2; j++)
{
if (Math.Abs(array2D[i, j]) < ar)
{
for (int k = len; k > i; k--)
{
for (int p = 0; p < len2; p++)
{
array2D[k, p] = array2D[k - 1, p];
}
}
for (int p = 0; p < len2; p++)
{
array2D[i + 1, p] = 0;
}
len++;
i++;
break;
}
}
}
// Âûâîä ðåçóëüòàòà
string str = "";
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len2; j++)
{
str += array2D[i, j].ToString();
str += " ";
}
str += "\n";
}
result = str;
flag = 5;*/
/*StringBuilder sbResult = new StringBuilder();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
sbResult.Append(array2D[i, j] + " ");
}
sbResult.AppendLine();
}
richTextBox1.Text = sbResult.ToString();*/
// Âñòàâëÿåì ñòîëáöû ñ íóëÿìè
/*int insertCount = 0; // Ñ÷åò÷èê âñòàâëåííûõ ñòîëáöîâ
for (int j = 0; j < len2; j++)
{
for (int i = 0; i < len; i++)
{
if (array2D[i, j] > ar)
{
// Ñäâèãàåì ýëåìåíòû ìàññèâà âïðàâî
for (int k = len2 - 1; k > j + insertCount; k--)
{
for (int p = 0; p < len; p++)
{
array2D[p, k] = array2D[p, k - 1];
}
}
// Âñòàâëÿåì ñòîëáåö ñ íóëÿìè
for (int p = 0; p < len; p++)
{
array2D[p, j + insertCount] = 0;
}
insertCount++; // Óâåëè÷èâàåì ñ÷åò÷èê âñòàâëåííûõ ñòîëáöîâ
break;
}
}
}*/
}
private void Button6_Click(object sender, EventArgs e)
{
/*for (int i = 0; i < len; i++)
{
//ñâåðõó âíèç
//SortLP(array2D, i);
//SortSN(array2D, i);
for (int j = 0; j < len; j++)
{
for (int k = 0; k < len; k++)
{
if (array2D[i, k] < array2D[i, k + 1])
{
int temp = array2D[i, k];
array2D[i, k] = array2D[i, k + 1];
array2D[i, k + 1] = temp;
}
}
for (int k = 0; k < len; k++)
{
if (array2D[k, 0] < array2D[k + 1, r])
{
int temp = array2D[k, r];
array2D[k, r] = array2D[k + 1, r];
array2D[k + 1, r] = temp;
}
}
}
}*/
/*for (int i = 0; i < len*len; i++)
{
// Ñîðòèðîâêà ñòðîêè ñëåâà íàïðàâî
for (int row = 0; row < len; row++)
{
for (int k = 0; k < len - 1; k++)
{
if (array2D[row, k] < array2D[row, k + 1])
{
int temp = array2D[row, k];
array2D[row, k] = array2D[row, k + 1];
array2D[row, k + 1] = temp;
}
}
}
// Ñîðòèðîâêà ñòîëáöà ñâåðõó âíèç
for (int col = 0; col < len ; col++)
{
for (int k = 0; k < len ; k++)
{
if (array2D[k, col] < array2D[k + 1, col])
{
int temp = array2D[k, col];
array2D[k, col] = array2D[k + 1, col];
array2D[k + 1, col] = temp;
}
}
}
}*/
//ðåøåíèå ïðàâèëüíîå
/*for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
for (int x = 0; x < len; x++)
{
for (int y = 0; y < len; y++)
{
if (array2D[i, j] > array2D[x, y])
{
int zam = array2D[i, j];
array2D[i, j] = array2D[x, y];
array2D[x, y] = zam;
}
}
}
}
}*/
//ñïðàâî íàëåâî, ñíèçó - ââåðõ
for (int i = len - 1; i >= 0; i--) // Èä¸ì ñ êîíöà ïî ñòðîêàì
{
for (int j = len - 1; j >= 0; j--) // Èä¸ì ñ êîíöà ïî ñòîëáöàì
{
for (int x = len - 1; x >= 0; x--)
{
for (int y = len - 1; y >= 0; y--)
{
if (array2D[i, j] < array2D[x, y])
{
int zam = array2D[i, j];
array2D[i, j] = array2D[x, y];
array2D[x, y] = zam;
}
}
}
}
}
/*//ñëåâà íà ïðàâî
for (int k = 0; k < len; k++)
{
if (array2D[0, k] < array2D[0, k + 1])
{
int temp = array2D[0, k];
array2D[0, k] = array2D[0, k + 1];
array2D[0, k + 1] = temp;
}
}
//ñâåðõó âíèç
for (int k = 0; k < len; k++)
{
if (array2D[k, 0] < array2D[k + 1, 0])
{
int temp = array2D[k, 0];
array2D[k, 0] = array2D[k + 1, 0];
array2D[k + 1, 0] = temp;
}
}*/
// Âûâîä ðåçóëüòàòà
string str = "";
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
str += array2D[i, j].ToString();
str += " ";
}
str += "\n";
}
result = str;
flag = 6;
}
public void SortLP(int[,] arr,int r)
{
//ñëåâà íà ïðàâî
for (int i = 0; i < len; i++)
{
for (int k = 0; k < len; k++)
{
if (array2D[r, k] < array2D[r, k + 1])
{
int temp = array2D[r, k];
array2D[r, k] = array2D[r, k + 1];
array2D[r, k + 1] = temp;
}
}
}
}
public void SortSN(int[,] arr, int r)
{
//ñÂÅÐÕÓ ÂÍÈÇ
for (int i = 0; i < len; i++)
{
for (int k = 0; k < len; k++)
{
if (array2D[k, r] < array2D[k + 1, r])
{
int temp = array2D[k, r];
array2D[k, r] = array2D[k + 1, r];
array2D[k + 1, r] = temp;
}
}
}
}
}
}