Don't like ads? PRO users don't see any ads ;-)

Vettore di liste + alberi v 1.2

By: Lux92 on Jul 15th, 2012  |  syntax: C#  |  size: 10.05 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* Vettore con contatore - Lista + Albero */
  2.  
  3. /******************************************************************************/
  4. /*                         TipobaseABR                                        */
  5. /******************************************************************************/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #define C 10
  9. #define FLUSH while(getchar()!='\n')
  10.  
  11.  
  12. /* materia */
  13. typedef struct {
  14. char materia[C];
  15. char codice[C];
  16. char professore[C];
  17. }tipobaseABR;
  18.  
  19. void LeggiStringa(char s[], unsigned long dim) {
  20. unsigned long i;
  21. for(i=0;(s[i]=getchar())!='\n' && i<dim-1;i++);
  22. s[i]='\0';
  23. if(i==dim-1) FLUSH;
  24. }
  25.  
  26.  
  27. int ConfrontaABR(tipobaseABR a, tipobaseABR b) {
  28. return strcmp(a.codice,b.codice);
  29. }
  30.  
  31.  
  32. void LeggiElementoABR(tipobaseABR * x) {
  33. printf("\nInserisci nome materia:");
  34. LeggiStringa(x->materia,C);
  35. printf("\nInserisci codice materia:");
  36. LeggiStringa(x->codice,C);
  37. printf("\nInserisci nome del professore:");
  38. LeggiStringa(x->professore,C);
  39. }
  40.  
  41. void CercaElementoABR(tipobaseABR * x) {
  42. printf("\nInserisci nome materia:");
  43. LeggiStringa(x->materia,C);
  44. printf("\nInserisci codice materia:");
  45. LeggiStringa(x->codice,C);
  46. }
  47.  
  48. void VisualizzaElementoABR(tipobaseABR x) {
  49. printf("\nNome materia:%s",x.materia);
  50. printf("\nCodice:%s",x.codice);
  51. printf("\nNome professore:%s",x.professore);
  52. }
  53.  
  54.  
  55. /******************************************************************************/
  56. /*                          Abr                                               */
  57. /******************************************************************************/
  58. #define ALBEROVUOTO NULL
  59. typedef short boolean;
  60.  
  61. typedef struct nodoABR {
  62. tipobaseABR info;
  63. struct nodoABR *leftchild,*rightchild;
  64. }*abr;
  65.  
  66. void MakeNullABR(abr * n) {
  67. *n=ALBEROVUOTO;
  68. }
  69.  
  70. boolean EmptyABR(abr n) {
  71. return(n==ALBEROVUOTO);
  72. }
  73.  
  74. boolean FullABR(abr n) {
  75. return 0;
  76. }
  77.  
  78. abr LeftChild(abr n) {
  79. if(!EmptyABR(n))
  80. return(n->leftchild);
  81. }
  82.  
  83. abr RightChild(abr n) {
  84. if(!EmptyABR(n))
  85. return(n->rightchild);
  86. }
  87.  
  88. tipobaseABR Label(abr n) {
  89. if(!EmptyABR(n))
  90. return(n->info);
  91. }
  92.  
  93. boolean Member(abr n,tipobaseABR x) {
  94. if(EmptyABR(n)) return 0;
  95. if(!ConfrontaABR(n->info,x)) return 1;
  96. else if(ConfrontaABR(n->info,x) > 0) return Member(n->leftchild,x);
  97. else  return Member(n->rightchild,x);
  98. }
  99.  
  100.  
  101. void InsertABR(abr * n , tipobaseABR x) {
  102. if(EmptyABR(*n)) {
  103. (*n)=(struct nodoABR *)malloc(sizeof(struct nodoABR));
  104. (*n)->info=x;
  105. (*n)->leftchild=(*n)->rightchild=ALBEROVUOTO;
  106. }
  107. else if(ConfrontaABR((*n)->info,x) >0 ) InsertABR(&(*n)->leftchild,x);
  108. else if(ConfrontaABR((*n)->info,x) <0 ) InsertABR(&(*n)->rightchild,x);
  109. }
  110.  
  111.  
  112. /******************************************************************************/
  113. /*                            tipobaseList                                    */
  114. /******************************************************************************/
  115.  
  116. /*Studente*/
  117. #define V 24
  118. typedef struct {
  119. char nome[C],cognome[C];
  120. char via[V];
  121. abr materia;
  122. }tipobaseList;
  123.  
  124.  
  125. int ConfrontaList(tipobaseList a , tipobaseList b) {
  126. if(!strcmp(a.cognome,b.cognome)) return strcmp(a.nome,b.nome);
  127. else return strcmp(a.cognome,b.cognome);
  128. }
  129.  
  130.  
  131. void AggiornaABR(tipobaseList *x,abr n ){
  132. x->materia=n;
  133. }
  134.  
  135. void CopiaABR(abr *n,tipobaseList x) {
  136. *n=x.materia;
  137. }
  138.  
  139. void LeggiElementoList(tipobaseList * x) {
  140. FLUSH;
  141. printf("\nInserisci nome studente:");
  142. LeggiStringa(x->nome,C);
  143. printf("\nInserisci cognome:");
  144. LeggiStringa(x->cognome,C);
  145. printf("\nInserisci via:");
  146. LeggiStringa(x->via,V);
  147. }
  148.  
  149.  
  150. void CercaElementoList(tipobaseList *x) {
  151. FLUSH;
  152. printf("\nInserisci nome studente:");
  153. LeggiStringa(x->nome,C);
  154. printf("\nInserisci cognome:");
  155. LeggiStringa(x->cognome,C);
  156. }
  157.  
  158. void VisualizzaElementoList(tipobaseList x) {
  159. FLUSH;
  160. printf("\nNome Studente:%s",x.nome);
  161. printf("\nCognome:%s",x.cognome);
  162. printf("\nVia:%s",x.via);
  163. }
  164.  
  165.  
  166. /******************************************************************************/
  167. /*                           List                                             */
  168. /******************************************************************************/
  169. #define LISTAVUOTA NULL
  170. typedef struct nodoList {
  171. tipobaseList info;
  172. struct nodoList * next;
  173. }*list;
  174.  
  175. typedef list position;
  176.  
  177. position End(list l ) {
  178. if(l==LISTAVUOTA) return (l);
  179. while(l->next!=LISTAVUOTA){
  180. l=l->next;
  181. return(l);
  182. }
  183. }
  184.  
  185. position First(list l) {
  186. return(LISTAVUOTA);
  187. }
  188.  
  189. void MakeNullList(list *l) {
  190. *l=LISTAVUOTA;
  191. }
  192.  
  193. boolean EmptyList(list l) {
  194. return(l==LISTAVUOTA);
  195. }
  196.  
  197. boolean FullList(list l) {
  198. return 0;
  199. }
  200.  
  201. void InsertList(list *l, position p,tipobaseList x) {
  202. struct nodoList *tmp;
  203. if(!FullList(*l)) {
  204. tmp=(struct nodoList *)malloc(sizeof(struct nodoList));
  205. tmp->info=x;
  206. if(p==LISTAVUOTA) {
  207. tmp->next=*l;
  208. *l=tmp;
  209. }else{
  210. tmp->next=p->next;
  211. p->next=tmp;
  212. }
  213. }
  214. }
  215.  
  216. void DeleteList(list * l, position p) {
  217. struct nodoList * tmp;
  218. if(!EmptyList(*l))
  219. if(p==LISTAVUOTA) {
  220. tmp=(*l)->next;
  221. free(*l);
  222. *l=tmp;
  223. }else{
  224. tmp=p->next;
  225. p->next=tmp->next;
  226. free(tmp);
  227. }
  228. }
  229.  
  230. position Locate(list l , tipobaseList x) {
  231. if(!EmptyList(l)) {
  232. if(!ConfrontaList(l->info,x)) return(LISTAVUOTA);
  233. while(l->next!=LISTAVUOTA) {
  234. if(!ConfrontaList(l->next->info,x)) return (l);
  235. l=l->next;
  236. }
  237. return(l);
  238. }
  239. }
  240.  
  241. tipobaseList Retrieve(list l , position p) {
  242. if(!EmptyList(l))
  243. if(p==LISTAVUOTA) return(l->info);
  244. else return(p->next->info);
  245. }
  246.  
  247. position Next(list l , position p) {
  248. if(p==LISTAVUOTA) return(l);
  249. else return(p->next);
  250. }
  251.  
  252.  
  253. /******************************************************************************/
  254. /*                            MAIN                                            */
  255. /******************************************************************************/
  256.  struct vettore {
  257. unsigned int contatore;
  258. list studenti;
  259. };
  260.  
  261.  
  262.  
  263. void AllocaVettore(struct vettore ** , unsigned int );
  264.  
  265.  void MakeNullVett(struct vettore *,unsigned int );
  266.  
  267. void Ins_studente(struct vettore *,tipobaseList ,unsigned int );
  268. void Ins_materia(struct vettore * , tipobaseList ,unsigned int );
  269. void Vis_studente(struct vettore *,tipobaseList , unsigned int );
  270.  
  271.  
  272. main() {
  273. tipobaseList studente;
  274. unsigned int scelta,index=0,n=0;
  275. struct nodoVettore *archivio;
  276. do{
  277. printf("\nMenu'");
  278. printf("\n1-Alloca il vettore");
  279. printf("\n2-Inserisci uno studente");
  280. printf("\n3-Inserisci una materia");
  281. printf("\n4-Visualizza studente e materie");
  282. printf("\n5-Esci");
  283. printf("\n-Scelta-->");
  284. scanf("%u",&scelta);
  285. switch(scelta) {
  286. case 1:
  287. printf("\nInserisci grandezza vettore : ");
  288. scanf("%u",&n);
  289. AllocaVettore(&archivio,n);
  290. MakeNullVett(archivio,n);
  291. printf("\nVettore di %u Allocato\n",n);
  292. break;
  293. case 2:
  294. LeggiElementoList(&studente);
  295. index=0;
  296. Ins_studente(archivio,studente,index);
  297. break;
  298.  
  299. case 3:
  300. CercaElementoList(&studente);
  301. index=0;
  302. Ins_materia(archivio,studente,n);
  303.  
  304. break;
  305. case 4:
  306. CercaElementoList(&studente);
  307. index=0;
  308. Vis_studente(archivio,studente,n);
  309. break;
  310. case 5:
  311. break;
  312. default: printf("\nScelta non valida");
  313. break;
  314. }
  315. }
  316. while(scelta!=5);
  317.  
  318.  
  319.  
  320. }
  321.  
  322.  
  323.  
  324. void AllocaVettore(struct vettore **v,unsigned int n) {
  325. *v=(struct vettore *)malloc(sizeof (struct vettore));
  326. }
  327.  
  328.  void MakeNullVett(struct vettore *v,unsigned int dim){
  329.          short i;
  330.          for(i=0;i<dim;i++){
  331.                             v[i].contatore=0;
  332.                             MakeNullList(&(v[i].studenti));}}
  333.  
  334.  
  335.  
  336.  
  337. void Insord(list * l , tipobaseList x) {
  338. position p,u;
  339. tipobaseList tmp;
  340. if(EmptyList(*l)) InsertList(l,First(*l),x);
  341. else {
  342. p=First(*l);
  343. u=End(*l);
  344. while(p!=u) {
  345. tmp=Retrieve(*l,p);
  346. if(ConfrontaList(tmp,x) >0 ) p=Next(*l,p);
  347. }
  348. InsertList(l,p,x);
  349. }
  350. }
  351.  
  352.  
  353.  
  354. void Ins_studente(struct vettore *v,tipobaseList x,unsigned int n){
  355. position pos;
  356. abr q;
  357. short i;
  358. for(i=0;i<n;i++) {
  359. if(!EmptyList(v[i].studenti)) pos=Locate(v[i].studenti,x);
  360. if(!EmptyList(v[i].studenti) && pos!=End(v[i].studenti));
  361. else {
  362. MakeNullABR(&q);
  363. AggiornaABR(&x,q);
  364. Insord(&(v[i].studenti),x);
  365.                                             (v[i].contatore)++ ;
  366.                                             printf("\n Studente inserito\n");}
  367.  
  368.  
  369. }
  370. }
  371.  
  372. void Ins_materia(struct vettore *v , tipobaseList x,unsigned int u) {
  373. unsigned long i;
  374. position pos;
  375. abr n;
  376. tipobaseABR materia;
  377.  
  378.  
  379. for(i=0;i<u;i++) {
  380.  
  381. if(!EmptyList(v[i].studenti)) pos=Locate(v[i].studenti,x);
  382. if(EmptyList(v[i].studenti) || pos==End(v[i].studenti) ) printf("\nStudente non trovato");
  383. else {
  384. x=Retrieve(v[i].studenti,pos);
  385. LeggiElementoABR(&materia);
  386.  
  387.  
  388. CopiaABR(&n,x);
  389.  
  390.  
  391. MakeNullABR(&n);
  392.  
  393. InsertABR(&n,materia);
  394.  
  395. AggiornaABR(&x,n);
  396.  
  397. DeleteList(&(v[i].studenti),pos);
  398. InsertList(&(v[i].studenti),pos,x);
  399.  
  400. }
  401. }
  402. }
  403.  
  404.  
  405. void CercaElemento( abr a , tipobaseABR x ) {  /*Cerca l'elemento nell'albero se lo trova lo visualizza*/
  406. tipobaseABR tmp;
  407. if(EmptyABR(a)) printf("\nElemento non trovato");
  408. else {
  409. tmp=Label(a);
  410. if(!ConfrontaABR(tmp,x)) VisualizzaElementoABR(tmp);
  411. else if(ConfrontaABR(tmp,x) > 0) CercaElemento(LeftChild(a),x);
  412. else  CercaElemento(RightChild(a),x);
  413. }
  414. }
  415.  
  416.  
  417.  
  418. void Vis_studente(struct vettore *v,tipobaseList x, unsigned int n){
  419.          position pos;
  420.          abr a;
  421.          tipobaseList tmp;
  422.          tipobaseABR materia;
  423.          unsigned int i;
  424.          
  425.          for(i=0;i<n;i++){
  426.                           if(EmptyList(v[i].studenti)) printf("\nStudente non trovato");
  427.                           else{
  428.                           pos=Locate(v[i].studenti,x);
  429.                           if(pos!=End(v[i].studenti)){
  430.                                                    x=Retrieve((v[i].studenti),pos);
  431.                                                    VisualizzaElementoList(x);
  432.                                                    CopiaABR(&a,x);
  433.                                                    if(EmptyABR(a)) printf("\nNon ci sono materie registrate");
  434.                                                     CercaElementoABR(&materia);
  435.                                                    CercaElemento(a,materia);
  436. }
  437. }
  438. }
  439.                                                    }