#include<conio.h>
#include<math.h>
#include<stdio.h>
#include<string.h>
#define N 10
struct type { char num[7]; char marka[10]; char Fio[20];};
struct table { type elem[N]; int n;}*t;
struct type2 { char num[7];};
struct table2 { type2 elem[N]; int n;};
//void heapsort(int l, int r)
//{ int m=(l+r)/2,i=l,j=r;
// char x[20];
// type d;
// strcpy(x,t->elem[m].num);
// do
// { while(strcmp(t->elem[i].num,x)<0) i++;
// while(strcmp(t->elem[j].num,x)>0) j--;
// if(i<=j)
// { d=t->elem[i];
// t->elem[i]=t->elem[j];
// t->elem[j]=d;}
// i++; j--;}
// while(i<=j);
// if(l<j) heapsort(l,j);
// if(i<r) heapsort(i,r);
//}
void create_tree(int i, int j)
{int max=i;
int b,l,r;
type x;
do {b=max; l=2*max+1; r=2*(max+1);
if(l<=j) if(strcmp(t->elem[l].num,t->elem[max].num)>0) max=l;
if(r<=j) if(strcmp(t->elem[r].num,t->elem[max].num)>0) max=r;
x=t->elem[b];
t->elem[b]=t->elem[max];
t->elem[max]=x;
}
while(b!=max);
}
void sorting(table *t, int n)
{type x;
int k;
for(k=(n/2);k>=0; k--)
create_tree(k,n-1);
for(k=n-1;k>=1;k--)
{
x=t->elem[0]; t->elem[0]=t->elem[k]; t->elem[k]=x;
create_tree(0,k);
}
}
void main()
{
table *d;
table2 *t2;
type *el;
type2 *elem;
int i,k,p;
FILE *fp;
FILE *f;
FILE *out;
FILE *sort;
t=new table;
d=new table;
t2=new table2;
t->n=0; t2->n=0; d->n=0;
fp=fopen("avto_base.txt","r");
for(i=0;!feof(fp);i++)
{ if (t->n<N)
{ el=new type;
fscanf(fp,"%s %s %s",el->num, el->marka, el->Fio);
t->elem[i]=*el;
t->n++; }
else { printf("error"); return ; }}
t->n--;
fclose(fp);
f=fopen("crash_car.txt","r");
for(i=0;!feof(f);i++)
{ if(t2->n<N)
{ elem=new type2;
fscanf(f,"%s", elem->num);
t2->elem[i]=*elem;
t2->n++;}
else { printf("error"); return ; }}
t2->n--;
fclose(f);
//heapsort(0,t->n);
sorting(t, t->n);
fopen_s ( &sort, "sort_car.txt", "w" );
for(i=0;i<=t->n;i++)
fprintf_s(sort,"%s %s %s\n", t->elem[i].num, t->elem[i].marka, t->elem[i].Fio);
fopen_s ( &out, "output.txt", "w" );
for(p=0;p<=t2->n;p++)
{
for(k=0;k<=t->n;k++)
{
if(strcmp(t2->elem[p].num,t->elem[k].num)==0) {fprintf_s(out,"%s \n", t->elem[k].Fio);}
}
}
getchar();
}