Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct celula
- {
- void* info;
- struct celula *urm;
- }TCelula,*TLista,**ALista;
- typedef struct stiva
- {
- size_t dime;
- TLista vf;
- }*TStiva;
- typedef struct sist
- {
- TStiva A;
- TStiva B;
- TStiva C;
- char* culoare;
- }*SistHanoi;
- typedef struct disc
- {
- int diam;
- }*Disc;
- TStiva aloca_stiva( size_t d)
- {
- TStiva s = (TStiva) malloc ( sizeof( struct stiva ));
- if(!s)
- return NULL;
- s->dime = d;
- s->vf = NULL;
- return s;
- }
- void elibereaza_stiva( TStiva s , TLista *l)
- {
- TLista aux;
- while( *l )
- {
- aux = *l ;
- *l = aux->urm;
- free( aux->info );
- free(aux);
- }
- free(s);
- }
- TLista alocarecelula(void* x,size_t d )
- {
- TLista aux = (TLista)malloc(sizeof(TCelula));
- if(!aux)
- return NULL;
- aux->info = malloc(sizeof(int));
- if(!aux->info)
- {
- free(aux);
- return NULL;
- }
- memcpy(aux->info,x,d);
- aux->urm = NULL;
- return aux;
- }
- int insSt(TStiva s,void* x,size_t d)
- {
- TLista aux = alocarecelula(x,d);
- if(!aux)
- return 0;
- aux->urm = s->vf;
- s->vf = aux;
- return 1;
- }
- void Rastoarna( TStiva a , TStiva b)
- {
- while(b->vf)
- {
- TLista p = b->vf;
- b->vf=b->vf->urm;
- p->urm = a->vf;
- a->vf = p;
- }
- }
- SistHanoi aloca_sistem(char* culoare)
- {
- SistHanoi H = malloc( sizeof (struct sist) );
- if( !H )
- return NULL;
- H->A = aloca_stiva( sizeof( int ) );
- if( !H->A )
- return NULL;
- H->B = aloca_stiva( sizeof (int) );
- if( !H->B )
- {
- elibereaza_stiva( H->A , &(H->A->vf) );
- return NULL;
- }
- H->C = aloca_stiva( sizeof ( int ));
- if( !H->C )
- {
- elibereaza_stiva( H->B , &(H->B->vf) );
- elibereaza_stiva( H->A , &(H->A->vf) );
- return NULL;
- }
- H->culoare = malloc (30);
- if( !H->culoare )
- {
- elibereaza_stiva( H->C , &(H->C->vf) );
- elibereaza_stiva( H->B , &(H->B->vf) );
- elibereaza_stiva( H->A , &(H->A->vf) );
- return NULL;
- }
- strcpy( H->culoare , culoare );
- return H;
- }
- int add(TLista *s, char *color_d, size_t diametru_d)
- { printf("$");
- TLista p=*s,u=NULL;
- while(p!=NULL)
- {
- if( strcmp(((SistHanoi)(p->info))-> culoare,color_d)==0)//cautam sistemul de culoare color_d
- break;
- u=p;
- p=p->urm;
- }
- if(p==NULL) // daca nu exista sistemul de culoarea color_d
- {
- SistHanoi sist_nou=aloca_sistem(color_d);//alocam un sistem nou
- if(!sist_nou)
- return 0;
- Disc d_nou=malloc(sizeof(struct disc));//alocam un disc nou
- if(!d_nou)
- {
- free(sist_nou);
- return 0;
- }
- d_nou->diam=diametru_d;
- insSt(sist_nou->A,(void*)d_nou,sizeof(int));//punem noul disc pe stiva A a noului sistem
- TLista aux=alocarecelula((void*)sist_nou,sizeof(struct sist));
- if(!aux)
- {
- return 0;
- }
- if(u==NULL) //daca lista de sisteme este goala
- *s=aux;
- else
- u->urm=aux;//legam noul sistem la lista de sisteme
- free(d_nou);
- free(sist_nou);
- return 1;
- }
- SistHanoi sist=((SistHanoi)(p->info));
- Disc d=malloc(sizeof(struct disc));//alocam un nou disc pentru sistemul de culoare color_d
- if(!d)
- return 0;
- d->diam=diametru_d;
- TStiva aux = aloca_stiva( sizeof(int));
- if(!aux)
- {
- free(d);
- return 0;
- }
- //tratam cazul in care discul are diametru mai mare decat varful
- while( sist-> A ->vf != NULL && ((Disc)(sist-> A ->vf ->info))->diam < diametru_d )
- // muta_varf(aux,sist->A);//mutam discurile in stiva auxiliara
- insSt(sist->A,(void*)d,sizeof(int));
- Rastoarna(sist->A,aux);//mutam discurile din stiva auiliara in apoi pe A
- return 1;
- }
- /*
- int add( TLista* ansamblu ,char* culoare , int diam)
- {
- Disc d= malloc (sizeof (struct disc));
- if( !d )
- return 0;
- d->diam = diam;
- if ( !(*ansamblu) )
- {
- SistHanoi H;
- H = aloca_sistem ( culoare );
- if( !H )
- return 0;
- strcpy( H->culoare, culoare) ;
- insSt( H->A , (void*) d , sizeof( int ));
- TLista aux = alocarecelula( (void*) H , sizeof (struct sist) );
- (*ansamblu) = aux;
- if( !(*ansamblu) )
- return 0;
- printf("%s\n" , ((SistHanoi)((*ansamblu)->info))->culoare);
- return 1;
- }
- TLista aux,u=NULL;printf("%s\n" , ((SistHanoi)((*ansamblu)->info))->culoare);
- for( aux = (*ansamblu) ; aux != NULL ; u=aux, aux=aux->urm )
- {printf("%s\n" , ((SistHanoi)(aux->info))->culoare);printf("^\n"); if( strcmp ( ((SistHanoi)(aux->info))->culoare , culoare ) == 0 )
- { printf("?\n");
- TStiva st1 = ((SistHanoi)aux->info)->A;
- if( !st1 )
- insSt( st1 , (void*) d , sizeof ( int) );
- TStiva st_aux = aloca_stiva( sizeof( int ) );
- if( !st_aux )
- return 0;
- while( ((Disc)(st1->vf->info))->diam < d->diam )
- {
- TLista p = st1->vf;
- st1->vf=st1->vf->urm;
- p->urm = st_aux->vf;
- st_aux->vf = p;
- }
- Rastoarna( st1 , st_aux );
- break;
- }}
- SistHanoi H;printf("&\n");
- H = aloca_sistem ( culoare );
- if( !H )
- return 0;
- H->culoare = culoare ;
- insSt( H->A , (void*) d , sizeof( int ));
- TLista y = alocarecelula( (void*) H , sizeof (struct sist) );
- if( !y )
- return 0;
- u->urm = y;
- y -> urm = NULL;
- return 1;
- }
- */
- int show(TLista ansamblu , char* culoare , FILE *out)
- {
- TLista p = ansamblu;
- for( ; p != NULL ; p=p->urm )
- {
- if( strcmp ( ((SistHanoi)(p->info))->culoare , culoare ) == 0 )
- { printf("#\n");
- SistHanoi H = ((SistHanoi)(p->info));
- fprintf( out , "A_%s:",culoare);
- TStiva aux1 = aloca_stiva( sizeof( int ) );
- if( !aux1 )
- return 0;
- Rastoarna(aux1,H->A);
- while(aux1->vf)
- {
- fprintf( out ," %d",((Disc)(aux1->vf->info))->diam);
- aux1->vf=aux1->vf->urm;
- }
- fprintf( out , "\n");
- fprintf( out , "B_%s:",culoare);
- TStiva aux2 = aloca_stiva( sizeof( int ) );
- if( !aux2 )
- return 0;
- Rastoarna(aux2,H->B);
- while(aux2->vf)
- {
- fprintf( out ," %d",((Disc)(aux2->vf->info))->diam);
- aux2->vf=aux2->vf->urm;
- }
- fprintf( out , "\n");
- fprintf( out , "C_%s:",culoare);
- TStiva aux3 = aloca_stiva( sizeof( int ) );
- if( !aux3 )
- return 0;
- Rastoarna(aux3,H->C);
- while(aux3->vf)
- {
- fprintf( out ," %d",((Disc)(aux3->vf->info))->diam);
- aux3->vf=aux3->vf->urm;
- }
- fprintf( out , "\n");
- break;
- }
- }
- return 1;
- }
- /*int recunoaste(TLista* ansamblu,char *comanda,FILE *f)
- {
- const char* delims=" \n";
- char *nume_comanda = strtok( comanda , delims);
- if(!nume_comanda)
- return 0;
- if( strcmp( nume_comanda , "add" ) == 0 )
- {
- char *culoare = strtok( NULL , delims );
- if( !culoare )
- return 0;
- char *diam = strtok( NULL , delims );
- if( !diam )
- return 0;
- unsigned int marime = atoi( diam );
- add( ansamblu,culoare,marime);
- printf("%s\n" , ((SistHanoi)((*ansamblu)->info))->culoare);
- }
- else if( strcmp( nume_comanda , "show" ) == 0 )
- {
- char *culoare = strtok( NULL , delims );
- if(!culoare)
- return 0;
- show(*ansamblu, culoare ,f);
- }
- return 1;
- }
- */
- int main(int argc , char const *argv[])
- {
- FILE *in,*out;
- printf("#");
- in = fopen(argv[1] ,"rt");
- if(!in)
- {
- fprintf( stderr,"Unable to open %s" , argv[1]);
- return -1;
- }
- out = fopen(argv[2] ,"wt");
- if(!out)
- {
- fprintf( stderr,"Unable to open %s" , argv[2]);
- return -1;
- }
- TLista ansamblu = NULL;
- char* buffer;
- size_t lungime = 32;
- buffer = malloc (sizeof(lungime));
- if(!buffer)
- return 0;
- getline( &buffer , &lungime , in);
- unsigned int N = atoi( buffer );
- int i;
- for( i = 0;i < N ; i++)
- {
- getline( &buffer , &lungime ,in);
- //recunoaste( &ansamblu,buffer,out);
- const char* delims=" \n";
- char *nume_comanda = strtok( buffer , delims);
- if(!nume_comanda)
- return 0;
- if( strcmp( nume_comanda , "add" ) == 0 )
- {
- char *culoare = strtok( NULL , delims );
- if( !culoare )
- return 0;
- char *diam = strtok( NULL , delims );
- if( !diam )
- return 0;
- unsigned int marime = atoi( diam );
- add( &ansamblu,culoare,marime);
- }
- else if( strcmp( nume_comanda , "show" ) == 0 )
- {
- char *culoare = strtok( NULL , delims );
- if(!culoare)
- return 0;
- show(ansamblu, culoare ,out);
- }
- }
- fclose(in);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement