Автор: Пользователь скрыл имя, 12 Июля 2013 в 00:49, курсовая работа
Теорія алгоритмів - дисципліна що вивчає як саме поняття алгоритму, так і поняття алгоритмічної розв'язності задач.
Перші, та найчисленніші застосування теорія алгоритмів мала в математичній логіці, адже вона виникла саме як розділ математичної логіки. Теорія алгоритмів є фундаментом програмування та інформатики.
Поштовхом до виникнення теорії алгоритмів, як окремого розділу математики, стала невдача в знаходженні алгоритмів розв'язку деяких масових проблем. Найвідомішими з них були проблема істинності для арифметичних формул, проблема істинності для формул числення предикатів першого порядку та десята проблема Гільберта про розв'язність діофантових рівнянь.
Умова задачі…………………………………………………………………………………..…2
Вступ…………………...……………………………………………………………………………3
Опис алгоритму………………………………………….………………………………….…4
Приклад………………….……………………………………………………………………..…7
Лістинг програми..………………………………………………………………………......8
Результат роботи програми…………………………………………….……………16
Висновок.……………………………………………………………………………………….17
Література………………………………………………………………..……………………18
/// 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::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
private: System::Windows::Forms::
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::
#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::
this->splitContainer2 = (gcnew System::Windows::Forms::
this->button1 = (gcnew System::Windows::Forms::
this->groupBox1 = (gcnew System::Windows::Forms::
this->textBox2 = (gcnew System::Windows::Forms::
this->listBox1 = (gcnew System::Windows::Forms::
this->Column1 = (gcnew System::Windows::Forms::
this->Column2 = (gcnew System::Windows::Forms::
this->dataGridView1 = (gcnew System::Windows::Forms::
this->menuStrip1 = (gcnew System::Windows::Forms::
this->пToolStripMenuItem = (gcnew System::Windows::Forms::
this->
this->
this->
this->выходToolStripMenuItem1 = (gcnew System::Windows::Forms::
this->openFileDialog1 = (gcnew System::Windows::Forms::
this->saveFileDialog1 = (gcnew System::Windows::Forms::
this->splitContainer1->Panel1-
this->splitContainer1->Panel2-
this->splitContainer1->
this->splitContainer2->Panel1-
this->splitContainer2->Panel2-
this->splitContainer2->
this->groupBox1->
(cli::safe_cast<System::
this->menuStrip1->
this->SuspendLayout();
//
// splitContainer1
//
this->splitContainer1->Dock = System::Windows::Forms::
this->splitContainer1->
this->splitContainer1->
this->splitContainer1->
this->splitContainer1->Name = L"splitContainer1";
this->splitContainer1->
//
// splitContainer1.Panel1
//
this->splitContainer1->Panel1-
//
// splitContainer1.Panel2
//
this->splitContainer1->Panel2-
this->splitContainer1->Size = System::Drawing::Size(394, 325);
this->splitContainer1->
this->splitContainer1->
//
// splitContainer2
//
this->splitContainer2->Dock = System::Windows::Forms::
this->splitContainer2->
this->splitContainer2->
this->splitContainer2->
this->splitContainer2->Name = L"splitContainer2";
//
// splitContainer2.Panel1
//
this->splitContainer2->Panel1-
//
// splitContainer2.Panel2
//
this->splitContainer2->Panel2-
this->splitContainer2->Size = System::Drawing::Size(394, 42);
this->splitContainer2->
this->splitContainer2->
//
// 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->
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// groupBox1
//
this->groupBox1->Controls->
this->groupBox1->Dock = System::Windows::Forms::
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::
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::
this->listBox1->
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::
//
// Column2
//
this->Column2->HeaderText = L"Левая часть функции переходов";
this->Column2->Name = L"Column2";
this->Column2->SortMode = System::Windows::Forms::
//
// dataGridView1
//
this->dataGridView1->
this->dataGridView1->Columns->
this->Column1});
this->dataGridView1->Dock = System::Windows::Forms::
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->
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->
this->
this->пToolStripMenuItem->Name = L"пToolStripMenuItem";
this->пToolStripMenuItem->Size = System::Drawing::Size(73, 20);
this->пToolStripMenuItem->Text = L"Программа";
//
// загрузитьИзФайлаToolStripMenuI
//
this->
this->
this->
this->
//
// сохранитьВФайлToolStripMenuIte
//
this->
this->
this->
this->
//
// очиститьВсёToolStripMenuItem
//
this->
this->
this->
this->
//
// выходToolStripMenuItem1
//
this->выходToolStripMenuItem1-
this->выходToolStripMenuItem1-
this->выходToolStripMenuItem1-
this->выходToolStripMenuItem1-
//
// openFileDialog1
//
this->openFileDialog1->Filter = L"Text files (*.txt)|*.txt";
this->openFileDialog1->FileOk += gcnew System::ComponentModel::
//
// saveFileDialog1
//
this->saveFileDialog1->
this->saveFileDialog1->
this->saveFileDialog1->Filter = L"Text files (*.txt)|*.txt";
this->saveFileDialog1->FileOk += gcnew System::ComponentModel::
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::
this->AutoSize = true;
this->ClientSize = System::Drawing::Size(638, 349);
this->Controls->Add(this->
this->Controls->Add(this->
this->Controls->Add(this->
this->MainMenuStrip = this->menuStrip1;
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::
this->Text = L"Эмулятор работы машины Тьюринга";
this->splitContainer1->Panel1-
this->splitContainer1->Panel2-
this->splitContainer1->
this->splitContainer2->Panel1-
this->splitContainer2->Panel2-
this->splitContainer2->
this->groupBox1->ResumeLayout(
this->groupBox1->
(cli::safe_cast<System::
this->menuStrip1->
this->menuStrip1->
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->
array<int>^ q_last = gcnew array<int>(dataGridView1->
array<wchar_t>^ moveto = gcnew array<wchar_t>(dataGridView1->
array<wchar_t>^ In = gcnew array<wchar_t>(dataGridView1->
array<wchar_t>^ Out = gcnew array<wchar_t>(dataGridView1->
dataGridView1->EndEdit();
for (int i = 0; i <= dataGridView1->Rows->Count - 2; ++i) {
array<wchar_t>^ Ar1 = gcnew array<wchar_t>(dataGridView1->
array<wchar_t>^ Ar2 = gcnew array<wchar_t>(dataGridView1->
Length = dataGridView1->Rows[i]->Cells[
Ar1 = dataGridView1->Rows[i]->Cells[
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[
Ar2 = dataGridView1->Rows[i]->Cells[
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->
str = MT.HeadDo(q_first[j], In[j], Out[j], q_last[j], moveto[j]);
if (MT.GetStatus() == -1)
listBox1->Items->Add(str->
break;
}
}
}
}
}
private: System::Void загрузитьИзФайлаToolStripMenuI
openFileDialog1->ShowDialog();
}
private: System::Void сохранитьВФайлToolStripMenuIte
dataGridView1->EndEdit();
saveFileDialog1->ShowDialog();
}
private: System::Void saveFileDialog1_FileOk(System:
dataGridView1->EndEdit();
IO::StreamWriter^ TMProg = gcnew IO::StreamWriter(
TMProg->WriteLine((
for (int i = 0; i < dataGridView1->Rows->Count; ++i) {
TMProg->WriteLine(
TMProg->WriteLine(
}
TMProg->Close();
}
private: System::Void openFileDialog1_FileOk(System:
dataGridView1->Rows->Clear();
listBox1->Items->Clear();
textBox2->Text = "";
IO::StreamReader^ TMProg = gcnew IO::StreamReader(
int Rows = Convert::ToInt32(TMProg->
for (int i = 0; i < Rows; ++i) {
dataGridView1->Rows->Add();
dataGridView1->Rows[i]->Cells[
dataGridView1->Rows[i]->Cells[
}
TMProg->Close();
}
private: System::Void выходToolStripMenuItem1_Click(
Close();
}
private: System::Void очиститьВсёToolStripMenuItem_
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(
{
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)