Теория Алгоритмов

Автор: Пользователь скрыл имя, 12 Июля 2013 в 00:49, курсовая работа

Описание работы

Теорія алгоритмів - дисципліна що вивчає як саме поняття алгоритму, так і поняття алгоритмічної розв'язності задач.
Перші, та найчисленніші застосування теорія алгоритмів мала в математичній логіці, адже вона виникла саме як розділ математичної логіки. Теорія алгоритмів є фундаментом програмування та інформатики.
Поштовхом до виникнення теорії алгоритмів, як окремого розділу математики, стала невдача в знаходженні алгоритмів розв'язку деяких масових проблем. Найвідомішими з них були проблема істинності для арифметичних формул, проблема істинності для формул числення предикатів першого порядку та десята проблема Гільберта про розв'язність діофантових рівнянь.

Содержание

Умова задачі…………………………………………………………………………………..…2
Вступ…………………...……………………………………………………………………………3
Опис алгоритму………………………………………….………………………………….…4
Приклад………………….……………………………………………………………………..…7
Лістинг програми..………………………………………………………………………......8
Результат роботи програми…………………………………………….……………16
Висновок.……………………………………………………………………………………….17
Література………………………………………………………………..……………………18

Работа содержит 1 файл

Zvit_osnovn.docx

— 898.33 Кб (Скачать)

///          the designers will not be able to interact properly with localized

///          resources associated with this form.

/// </summary>

public ref class Form1 : public System::Windows::Forms::Form

{

public:

Form1(void)

{

InitializeComponent();

//

//TODO: Add the constructor code here

//

}

 

protected:

/// <summary>

/// Clean up any resources being used.

/// </summary>

~Form1()

{

if (components)

{

delete components;

}

}

private: System::Windows::Forms::SplitContainer^  splitContainer1;

private: System::Windows::Forms::ListBox^  listBox1;

private: System::Windows::Forms::SplitContainer^  splitContainer2;

private: System::Windows::Forms::Button^  button1;

 

private: System::Windows::Forms::GroupBox^  groupBox1;

private: System::Windows::Forms::TextBox^  textBox2;

private: System::Windows::Forms::DataGridViewTextBoxColumn^  Column1;

private: System::Windows::Forms::DataGridViewTextBoxColumn^  Column2;

private: System::Windows::Forms::DataGridView^  dataGridView1;

private: System::Windows::Forms::MenuStrip^  menuStrip1;

private: System::Windows::Forms::ToolStripMenuItem^  пToolStripMenuItem;

private: System::Windows::Forms::ToolStripMenuItem^  загрузитьИзФайлаToolStripMenuItem;

private: System::Windows::Forms::ToolStripMenuItem^  сохранитьВФайлToolStripMenuItem1;

private: System::Windows::Forms::ToolStripMenuItem^  очиститьВсёToolStripMenuItem;

private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;

private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;

private: System::Windows::Forms::ToolStripMenuItem^  выходToolStripMenuItem1;

protected:

 

private:

/// <summary>

/// Required designer variable.

/// </summary>

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

void InitializeComponent(void)

{

this->splitContainer1 = (gcnew System::Windows::Forms::SplitContainer());

this->splitContainer2 = (gcnew System::Windows::Forms::SplitContainer());

this->button1 = (gcnew System::Windows::Forms::Button());

this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());

this->textBox2 = (gcnew System::Windows::Forms::TextBox());

this->listBox1 = (gcnew System::Windows::Forms::ListBox());

this->Column1 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());

this->Column2 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());

this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());

this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());

this->пToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());

this->загрузитьИзФайлаToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());

this->сохранитьВФайлToolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());

this->очиститьВсёToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());

this->выходToolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());

this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());

this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());

this->splitContainer1->Panel1->SuspendLayout();

this->splitContainer1->Panel2->SuspendLayout();

this->splitContainer1->SuspendLayout();

this->splitContainer2->Panel1->SuspendLayout();

this->splitContainer2->Panel2->SuspendLayout();

this->splitContainer2->SuspendLayout();

this->groupBox1->SuspendLayout();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->BeginInit();

this->menuStrip1->SuspendLayout();

this->SuspendLayout();

//

// splitContainer1

//

this->splitContainer1->Dock = System::Windows::Forms::DockStyle::Fill;

this->splitContainer1->FixedPanel = System::Windows::Forms::FixedPanel::Panel1;

this->splitContainer1->IsSplitterFixed = true;

this->splitContainer1->Location = System::Drawing::Point(244, 24);

this->splitContainer1->Name = L"splitContainer1";

this->splitContainer1->Orientation = System::Windows::Forms::Orientation::Horizontal;

//

// splitContainer1.Panel1

//

this->splitContainer1->Panel1->Controls->Add(this->splitContainer2);

//

// splitContainer1.Panel2

//

this->splitContainer1->Panel2->Controls->Add(this->listBox1);

this->splitContainer1->Size = System::Drawing::Size(394, 325);

this->splitContainer1->SplitterDistance = 42;

this->splitContainer1->TabIndex = 1;

//

// splitContainer2

//

this->splitContainer2->Dock = System::Windows::Forms::DockStyle::Fill;

this->splitContainer2->FixedPanel = System::Windows::Forms::FixedPanel::Panel1;

this->splitContainer2->IsSplitterFixed = true;

this->splitContainer2->Location = System::Drawing::Point(0, 0);

this->splitContainer2->Name = L"splitContainer2";

//

// splitContainer2.Panel1

//

this->splitContainer2->Panel1->Controls->Add(this->button1);

//

// splitContainer2.Panel2

//

this->splitContainer2->Panel2->Controls->Add(this->groupBox1);

this->splitContainer2->Size = System::Drawing::Size(394, 42);

this->splitContainer2->SplitterDistance = 131;

this->splitContainer2->TabIndex = 0;

//

// button1

//

this->button1->Location = System::Drawing::Point(6, 11);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(122, 28);

this->button1->TabIndex = 0;

this->button1->Text = L"Эмулировать";

this->button1->UseVisualStyleBackColor = true;

this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

//

// groupBox1

//

this->groupBox1->Controls->Add(this->textBox2);

this->groupBox1->Dock = System::Windows::Forms::DockStyle::Fill;

this->groupBox1->Location = System::Drawing::Point(0, 0);

this->groupBox1->Name = L"groupBox1";

this->groupBox1->Size = System::Drawing::Size(259, 42);

this->groupBox1->TabIndex = 0;

this->groupBox1->TabStop = false;

//

// textBox2

//

this->textBox2->Dock = System::Windows::Forms::DockStyle::Fill;

this->textBox2->Location = System::Drawing::Point(3, 16);

this->textBox2->Name = L"textBox2";

this->textBox2->Size = System::Drawing::Size(253, 20);

this->textBox2->TabIndex = 0;

//

// listBox1

//

this->listBox1->Dock = System::Windows::Forms::DockStyle::Fill;

this->listBox1->FormattingEnabled = true;

this->listBox1->Location = System::Drawing::Point(0, 0);

this->listBox1->Name = L"listBox1";

this->listBox1->Size = System::Drawing::Size(394, 277);

this->listBox1->TabIndex = 0;

//

// Column1

//

this->Column1->HeaderText = L"Правая часть функции переходов";

this->Column1->Name = L"Column1";

this->Column1->SortMode = System::Windows::Forms::DataGridViewColumnSortMode::NotSortable;

//

// Column2

//

this->Column2->HeaderText = L"Левая часть функции переходов";

this->Column2->Name = L"Column2";

this->Column2->SortMode = System::Windows::Forms::DataGridViewColumnSortMode::NotSortable;

//

// dataGridView1

//

this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;

this->dataGridView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^  >(2) {this->Column2,

this->Column1});

this->dataGridView1->Dock = System::Windows::Forms::DockStyle::Left;

this->dataGridView1->Location = System::Drawing::Point(0, 24);

this->dataGridView1->Name = L"dataGridView1";

this->dataGridView1->Size = System::Drawing::Size(244, 325);

this->dataGridView1->TabIndex = 0;

//

// menuStrip1

//

this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->пToolStripMenuItem});

this->menuStrip1->Location = System::Drawing::Point(0, 0);

this->menuStrip1->Name = L"menuStrip1";

this->menuStrip1->Size = System::Drawing::Size(638, 24);

this->menuStrip1->TabIndex = 2;

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

//

// пToolStripMenuItem

//

this->пToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(4) {this->загрузитьИзФайлаToolStripMenuItem,

this->сохранитьВФайлToolStripMenuItem1, this->очиститьВсёToolStripMenuItem, this->выходToolStripMenuItem1});

this->пToolStripMenuItem->Name = L"пToolStripMenuItem";

this->пToolStripMenuItem->Size = System::Drawing::Size(73, 20);

this->пToolStripMenuItem->Text = L"Программа";

//

// загрузитьИзФайлаToolStripMenuItem

//

this->загрузитьИзФайлаToolStripMenuItem->Name = L"загрузитьИзФайлаToolStripMenuItem";

this->загрузитьИзФайлаToolStripMenuItem->Size = System::Drawing::Size(180, 22);

this->загрузитьИзФайлаToolStripMenuItem->Text = L"Загрузить с файла";

this->загрузитьИзФайлаToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::загрузитьИзФайлаToolStripMenuItem_Click);

//

// сохранитьВФайлToolStripMenuItem1

//

this->сохранитьВФайлToolStripMenuItem1->Name = L"сохранитьВФайлToolStripMenuItem1";

this->сохранитьВФайлToolStripMenuItem1->Size = System::Drawing::Size(180, 22);

this->сохранитьВФайлToolStripMenuItem1->Text = L"Сохранить в файл";

this->сохранитьВФайлToolStripMenuItem1->Click += gcnew System::EventHandler(this, &Form1::сохранитьВФайлToolStripMenuItem1_Click);

//

// очиститьВсёToolStripMenuItem

//

this->очиститьВсёToolStripMenuItem->Name = L"очиститьВсёToolStripMenuItem";

this->очиститьВсёToolStripMenuItem->Size = System::Drawing::Size(180, 22);

this->очиститьВсёToolStripMenuItem->Text = L"Очистить всё";

this->очиститьВсёToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::очиститьВсёToolStripMenuItem_Click);

//

// выходToolStripMenuItem1

//

this->выходToolStripMenuItem1->Name = L"выходToolStripMenuItem1";

this->выходToolStripMenuItem1->Size = System::Drawing::Size(180, 22);

this->выходToolStripMenuItem1->Text = L"Выход";

this->выходToolStripMenuItem1->Click += gcnew System::EventHandler(this, &Form1::выходToolStripMenuItem1_Click);

//

// openFileDialog1

//

this->openFileDialog1->Filter = L"Text files (*.txt)|*.txt";

this->openFileDialog1->FileOk += gcnew System::ComponentModel::CancelEventHandler(this, &Form1::openFileDialog1_FileOk);

//

// saveFileDialog1

//

this->saveFileDialog1->DefaultExt = L"txt";

this->saveFileDialog1->FileName = L"Untitled";

this->saveFileDialog1->Filter = L"Text files (*.txt)|*.txt";

this->saveFileDialog1->FileOk += gcnew System::ComponentModel::CancelEventHandler(this, &Form1::saveFileDialog1_FileOk);

//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->AutoSize = true;

this->ClientSize = System::Drawing::Size(638, 349);

this->Controls->Add(this->splitContainer1);

this->Controls->Add(this->dataGridView1);

this->Controls->Add(this->menuStrip1);

this->MainMenuStrip = this->menuStrip1;

this->Name = L"Form1";

this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;

this->Text = L"Эмулятор работы машины Тьюринга";

this->splitContainer1->Panel1->ResumeLayout(false);

this->splitContainer1->Panel2->ResumeLayout(false);

this->splitContainer1->ResumeLayout(false);

this->splitContainer2->Panel1->ResumeLayout(false);

this->splitContainer2->Panel2->ResumeLayout(false);

this->splitContainer2->ResumeLayout(false);

this->groupBox1->ResumeLayout(false);

this->groupBox1->PerformLayout();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->EndInit();

this->menuStrip1->ResumeLayout(false);

this->menuStrip1->PerformLayout();

this->ResumeLayout(false);

this->PerformLayout();

 

}

#pragma endregion

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

listBox1->Items->Clear();

 TuringMachine MT(textBox2->Text);

String^ str;

 int ArNum = 0;

 int Length;

 

 array<wchar_t>^ Tape;

 array<int>^ q_first = gcnew array<int>(dataGridView1->Rows->Count);

 array<int>^ q_last = gcnew array<int>(dataGridView1->Rows->Count);

 array<wchar_t>^ moveto = gcnew array<wchar_t>(dataGridView1->Rows->Count);

 array<wchar_t>^ In = gcnew array<wchar_t>(dataGridView1->Rows->Count);

 array<wchar_t>^ Out = gcnew array<wchar_t>(dataGridView1->Rows->Count);

 

dataGridView1->EndEdit();

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

 array<wchar_t>^ Ar1 = gcnew array<wchar_t>(dataGridView1->Rows[i]->Cells[0]->Value->ToString()->Length);

 array<wchar_t>^ Ar2 = gcnew array<wchar_t>(dataGridView1->Rows[i]->Cells[1]->Value->ToString()->Length);

Length = dataGridView1->Rows[i]->Cells[0]->Value->ToString()->Length;

Ar1 = dataGridView1->Rows[i]->Cells[0]->Value->ToString()->ToCharArray();

 In[ArNum] = Ar1[Length - 1];

Length -= 2;

 while (Length > 0) {

if (Ar1[Length] != 'q' && Ar1[Length] != 'Q') {

str += Ar1[Length];

Length--;

}

else

break;

}

 q_first[i] = Convert::ToInt32(str);

 

 str = "";

Length = dataGridView1->Rows[i]->Cells[1]->Value->ToString()->Length;

Ar2 = dataGridView1->Rows[i]->Cells[1]->Value->ToString()->ToCharArray();

 if ((Ar2[Length - 1] != 'R' && Ar2[Length - 1] != 'r' && Ar2[Length - 1] != 'L' && Ar2[Length - 1] != 'l')) {

 moveto[i] = 'E';

 Out[ArNum] = Ar2[Length - 1];

Length -= 2;

 while (true) {

if (Ar2[Length] != 'q' && Ar2[Length] != 'Q') {

str += Ar2[Length];

Length--;

}

else

break;

}

}

 else {

 moveto[i] = Ar2[Length - 1];

 Out[ArNum] = Ar2[Length - 2];

Length -= 3;

 while (Length > 0) {

if (Ar2[Length] != 'q' && Ar2[Length] != 'Q') {

str += Ar2[Length];

Length--;

}

else

break;

}

}

 if (str == "") {

Length = Ar2->Length;

 moveto[i] = 'E';

 Out[ArNum] = Ar2[Length - 1];

Length -= 2;

 while (true) {

if (Ar2[Length] != 'q' && Ar2[Length] != 'Q') {

str += Ar2[Length];

Length--;

}

else

break;

}

}

 if (str == "*")

 q_last[i] = -1;

 else

 q_last[i] = Convert::ToInt32(str);

 str = "";

 ArNum++; }

 while (MT.GetStatus() >= 0) {

 for (int j = 0; j < In->Length - 1; ++j) {

 str = MT.GetTape();

Tape = gcnew array<wchar_t>(str->Length);

Tape = str->ToCharArray();

 if (MT.GetStatus() == q_first[j]) {

 if (Tape[MT.GetHead()] == In[j]) {

listBox1->Items->Add(str->Substring(0, MT.GetHead()) + "(q" + MT.GetStatus().ToString() + ")" + str->Substring(MT.GetHead()));

str = MT.HeadDo(q_first[j], In[j], Out[j], q_last[j], moveto[j]);

if (MT.GetStatus() == -1)

listBox1->Items->Add(str->Substring(0, MT.GetHead()) + "(q*)" + str->Substring(MT.GetHead()));

break;

}

}

}

}

}

private: System::Void загрузитьИзФайлаToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {

openFileDialog1->ShowDialog();

}

private: System::Void сохранитьВФайлToolStripMenuItem1_Click(System::Object^  sender, System::EventArgs^  e) {

dataGridView1->EndEdit();

saveFileDialog1->ShowDialog();

}

private: System::Void saveFileDialog1_FileOk(System::Object^  sender, System::ComponentModel::CancelEventArgs^  e) {

dataGridView1->EndEdit();

IO::StreamWriter^ TMProg = gcnew IO::StreamWriter(saveFileDialog1->FileName);

 TMProg->WriteLine((dataGridView1->Rows->Count - 1).ToString());

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

 TMProg->WriteLine(dataGridView1->Rows[i]->Cells[0]->Value);

 TMProg->WriteLine(dataGridView1->Rows[i]->Cells[1]->Value);

 }

 TMProg->Close();

}

private: System::Void openFileDialog1_FileOk(System::Object^  sender, System::ComponentModel::CancelEventArgs^  e) {

dataGridView1->Rows->Clear();

listBox1->Items->Clear();

textBox2->Text = "";

IO::StreamReader^ TMProg = gcnew IO::StreamReader(openFileDialog1->FileName);

 int Rows = Convert::ToInt32(TMProg->ReadLine());

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

dataGridView1->Rows->Add();

dataGridView1->Rows[i]->Cells[0]->Value = TMProg->ReadLine();

dataGridView1->Rows[i]->Cells[1]->Value = TMProg->ReadLine();

}

 TMProg->Close();

}

private: System::Void выходToolStripMenuItem1_Click(System::Object^  sender, System::EventArgs^  e) {

 Close();

}

private: System::Void очиститьВсёToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {

dataGridView1->Rows->Clear();

listBox1->Items->Clear();

 textBox2->Text = ""; }

};

}

 

TuringMashine.cpp

// TuringMachine.cpp

#include "stdafx.h"

#include "TuringMachine.h"

using namespace System;

using namespace MachineTuring;

TuringMachine::TuringMachine(String^ str)

{

    tape = "^"+str+"^";

    q = 0;

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

if (tape[i] != '^') {

head = i;

break;

}                                

}

 

String^ TuringMachine::HeadDo(int q_f, wchar_t in, wchar_t out, int q_l, wchar_t move)

{

   array<wchar_t>^ Ar = gcnew array<wchar_t>(tape->Length);

   if (head >= tape->Length - 1)

Информация о работе Теория Алгоритмов