Добавил:
Я и кто? Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторная работа №7.docx
Скачиваний:
0
Добавлен:
02.02.2023
Размер:
161.82 Кб
Скачать

2 Ход работы

2.1 Разработка блок-схем

Блок-схема функции func2 представлена на рисунке 1

Рисунок 1-Блок-схема функции func2, осуществляющей сортировку массива простой вставкой

Блок-схема функции func3 представлена на рисунке 2.

Рисунок 2-Блок-схема функции func3, осуществляющей сортировку массива бинарными вставками.

Блок-схема функции func4 представлена на рисунке 3

Рисунок 3-Блок-схема функции func4, осуществляющая сортировку массива простым выбором

На рисунке 4 представлена событийная функция

Рисунок 4-Блок-схема событийной функции

2.2 Код программы

DLL

void Class1::enter_mas(int* mas, int n) {

for (int i = 0; i < n; i++)

mas[i] = (int)(rand() % 200) - 100;

}

void Class1::output_mas(int* mas, int n, DataGridView^ grid) {

grid->ColumnCount = n;

grid->RowCount = 2;

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

grid->Rows[0]->Cells[i]->Value = "[" + i + "]";

grid->Rows[1]->Cells[i]->Value = mas[i];

}

int sum = 0;

for (int s = 0; s < grid->ColumnCount; s++) {

sum += grid->Columns[s]->Width;

if (sum > 410) grid->Width = 410;

else grid->Width = sum;

}

}

int Class1::kolvo(int* mas, int n) {

int k = 0;

for (int i = 0; i < n; i++)

if (((mas[i] * mas[i + 1] % 2) != 0) && ((mas[i] + mas[i + 1]) > 0))

k = k + 1;

return k;

}

void Class1::rezmas(int* mas, int* rezmas, int n, int k, int& m) {

int cnt = 0;

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

if (mas[i] % k == 0) {

rezmas[cnt] = mas[i];

cnt = cnt + 1;

}

m = cnt;

}

}

void Class1::rezmas2(int* mas, int* rezmas2, int n, int k, int& m) {

int cnt = 0;

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

if (mas[i] % k == 0) {

rezmas2[cnt] = mas[i];

cnt = cnt + 1;

}

m = cnt;

}

}

void Class1::delite_mas(int* rezmas, int cnt, int k, int& j) {

for (int i = k; i < cnt - 1; i++) {

rezmas[i] = rezmas[i + 1];

}

j = cnt - 1;

}

void Class1::vstavka(int* rezmas, int cnt, int k, int m, int& l) {

for (int i = cnt; i > k - 1; i--) {

rezmas[i + 1] = rezmas[i];

}

rezmas[k + 1] = m;

l = cnt + 1;

}

int Class1::mono(int* rezmas, int cnt) {

int i = 0;

int flag = 1;

while (i < cnt - 1 && flag == 1) {

if (rezmas[i] >= rezmas[i + 1])

i = i + 1;

else flag = 0;

}

return flag;

}

int Class1::func(int* rezmas, int cnt) {

int i = 0;

int flag = 0;

while (i <= cnt && flag == 0) {

if (rezmas[i] < 0)

flag = 1;

else

i = i + 1;

}

return i;

}

void Class1::output(double c, TextBox^ t) {

t->Text = Convert::ToString(c);

}

void Class1::func1(double &y, double&z, int n, double x, double a, double k, ListBox^ t) {

t->Items->Clear();

a = 1;

z = log(x);

while (fabs(y-a) > k) {

a = y;

n = n + 1;

y = y+(pow(-1, (n+1)) * (pow((x - 1), n) / n));

String^ fs = String::Format("{0,6:F0}{1,15:F7}", n, y);

t->Items->Add(fs);

}

}

double Class1::input(TextBox^ t) {

return Convert::ToDouble(t->Text);

}

void Class1::func2(int*rezmas, int n) { //Сортировка простой вставкой

for (int i = 1; i < n; i++) {

int x = rezmas[i];

int j = i - 1;

while (x < rezmas[j]) {

rezmas[j + 1] = rezmas[j];

j = j - 1;

}

rezmas[j + 1] = x;

}

}

void Class1::func4(int* rezmas, int n) { //Сортировка простым выбором

for (int i = 0; i < n - 1; i++) {

int k = i;

int x = rezmas[i];

for (int j = i + 1; j < n; j++) {

if (rezmas[j] < x) {

k = j;

x = rezmas[j];

}

}

rezmas[k] = rezmas[i];

rezmas[i] = x;

}

}

void Class1::func3(int* rezmas, int n) { //Сортировка бинарными вставками

for (int i = 1; i < n; i++) {

int x = rezmas[i];

int left = 0;

int right = i - 1;

while (left <= right) {

int m = (left + right) / 2;

if (x < rezmas[m])

right = m - 1;

else

left = m + 1;

}

for (int j = i - 1; j >= left; j--) {

rezmas[j + 1] = rezmas[j];

}

rezmas[left] = x;

}

}

void Class1::Zapis(DataGridView^ grid)

{

String^ f = Interaction::InputBox("Введите путь по которому будет сохранен файл и его имя. Расширение добавляется автоматически" + "/nНапример:C:\\Users\\name\\repos\'имя файла", "Ввод", "Ввод", -1, -1);

String^ filename = f + ".txt";

try

{

auto fk = System::Text::Encoding::GetEncoding(1251);

auto Z = gcnew IO::StreamWriter(filename, false, fk);

for (int i = 0; i < grid->ColumnCount; i++)

{

Z->WriteLine(Convert::ToString(grid->Rows[1]->Cells[i]->Value));

}

Z->Close();

MessageBox::Show("Запись успешна");

}

catch (System::Exception^ E)

{

MessageBox::Show(E->Message + "\nОшибка", "Ошибка");

}

}

void Class1::Chtenie(int n, DataGridView^ grid)

{

String^ f = Interaction::InputBox("Введите путь", "Заголовок", "C:\\user\\...", -1, -1);

String^ fn = f + ".txt";

grid->ColumnCount = n;

grid->RowCount = 2;

try {

IO::StreamReader^ z = gcnew IO::StreamReader(fn);

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

grid->Rows[1]->Cells[i]->Value = z->ReadLine();

}

z->Close();

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

grid->Rows[0]->Cells[i]->Value = "[" + i + "]";

}

}

catch (System::Exception^ E) {

MessageBox::Show(E->Message + "\nОшибка", "Error");

}

int sum = 0;

for (int s = 0; s < grid->ColumnCount; s++) {

sum += grid->Columns[s]->Width;

if (sum > 410) grid->Width = 410;

else grid->Width = sum;

}

}

Событийная

int n, k, cnt, del, j, m, v, l;

String^ g = Interaction::InputBox("Введите количество элементов в массиве", "Ввод", "", -1, -1);

String^ L = Interaction::InputBox("Введите номер элемента массива для удаления", "Ввод", "", -1, -1);

String^ q = Interaction::InputBox("Введите номер элемента массива для вставки", "Ввод", "", -1, -1);

String^ w = Interaction::InputBox("Введите элемент массива, который вы хотите вставить", "Ввод", "", -1, -1);

n = Convert::ToInt16(g);

del = Convert::ToInt16(L);

v = Convert::ToInt16(q);

m = Convert::ToInt16(w);

int* mas = new int[n];

int* rezmas = new int[n];

int* rezmas2 = new int[n];

DLL5::Class1::enter_mas(mas, n);

k = DLL5::Class1::kolvo(mas, n);

MessageBox::Show("Количество элементов = " + Convert::ToString(k),

" ", MessageBoxButtons::OK, MessageBoxIcon::Asterisk);

DLL5::Class1::rezmas(mas, rezmas, n, k, cnt);

DLL5::Class1::rezmas2(mas, rezmas2, n, k, cnt);

DLL5::Class1::output_mas(mas, n, dataGridView1);

DLL5::Class1::output_mas(rezmas, cnt, dataGridView2);

DLL5::Class1::delite_mas(rezmas, cnt, del, j);

DLL5::Class1::output_mas(rezmas, j, dataGridView3);

DLL5::Class1::func2(rezmas, j);

DLL5::Class1::output_mas(rezmas, j, dataGridView5);

DLL5::Class1::vstavka(rezmas, j, v, m, l);

DLL5::Class1::output_mas(rezmas, l, dataGridView4);

DLL5::Class1::func3(rezmas, cnt);

DLL5::Class1::output_mas(rezmas, cnt, dataGridView6);

DLL5::Class1::func4(rezmas2, cnt);

DLL5::Class1::output_mas(rezmas2, cnt, dataGridView7);

DLL5::Class1::Zapis(dataGridView7);

DLL5::Class1::Chtenie(cnt, dataGridView8);

delete[] rezmas2;

delete[] rezmas;

delete[] mas;

}