
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

struct predpriqtie{
  char name_otr[20];
  char name[20];
  int num;
  int sluj;
  float prih;
  float dulg;
};

struct predpriqtie pp[100];

struct date{
  int day;
  int month;
  int year;
};

int nLines=0;

//--------------------------------------------

int write_to_file(char *file_name)
{
  FILE *out;
  struct predpriqtie p;
  char ch,tmp[10];
  nLines=0;
   if((out=fopen(file_name,"wb"))==NULL)
   {
     printf("Ne moje da otvori napraveniq fail.\n");
     return 1;
   }
    do{
       printf("\nVuvejdane na danni:\n"); gets(tmp);
       printf("\nOtrasul(maximum 20 simvola):");
       gets(p.name_otr);
       printf("\nIme(maximum 20 simvola):");
       gets(p.name);
       printf("\nNomer:");
       scanf("%d",&p.num);
       printf("\nBroi slujiteli:");
       scanf("%d",&p.sluj);
       printf("\nPrihodi:");
       scanf("%f",&p.prih);
       printf("\nDulgove:");
       scanf("%f",&p.dulg);
        while((getchar())!='\n');
        fwrite(&p,sizeof(p),1,out);
        pp[nLines]=p;
        nLines++;
        printf("Shte vuvejdate li sledvasht element? Y/N=>");
      }while(toupper(getch())=='Y');

    fclose(out);
    printf("\n Broi na zapisanite: %d",nLines);
 return 0;
}

//------------------------------------------------

int read_file(char *file_name)
{
 struct predpriqtie p;
 int i,j,num,sluj;
 FILE *fp;
 char str[20];
 char name[20];
 float prih,dulg;

 if((fp=fopen(file_name,"rb"))==NULL)
   {
      printf("Ne moje da otvori faila.\n");
      return 1;
   }

   i=0; nLines=0;
     while(fread(&p,sizeof(p),1,fp)==1)
     {
      pp[i]=p;
      i++; nLines++;
     }
 
   fclose(fp);

   for(j=0;j<=nLines;j++)
   {
     for(i=1;i<nLines;i++)
     {
       if(pp[i-1].num>pp[i].num)
       {
		 num=pp[i-1].num;
         pp[i-1].num=pp[i].num;
         pp[i].num=num;

         strcpy(str,pp[i-1].name_otr);
         strcpy(pp[i-1].name_otr,pp[i].name_otr);
         strcpy(pp[i].name_otr,str);

         strcpy(name,pp[i-1].name);
         strcpy(pp[i-1].name,pp[i].name);
         strcpy(pp[i].name,name);

         sluj=pp[i-1].sluj;
         pp[i-1].sluj=pp[i].sluj;
         pp[i].sluj=sluj;
       
	     prih=pp[i-1].prih;
         pp[i-1].prih=pp[i].prih;
         pp[i].prih=prih;
	  
		 dulg=pp[i-1].dulg;
         pp[i-1].dulg=pp[i].dulg;
         pp[i].dulg=dulg;
	    
	   }
     }
   }
  
 for(i=0;i<nLines;i++)
 {
	 printf("\nNomer:%d Otrasul:%s Ime:%s Slujiteli:%d Prihodi:%.2f Dulgove:%.2f\n",pp[i].num,pp[i].name_otr,pp[i].name,pp[i].sluj,pp[i].prih,pp[i].dulg);
 }

 if((fp=fopen(file_name,"wb"))==NULL)
   {
     printf("Ne moje da otvori faila.\n");
     return 1;
   }

 for(i=0;i<nLines;i++)
 {
   p=pp[i];
   fwrite(&p,sizeof(p),1,fp);
 }

 fclose(fp);
 return 0;
}

//---------------------------------------------------

int sort_file(char *file_name)
{
 struct predpriqtie p;
 int i,j,num,sluj;
 FILE *fp;
 char str[20];
 char name[20];
 float prih,dulg;

 if((fp=fopen(file_name,"rb"))==NULL)
   {
      printf("Ne moje da otvori faila.\n");
      return 1;
   }

   i=0; nLines=0;
     while(fread(&p,sizeof(p),1,fp)==1)
     {
      pp[i]=p;
      i++; nLines++;
     }
 
   fclose(fp);

   for(j=0;j<=nLines;j++)
   {
     for(i=1;i<nLines;i++)
     {
       if(strcmp(pp[i-1].name_otr,pp[i].name_otr)>0)
       {

         strcpy(str,pp[i-1].name_otr);
         strcpy(pp[i-1].name_otr,pp[i].name_otr);
         strcpy(pp[i].name_otr,str);

         strcpy(name,pp[i-1].name);
         strcpy(pp[i-1].name,pp[i].name);
         strcpy(pp[i].name,name);

         num=pp[i-1].num;
         pp[i-1].num=pp[i].num;
         pp[i].num=num;

         sluj=pp[i-1].sluj;
         pp[i-1].sluj=pp[i].sluj;
         pp[i].sluj=sluj;
       
	     prih=pp[i-1].prih;
         pp[i-1].prih=pp[i].prih;
         pp[i].prih=prih;
	  
		 dulg=pp[i-1].dulg;
         pp[i-1].dulg=pp[i].dulg;
         pp[i].dulg=dulg;
	    
	   }
     }
   }
  
 for(i=0;i<nLines;i++)
 {
	 printf("\nOtrasul:%s Ime:%s Nomer:%d Slujiteli:%d Prihodi:%.2f Dulgove:%.2f\n",pp[i].name_otr,pp[i].name,pp[i].num,pp[i].sluj,pp[i].prih,pp[i].dulg);
 }

 if((fp=fopen(file_name,"wb"))==NULL)
   {
     printf("Ne moje da otvori faila.\n");
     return 1;
   }

 for(i=0;i<nLines;i++)
 {
   p=pp[i];
   fwrite(&p,sizeof(p),1,fp);
 }

 fclose(fp);
 return 0;
}

//-------------------------------------------------------------

int del_predpr(char *file_name,float dulg)
{
 FILE *fp;
 struct predpriqtie p;
 int i;

 if((fp=fopen(file_name,"rb"))==NULL)
 {
   printf("Ne moje da otvori faila.\n");
   return 1;
 }

  i=0; nLines=0;
  while(fread(&p,sizeof(p),1,fp)==1)
  {
    if(p.dulg<dulg)
    {
      pp[i]=p;
      nLines++; i++;
    }
    else 
    {
		printf("\nTova predpriqtie trqbva da bude iztrito:\nOtrasul:%s Ime:%s Nomer:%d Slujiteli:%d Prihodi:%.2f Dulgove:%.2f\n",p.name_otr,p.name,p.num,p.sluj,p.prih,p.dulg);
    }
  }
 fclose(fp);
 if(nLines>0)
 {
   if((fp=fopen(file_name,"wb"))==NULL)
   {
     printf("Ne moje da otvori faila.\n");
     return 1;
   }

   for(i=0;i<nLines;i++)
   {
     p=pp[i];
     fwrite(&p,sizeof(p),1,fp);
   }
   fclose(fp);
 }
 return 0;
}

//---------------------------------------------------------

int main(int argc,char* argv[])
{
 struct date d;
 int izbor;
 FILE *fp;
 char file_name[30];
 float dulg_min=0.0;

 printf("\nVuvedi data:\n");
do{
 printf("\nDen:");
 scanf("%2d",&d.day);
}while(d.day<1||d.day>31);
do{
 printf("\nMesec:");
 scanf("%2d",&d.month);
}while(d.month<1||d.month>12);
do{
 printf("\nGodina:");
 scanf("%4d",&d.year);
}while(d.year<0||d.year>2008);
 printf("\nVie vuvedohte data: %2d-%2d-%4d\n",d.day,d.month,d.year);

 printf("Vuvedete imeto na faila:");
 scanf("%s",file_name);

do {
     printf("\n\n1. Vuvejdane na predpriqtie\n\n");
     printf("2. Pokazvane spisuk na predpriqtiqta\n\n");
     printf("3. Pozazvane spisuk na predpriqtiqta podredeni po otrasul\n\n");
     printf("4. Iztrivane na predpriqtiqta, koito sa s dulgove po golemi ot dopustimite\n\n");
     printf("Napravete svoq izbor...\n\n");
     scanf("%d",&izbor);

  switch(izbor)
  {
   case 1:write_to_file(file_name); break;
   case 2:read_file(file_name); break;
   case 3:sort_file(file_name); break;
   case 4:
      printf("\nVuvedete maximalni dulgove:");
      scanf("%f",&dulg_min);
      del_predpr(file_name,dulg_min);
      break;
   default:printf("Bye, bye...!!\n"); break;
  }
 }while(izbor!=7);

 return 0;
}  