
Добавил:
eipimru
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лаба 6 (Массив) / MyForm
.h#pragma once
namespace Pr5 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Сводка для MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::GroupBox^ groupBox1;
protected:
private: System::Windows::Forms::GroupBox^ groupBox2;
private: System::Windows::Forms::GroupBox^ groupBox3;
private: System::Windows::Forms::TextBox^ OutputTextBox;
private: System::Windows::Forms::GroupBox^ groupBox4;
private: System::Windows::Forms::TextBox^ InputTextBox;
private: System::Windows::Forms::Button^ generateButton;
private: System::Windows::Forms::TextBox^ elements_max;
private: System::Windows::Forms::TextBox^ elements_min;
private: System::Windows::Forms::TextBox^ elements_ammount;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ loadButton;
private: System::Windows::Forms::RadioButton^ radioButton1;
private: System::Windows::Forms::TextBox^ textBox_FileNameFrom;
private: System::Windows::Forms::RadioButton^ radioButton7;
private: System::Windows::Forms::RadioButton^ radioButton6;
private: System::Windows::Forms::RadioButton^ radioButton5;
private: System::Windows::Forms::RadioButton^ radioButton4;
private: System::Windows::Forms::RadioButton^ radioButton3;
private: System::Windows::Forms::RadioButton^ radioButton2;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::RadioButton^ radioButton8;
private: System::Windows::Forms::TextBox^ FileNameTo;
private: System::Windows::Forms::Button^ saveButton;
private: System::Windows::Forms::Button^ ExecuteButton;
protected:
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
this->label4 = (gcnew System::Windows::Forms::Label());
this->textBox_FileNameFrom = (gcnew System::Windows::Forms::TextBox());
this->loadButton = (gcnew System::Windows::Forms::Button());
this->generateButton = (gcnew System::Windows::Forms::Button());
this->elements_max = (gcnew System::Windows::Forms::TextBox());
this->elements_min = (gcnew System::Windows::Forms::TextBox());
this->elements_ammount = (gcnew System::Windows::Forms::TextBox());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label1 = (gcnew System::Windows::Forms::Label());
this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
this->label5 = (gcnew System::Windows::Forms::Label());
this->radioButton8 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton7 = (gcnew System::Windows::Forms::RadioButton());
this->FileNameTo = (gcnew System::Windows::Forms::TextBox());
this->radioButton6 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton5 = (gcnew System::Windows::Forms::RadioButton());
this->saveButton = (gcnew System::Windows::Forms::Button());
this->radioButton4 = (gcnew System::Windows::Forms::RadioButton());
this->ExecuteButton = (gcnew System::Windows::Forms::Button());
this->radioButton3 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
this->groupBox3 = (gcnew System::Windows::Forms::GroupBox());
this->OutputTextBox = (gcnew System::Windows::Forms::TextBox());
this->groupBox4 = (gcnew System::Windows::Forms::GroupBox());
this->InputTextBox = (gcnew System::Windows::Forms::TextBox());
this->groupBox1->SuspendLayout();
this->groupBox2->SuspendLayout();
this->groupBox3->SuspendLayout();
this->groupBox4->SuspendLayout();
this->SuspendLayout();
//
// groupBox1
//
this->groupBox1->Controls->Add(this->label4);
this->groupBox1->Controls->Add(this->textBox_FileNameFrom);
this->groupBox1->Controls->Add(this->loadButton);
this->groupBox1->Controls->Add(this->generateButton);
this->groupBox1->Controls->Add(this->elements_max);
this->groupBox1->Controls->Add(this->elements_min);
this->groupBox1->Controls->Add(this->elements_ammount);
this->groupBox1->Controls->Add(this->label3);
this->groupBox1->Controls->Add(this->label2);
this->groupBox1->Controls->Add(this->label1);
this->groupBox1->Location = System::Drawing::Point(12, 12);
this->groupBox1->Name = L"groupBox1";
this->groupBox1->Size = System::Drawing::Size(721, 130);
this->groupBox1->TabIndex = 0;
this->groupBox1->TabStop = false;
this->groupBox1->Text = L"Исходные данные";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(481, 42);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(122, 16);
this->label4->TabIndex = 9;
this->label4->Text = L"Имя файла ввода";
//
// textBox_FileNameFrom
//
this->textBox_FileNameFrom->Location = System::Drawing::Point(484, 61);
this->textBox_FileNameFrom->Name = L"textBox_FileNameFrom";
this->textBox_FileNameFrom->Size = System::Drawing::Size(231, 22);
this->textBox_FileNameFrom->TabIndex = 8;
//
// loadButton
//
this->loadButton->Location = System::Drawing::Point(484, 87);
this->loadButton->Name = L"loadButton";
this->loadButton->Size = System::Drawing::Size(231, 32);
this->loadButton->TabIndex = 7;
this->loadButton->Text = L"Ввод из файла";
this->loadButton->UseVisualStyleBackColor = true;
this->loadButton->Click += gcnew System::EventHandler(this, &MyForm::loadButton_Click);
//
// generateButton
//
this->generateButton->Location = System::Drawing::Point(484, 11);
this->generateButton->Name = L"generateButton";
this->generateButton->Size = System::Drawing::Size(231, 32);
this->generateButton->TabIndex = 6;
this->generateButton->Text = L"Генерация массива";
this->generateButton->UseVisualStyleBackColor = true;
this->generateButton->Click += gcnew System::EventHandler(this, &MyForm::generateButton_Click);
//
// elements_max
//
this->elements_max->Location = System::Drawing::Point(290, 97);
this->elements_max->MaxLength = 9;
this->elements_max->Name = L"elements_max";
this->elements_max->Size = System::Drawing::Size(144, 22);
this->elements_max->TabIndex = 5;
this->elements_max->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress);
//
// elements_min
//
this->elements_min->Location = System::Drawing::Point(290, 58);
this->elements_min->MaxLength = 9;
this->elements_min->Name = L"elements_min";
this->elements_min->Size = System::Drawing::Size(144, 22);
this->elements_min->TabIndex = 4;
this->elements_min->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress);
//
// elements_ammount
//
this->elements_ammount->Location = System::Drawing::Point(290, 16);
this->elements_ammount->MaxLength = 4;
this->elements_ammount->Name = L"elements_ammount";
this->elements_ammount->Size = System::Drawing::Size(144, 22);
this->elements_ammount->TabIndex = 3;
this->elements_ammount->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress_non_negative);
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(10, 103);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(246, 16);
this->label3->TabIndex = 2;
this->label3->Text = L"Максимальное значение диапазона";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(10, 64);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(240, 16);
this->label2->TabIndex = 1;
this->label2->Text = L"Минимальное значение диапазона";
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(9, 22);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(160, 16);
this->label1->TabIndex = 0;
this->label1->Text = L"Количество элементов";
//
// groupBox2
//
this->groupBox2->Controls->Add(this->label5);
this->groupBox2->Controls->Add(this->radioButton8);
this->groupBox2->Controls->Add(this->radioButton7);
this->groupBox2->Controls->Add(this->FileNameTo);
this->groupBox2->Controls->Add(this->radioButton6);
this->groupBox2->Controls->Add(this->radioButton5);
this->groupBox2->Controls->Add(this->saveButton);
this->groupBox2->Controls->Add(this->radioButton4);
this->groupBox2->Controls->Add(this->ExecuteButton);
this->groupBox2->Controls->Add(this->radioButton3);
this->groupBox2->Controls->Add(this->radioButton2);
this->groupBox2->Controls->Add(this->radioButton1);
this->groupBox2->Location = System::Drawing::Point(12, 212);
this->groupBox2->Name = L"groupBox2";
this->groupBox2->Size = System::Drawing::Size(721, 137);
this->groupBox2->TabIndex = 1;
this->groupBox2->TabStop = false;
this->groupBox2->Text = L"Операции с массивом";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(481, 47);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(122, 16);
this->label5->TabIndex = 13;
this->label5->Text = L"Имя файла ввода";
//
// radioButton8
//
this->radioButton8->AutoSize = true;
this->radioButton8->Location = System::Drawing::Point(238, 99);
this->radioButton8->Name = L"radioButton8";
this->radioButton8->Size = System::Drawing::Size(196, 20);
this->radioButton8->TabIndex = 7;
this->radioButton8->TabStop = true;
this->radioButton8->Text = L"Сортировка по убыванию";
this->radioButton8->UseVisualStyleBackColor = true;
this->radioButton8->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// radioButton7
//
this->radioButton7->AutoSize = true;
this->radioButton7->Location = System::Drawing::Point(238, 73);
this->radioButton7->Name = L"radioButton7";
this->radioButton7->Size = System::Drawing::Size(217, 20);
this->radioButton7->TabIndex = 6;
this->radioButton7->TabStop = true;
this->radioButton7->Text = L"Сортировка по возрастанию";
this->radioButton7->UseVisualStyleBackColor = true;
this->radioButton7->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// FileNameTo
//
this->FileNameTo->Location = System::Drawing::Point(484, 65);
this->FileNameTo->Name = L"FileNameTo";
this->FileNameTo->Size = System::Drawing::Size(231, 22);
this->FileNameTo->TabIndex = 12;
//
// radioButton6
//
this->radioButton6->AutoSize = true;
this->radioButton6->Location = System::Drawing::Point(238, 47);
this->radioButton6->Name = L"radioButton6";
this->radioButton6->Size = System::Drawing::Size(162, 20);
this->radioButton6->TabIndex = 5;
this->radioButton6->TabStop = true;
this->radioButton6->Text = L"Нечётные элементы";
this->radioButton6->UseVisualStyleBackColor = true;
this->radioButton6->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// radioButton5
//
this->radioButton5->AutoSize = true;
this->radioButton5->Location = System::Drawing::Point(238, 21);
this->radioButton5->Name = L"radioButton5";
this->radioButton5->Size = System::Drawing::Size(145, 20);
this->radioButton5->TabIndex = 4;
this->radioButton5->TabStop = true;
this->radioButton5->Text = L"Чётные элементы";
this->radioButton5->UseVisualStyleBackColor = true;
this->radioButton5->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// saveButton
//
this->saveButton->Location = System::Drawing::Point(484, 91);
this->saveButton->Name = L"saveButton";
this->saveButton->Size = System::Drawing::Size(231, 32);
this->saveButton->TabIndex = 11;
this->saveButton->Text = L"Сохранить в файл";
this->saveButton->UseVisualStyleBackColor = true;
this->saveButton->Click += gcnew System::EventHandler(this, &MyForm::saveButton_Click);
//
// radioButton4
//
this->radioButton4->AutoSize = true;
this->radioButton4->Location = System::Drawing::Point(13, 99);
this->radioButton4->Name = L"radioButton4";
this->radioButton4->Size = System::Drawing::Size(185, 20);
this->radioButton4->TabIndex = 3;
this->radioButton4->TabStop = true;
this->radioButton4->Text = L"Максимальный элемент";
this->radioButton4->UseVisualStyleBackColor = true;
this->radioButton4->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// ExecuteButton
//
this->ExecuteButton->Location = System::Drawing::Point(484, 15);
this->ExecuteButton->Name = L"ExecuteButton";
this->ExecuteButton->Size = System::Drawing::Size(231, 32);
this->ExecuteButton->TabIndex = 10;
this->ExecuteButton->Text = L"Выполнить";
this->ExecuteButton->UseVisualStyleBackColor = true;
this->ExecuteButton->Click += gcnew System::EventHandler(this, &MyForm::ExecuteButton_Click);
//
// radioButton3
//
this->radioButton3->AutoSize = true;
this->radioButton3->Location = System::Drawing::Point(13, 73);
this->radioButton3->Name = L"radioButton3";
this->radioButton3->Size = System::Drawing::Size(179, 20);
this->radioButton3->TabIndex = 2;
this->radioButton3->TabStop = true;
this->radioButton3->Text = L"Минимальный элемент";
this->radioButton3->UseVisualStyleBackColor = true;
this->radioButton3->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// radioButton2
//
this->radioButton2->AutoSize = true;
this->radioButton2->Location = System::Drawing::Point(12, 47);
this->radioButton2->Name = L"radioButton2";
this->radioButton2->Size = System::Drawing::Size(152, 20);
this->radioButton2->TabIndex = 1;
this->radioButton2->TabStop = true;
this->radioButton2->Text = L"Среднее значение";
this->radioButton2->UseVisualStyleBackColor = true;
this->radioButton2->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// radioButton1
//
this->radioButton1->AutoSize = true;
this->radioButton1->Checked = true;
this->radioButton1->Location = System::Drawing::Point(13, 21);
this->radioButton1->Name = L"radioButton1";
this->radioButton1->Size = System::Drawing::Size(146, 20);
this->radioButton1->TabIndex = 0;
this->radioButton1->TabStop = true;
this->radioButton1->Text = L"Сумма элементов";
this->radioButton1->UseVisualStyleBackColor = true;
this->radioButton1->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// groupBox3
//
this->groupBox3->Controls->Add(this->OutputTextBox);
this->groupBox3->Location = System::Drawing::Point(12, 355);
this->groupBox3->Name = L"groupBox3";
this->groupBox3->Size = System::Drawing::Size(721, 56);
this->groupBox3->TabIndex = 2;
this->groupBox3->TabStop = false;
this->groupBox3->Text = L"Результат операции";
//
// OutputTextBox
//
this->OutputTextBox->Location = System::Drawing::Point(13, 21);
this->OutputTextBox->Name = L"OutputTextBox";
this->OutputTextBox->ReadOnly = true;
this->OutputTextBox->Size = System::Drawing::Size(376, 22);
this->OutputTextBox->TabIndex = 1;
//
// groupBox4
//
this->groupBox4->Controls->Add(this->InputTextBox);
this->groupBox4->Location = System::Drawing::Point(12, 148);
this->groupBox4->Name = L"groupBox4";
this->groupBox4->Size = System::Drawing::Size(721, 58);
this->groupBox4->TabIndex = 3;
this->groupBox4->TabStop = false;
this->groupBox4->Text = L"Исходный массив";
//
// InputTextBox
//
this->InputTextBox->Location = System::Drawing::Point(13, 22);
this->InputTextBox->MaxLength = 500;
this->InputTextBox->Name = L"InputTextBox";
this->InputTextBox->Size = System::Drawing::Size(376, 22);
this->InputTextBox->TabIndex = 0;
this->InputTextBox->Leave += gcnew System::EventHandler(this, &MyForm::InputTextBox_Leave);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(738, 418);
this->Controls->Add(this->groupBox3);
this->Controls->Add(this->groupBox4);
this->Controls->Add(this->groupBox2);
this->Controls->Add(this->groupBox1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
this->MaximizeBox = false;
this->Name = L"MyForm";
this->Text = L"Массив";
this->groupBox1->ResumeLayout(false);
this->groupBox1->PerformLayout();
this->groupBox2->ResumeLayout(false);
this->groupBox2->PerformLayout();
this->groupBox3->ResumeLayout(false);
this->groupBox3->PerformLayout();
this->groupBox4->ResumeLayout(false);
this->groupBox4->PerformLayout();
this->ResumeLayout(false);
}
#pragma endregion
array<int>^ NumbersArray;// = gcnew array<int>(arraySize);
int choosed_radiobutton = 0;
private: System::Void textBox_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
e->Handled = true;
System::Windows::Forms::TextBox^ textbox = safe_cast <System::Windows::Forms::TextBox^> (sender);
if ((e->KeyChar >= '0') && (e->KeyChar <= '9') || e->KeyChar == 127 || e->KeyChar == 8)
e->Handled = false;
else if (e->KeyChar == '-')
{
int selectionStart = textbox->SelectionStart;
if (textbox->Text->IndexOf("-") != -1)
{
textbox->Text = textbox->Text->Replace("-", "");
textbox->SelectionStart = selectionStart - 1;
if (textbox->SelectionStart < 0)
textbox->SelectionStart = 0;
}
else
{
textbox->Text = "-" + textbox->Text;
textbox->SelectionStart = selectionStart + 1;
}
}
};
private: System::Void textBox_KeyPress_non_negative(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
e->Handled = true;
System::Windows::Forms::TextBox^ textbox = safe_cast <System::Windows::Forms::TextBox^> (sender);
if ((e->KeyChar >= '0') && (e->KeyChar <= '9') || e->KeyChar == 127 || e->KeyChar == 8)
e->Handled = false;
else if (e->KeyChar == '-')
{
int selectionStart = textbox->SelectionStart;
}
};
private: System::Void radioButton_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
{
OutputTextBox->Text = "";
System::Windows::Forms::RadioButton^ radiobutton = safe_cast <System::Windows::Forms::RadioButton^> (sender);
if (!radiobutton->Checked)
return;
if (radiobutton == radioButton1)
choosed_radiobutton = 0;
else if (radiobutton == radioButton2)
choosed_radiobutton = 1;
else if (radiobutton == radioButton3)
choosed_radiobutton = 2;
else if (radiobutton == radioButton4)
choosed_radiobutton = 3;
else if (radiobutton == radioButton5)
choosed_radiobutton = 4;
else if (radiobutton == radioButton6)
choosed_radiobutton = 5;
else if (radiobutton == radioButton7)
choosed_radiobutton = 6;
else if (radiobutton == radioButton8)
choosed_radiobutton = 7;
}
private: System::Void generateButton_Click(System::Object^ sender, System::EventArgs^ e)
{
elements_ammount->Text = elements_ammount->Text->Replace(" ", "");
elements_min->Text = elements_min->Text->Replace(" ", "");
elements_max->Text = elements_max->Text->Replace(" ", "");
if (elements_ammount->Text == "" || elements_min->Text == "" || elements_max->Text == "" ||
elements_ammount->Text == "-" || elements_min->Text == "-" || elements_max->Text == "-")
{
MessageBox::Show(this, "Введите числа", "Массивы", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
int ammount = Convert::ToInt16(elements_ammount->Text);
int min = Convert::ToInt32(elements_min->Text);
int max = Convert::ToInt32(elements_max->Text);
if (min >= max)
{
int c = min;
min = max;
max = c;
elements_min->Text = min.ToString();
elements_max->Text = max.ToString();
}
if (ammount < 1)
{
MessageBox::Show(this, "Количество элементов должно быть больше 0", "Массивы", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
Random^ rnd = gcnew Random();
array<int>^ arr = gcnew array<int>(ammount);
for (int i = 0; i < ammount; i++)
arr[i] = rnd->Next(min, max+1);
InputTextBox->Text = arrayToString(arr);
}
private: System::Void ExecuteButton_Click(System::Object^ sender, System::EventArgs^ e)
{
array<int>^ resultedArray;
String^ result;
OutputTextBox->Text = "";
switch (choosed_radiobutton)
{
case 0:
result = "Сумма элементов: " + sum(getArray());
break;
case 1:
result = "Среднее значение: " + average(getArray());
break;
case 2:
result = "Минимальный элемент: " + min(getArray());
break;
case 3:
result = "Максимальный элемент: " + max(getArray());
break;
case 4:
result = arrayToString(even(getArray()));
if (result == "")
{
MessageBox::Show(this, "Чётных элементов нет!", "Массив", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
else
{
result = "Чётные элементы: " + result;
}
break;
case 5:
result = arrayToString(odd(getArray()));
if (result == "")
{
MessageBox::Show(this, "Нечётных элементов нет!", "Массив", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
else
{
result = "нечётные элементы: " + result;
}
break;
case 6:
result = "Сортировка по возрастанию: " + arrayToString(sort(getArray(), false));
break;
case 7:
result = "Сортировка по убыванию: " + arrayToString(sort(getArray(), true));
break;
default:
break;
}
OutputTextBox->Text = result;
}
private: System::Void loadButton_Click(System::Object^ sender, System::EventArgs^ e)
{
String^ filename = GetInputFile();
if (filename != "")
{
InputTextBox->Text = formatString(ReadFile(filename));
textBox_FileNameFrom->Text = filename;
}
else
textBox_FileNameFrom->Text = "";
}
private: System::Void InputTextBox_Leave(System::Object^ sender, System::EventArgs^ e)
{
InputTextBox->Text = formatString(InputTextBox->Text);
OutputTextBox->Text = "";
}
private: array<int>^ getArray()
{
System::String^ resultString = formatString(InputTextBox->Text);
InputTextBox->Text = resultString;
array<int>^ numbers = gcnew array<int>(resultString->Split(' ')->Length);
int n = 0;
if (resultString == "")
return numbers;
// создание списка с массива
for each (String ^ number in resultString->Split(' '))
{
numbers[n++] = Convert::ToInt32(number);
}
return numbers;
}
private: System::Void saveButton_Click(System::Object^ sender, System::EventArgs^ e)
{
if (OutputTextBox->Text == "")
{
MessageBox::Show(this, "Невозможно записать файл! Совершите операцию над массивом", "Массивы", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
String^ filename = GetOutputFile();
if (filename != "")
{
String^ fileText = ReadFile(filename);
System::IO::StreamWriter^ writer = gcnew System::IO::StreamWriter(filename, true);
if (fileText->IndexOf("Исходный массив: " + InputTextBox->Text) == -1)
{
writer->WriteLine("\nИсходный массив: " + InputTextBox->Text);
writer->WriteLine(OutputTextBox->Text);
}
else
{
if (fileText->IndexOf(OutputTextBox->Text) == -1)
{
writer->WriteLine(OutputTextBox->Text);
}
}
writer->Close();
}
}
// работа с файлами
private: String^ GetOutputFile()
// создаёт диалог для сохранения и возвращает имя файла
{
String^ filename = "";
SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog();
saveFileDialog1->FileName = FileNameTo->Text;
saveFileDialog1->Title = "Сохранение файла";
saveFileDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1->FilterIndex = 1;
saveFileDialog1->RestoreDirectory = true;
saveFileDialog1->OverwritePrompt = true;
if (saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
filename = saveFileDialog1->FileName;
FileNameTo->Text = filename;
}
return filename;
}
private: String^ GetInputFile()
{
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->Title = "Открытие файла";
openFileDialog1->FileName = textBox_FileNameFrom->Text;
openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 1;
openFileDialog1->RestoreDirectory = true;
String^ filename = "";
if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
filename = openFileDialog1->FileName;
return filename;
}
private: String^ ReadFile(String^ filename)
// читает файл и возвращает его содержимое
{
String^ outputFileText;
try
{
outputFileText = IO::File::ReadAllText(filename);
}
catch (IO::FileNotFoundException^ error)
{
outputFileText = "";
};
return outputFileText;
}
// функции обработки массива
private: int sum(array<int>^ arr)
{
int s = 0;
for each (int number in arr)
{
s += number;
}
return s;
}
private: double average(array<int>^ arr)
{
return Convert::ToDouble(sum(arr)) / Convert::ToDouble(arr->Length);
}
private: int min(array<int>^ arr)
{
int min_n = arr[0];
for each (int number in arr)
{
if (min_n > number)
min_n = number;
}
return min_n;
}
private: int max(array<int>^ arr)
{
int max_n = arr[0];
for each (int number in arr)
{
if (max_n < number)
max_n = number;
}
return max_n;
}
private: array<int>^ even(array<int>^ arr)
{
array<int>^ evenNumbersBig = gcnew array<int>(arr->Length);
int count = 0;
for each (int number in arr)
{
if (Math::Abs(number) % 2 == 0)
{
evenNumbersBig[count++] = number;
}
}
array<int>^ evenNumbers = gcnew array<int>(count);
for (int i = 0; i < count; i++)
{
evenNumbers[i] = evenNumbersBig[i];
}
return evenNumbers;
}
private: array<int>^ odd(array<int>^ arr)
{
array<int>^ oddNumbersBig = gcnew array<int>(arr->Length);
int count = 0;
for each (int number in arr)
{
if (Math::Abs(number) % 2 == 1)
{
oddNumbersBig[count++] = number;
}
}
array<int>^ oddNumbers = gcnew array<int>(count);
for (int i = 0; i < count; i++)
{
oddNumbers[i] = oddNumbersBig[i];
}
return oddNumbers;
}
private: array<int>^ sort(array<int>^ arr, bool reverse)
{
array<int>^ sortedArr = gcnew array<int>(arr->Length);
for (int i = 0; i < arr->Length; i++)
sortedArr[i] = arr[i];
for (int i = 0; i < arr->Length; i++)
{
for (int j = 0; j < arr->Length - 1; j++)
{
if (sortedArr[j] > sortedArr[j + 1]) {
int b = sortedArr[j];
sortedArr[j] = sortedArr[j + 1];
sortedArr[j + 1] = b;
}
}
}
if (reverse)
{
int temp, i;
for (i = 0; i < arr->Length / 2; ++i) {
temp = sortedArr[arr->Length - i - 1];
sortedArr[arr->Length - i - 1] = sortedArr[i];
sortedArr[i] = temp;
}
}
return sortedArr;
}
// работа с строками
private: String^ arrayToString(array<int>^ arr)
{
System::String^ str = "";
for each (int number in arr)
{
str += number.ToString() + " ";
}
if (str != "")
return str->TrimEnd();
return "";
}
private: String^ formatString(String^ str)
{
System::String^ resultString = "";
str = str->Replace("\t", " ")->Replace("-", " -");
for (int i = 0; i < str->Length; i++)
{
if (str[i] == '-' || str[i] == ' ' || (str[i] >= '0' && str[i] <= '9'))
resultString += str[i];
else
resultString += " ";
}
do
{
resultString = resultString->Replace(" ", " ")->Replace("- ", "-")->Replace("--", "-");
} while (resultString->IndexOf(" ") != -1 || resultString->IndexOf("--") != -1);
resultString = resultString->TrimStart()->TrimEnd();
if (resultString->EndsWith(" -"))
resultString = resultString->Substring(0, resultString->Length - 2);
return resultString;
}
};
}
Соседние файлы в папке Лаба 6 (Массив)