Скачиваний:
0
Добавлен:
01.04.2014
Размер:
8.27 Кб
Скачать
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

void init_arr_1(void);
void init_arr_2(int *a1);
void swap(int *x,int *y);
void bubble_sort(int *a1,int count); //функция пузырьковой сортировки
void do_bubble_sort(int *a1); 
void min_sort(int *a1,int count); //функция сортировки методом выбора
void do_min_sort(int *a1); 
void insert_sort(int *a1,int count); //функция сортировки методом вставки
void do_insert_sort(int *a1); 
void shell_sort(int *x,int count); //функция сортировки методом Шелла
void do_shell_sort(int *a1); 
void hoare_sort(int *x,int l,int r);
void do_hoare_sort(int *a1); 
void vvod(int *a1,int count); //функция ввода значений массива
void out(int *a1,int count); //функция вывода значений массива
void file_write(int *a1,int count);
void poisk(void);
void lin_poisk(int *a1,char ch,int v,char t);
char do_bin_poisk(void);
void bin_poisk(int *a1,char ch,int v,char t);
void do_lin_poisk(void);
void arr_copy(int *a1,int *a0, int n);
FILE *file;
int i,j;
int *a,*b,*c,*d,*e,n;
int *a0,*b0,*c0,*d0,*e0;
int h[7];
int choice;
char init=0;
char sort1,sort2,sort3,sort4,sort5;

void main(void)
{
  file=fopen("result_sort.txt","w");
  fprintf(file,"\t\tRESULTS OF SORTS AND SERACHES\n");
  init=0;
m3:
		do  
	{   cout << "(1) Initilization of arrays  \n";
        cout << "(2) Result of bubble sort.\n";
        cout << "(3) Result of min sort.\n"; 
        cout << "(4) Result of insert sort.\n";
        cout << "(5) Result of Shell sort.\n";
		cout << "(6) Result of Hoor sort.\n";
		cout << "(7) Poisk .\n";
		cout << "(8) Exit programm.\n";
        cout << "Enter your choice: ";
 m6:
		cin >> choice;
if (init==0)
switch(choice)
{
 case 1: goto m1;
 case 8:goto m2;
 default: cout <<"Viberite 1 ili 8\n";goto m6;
}


switch(choice) 
 {    
case 1: goto m1;
case 2:do_bubble_sort(a);break;
case 3:do_min_sort(b);break;
case 4:do_insert_sort(c);break;
case 5:do_shell_sort(d);break;
case 6:do_hoare_sort(e);break;
case 7: poisk();goto m3;
case 8:goto m2; 
default: cout <<"Input from 1-8! \n";
 }

   }
while(choice !=8 );

m1:
 
 	init_arr_1();	 
    h[2]=n;
	a=new int[n];
	a0=new int[n];
	init_arr_2(a);
	arr_copy(a,a0,n);
	
	init_arr_1();
	h[3]=n;
	b=new int[n];
	b0=new int[n];
	init_arr_2(b);
	arr_copy(b,b0,n);

	init_arr_1();
 	h[4]=n;
	c=new int[n];
	c0=new int[n];
	init_arr_2(c);
	arr_copy(c,c0,n);

	init_arr_1();
	h[5]=n;
	d=new int[n];
	d0=new int[n];
	init_arr_2(d);
	arr_copy(d,d0,n);
	
	init_arr_1();
	h[6]=n;
	e=new int[n];
	e0=new int[n];
	init_arr_2(e);
	arr_copy(e,e0,n);
	
	init++;

goto m3;


m2:
		fclose(file);
        delete []a;
		delete []b;
		delete []c;
		delete []d;
		delete []e;

	puts("Press any key");
	getch();
	}

void arr_copy(int *a1,int *a0, int n)
{int i;
	for (i=0;i<n;i++)
	*(a0+i)=*(a1+i);
}

void poisk(void)
{	int choice;
	do  
	{   cout << "(1) Lineiniy poisk. \n";
        cout << "(2) Binarniy poisk. \n";
		cout << "(8) Exit to main menu.\n";
               cout << "Enter your choice: ";
        cin >> choice;

switch(choice) 
 {    
case 1:  do_lin_poisk();	break; 
case 2: do_bin_poisk();  break;
case 8: break; 
default: cout <<"Input from 1,2,8! \n";
 }

   }
while(choice !=8 );
}

void do_lin_poisk(void)
{int v;
	puts("vvedite element dlja poiska");
    scanf(" %d",&v);
	fprintf(file,"\n Lineiniy poisk. \n");
	lin_poisk(a0,'a',v,2);lin_poisk(b0,'b',v,3);
	lin_poisk(c0,'c',v,4);lin_poisk(d0,'d',v,5);
	lin_poisk(e0,'e',v,6);
}
void lin_poisk(int *a1,char ch,int v,char t)
{
	int count;
    i=0;count =0;
	  while(i<h[t])
	   {   	     if (*(a1+i)==v)     
		   {		 
		   count++;
		   }
            i++;
	   }
	  if(count==0){ printf("V ishodnom massive %c  element %d ne naiden \n",ch,v);
					fprintf(file,"V ishodnom massive %c  element %d ne naiden \n",ch,v);}

	  else   {printf("V ishodnom  massive %c  sovpadenii -  %d \n",ch,count);
			   fprintf(file,"V ishodnom  massive %c  sovpadenii -  %d \n",ch,count);}

}
char do_bin_poisk(void)
{int v;
	if (sort1*sort2*sort3*sort4*sort5==0)
		{puts("minimum odin iz massivov ne otsortirovan");return 0;}
		puts("vvedite element dlja poiska");
		scanf(" %d",&v);
		fprintf(file,"\n Binarniy poisk. \n");    
		bin_poisk(a,'a',v,2);bin_poisk(b,'b',v,3);
		bin_poisk(c,'c',v,4);bin_poisk(d,'d',v,5);
		bin_poisk(e,'e',v,6);
		return 1;
}
void bin_poisk(int *a1,char ch,int v,char t)
{  int m,count;
 count=i=0;   j=h[t];   m=j/2;
     while (*(a1+m)!=v) 
     	 {  count++;
        if (*(a1+m)<v)  i+=m;
       else j=m-i;
        m=(i+j)/2;
		if (m<0) m=0;
		if (m>=h[t]) m=h[t]-1;
		if (count>2*h[t]) break;
		     }
	 if (*(a1+m)==v) {printf("V otsortirovannom massive %c element %d naiden na  %d pozicii \n",ch,v,m);
	 fprintf(file,"V otsortirovannom massive %c element %d naiden na  %d pozicii \n",ch,v,m);}
	 else {printf("V otsortirovannom  massive %c element %d ne naiden \n",ch,v);
	       fprintf(file,"V otsortirovannom massive %c element %d ne naiden \n",ch,v);}
	}

void init_arr_1(void)
{puts("Enter length of array:");
	scanf("%d",&n);
}

void init_arr_2(int *a1)
{puts("Enter elements of array");
 vvod(a1,n);
 }


void swap(int *x,int *y)
{
	int t;
	t=*x;
	*x=*y;
	*y=t;
}
void vvod(int *a1,int count)
{
	for(i=0;i<count;i++)
	cin>>*(a1+i);
}
void out(int *a1,int count)
{
	for(i=0;i<count;i++)
        cout<<*(a1+i)<<" ";
	cout<<endl;
}



void bubble_sort(int *a1,int count)
{
for (i=0; i<count; i++)
        for (j=count-1; j>i; j--)
        if (*(a1+j-1) > *(a1+j)) 
        swap((a1+j-1),(a1+j)); /*поменять их местами */
}
void do_bubble_sort(int *a1)
{int t=h[2];
puts("Sorted array by method \\Bubble Sort\\:");
bubble_sort(a1,t);
	out(a1,t);
	fprintf(file,"\n Bubble Sort\n");
	file_write(a1,t);
	sort1++;
}
void min_sort(int *a1,int count)
{
        int k;
       for (i=0; i<count-1; i++)
  {
     for (k=i, j=i+1; j<count; j++) // находим в цикле  
        if (*(a1+j)<*(a1+k))
	               // минимальный элемент   
          k=j; 			// запоминаем его номер в к  
          swap((a1+k),(a1+i));
	
		
  }                  
        
} 
void do_min_sort(int *a1) 
{ int t=h[3];
	puts("Sorted array by method \\Min element\\:");
		min_sort(a1,t);
    out(a1,t);
	fprintf(file,"\n Min Sort\n");
	file_write(a1,t);
	sort2++;
}

void insert_sort(int *a1,int count)
{
	for (i=1; i<=count-1; i++)
	{
        j=i;
        while (*(a1+j)<*(a1+j-1) && j>=1) 
		{
               swap((a1+j),(a1+j-1));
               j--;
		}
	}
}


void do_insert_sort(int *a1) 
{ int t=h[4];
	puts("Sorted array by method \\Insert Sort\\:");
	insert_sort(a1,t);
	out(a1,t);
	fprintf(file,"\n Insert Sort\n");
	file_write(a1,t);
	sort3++;
}
void shell_sort(int *x,int count)
{
     int i,j;     //две переменные цикла         
     int gap;     //шаг сортировки     
     int sorted;  //флаг окончания этапа сортировки
     for(gap=count/2;gap>0;gap/=2)//начало сортировки
     do
          {
             sorted = 0;
             for(i=0,j=gap;j<count;i++,j++)
               if(*(x+i)>*(x+j))
                {
                  swap((x+i),(x+j));
     	sorted = 1;
                } 
          }while(sorted);
}



void do_shell_sort(int *a1) 
{ int t=h[5];
 puts("Sorted array by method \\Shell Sort\\:");
	shell_sort(a1,t);
	out(a1,t);
	fprintf(file,"\n Shell Sort\n");
	file_write(a1,t);
	sort4++;
}

void hoare_sort(int *x,int l,int r)
{
	int sr=*(x+(l+r)/2);
	i=l;
	j=r;
	do
	{
		while(*(x+i)<sr) i++;
		while(*(x+j)>sr) j--;
		if(i<=j)
		{
			swap((x+i),(x+j));
			i++;
                        j--;  
		}
	}while(i<=j);
	if(i<r)
		hoare_sort(x,i,r);
	if(j>l)
		hoare_sort(x,l,j);
}


void do_hoare_sort(int *a1) 
{int t=h[6];
	puts("Sorted array by method \\Hoare Sort\\: \n");
	hoare_sort(a1,0,t-1);
	out(a1,t);
	fprintf(file,"\n Hoare Sort\n");
	file_write(a1,t);
	sort5++;
}
void file_write(int *a1,int count)
{	
	for(i=0;i<count;i++)
		fprintf(file,"%d ",*(a1+i));
		
}
Соседние файлы в папке В-15 Лабораторная работа ИВТ-сдана