Курсовая работа: Логика и основы алгоритмизации в инженерных задачах

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам

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

for (int j=0;j < size; j++){

mass[i][j] = Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value);

}

}

}

//функция clear

private: void clear() {

for (int i=0;i < dataGridView1->Rows->Count; i++){

for (int j=0;j < dataGridView1->Rows->Count; j++){

dataGridView1->Rows[i]->Cells[j]->Value = 0;

}

}

}

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

{

label14->Text= "";

//размер матрицы

size = Convert::ToInt32(numericUpDown1->Value);

if (size<=12){

//создание массива для матрицы

int **mass = new int *[size];

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

mass[i]= new int[size];

}

//создание таблицы под матрицу

dataGridView1->ColumnCount = size;

dataGridView1->RowCount = size;

//заполнение значениями вручную

typing(size, mass);

//вывод матрицы

show(size, mass);

}

else label14->Text = "Выберите меньшее число вершин (не более 12)";

}

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

{

label14->Text = "";

label15->Text = "";

clear();

size=0;

memset(mass, 0, sizeof(mass));

memset(mass2, 0, sizeof(mass2));

memset(mass3, 0, sizeof(mass3));

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

{

versh[i].stepen = 0;

versh[i].num = 0;

versh[i].colour = 0;

}

}

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

{

switch (size)

{

case 1:

this->ovalShape1->Visible = true;

this->label2->Visible = true;

break;

case 2:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j))

{

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

break;

}

}

}

}

break;

case 3:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape4->Visible = true;

this->label4->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

case 4:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape3->Visible = true;

this->label4->Visible = true;

this->ovalShape4->Visible = true;

this->label5->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

case 5:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape3->Visible = true;

this->label4->Visible = true;

this->ovalShape4->Visible = true;

this->label5->Visible = true;

this->ovalShape5->Visible = true;

this->label6->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

case 6:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape3->Visible = true;

this->label4->Visible = true;

this->ovalShape4->Visible = true;

this->label5->Visible = true;

this->ovalShape5->Visible = true;

this->label6->Visible = true;

this->ovalShape6->Visible = true;

this->label7->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

case 7:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape3->Visible = true;

this->label4->Visible = true;

this->ovalShape4->Visible = true;

this->label5->Visible = true;

this->ovalShape5->Visible = true;

this->label6->Visible = true;

this->ovalShape6->Visible = true;

this->label7->Visible = true;

this->ovalShape8->Visible = true;

this->label8->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

case 8:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape3->Visible = true;

this->label4->Visible = true;

this->ovalShape4->Visible = true;

this->label5->Visible = true;

this->ovalShape5->Visible = true;

this->label6->Visible = true;

this->ovalShape6->Visible = true;

this->label7->Visible = true;

this->ovalShape7->Visible = true;

this->label8->Visible = true;

this->ovalShape8->Visible = true;

this->label9->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

case 9:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape3->Visible = true;

this->label4->Visible = true;

this->ovalShape4->Visible = true;

this->label5->Visible = true;

this->ovalShape5->Visible = true;

this->label6->Visible = true;

this->ovalShape6->Visible = true;

this->label7->Visible = true;

this->ovalShape7->Visible = true;

this->label8->Visible = true;

this->ovalShape8->Visible = true;

this->label9->Visible = true;

this->ovalShape9->Visible = true;

this->label10->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

case 10:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape3->Visible = true;

this->label4->Visible = true;

this->ovalShape4->Visible = true;

this->label5->Visible = true;

this->ovalShape5->Visible = true;

this->label6->Visible = true;

this->ovalShape6->Visible = true;

this->label7->Visible = true;

this->ovalShape7->Visible = true;

this->label8->Visible = true;

this->ovalShape8->Visible = true;

this->label9->Visible = true;

this->ovalShape9->Visible = true;

this->label10->Visible = true;

this->ovalShape10->Visible = true;

this->label11->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

case 11:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape3->Visible = true;

this->label4->Visible = true;

this->ovalShape4->Visible = true;

this->label5->Visible = true;

this->ovalShape5->Visible = true;

this->label6->Visible = true;

this->ovalShape6->Visible = true;

this->label7->Visible = true;

this->ovalShape7->Visible = true;

this->label8->Visible = true;

this->ovalShape8->Visible = true;

this->label9->Visible = true;

this->ovalShape9->Visible = true;

this->label10->Visible = true;

this->ovalShape10->Visible = true;

this->label11->Visible = true;

this->ovalShape11->Visible = true;

this->label12->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

case 12:

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

for (int j=0;j < size; j++)

{

{

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i!=j)) {

if (z > 0) {break;}

x1 = coords[i][0];

y1 = coords[i][1];

x2 = coords[j][0];

y2 = coords[j][1];

Graphics^ g = Graphics::FromHwnd(this->panel1->Handle);

g->DrawLine(gcnew Pen(Color::Black,3),x1,y1,x2,y2);

x1=0;y1=0;x2=0;y2=0;

this->ovalShape1->Visible = true;

this->label2->Visible = true;

this->ovalShape2->Visible = true;

this->label3->Visible = true;

this->ovalShape3->Visible = true;

this->label4->Visible = true;

this->ovalShape4->Visible = true;

this->label5->Visible = true;

this->ovalShape5->Visible = true;

this->label6->Visible = true;

this->ovalShape6->Visible = true;

this->label7->Visible = true;

this->ovalShape7->Visible = true;

this->label8->Visible = true;

this->ovalShape8->Visible = true;

this->label9->Visible = true;

this->ovalShape9->Visible = true;

this->label10->Visible = true;

this->ovalShape10->Visible = true;

this->label11->Visible = true;

this->ovalShape11->Visible = true;

this->label12->Visible = true;

this->ovalShape12->Visible = true;

this->label13->Visible = true;

}

else {

if ((Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value)==1) && (i==j))

{

label14->Text = "Ошибка ввода";

z++;

}

};

};

};

break;

}

z=0;

}

Алгоритм нахождения хроматического числа:

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

{

memset(mass2, -1, sizeof(mass2));

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

for (int j=0;j < size; j++)

{

mass2[i][j] = Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value);

}

}

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

for (int j=0;j < 12; j++)

{

if (mass2[i][j] == -1) {continue;}

else

mass3[i][j] = mass2[i][j];

}

}

int i=0, j=0;

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

for (int j=0;j < size; j++)

{

if (mass3[i][j]==1)

{c=c+1;}

}

versh[i].stepen=c;

versh[i].num=i;

c=0;

}

for (int i = size - 1; i >= 0; i--)

{

ns = 1;

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

{

if (versh[j].stepen < versh[j+1].stepen)

{

t = versh[j].stepen;

m = versh[j].num;

versh[j].stepen = versh[j+1].stepen;

versh[j].num = versh[j+1].num;

versh[j+1].stepen = t;

versh[j+1].num = m;

ns = 0;

}

}

if (ns == 1)

break;

}

k = 0;

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

{

if (versh[i].colour == 0)

{

k++;

versh[i].colour = k;

memset(versh2, -1, sizeof(versh2));

versh2[i] = versh[i].num;

d++;

}

for (int j = i + 1; j < size; j++)

{

h = versh[i].num;

n = versh[j].num;

if ((mass3[h][n]==0) && (versh[j].colour == 0))

{

b = proverka(j,n);

if (b==1){

versh[j].colour = k;

versh2[j] = versh[j].num;

d++;

}

}

}

d=0;

}

label15->Text =System::Convert::ToString(k);

this->label16->Visible = true;

}

}

int proverka(int j,int n)

{

ch=0;

for(s=j-1; s >= 0; s--)

{

int e = versh2[s];

if((versh2[s] >= 0) && (mass3[n][e]==0))

ch++;

}

if(ch==d) return 1;

else return 0;

}

}

};

};

Приложение 2. Результаты работы программы на разных наборах входных данных

Рисунок 12. Результат работы программы.

Рисунок 13. Результат работы программы.

Источник: https://otherreferats.allbest.ru/download/1125970/