Листинг (1077221), страница 2
Текст из файла (страница 2)
(*(Last->Next))=(*(temp->Next));
Last->Next->position=temp->Next->position;
temp2=Last;
Last=Last->Next;
temp=temp->Next;
Last->Prev=temp2;
}
}
count=op1.count;
}
}
Sentence::~Sentence()
{
Word *temp=First;
Word *temp1;
while(temp!=Last)
{
temp1=temp;
temp=temp->Next;
delete temp1;
}
delete Last;
}
void Sentence::Add()
{
const String tmppp="y";
String buf;
char buf1[10];
do
{
if(First==NULL)
{
First= new Word;
First->CreateWord();
Last=First;
count = count+1;
First->position=1;
} else
{
Word *temp;
Word *C = new Word;
C->CreateWord();
Last->Next=new Word;
(*(Last->Next))=(*C);
temp=Last;
Last=Last->Next;
Last->Prev=temp;
count = count+1;
Last->position=Last->Prev->position+1;
delete C;
}
cout << "Добавить ещё слово?(y/n):";
cin >> buf1;
buf=buf1;
cout << endl;
} while(buf==tmppp);
}
void Sentence::Print()
{
Word *temp=First;
if(temp!=0)
{
while(temp!=Last)
{
temp->PrintWord();
//cout << temp->position;
cout <<" ";
temp=temp->Next;
}
temp->PrintWord();
//cout << temp->position;
cout << endl << endl;
this->GetCount();
} else cout << "В предложении нет слов";
}
void Sentence::Delete(Word *temp)
{
if(temp==First && temp==Last)
{
delete First;
First=NULL;
Last=NULL;
} else
if(temp==First)
{
First->Next->Prev = NULL;
Word *temp1;
temp1=First;
First=First->Next;
delete temp1;
Word *param=First;
while(param!=Last)
{
param->position=param->position-1;
param=param->Next;
}
param->position=param->position-1;
} else
if(temp==Last)
{
Last->Prev->Next=NULL;
Word *temp1;
temp1=Last;
Last=Last->Prev;
delete temp1;
} else
{
Word *temp1=First;
while(temp1!=temp)
{
temp1=temp1->Next;
}
temp1->Prev->Next=temp1->Next;
Word *param=temp1->Next;
temp1->Next->Prev=temp1->Prev;
delete temp1;
while(param!=Last)
{
param->position=param->position-1;
param=param->Next;
}
param->position=param->position-1;
}
count=count-1;
}
void Sentence::GetCount()
{
cout << "Количество слов в предложении:" << this->count;
}
void Sentence::DeleteAll()
{
if(First!=NULL && Last!=NULL)
{
if(First!=Last)
{
Word *temp=First;
Word *temp1;
while(temp!=Last)
{
temp1=temp;
temp=temp->Next;
delete temp1;
}
delete Last;
First=NULL;
Last=NULL;
} else
{
delete First;
First = NULL;
Last = NULL;
}
}
count=0;
}
void Sentence::FindAndDelete()
{
String word1;
String tmp;
const String tmppp="y";
char tmp1[10];
char word2[50];
int flag=0;
cout << "Введите слово для удаления:";
cin >> word2;
word1=word2;
Word *temp=First;
if(First!= NULL & Last!=NULL)
{
do
{
if(temp->word==word1)
{
flag=1;
cout << endl;
Delete(temp);
cout << "Удаление успешно выполнено";
if(count!=1 && count!=0)
{
cout << endl << endl << "Продолжить поиск?(y/n):";
cin >> tmp1;
tmp=tmp1;
} else
{
tmp="y";
}
if(tmp!=tmppp)
{
break;
}
if(temp!=Last)
{
temp=temp->Next;
}
} else
{
if(temp!=Last)
{
temp=temp->Next;
}
}
} while(temp!=Last);
if(flag==1 && tmp==tmppp && First!=Last)
{
if(temp->word==word1)
{
Delete(temp);
cout << "Удаление успешно выполнено";
}
}
if(First==Last)
{
cout << endl << endl << "Продолжить поиск?(y/n):";
cin >> tmp1;
tmp=tmp1;
if(temp->word==word1)
{
flag=1;
cout << endl;
Delete(temp);
cout << "Удаление успешно выполнено";
}
}
if(flag==0)
{
if(temp->word==word1)
{
flag=1;
Delete(temp);
cout << "Удаление успешно выполнено";
}
}
if(flag==1)
{
cout << endl << endl << "Поиск завершен";
} else
{
cout << endl << "Слов не найдено";
}
} else
{
cout << endl << "В предложении нет слов";
}
}
void Sentence::FindPositionAndDelete()
{
int position1;
if(First!=NULL && Last!=NULL)
{
do
{
cout << "Введите позицию слова которое желаете удалить:";
cin >> position1;
if(position1<=0 || position1>Last->position)
{
cout << "Неверный ввод" << endl;
}
} while(position1<=0 || position1>Last->position);
Word *temp=First;
while(temp!=Last)
{
if(temp->position==position1)
{
Delete(temp);
break;
} else
{
temp=temp->Next;
}
}
if(temp->position==position1)
{
Delete(temp);
}
} else
{
cout << "В предложении нет слов"<<endl;
}
}
void Sentence::Sort()
{
if(First!=NULL && Last!=NULL && First!=Last)
{
Word *temp=First;
Word *temp1=First->Next;
for(int i=0;i<count-1;i++)
{
for(int j=i+1;j<count;j++)
{
if(temp1>temp)
{
Word *temp2 = new Word;
(*temp2)=(*temp);
(*temp)=(*temp1);
(*temp1)=(*temp2);
cout << "1";
delete temp2;
}
temp1=temp1->Next;
}
temp=temp->Next;
temp1=First->Next;
for(int k=0;k<i+1;k++)
{
temp1=temp1->Next;
}
}
} else
{
cout << "В предложении нет слов"<<endl;
}
}
Sentence &Sentence::operator = (Sentence &op1)
{
this->DeleteAll();
Word *temp=op1.First;
Word *temp2;
if(op1.First!=NULL && op1.Last!=NULL)
{
if(temp==op1.Last)
{
First = new Word;
(*First)=(*temp);
Last=First;
}else
{
First = new Word;
(*First)=(*temp);
Last=First;
while (temp!=op1.Last)
{
Last->Next = new Word;
(*(Last->Next))=(*(temp->Next));
temp2=Last;
Last=Last->Next;
temp=temp->Next;
Last->Prev=temp2;
}
}
count=op1.count;
int number=1;
temp=First;
while(temp!=Last)
{
temp->position=number;
temp=temp->Next;
number++;
}
temp->position=number;
}
return *this;
}
Sentence &Sentence:: operator + (Sentence &op1)
{
Sentence *loc;
loc=new Sentence;
(*loc)=(*this);
if(op1.First!=NULL && op1.Last!=NULL)
{
(*loc)+=op1;
}
return *loc;
}
Sentence &Sentence:: operator += (Sentence &op1)
{
if(First!=NULL && Last!=NULL)
{
if(op1.First!=NULL && op1.Last!=NULL)
{
Word *temp=op1.First;
while(temp!=op1.Last)
{
Last->Next=new Word;
(*(Last->Next))=(*temp);
Last=Last->Next;
count=count+1;
temp=temp->Next;
}
Last->Next=new Word;
(*(Last->Next))=(*temp);
Last=Last->Next;
count=count+1;
}
} else
{
(*this)=op1;
}
int number=1;
Word *ttmp=First;
while(ttmp!=Last)
{
ttmp->position=number;
ttmp=ttmp->Next;
number++;
}
ttmp->position=number;
return *this;
}
Sentence &Sentence::operator -=(Sentence &op1)
{
if(First!= NULL && Last!=NULL)
{
if(op1.First!=NULL && op1.Last!=NULL)
{
Word *temp=op1.First;
Word *temp1=First;
int flag=0;
while(First!=NULL && temp!=op1.Last)
{
while(First!=NULL && temp1!=Last && flag==0)
{
if((*temp)==(*temp1))
{
this->Delete(temp1);
flag=1;
}
temp1=temp1->Next;
}
if(First!=NULL && (*temp)==(*temp1) && flag==0)
{
this->Delete(temp1);
}
temp=temp->Next;
temp1=First;
flag=0;
}
while(First!=NULL && temp1!=Last && flag==0)
{
if((*temp)==(*temp1))
{
this->Delete(temp1);
flag=1;
}
temp1=temp1->Next;
}
if(First!=NULL && (*temp)==(*temp1) && flag==0)
{
this->Delete(temp1);
}
}
}
int number=1;
Word *ttmp=First;
while(ttmp!=Last)
{
ttmp->position=number;
ttmp=ttmp->Next;
number++;
}
ttmp->position=number;
return *this;
}
Sentence &Sentence:: operator - (Sentence &op1)
{
if(First!=NULL && Last!=NULL)
{
Sentence *loc;
loc=new Sentence;
(*loc)=(*this);
if(op1.First!=NULL && op1.Last!=NULL)
{
(*loc)-=op1;
}
return *loc;
} else return *this;
}
20