Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct celula
  6. {
  7. void* info;
  8. struct celula *urm;
  9.  
  10. }TCelula,*TLista,**ALista;
  11.  
  12. typedef struct stiva
  13. {
  14. size_t dime;
  15. TLista vf;
  16. }*TStiva;
  17. typedef struct sist
  18. {
  19. TStiva A;
  20. TStiva B;
  21. TStiva C;
  22. char* culoare;
  23. }*SistHanoi;
  24.  
  25. typedef struct disc
  26. {
  27. int diam;
  28. }*Disc;
  29.  
  30. TStiva aloca_stiva( size_t d)
  31. {
  32. TStiva s = (TStiva) malloc ( sizeof( struct stiva ));
  33. if(!s)
  34. return NULL;
  35. s->dime = d;
  36. s->vf = NULL;
  37. return s;
  38. }
  39.  
  40. void elibereaza_stiva( TStiva s , TLista *l)
  41. {
  42. TLista aux;
  43. while( *l )
  44. {
  45. aux = *l ;
  46. *l = aux->urm;
  47. free( aux->info );
  48. free(aux);
  49. }
  50. free(s);
  51. }
  52.  
  53. TLista alocarecelula(void* x,size_t d )
  54. {
  55. TLista aux = (TLista)malloc(sizeof(TCelula));
  56. if(!aux)
  57. return NULL;
  58. aux->info = malloc(sizeof(int));
  59. if(!aux->info)
  60. {
  61. free(aux);
  62. return NULL;
  63. }
  64. memcpy(aux->info,x,d);
  65. aux->urm = NULL;
  66. return aux;
  67.  
  68. }
  69. int insSt(TStiva s,void* x,size_t d)
  70. {
  71. TLista aux = alocarecelula(x,d);
  72. if(!aux)
  73. return 0;
  74. aux->urm = s->vf;
  75. s->vf = aux;
  76. return 1;
  77. }
  78.  
  79. void Rastoarna( TStiva a , TStiva b)
  80. {
  81. while(b->vf)
  82. {
  83. TLista p = b->vf;
  84. b->vf=b->vf->urm;
  85. p->urm = a->vf;
  86. a->vf = p;
  87. }
  88. }
  89. SistHanoi aloca_sistem(char* culoare)
  90. {
  91. SistHanoi H = malloc( sizeof (struct sist) );
  92. if( !H )
  93. return NULL;
  94.  
  95. H->A = aloca_stiva( sizeof( int ) );
  96. if( !H->A )
  97. return NULL;
  98.  
  99. H->B = aloca_stiva( sizeof (int) );
  100. if( !H->B )
  101. {
  102. elibereaza_stiva( H->A , &(H->A->vf) );
  103. return NULL;
  104. }
  105.  
  106. H->C = aloca_stiva( sizeof ( int ));
  107. if( !H->C )
  108. {
  109. elibereaza_stiva( H->B , &(H->B->vf) );
  110. elibereaza_stiva( H->A , &(H->A->vf) );
  111. return NULL;
  112. }
  113. H->culoare = malloc (30);
  114. if( !H->culoare )
  115. {
  116. elibereaza_stiva( H->C , &(H->C->vf) );
  117. elibereaza_stiva( H->B , &(H->B->vf) );
  118. elibereaza_stiva( H->A , &(H->A->vf) );
  119. return NULL;
  120. }
  121. strcpy( H->culoare , culoare );
  122. return H;
  123.  
  124. }
  125. int add(TLista *s, char *color_d, size_t diametru_d)
  126. { printf("$");
  127. TLista p=*s,u=NULL;
  128. while(p!=NULL)
  129. {
  130. if( strcmp(((SistHanoi)(p->info))-> culoare,color_d)==0)//cautam sistemul de culoare color_d
  131. break;
  132. u=p;
  133. p=p->urm;
  134. }
  135. if(p==NULL) // daca nu exista sistemul de culoarea color_d
  136. {
  137. SistHanoi sist_nou=aloca_sistem(color_d);//alocam un sistem nou
  138. if(!sist_nou)
  139. return 0;
  140. Disc d_nou=malloc(sizeof(struct disc));//alocam un disc nou
  141. if(!d_nou)
  142. {
  143. free(sist_nou);
  144. return 0;
  145. }
  146. d_nou->diam=diametru_d;
  147. insSt(sist_nou->A,(void*)d_nou,sizeof(int));//punem noul disc pe stiva A a noului sistem
  148.  
  149. TLista aux=alocarecelula((void*)sist_nou,sizeof(struct sist));
  150. if(!aux)
  151. {
  152.  
  153. return 0;
  154. }
  155.  
  156. if(u==NULL) //daca lista de sisteme este goala
  157. *s=aux;
  158. else
  159. u->urm=aux;//legam noul sistem la lista de sisteme
  160. free(d_nou);
  161. free(sist_nou);
  162. return 1;
  163. }
  164. SistHanoi sist=((SistHanoi)(p->info));
  165. Disc d=malloc(sizeof(struct disc));//alocam un nou disc pentru sistemul de culoare color_d
  166. if(!d)
  167. return 0;
  168. d->diam=diametru_d;
  169.  
  170. TStiva aux = aloca_stiva( sizeof(int));
  171. if(!aux)
  172. {
  173. free(d);
  174. return 0;
  175. }
  176. //tratam cazul in care discul are diametru mai mare decat varful
  177. while( sist-> A ->vf != NULL && ((Disc)(sist-> A ->vf ->info))->diam < diametru_d )
  178. // muta_varf(aux,sist->A);//mutam discurile in stiva auxiliara
  179. insSt(sist->A,(void*)d,sizeof(int));
  180.  
  181.  
  182. Rastoarna(sist->A,aux);//mutam discurile din stiva auiliara in apoi pe A
  183.  
  184. return 1;
  185.  
  186. }
  187. /*
  188. int add( TLista* ansamblu ,char* culoare , int diam)
  189. {
  190. Disc d= malloc (sizeof (struct disc));
  191. if( !d )
  192. return 0;
  193.  
  194. d->diam = diam;
  195. if ( !(*ansamblu) )
  196. {
  197. SistHanoi H;
  198.  
  199. H = aloca_sistem ( culoare );
  200. if( !H )
  201. return 0;
  202. strcpy( H->culoare, culoare) ;
  203.  
  204. insSt( H->A , (void*) d , sizeof( int ));
  205.  
  206. TLista aux = alocarecelula( (void*) H , sizeof (struct sist) );
  207. (*ansamblu) = aux;
  208. if( !(*ansamblu) )
  209. return 0;
  210. printf("%s\n" , ((SistHanoi)((*ansamblu)->info))->culoare);
  211. return 1;
  212.  
  213. }
  214.  
  215.  
  216.  
  217.  
  218. TLista aux,u=NULL;printf("%s\n" , ((SistHanoi)((*ansamblu)->info))->culoare);
  219. for( aux = (*ansamblu) ; aux != NULL ; u=aux, aux=aux->urm )
  220. {printf("%s\n" , ((SistHanoi)(aux->info))->culoare);printf("^\n"); if( strcmp ( ((SistHanoi)(aux->info))->culoare , culoare ) == 0 )
  221. { printf("?\n");
  222. TStiva st1 = ((SistHanoi)aux->info)->A;
  223. if( !st1 )
  224. insSt( st1 , (void*) d , sizeof ( int) );
  225.  
  226. TStiva st_aux = aloca_stiva( sizeof( int ) );
  227. if( !st_aux )
  228. return 0;
  229.  
  230. while( ((Disc)(st1->vf->info))->diam < d->diam )
  231. {
  232. TLista p = st1->vf;
  233. st1->vf=st1->vf->urm;
  234. p->urm = st_aux->vf;
  235. st_aux->vf = p;
  236.  
  237. }
  238. Rastoarna( st1 , st_aux );
  239. break;
  240.  
  241. }}
  242.  
  243. SistHanoi H;printf("&\n");
  244. H = aloca_sistem ( culoare );
  245. if( !H )
  246. return 0;
  247. H->culoare = culoare ;
  248.  
  249. insSt( H->A , (void*) d , sizeof( int ));
  250.  
  251. TLista y = alocarecelula( (void*) H , sizeof (struct sist) );
  252. if( !y )
  253. return 0;
  254. u->urm = y;
  255. y -> urm = NULL;
  256.  
  257. return 1;
  258. }
  259. */
  260. int show(TLista ansamblu , char* culoare , FILE *out)
  261. {
  262. TLista p = ansamblu;
  263. for( ; p != NULL ; p=p->urm )
  264. {
  265. if( strcmp ( ((SistHanoi)(p->info))->culoare , culoare ) == 0 )
  266. { printf("#\n");
  267. SistHanoi H = ((SistHanoi)(p->info));
  268.  
  269. fprintf( out , "A_%s:",culoare);
  270. TStiva aux1 = aloca_stiva( sizeof( int ) );
  271. if( !aux1 )
  272. return 0;
  273. Rastoarna(aux1,H->A);
  274. while(aux1->vf)
  275. {
  276. fprintf( out ," %d",((Disc)(aux1->vf->info))->diam);
  277. aux1->vf=aux1->vf->urm;
  278. }
  279. fprintf( out , "\n");
  280.  
  281. fprintf( out , "B_%s:",culoare);
  282. TStiva aux2 = aloca_stiva( sizeof( int ) );
  283. if( !aux2 )
  284. return 0;
  285. Rastoarna(aux2,H->B);
  286. while(aux2->vf)
  287. {
  288. fprintf( out ," %d",((Disc)(aux2->vf->info))->diam);
  289. aux2->vf=aux2->vf->urm;
  290. }
  291. fprintf( out , "\n");
  292.  
  293. fprintf( out , "C_%s:",culoare);
  294. TStiva aux3 = aloca_stiva( sizeof( int ) );
  295. if( !aux3 )
  296. return 0;
  297. Rastoarna(aux3,H->C);
  298. while(aux3->vf)
  299. {
  300. fprintf( out ," %d",((Disc)(aux3->vf->info))->diam);
  301. aux3->vf=aux3->vf->urm;
  302. }
  303. fprintf( out , "\n");
  304. break;
  305. }
  306. }
  307. return 1;
  308. }
  309.  
  310. /*int recunoaste(TLista* ansamblu,char *comanda,FILE *f)
  311. {
  312.  
  313. const char* delims=" \n";
  314. char *nume_comanda = strtok( comanda , delims);
  315.  
  316. if(!nume_comanda)
  317. return 0;
  318.  
  319. if( strcmp( nume_comanda , "add" ) == 0 )
  320. {
  321. char *culoare = strtok( NULL , delims );
  322. if( !culoare )
  323. return 0;
  324. char *diam = strtok( NULL , delims );
  325. if( !diam )
  326. return 0;
  327. unsigned int marime = atoi( diam );
  328.  
  329. add( ansamblu,culoare,marime);
  330. printf("%s\n" , ((SistHanoi)((*ansamblu)->info))->culoare);
  331.  
  332. }
  333. else if( strcmp( nume_comanda , "show" ) == 0 )
  334. {
  335. char *culoare = strtok( NULL , delims );
  336. if(!culoare)
  337. return 0;
  338.  
  339. show(*ansamblu, culoare ,f);
  340. }
  341. return 1;
  342.  
  343. }
  344. */
  345. int main(int argc , char const *argv[])
  346. {
  347. FILE *in,*out;
  348. printf("#");
  349. in = fopen(argv[1] ,"rt");
  350. if(!in)
  351. {
  352. fprintf( stderr,"Unable to open %s" , argv[1]);
  353. return -1;
  354. }
  355. out = fopen(argv[2] ,"wt");
  356. if(!out)
  357. {
  358. fprintf( stderr,"Unable to open %s" , argv[2]);
  359. return -1;
  360. }
  361.  
  362. TLista ansamblu = NULL;
  363.  
  364.  
  365.  
  366. char* buffer;
  367. size_t lungime = 32;
  368. buffer = malloc (sizeof(lungime));
  369. if(!buffer)
  370. return 0;
  371. getline( &buffer , &lungime , in);
  372. unsigned int N = atoi( buffer );
  373. int i;
  374. for( i = 0;i < N ; i++)
  375. {
  376. getline( &buffer , &lungime ,in);
  377. //recunoaste( &ansamblu,buffer,out);
  378. const char* delims=" \n";
  379. char *nume_comanda = strtok( buffer , delims);
  380.  
  381. if(!nume_comanda)
  382. return 0;
  383.  
  384. if( strcmp( nume_comanda , "add" ) == 0 )
  385. {
  386. char *culoare = strtok( NULL , delims );
  387. if( !culoare )
  388. return 0;
  389. char *diam = strtok( NULL , delims );
  390. if( !diam )
  391. return 0;
  392. unsigned int marime = atoi( diam );
  393.  
  394. add( &ansamblu,culoare,marime);
  395.  
  396.  
  397. }
  398. else if( strcmp( nume_comanda , "show" ) == 0 )
  399. {
  400. char *culoare = strtok( NULL , delims );
  401. if(!culoare)
  402. return 0;
  403.  
  404. show(ansamblu, culoare ,out);
  405. }
  406.  
  407. }
  408.  
  409. fclose(in);
  410. fclose(out);
  411.  
  412. return 0;
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement