Potrebujem poradit, ako do tohoto programu dorobim funkciu na vyhladanie podla evidencneho cisla...nevie to niekto nahodou? Mne to stale nejak nechce ist.
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#define MAX_LENGTH 100
#define COUNT_ITEM 50
struct PARAM
{
char *Meno;
char *Priezvisko;
char *Rocnik;
char *Odbor;
char *Cislo;
};
struct PARAM *g_Param[COUNT_ITEM];
FILE *g_file;
void Vypis( int PocetZ )
{
int i;
for( i = 0; i < PocetZ; i++ )
{
printf( "[%d] %s\t %s\t\t %s %s %s\n", i,
g_Param[i]->Priezvisko,
g_Param[i]->Meno,
g_Param[i]->Rocnik,
g_Param[i]->Odbor,
g_Param[i]->Cislo);
}
}
void FreeMem( int UvolniP )
{
free( g_Param[UvolniP ]->Meno );
free( g_Param[UvolniP ]->Priezvisko );
free( g_Param[UvolniP ]->Rocnik );
free( g_Param[UvolniP ]->Odbor );
free( g_Param[UvolniP ]->Cislo );
free( g_Param[UvolniP ]);
}
void Pridaj( int *pPocetZ )
{
if( *pPocetZ < COUNT_ITEM )
{
g_Param[*pPocetZ] = (struct PARAM *) malloc(sizeof(struct PARAM));
g_Param[*pPocetZ]->Meno = (char*) malloc(MAX_LENGTH);
printf( "Zadaj meno : ");
scanf( "%s", g_Param[*pPocetZ]->Meno );
g_Param[*pPocetZ]->Priezvisko = (char*) malloc(MAX_LENGTH);
printf( "\nZadaj priezvisko : ");
scanf( "%s", g_Param[*pPocetZ]->Priezvisko );
g_Param[*pPocetZ]->Rocnik = (char*) malloc(MAX_LENGTH);
printf( "\nZadaj aktualny rocnik : ");
scanf( "%s", g_Param[*pPocetZ]->Rocnik );
g_Param[*pPocetZ]->Odbor = (char*) malloc(MAX_LENGTH);
printf( "\nZadaj studijny odbor : ");
scanf( "%s", g_Param[*pPocetZ]->Odbor );
g_Param[*pPocetZ]->Cislo = (char*) malloc(MAX_LENGTH);
printf( "\nZadaj evidencne cislo : ");
scanf( "%s", g_Param[*pPocetZ]->Cislo );
(*pPocetZ)++;
}
else
{
printf( "Zoznam je plny" );
}
}
void Zmaz(int *pPocetZ)
{
int i;
int UvolniP ;
Vypis( *pPocetZ );
if( *pPocetZ != 0 )
{
printf( "Zadaj cislo zaznamu ktory chces zmazat : " );
scanf( "%d", &UvolniP );
for( i = UvolniP ; i < ((*pPocetZ)-1); i++ )
{
strcpy( g_Param[i]->Meno,g_Param[i+1]->Meno);
strcpy( g_Param[i]->Priezvisko,g_Param[i+1]->Priezvisko);
strcpy( g_Param[i]->Rocnik,g_Param[i+1]->Rocnik);
strcpy( g_Param[i]->Odbor,g_Param[i+1]->Odbor);
strcpy( g_Param[i]->Cislo,g_Param[i+1]->Cislo);
}
if ((*pPocetZ)-1==0)
{
FreeMem( 0 );
}
else
{
FreeMem( (*pPocetZ)-1 );
}
(*pPocetZ)--;
}
else
{
printf( "!!! Prazdny zoznam !!!\n" );
}
}
void Zotried(int PocetZ)
{
int i;
int j;
struct PARAM *helpParam;
for( i = 0; i < PocetZ-1; i++)
{
for( j = i+1; j < PocetZ; j++ )
{
if( strcmp( g_Param[j]->Priezvisko, g_Param[i]->Priezvisko ) < 0 )
{
helpParam = g_Param[j];
g_Param[j] = g_Param[i];
g_Param[i] = helpParam;
}
}
}
}
void Nacitaj( int *pPocetZ )
{
int nRet;
int i;
for( i = 0; i < *pPocetZ; i++ )
{
FreeMem( i );
}
*pPocetZ = 0;
g_file = fopen( "zoznam studentov.dat", "r" );
do
{
if( *pPocetZ < COUNT_ITEM )
{
g_Param[*pPocetZ] = (struct PARAM *) malloc(sizeof(struct PARAM));
g_Param[*pPocetZ]->Meno = (char*) malloc(MAX_LENGTH);
g_Param[*pPocetZ]->Priezvisko = (char*) malloc(MAX_LENGTH);
g_Param[*pPocetZ]->Rocnik = (char*) malloc(MAX_LENGTH);
g_Param[*pPocetZ]->Odbor = (char*) malloc(MAX_LENGTH);
g_Param[*pPocetZ]->Cislo = (char*) malloc(MAX_LENGTH);
nRet = fscanf( g_file, "%s\t%s\t%s\t%s\t%s\n",
g_Param[*pPocetZ]->Meno,
g_Param[*pPocetZ]->Priezvisko,
g_Param[*pPocetZ]->Rocnik,
g_Param[*pPocetZ]->Odbor,
g_Param[*pPocetZ]->Cislo);
if( nRet != EOF )
{
(*pPocetZ)++;
}
else
{
FreeMem( *pPocetZ );
// (*pPocetZ)--;
}
}
else
{
printf( "Zoznam je plny" );
break;
}
}while( nRet != EOF );
fclose( g_file );
}
void Uloz( int PocetZ )
{
int i;
g_file = fopen( "zoznam studentov.dat", "w" );
if( g_file == NULL )
{
printf("nemozem zapisat");
}
else
{
for( i = 0; i < PocetZ; i++ )
{
fprintf( g_file, "%s\t%s\t%s\t%s\t%s\n",
g_Param[i]->Meno,
g_Param[i]->Priezvisko,
g_Param[i]->Rocnik,
g_Param[i]->Odbor,
g_Param[i]->Cislo);
}
fclose( g_file );
}
}
void main()
{
char CisloO;
int PocetZ;
int i;
PocetZ = 0;
do
{
printf( "\n******************* MENU *******************\n\n" );
printf( "\t[1]. \tPridanie studenta\n");
printf( "\t[2]. \tOdstranenie studenta\n");
printf( "\t[3]. \tUsporiadanie podla priezviska\n");
printf( "\t[4]. \tHladanie podla evidencneho cisla\n");
printf( "\t[5]. \tVypis studentov\n" );
printf( "\t[6]. \tUlozenie do suboru\n");
printf( "\t[7]. \tNacitanie zo suboru\n");
printf( "\t[8]. \tUkoncenie programu\n" );
printf( "\n******************* MENU *******************\n" );
printf( "\nZadaj volbu : " );
scanf( "%s", &CisloO );
printf( "\n" );
switch( CisloO )
{
case '1': Pridaj( &PocetZ );
break;
case '2': Zmaz( &PocetZ );
break;
case '3': Zotried( PocetZ);
break;
case '4': Hladaj( PocetZ );
break;
case '5': Vypis( PocetZ );
break;
case '6': Uloz( PocetZ );
break;
case '7': Nacitaj( &PocetZ );
break;
}
}while( CisloO != '8' );
for( i = 0; i < PocetZ; i++ )
{
FreeMem( i );
}
}