Добавил:
Tushkan
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Готовые лабораторные работы (ММ) / DMath_lab2 / Unit1
.cpp//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <iostream>
#include <list>
#include <algorithm>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
long readstring(char* s)
{
long x1=0;
for(int i=0; s[i]!=0 && i< 16;i++)
{
x1=x1<<2;
if(s[i]=='1') x1=x1|1;// else x1=x1&&0xfffffe;
if(s[i]=='0') x1=x1|2;// else x1=x1&&0xfffffe;
if(s[i]=='x') x1=x1|3;// else x1=x1&&0xfffffe;
}
return x1;
}
long normalize(long x1, long x2,int l1)
{
x1=x1&x2;
int nc=0;
long mask=3;
for(int i=0; i<l1;i++)
{
if((x1&mask)==0)
{
x1=x1|mask;
nc++;
}
mask=mask<<2;
}
if(nc==1)
return x1;
}
char* getstring(long x3,unsigned l1)
{
char* ch = new char[32];
for(int i=0; i<32;i++) ch[i]=' ';
for(int i=1; i<=l1;i++)
{
long mask=3;
if((x3&mask)==1) ch[l1-i]='1';
if((x3&mask)==2) ch[l1-i]='0';
if((x3&mask)==3) ch[l1-i]='x';
x3=x3>>2;
}
ch[l1] = 0;
return ch;
}
bool inc(long x1, long x2)
{
return ((x1 & x2) == (x2));
}
unsigned l1;
std::list<long> lst1;
std::list<long> lst2;
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_Test1Click(TObject *Sender)
{
//lst1.push_back(readstring("10xx01001xxxx101"));
//lst1.push_back(readstring("0x11x1xx1x00xx01"));
//lst1.push_back(readstring("xxxxxxxxxxxxx01x"));
//Memo1->Lines->Add("10xx01001xxxx101");
//Memo1->Lines->Add("0x11x1xx1x00xx01");
//Memo1->Lines->Add("xxxxxxxxxxxxx01x");
Edit1->Text = 3;
StringGrid1->Cells[0][0] = "10xx01001xxxx101";
StringGrid1->Cells[0][1] = "0x11x1xx1x00xx01";
StringGrid1->Cells[0][2] = "xxxxxxxxxxxxx01x";
Edit2->Text = 0;
//StringGrid1->Cells[0][0] = "10xx01001xxxx101";
Edit3->Text = 16;
}
void __fastcall TForm1::Button_Test2Click(TObject *Sender)
{
//lst1.push_back(readstring("10xx01001xxxx101"));
//lst1.push_back(readstring("0x11x1xx1x00xx01"));
//lst1.push_back(readstring("xxxxxxxxxxxxx01x"));
//Memo1->Lines->Add("10xx01001xxxx101");
//Memo1->Lines->Add("0x11x1xx1x00xx01");
//Memo1->Lines->Add("xxxxxxxxxxxxx01x");
Edit1->Text = 3;
StringGrid1->Cells[0][0] = "10xx01001xxxx101";
StringGrid1->Cells[0][1] = "0x11x1xx1x00xx01";
StringGrid1->Cells[0][2] = "xxxxxxxxxxxxx01x";
Edit2->Text = 2;
StringGrid2->Cells[0][0] = "0101010101010101";
StringGrid2->Cells[0][1] = "0x11x1xx1x00xx01";
//StringGrid1->Cells[0][1] = "xxxxxxxxxxxxx01x";
Edit3->Text = 16;
}
//---------------------------------------------------------------------------
void sokr(std::list<long> &lst)
{
bool newIteration = true;
while(newIteration)
{
newIteration = false;
for(std::list<long>::iterator i = lst.begin(); i!=lst.end() && !newIteration;)
{
long curr = *i;
bool weryty = false;
for(std::list<long>::iterator j = lst.begin(); j!=lst.end(); ++j)
{
long nxt = *j;
if(inc(nxt,curr) && i != j)
{
//std::list<long>::iterator k = i;
//++k;
//i.remove();
i = lst.erase(i);
weryty = true;
break;
}
}
if(weryty) continue;
for(std::list<long>::iterator j = lst.begin(); j!=lst.end();)
{
long nxt = *j;
if(inc(curr,nxt) && i != j)
{
//std::list<long>::iterator k = j;
//++k;
//j.remove();
j = lst.erase(j);
}
else ++j;
}
std::list<long>::iterator j = i;
++j;
for(; j!=lst.end(); ++j)
{
long nxt = *j;
try
{
long abc = normalize(curr , nxt, l1);
bool absorpted = false;
for(std::list<long>::iterator q = lst.begin(); q!=lst.end(); ++q)
{
long dd = *q;
if(inc(dd,abc))
{
absorpted = true;
break;
}
}
if(!absorpted)
{
lst.push_front(abc);
newIteration = true;
break;
}
} catch(...){}
}
++i;
}
}
//print
}
bool peresek(long x1, long x2)
{
long mask=3;
for(int i=0; i<16; i++)
{
if ((x1&mask)==1&&(x2&mask)==2||(x1&mask)==2&&(x2&mask)==1)
return false;
x1=x1>>2;
x2=x2>>2;
}
return true;
}
bool srav(std::list<long> lst, std::list<long> lst2)
{
bool newIteration = true;
while(newIteration)
{
newIteration = false;
for(std::list<long>::iterator i = lst.begin(); i!=lst.end() && !newIteration;)
{
long curr = *i;
bool weryty = false;
for(std::list<long>::iterator j = lst.begin(); j!=lst.end(); ++j)
{
long nxt = *j;
if(inc(nxt,curr) && i != j)
{
//std::list<long>::iterator k = i;
//++k;
//i.remove();
i = lst.erase(i);
weryty = true;
break;
}
}
if(weryty) continue;
for(std::list<long>::iterator j = lst.begin(); j!=lst.end();)
{
long nxt = *j;
if(inc(curr,nxt) && i != j)
{
//std::list<long>::iterator k = j;
//++k;
//j.remove();
j = lst.erase(j);
}
else ++j;
}
for(std::list<long>::iterator j = lst2.begin(); j!=lst2.end();)
{
long nxt = *j;
if(inc(curr,nxt) && i != j)
{
//std::list<long>::iterator k = j;
//++k;
//j.remove();
j = lst2.erase(j);
}
else ++j;
}
std::list<long>::iterator j = i;
++j;
for(; j!=lst.end(); ++j)
{
long nxt = *j;
try
{
long abc = normalize(curr , nxt, l1);
bool absorpted = false;
for(std::list<long>::iterator q = lst.begin(); q!=lst.end(); ++q)
{
long dd = *q;
if(inc(dd,abc))
{
absorpted = true;
break;
}
}
if(!absorpted)
{
lst.push_front(abc);
newIteration = true;
break;
}
} catch(...){}
}
++i;
}
}
return (lst2.empty());
}
void ReadDataFromSG(TStringGrid* sg, std::list<long>& lst, unsigned n)
{
lst.clear();
//if(sg->RowCount == 1 && sg->Cells[0][0] == "") return;
for(int i = 0; i < n/*sg->RowCount*/; ++i)
lst.push_back(readstring(sg->Cells[0][i].c_str()));
}
void __fastcall TForm1::Button_CompareClick(TObject *Sender)
{
ReadDataFromSG(StringGrid1, lst1, StrToInt(Edit1->Text));
ReadDataFromSG(StringGrid2, lst2, StrToInt(Edit2->Text));
/* sokr(lst1);
StringGrid3->RowCount = lst1.size();
StringGrid3->Cells[0][0] = "";
int jj = 0;
for(std::list<long>::iterator i = lst1.begin(); i!=lst1.end() ; ++i, ++jj)
{
long curr = *i;
StringGrid3->Cells[0][jj] = getstring(curr,l1);
}
for(std::list<long>::iterator i = lst1.begin(); i!=lst1.end() ; )
{
long curr = *i;
bool e=false;
for(std::list<long>::iterator j = lst2.begin(); j!=lst2.end() ; ++j)
{
long t = *j;
if(peresek(curr,t)) e=true;
}
if(!e)
{
i=lst1.erase(i);
} else ++i;
}
*/
int result = 0;
if(srav(lst1,lst2)) result = 2;//MessageBoxA(Form1->Handle,"2","Результат",MB_OK);
else
if(srav(lst2,lst1)) result = 1;//MessageBoxA(Form1->Handle,"1","Результат",MB_OK);
//else MessageBoxA(Form1->Handle,"0","Результат",MB_OK);
StringGrid4->RowCount = lst1.size();
StringGrid4->Cells[0][0] = "";
//jj = 0;
/* for(std::list<long>::iterator i = lst1.begin(); i!=lst1.end() ; ++i, ++jj)
{
long curr = *i;
StringGrid4->Cells[0][jj] = getstring(curr,l1);
}
*/
MessageBoxA(Form1->Handle, IntToStr(result).c_str(),"Результат",MB_OK);
//Memo2->Clear();
/*for(std::list<long>::iterator i = lst2.begin(); i!=lst2.end() ; ++i)
{
long curr = *i;
Memo2->Lines->Add(getstring(curr,l1));
} */
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1Change(TObject *Sender)
{
int nn = StrToInt(Edit1->Text);
StringGrid1->RowCount = nn > 0 ? nn : 1;
for(int i = 0; i < StringGrid1->RowCount; ++i)
StringGrid1->Cells[0][i] = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2Change(TObject *Sender)
{
int nn = StrToInt(Edit2->Text);
StringGrid2->RowCount = nn > 0 ? nn : 1;
for(int i = 0; i < StringGrid2->RowCount; ++i)
StringGrid2->Cells[0][i] = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit3Change(TObject *Sender)
{
l1 = StrToInt(Edit3->Text);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ReadDataFromSG(StringGrid1, lst1, StrToInt(Edit1->Text));
ReadDataFromSG(StringGrid2, lst2, StrToInt(Edit2->Text));
sokr(lst1);
StringGrid3->RowCount = lst1.size();
StringGrid3->Cells[0][0] = "";
int jj = 0;
for(std::list<long>::iterator i = lst1.begin(); i!=lst1.end() ; ++i, ++jj)
{
long curr = *i;
StringGrid3->Cells[0][jj] = getstring(curr,l1);
}
for(std::list<long>::iterator i = lst1.begin(); i!=lst1.end() ; )
{
long curr = *i;
bool e=false;
for(std::list<long>::iterator j = lst2.begin(); j!=lst2.end() ; ++j)
{
long t = *j;
if(peresek(curr,t)) e=true;
}
if(!e)
{
i=lst1.erase(i);
} else ++i;
}
StringGrid4->RowCount = lst1.size();
StringGrid4->Cells[0][0] = "";
jj = 0;
for(std::list<long>::iterator i = lst1.begin(); i!=lst1.end() ; ++i, ++jj)
{
long curr = *i;
StringGrid4->Cells[0][jj] = getstring(curr,l1);
}
//MessageBoxA(Form1->Handle, IntToStr(result).c_str(),"Результат",MB_OK);
//Memo2->Clear();
/*for(std::list<long>::iterator i = lst2.begin(); i!=lst2.end() ; ++i)
{
long curr = *i;
Memo2->Lines->Add(getstring(curr,l1));
} */
}
//---------------------------------------------------------------------------
Соседние файлы в папке DMath_lab2