Сортировка массивов методом вставокРефераты >> Программирование и компьютеры >> Сортировка массивов методом вставок
strcpy(fname,argv[1]);//êàê íàçâàíèå äëÿ ôàéëà îò÷åòà.
else
strcpy(fname,"Log.txt");
if (argc>2)
strcpy(foname,argv[2]);//Âòîðîé àðãóìåíò - äëÿ ÷òåíèÿ.
f.open(fname);//Ñîçäàíèå è ïîäãîòîâêà ôàéëà ê çàïèñè.
do{//Âûïîëíÿòü ïîêà bool=0.
bool=menu();//Áàçîâàÿ ôóíêöèÿ ïðîãðàììû.
}while (bool==0);
f.close();//Çàêðûòèå ôàéëà è çàïèñü íà ÆÄ.
cout<<"\n\n\n\n\n\n\n\n\n\n";
cout<<"InsetSort. v 0.3 (C) 2003-2004 Created by Chaplygin Vasilij.";
cin.get();
cin.get();
return 0;
}
////////////////////MENU////////////////////
int menu(){
cout<<endl<<" Main Menu:"<<endl;
cout<<" 1. Make New List." <<endl;
cout<<" 2. Add Element." <<endl;
cout<<" 3. Print List." <<endl;
cout<<" 4. Delete Element."<<endl;
cout<<" 5. Save List." <<endl;
cout<<" 6. Erase List." <<endl;
cout<<" 7. Open File." <<endl;
cout<<" 8. Find Element." <<endl;
cout<<" 9. Sort List." <<endl;
cout<<" 0. Exit." <<endl;
cout<<endl<<"Your choice : ";
int i;
do
{cin>>i;
if (i<0 || i>9) cout<<endl<<"Error! Try again : ";
}
while (i<0 || i>9);
switch (i)
{case 1 : MakeNewList(); break; //Make New List
case 2 : AddElements(); break; //Add Element
case 3 : PrintList(); break; //Print List
case 4 : DeleteElement(); break; //Delete Element
case 5 : SaveList(); break; //Save List
case 6 : n=0; break; //Erase List
case 7 : OpenList(); break; //Open File
case 8 : FindElement(); break; //Find Element
case 9 : SubMenu(); break; //Sort List
case 0 : return -1; //Exit
}
return 0;
}//menu
----------------------------------------------------------------- func.cpp
#include "func.h"
extern int *MasP[100],n,argcGlobal;
extern ofstream f;
const int N=100;
//////////////////MakeNewList//////////////////
void MakeNewList(){
if (n!=0) {cout<<endl<<"Error! You have existing list.";
cout<<endl<<"Erase your prvious list ang try again."<<endl;}
else {cout<<endl<<"Input quantity of elements: ";
do{
cin>>n;
if ((n<1)||n>N){
cout<<endl<<"The quantity is incorrect!"<<endl;
cout<<"Max quantity of elemets: "<<N<<endl;
cout<<"Try again: ";}
}while ((n<1)||(n>N));
srand(time(NULL));
for (int i=0; i<n; i++){
MasP[i]=new int;
*MasP[i]=rand()%100;}
}
}
//////////////////AddElements///////////////////
void AddElements(){
cout<<endl<<"Input quantity of elements: ";
int p;
do{
cin>>p;
if ((p<1)||((n+p)>N)){
cout<<endl<<"The quantity is incorrect!"<<endl;
cout<<"You have "<<N-n<<" free cells."<<endl;
cout<<"Try again: ";}
}while ((p<1)||((n+p)>N));
srand(time(NULL));
for (int i=n; i<(n+p); i++){
MasP[i]=new int;
*MasP[i]=rand()%100;}
n=n+p;
}
////////////////////PrintList///////////////////
void PrintList(){
if (n==0) cout<<endl<<"List is empty."<<endl;
else{
cout<<endl;
for(int i=0; i<n; i++){
if (i%10==0) cout<<endl;
cout<<setw(3)<<*MasP[i];}
cout<<endl;
}
}
////////////////DeleteElement///////////////////
void DeleteElement(){
if (n==0) cout<<endl<<"List is empty."<<endl;
else{
cout<<endl<<"Input number of element: ";
int p;
do{
cin>>p;
if ((p<0)||(p>n)) cout<<endl<<"Error! Try again: ";}
while ((p<0)||(p>n));
cout<<endl;
for (int j=(p-1); j<n; j++)
MasP[j]=MasP[j+1];
MasP[n]=0;
n--;
}
}
////////////////////Save List/////////////////////
void SaveList(){
if (n==0) cout<<endl<<"List is empty."<<endl;
else{
for (int i=0; i<n; i++){
if (i%10==0) f<<endl;
f<<setw(3)<<*MasP[i];}
f<<endl;
}
}
///////////////////FindElement////////////////////
void FindElement(){
if (n==0) cout<<endl<<"List is empty."<<endl;
else{
cout<<endl<<"Input the value, which must be finded: ";
int a,s=0; cin>>a;
for (int i=0; i<n; i++){
if (*MasP[i]==a) {
cout<<endl<<(i+1)<<"-th element"<<" - "<<*MasP[i];
s=i;}}
if (s==0) cout<<endl<<"The existing list hasn't element with this value";
cout<<endl;
}
}
//////////////////SubWork(Sort)///////////////////
void SubMenu(){
if (n==0) cout<<endl<<"List is empty."<<endl;
else{
cout<<endl<<" Sub Menu:"<<endl;
cout<<" 1. Sort list by increase."<<endl;
cout<<" 2. Sort list by decrease."<<endl<<endl;
int i;
cout<<"Your choice: ";
do {
cin>>i;
if (i<1 || i>2) cout<<endl<<"Error! Try again : "<<endl;
}while (i<1 || i>2);
switch (i)
{case 1 : SortByIncrease(); break; //Increase
case 2 : SortByDecrease(); break; //Decrease
}
}
}
/////////////////SortByIncrease//////////////////
void SortByIncrease(){
int buf;