Программирование на С++Рефераты >> Программирование и компьютеры >> Программирование на С++
cout<<endl<<"Total elements: "<<ElementQuantity<<endl;
PList=BegP;
int i=1;
do
{cout<<endl<<" "<<i<<" element: "<<endl;
GoThroughTree(PList->Root);
cout<<endl;
i++;
PList=PList->LNext;}
while (PList!=BegP);
return 0;
}
void TreeWork::GoThroughTree(Tree *L)
{Tree *PL=L, *PL1;
if (PL->LP!=NULL)
{PL1=PL;
PL=PL->LP;
cout<<"("<<PL1->Body<<","<<PL->Body<<") ";
GoThroughTree(PL);}
if (PL->RP!=NULL)
{PL1=PL;
PL=PL->RP;
cout<<"("<<PL1->Body<<","<<PL->Body<<") ";
GoThroughTree(PL);}
}
void TreeWork::Erase(Tree *L)
{Tree *PL=L;
if (PL->LP!=NULL)
{PL=PL->LP;
Erase(PL);}
if (PL->RP!=NULL)
{PL=PL->RP;
Erase(PL);}
PL->LP=NULL;
PL->RP=NULL;
}
void TreeWork::EraseList()
{if (BegP!=NULL)
{do
{List *PList1=BegP->LNext;
PList=PList1->LNext;
BegP->LNext=PList;
PList->LPrev=BegP;
Erase(PList1->Root);
delete [] PList1;
}
while (PList!=BegP);
BegP=NULL; PList=NULL;
}
}
int TreeWork::DeleteElement()
{cout<<endl<<"Input number of element: ";
int Number=0;
cin>>Number;
if (Number>ElementQuantity || Number<0)
{cout<<endl<<"Error!"; return -1;}
Number--;
PList=BegP;
for (int i=0; i<Number; i++)
PList=PList->LNext;
List *PList1=PList->LNext, *PList2=PList->LPrev;
if (PList==BegP)
{PList1->LPrev=PList2;
PList2->LNext=PList1;
PList->LNext=NULL;
PList->LPrev=NULL;
delete [] PList;
BegP=PList1;
PList1=NULL; PList2=NULL;}
else
{PList1->LPrev=PList2;
PList2->LNext=PList1;
PList->LNext=NULL;
PList->LPrev=NULL;
delete [] PList;
PList1=NULL; PList2=NULL;}
ElementQuantity--;
return 0;
}
int TreeWork::FindElement()
{cout<<endl<<"Input number, you want to find: ";
int Number=0;
cin>>Number;
PList=BegP;
do
{Tree *PT=PList->Root;
if (Number>PT->Body && Number<PT->Body+10)
{cout<<endl<<"Element with this number: "<<endl;
GoThroughTree(PList->Root);
PList=NULL; cout<<endl; return 0;}
PList=PList->LNext;
}
while (PList!=BegP);
cout<<endl<<"There aren't such number in list!"<<endl;
PList=NULL;
return -1;
}
int TreeWork::SortByIncrease()
{
if(BegP==NULL) {cout<<endl<<"Error!"<<endl; return -1;}
List *PList1=BegP; PList=BegP;
do
{do
{if (PList1->Root->Body>PList->Root->Body)
{Tree *PT;
PT=PList1->Root;
PList1->Root=PList->Root;
PList->Root=PT;
PT=NULL;}
PList1=PList1->LNext;
}
while (PList1!=BegP);
PList=PList->LNext;
}
while (PList!=BegP);
return 0;
}
int TreeWork::SortByDecrease()
{
if(BegP==NULL) {cout<<endl<<"Error!"<<endl; return -1;}
List *PList1=BegP; PList=BegP;
do
{do
{if (PList1->Root->Body<PList->Root->Body)
{Tree *PT;
PT=PList1->Root;
PList1->Root=PList->Root;
PList->Root=PT;
PT=NULL;}
PList1=PList1->LNext;
}
while (PList1!=BegP);
PList=PList->LNext;
}
while (PList!=BegP);
return 0;
}
int TreeWork::SaveList()
{if (BegP==NULL)
{cout<<endl<<"The list is empty!"<<endl; return -1;}
ofstream F;
char *FileName=new char[25];
cout<<endl<<"Input file name: "; cin>>FileName;
F.open(FileName);
PList=BegP;
do
{i=0;
Mass=PList->Root->Body;
PList=PList->LNext;
if (PList!=BegP)
F<<Mass<<endl;
else
F<<Mass;
}
while (PList!=BegP);
F.close();
delete [] FileName;
return 0;
}
int TreeWork::OpenList()
{if (BegP!=NULL)
{cout<<endl<<"The list is alredy exist!"<<endl; return -1;}
cout<<endl<<"Input file name: ";
char *FileName=new char[25];
cin>>FileName;
ifstream f;
ElementQuantity=0;
f.open(FileName);
char Next;
Next=f.peek();
while (Next!=EOF)
{
f>>Mass;
PList=new List(Mass/10);
if (BegP==NULL)
{BegP=PList;
BegP->LNext=BegP;
BegP->LPrev=BegP;
PList=NULL;}
else
{List *PList1=BegP->LPrev;
if (PList1==BegP)
{BegP->LNext=PList;
BegP->LPrev=PList;
PList->LNext=BegP;
PList->LPrev=BegP;
PList=NULL; PList1=NULL;}
else
{BegP->LPrev=PList;
PList1->LNext=PList;
PList->LNext=BegP;
PList->LPrev=PList1;
PList=NULL; PList1=NULL;}
}
Next=f.peek();
ElementQuantity++;
}
f.close();
delete [] FileName;
return 0;
}
TreeWork TW;
void main()
{TW.TreeWorkStart();}
Результаты работы программы.
Начало работы:
Main Menu: 1. Make New List. 2. Add Element. 3. Print List. 4. Delete Element. 5. Save List. 6. Erase List. 7. Open File. 8. Find Element. 9. Sort List. 0. Exit. Your choice : |
Для создания списка выбираем пункт 1:
Main Menu: 1. Make New List. 2. Add Element. 3. Print List. 4. Delete Element. 5. Save List. 6. Erase List. 7. Open File. 8. Find Element. 9. Sort List. 0. Exit. Your choice : 1 Input kol-vo of elements: |
Вводим количество элементов в списке (предположим 4):
Main Menu: 1. Make New List. 2. Add Element. 3. Print List. 4. Delete Element. 5. Save List. 6. Erase List. 7. Open File. 8. Find Element. 9. Sort List. 0. Exit. Your choice : 1 Input kol-vo of elements: 4 Input digit: |
Успешное завершение ввода списка:
Input kol-vo of elements: 4 Input digit: 1 Input digit: 2 Input digit: 3 Input digit: 4 Main Menu: 1. Make New List. 2. Add Element. 3. Print List. 4. Delete Element. 5. Save List. 6. Erase List. 7. Open File. 8. Find Element. 9. Sort List. 0. Exit. Your choice : |