Добавил:
больше работ здесь: https://github.com/alisadex Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Отчёт по массиву.docx
Скачиваний:
29
Добавлен:
10.01.2023
Размер:
1.38 Mб
Скачать

3 Текст программы

String^ FilePath = "";

String^ SaveFilePath = "";

bool flagfail = false;

bool flagfile = false;

int button;

String^ hist;

#pragma endregion

void key_press(TextBox^ txt, KeyPressEventArgs^ e) {

char key = e->KeyChar;

int a;

if (!Char::IsDigit(e->KeyChar) && key != 8 && key != '-' && key !=

' ') {

e->Handled = true;

}

}

void fFileIN() {

SaveFileDialog^ sfd = gcnew SaveFileDialog();

sfd->Title = "Сохранить файл";

sfd->Filter = "Текстовые файлы (*.txt)|*.txt";

sfd->RestoreDirectory = true;

sfd->OverwritePrompt = false;

sfd->ShowDialog();

if (sfd->FileName != "") {

StreamWriter^ sw = gcnew StreamWriter(sfd->FileName, true);

txtOutFile->Text = sfd->FileName;

if (flagfile) {

sw->WriteLine();

sw->WriteLine("Исходный массив: " +

this->txtIn->Text);

flagfile = false;

}

if (flagfail && (hist != txtEndMas->Text)) {

sw->WriteLine(this->txtEndMas->Text);

hist = txtEndMas->Text;

flagfail = false;

}

sw->Close();

}

}

void fFileOP(String^ text, Windows::Forms::TextBox^ txt) {

OpenFileDialog^ opFileD = gcnew OpenFileDialog;

opFileD->Title = "Открытие файла со списком маршрутов";

opFileD->InitialDirectory = "d:\\";

opFileD->Filter = "Текстовые файлы (*.txt)|*.txt";

opFileD->ShowReadOnly = false;

opFileD->RestoreDirectory = true;

if(opFileD->ShowDialog()==

System::Windows::Forms::DialogResult::OK)

{

text = opFileD->FileName;

txt->Text = text;

try {

String^ str = IO::File::ReadAllText(opFileD->FileName);

txtIn->Text = formatString(str);

}

catch (IO::FileNotFoundException^ error) {

MessageBox::Show("Ошибка", error->Message + "\nНет" +

"такого файла, Ошибка",

MessageBoxButtons::OK,

MessageBoxIcon::Exclamation);

}

}

else

text = "";

}

private: System::String^ formatString(String^ str) {

String^ str2;

int flag = 0;

for (int i = 0; i < str->Length - 1; i++) {

if (str[i] == '-' && Char::IsNumber(str[i + 1])) {

str2 += (wchar_t)' ';

str2 += str[i];

}

else if (!Char::IsNumber(str[i]) && Char::IsNumber(str[i + 1])) {

str2 += (wchar_t)' ';

}

else if (Char::IsNumber(str[i])) {

str2 += str[i];

}

}

if (str->Length != 0) {

if (Char::IsNumber(str[str->Length - 1])) { str2 += str[str

->Length - 1]; }

}

return str2;

}

private: System::Void btn_File_Click(System::Object^ sender, System::EventArgs^ e) {

fFileOP(FilePath, textBox1);

}

int CheckRB(void) {

int button;

if (radioButton1->Checked) {

button = 1; flagfail = true;

}

else if (radioButton2->Checked) {

button = 2; flagfail = true;

}

else if (radioButton3->Checked) {

button = 3; flagfail = true;

}

else if (radioButton4->Checked) {

button = 4; flagfail = true;

}

else if (radioButton5->Checked) {

button = 5; flagfail = true;

}

else if (radioButton6->Checked) {

button = 6; flagfail = true;

}

else if (radioButton7->Checked) {

button = 7; flagfail = true;

}

else {

button = 8; flagfail = true;

}

return button;

}

private: Void btnSol_Click(System::Object^ sender, System::EventArgs^ e) {

this->txtEndMas->Text = L"";

if (txtIn->Text == "") {

MessageBox::Show("Исходный массив не задан.", "\nОшибка!",

MessageBoxButtons::OK,

MessageBoxIcon::Exclamation);

}

else {

String^ st = formatString(txtIn->Text);

this->txtIn->Text = st;

array<String^>^ str;

str = txtIn->Text->Split(' ', 1);

Collections::Generic::List<Double> masList;

String^ n;

for each (n in str) {

if (n == "") { continue; }

masList.Add(Convert::ToDouble(n));

}

Double sum = 0;

Double sr = 0;

Double min = masList[0];

Double max = masList[0];

switch (CheckRB()) {

case 1:

for each (Double n in masList)

{

sum += n;

};

this->txtEndMas->Text = L"Сумма элементов : " + sum;

break;

case 2:

int i;

for each (Double n in masList)

{

sum += n;

i++;

};

sr = sum / i;

this->txtEndMas->Text = L"Среднее значение элементов: " +

sr;

break;

case 3:

for each (Double n in masList)

{

if (n < min) {

min = n;

}

};

this->txtEndMas->Text = L"Минимальный элемент: " + min;

break;

case 4:

for each (Double n in masList)

{

if (n > max) {

max = n;

}

};

this->txtEndMas->Text = L"Максимальный элемент: " + max;

break;

case 5:

for each (Double n in masList) {

if ((fmod(n, 2)) == 0) {

this->txtEndMas->Text += Convert::ToString(n)

+ " ";

}

}

this->txtEndMas->Text = "Чётные элементы: " +

txtEndMas->Text;

break;

case 6:

for each (Double n in masList) {

if (!((fmod(n, 2)) == 0)) {

this->txtEndMas->Text += Convert::ToString(n)

+ " ";

}

}

this->txtEndMas->Text = "Нечётные элементы : " +

txtEndMas->Text;

break;

case 7:

txtEndMas->Text = "Сортировка по возрастанию: ";

for (int i = 0; i < masList.Count; i++) {

for (int j = 0; j < masList.Count - 1; j++) {

if (masList[j] > masList[j + 1]) {

Double b = masList[j];

masList[j] = masList[j + 1];

masList[j + 1] = b;

}

}

}

for (int i = 0; i < masList.Count; i++) {

txtEndMas->Text = txtEndMas->Text + masList[i] + " ";

}

break;

case 8:

txtEndMas->Text = "Сортировка по убыванию: ";

for (int i = 0; i < masList.Count; i++) {

for (int j = 0; j < masList.Count - 1; j++) {

if (masList[j] < masList[j + 1]) {

Double b = masList[j];

masList[j] = masList[j + 1];

masList[j + 1] = b;

}

}

}

for (int i = 0; i < masList.Count; i++) {

txtEndMas->Text = txtEndMas->Text + masList[i] + " ";

}

break;

}

}

}

private: Void button2_Click(System::Object^ sender, System::EventArgs^ e) {

this->Close();

}

private: Void btnSaveFile_Click(System::Object^ sender, System::EventArgs^ e) {

fFileINп();

}

private: Void btnGen_Click(System::Object^ sender, System::EventArgs^ e) {

flagfile = true;

flagfail = true;

this->txtIn->Text = L"";

if ((txtCount->Text == "") || (txtMin->Text == "") ||

(txtMax->Text == "")) {

MessageBox::Show("Отсутствуют параметры для генерации массива",

"\nОшибка!", MessageBoxButtons::OK,

MessageBoxIcon::Exclamation);

}

else {

try {

int N = Convert::ToInt32(txtCount->Text);

if (N <= 0) {

MessageBox::Show("Количество элементов в массиве" +

"должно быть положительным",

"\nОшибка!",

MessageBoxButtons::OK,

MessageBoxIcon::Exclamation);

}

else {

int amin = Convert::ToInt32(txtMin->Text);

int bmax = Convert::ToInt32(txtMax->Text);

array<int>^ arr = gcnew array<int>(N);

Random^ rnd = gcnew Random();

if (bmax >= amin) {

for (int i = 0; i < N; i++) {

arr[i] = rnd->Next(amin, bmax + 1); }

for (int i = 0; i < N; i++) {

this->txtIn->Text+=

Convert::ToString(arr[i]) + " "; }

}

else {

MessageBox::Show("Максимальное значение для" +

"диапазона должно " +

" быть больше минимального",

"\nОшибка!",

MessageBoxButtons::OK,

MessageBoxIcon::Exclamation);

}

}

}

catch (FormatException^ e) {

MessageBox::Show("Неверные параметры для генерации" +

"массива", "\nОшибка!",

MessageBoxButtons::OK,

MessageBoxIcon::Exclamation);

}

}

}

private: System::Void txtIn_Leave(System::Object^ sender, System::EventArgs^ e) {

//txtIn->Text = replacer(txtIn->Text);

txtIn->Text = formatString(txtIn->Text);

flagfile = true;

flagfail = true;

txtCount->Text = "";

txtMax->Text = "";

txtMin->Text = "";

}

private: System::Void txtCount_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {

char key = e->KeyChar;

if (!Char::IsDigit(e->KeyChar) && key != 8) {

e->Handled = true;

}

}

void keypressMinMax(System::Windows::Forms::KeyPressEventArgs^ e,

System::Windows::Forms::TextBox^ txt) {

char key = e->KeyChar;

if (!Char::IsDigit(e->KeyChar) && key != 8 && key != '-') {

e->Handled = true;

}

else {

try {

if (key == '-') {

double a = System::Double::Parse(txt->Text);

a *= -1;

txt->Text = Convert::ToString(a);

e->Handled = true;

txt->SelectionStart = txt->Text->Length;

}

}

catch (System::FormatException^ r) {

if (txt->Text->Length == 1) {

e->Handled = true;

}

}

}

}

private: Void txtMin_KeyPress(System::Object^ sender,

System::Windows::Forms::KeyPressEventArgs^ e) {

keypressMinMax(e, txtMin);

}

private: :Void txtMax_KeyPress(System::Object^ sender,

System::Windows::Forms::KeyPressEventArgs^ e) {

keypressMinMax(e, txtMax);

}

private: System::Void txtIn_TextChanged(System::Object^ sender, System::EventArgs^ e) {

flagfile = true;

}