Guest User

Untitled

a guest
Oct 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. /*Compito 20/07/2012
  2. In un condominio si gestisce il conto spese con un archivio informatico rappresentato da un vettore.
  3. Ogni elemento del vettore
  4. contiene due liste che si riferiscono a scala a e b.
  5. Ogni lista contiene:
  6. -cognome
  7. -nome
  8. -spese da sostenere
  9. La dimensione del vettore corrisponde al numero di piani.
  10. Codificare:
  11. un main
  12. una funzione che allochi il vettore
  13. una funzione che riceva in ingresso: piano, scala, cognome e nome e inserisca un nuovo condomine
  14. (le spese vengono poste a 0)
  15. una funzione che riceva in ingresso: piano, scala, cognome, nome e il nuovo valore delle spese condominiali,
  16. la funzione deve rimpiazzare il precedente valore con il nuovo
  17. */
  18. #include<stdio.h>
  19. #include<string.h>
  20. #include<stdlib.h>
  21. #define N 12
  22.  
  23. #define FLUSH while(getchar()!='\n')
  24.  
  25.  
  26. typedef struct{
  27. char nome[N],cognome[N];
  28. unsigned int spese;
  29. }tipobaseList;
  30.  
  31. void LeggiStringa(char s[],unsigned int dim){
  32. unsigned int i;
  33.  
  34. for(i=0; i<dim-1 && (s[i]=getchar())!='\n'; i++);
  35. s[i]='\0';
  36. if(i==dim-1) FLUSH;
  37. }
  38.  
  39. typedef short boolean;
  40.  
  41. void LeggiElemento(tipobaseList *x){
  42. printf("\nNome: ");
  43. LeggiStringa(x->nome,N);
  44. printf("\nCognome: ");
  45. LeggiStringa(x->cognome,N);
  46.  
  47. }
  48. void CercaElemento(tipobaseList *x){
  49. printf("\nNome: ");
  50. LeggiStringa(x->nome,N);
  51. printf("\nCognome: ");
  52. LeggiStringa(x->cognome,N);
  53. printf("\nspese: ");
  54. scanf("%u",&x->spese);
  55. FLUSH;
  56.  
  57. }
  58.  
  59. void AzzeraSpese(tipobaseList *x){
  60. x->spese=0;
  61. }
  62.  
  63. int Confronta(tipobaseList a,tipobaseList b){
  64. if(!strcmp(a.cognome,b.cognome)) return(strcmp(a.nome,b.nome));
  65. else return(strcmp(a.cognome,b.cognome));
  66. }
  67.  
  68. //lista.h
  69. #define LISTAVUOTA NULL
  70. typedef struct nodoList{
  71. tipobaseList info;
  72. struct nodoList *next;
  73. }*list;
  74.  
  75. typedef list position;
  76. typedef short boolean;
  77.  
  78. void MakeNullList(list *l){
  79. *l=LISTAVUOTA;
  80. }
  81. position First(list l){
  82. return(LISTAVUOTA);
  83. }
  84. position End(list l){
  85. if(l==LISTAVUOTA) return(LISTAVUOTA);
  86. else while(l->next!=LISTAVUOTA){
  87. l=l->next;
  88. return(l);//attenzione
  89. }
  90. }
  91. boolean EmptyList(list l){
  92. return(l==LISTAVUOTA);
  93. }
  94.  
  95. boolean FullList(list l){
  96. struct nodoList *tmp;
  97.  
  98. tmp=(struct nodoList *)malloc(sizeof(struct nodoList));
  99. if(tmp==LISTAVUOTA) return 1;
  100. free(tmp);
  101. return 0;
  102. }
  103.  
  104. void InsertList(list *l,position p,tipobaseList x){
  105. struct nodoList *tmp;
  106. if(!FullList(*l)){
  107. tmp=(struct nodoList *)malloc(sizeof(struct nodoList *));
  108. tmp->info=x;
  109.  
  110. if(p==LISTAVUOTA){
  111. tmp->next=(*l);
  112. (*l)=tmp;
  113. }else{
  114. tmp->next=p->next;
  115. p->next=tmp;
  116. }
  117. }
  118. }
  119.  
  120. position Locate(list l,tipobaseList x){
  121. if(!EmptyList(l)){
  122. if(!Confronta(l->info,x)) return(LISTAVUOTA);
  123. else while(l->next!=LISTAVUOTA) {
  124. if(!Confronta(l->next->info,x)) return(l);
  125. l=l->next;
  126. }return(l);
  127.  
  128. }
  129. }
  130.  
  131. tipobaseList Retrieve(list l,position p){
  132. if(!EmptyList(l)){
  133. if(p==LISTAVUOTA) return(l->info);
  134. else return(p->next->info);
  135. }
  136. }
  137.  
  138. position Next(list l,position p){
  139. if(!EmptyList(l)){
  140. if(p==LISTAVUOTA) return(l);
  141. else return(p->next);
  142. }
  143. }
  144.  
  145. void DeleteList(list *l,position p){
  146. struct nodoList *tmp;
  147. if(!EmptyList(*l)){
  148.  
  149. if(p==LISTAVUOTA){
  150. tmp=(*l)->next;
  151. free(*l);
  152. (*l)=tmp;
  153. }else{
  154. tmp=p->next;
  155. p->next=tmp->next;
  156. free(tmp);//attenzione
  157. }
  158. }
  159. }
  160.  
  161.  
  162.  
  163.  
  164.  
  165. //compito
  166.  
  167. struct nodoVect{
  168. list ListaA;
  169. list ListaB;
  170. }*archivio;
  171.  
  172. void Inizializza(struct nodoVect *, unsigned int);
  173. void Alloca(struct nodoVect **, unsigned int);
  174. unsigned int Index(unsigned int);
  175. void Insord(list *,tipobaseList);
  176. void InserisciCondomine(struct nodoVect *,tipobaseList ,unsigned int);
  177. void AggiornaSpese(struct nodoVect *,tipobaseList ,unsigned int);
  178.  
  179.  
  180. main(){
  181. unsigned int n;
  182. unsigned short scelta;
  183. tipobaseList condomine;
  184.  
  185. do{
  186. printf("dimensione vettore:\n");
  187. scanf("%u",&n);
  188. FLUSH;
  189. }while(n<=0);
  190.  
  191. Alloca(&archivio,n);
  192.  
  193. do{
  194. printf("------MENU'-----\n\n");
  195. printf("1) Inserisci condomine\n");
  196. printf("2) aggiorna spese\n");
  197. printf("3) FINE\n");
  198. printf("Inserisci scelta----> \n");
  199. scanf("%u",&scelta);
  200. FLUSH;
  201.  
  202. switch(scelta){
  203. case 1:
  204. LeggiElemento(&condomine);
  205. InserisciCondomine(archivio,condomine,n);
  206. break;
  207. case 2:
  208. CercaElemento(&condomine);
  209. AggiornaSpese(archivio,condomine,n);
  210. break;
  211.  
  212. }
  213. }while(scelta<3);
  214.  
  215. }
  216.  
  217.  
  218.  
  219. void Inizializza(struct nodoVect *v, unsigned int dim){
  220. unsigned int j;
  221. for(j=0; j<dim; j++){
  222. MakeNullList(&(v[j].ListaA));
  223. MakeNullList(&(v[j].ListaB));
  224. }
  225.  
  226. }
  227.  
  228. void Alloca(struct nodoVect **v, unsigned int dim){
  229. *v=(struct nodoVect *)malloc(dim*sizeof(struct nodoVect));
  230. Inizializza(*v,dim);
  231. }
  232.  
  233. unsigned int Index(unsigned int dim){
  234. unsigned int i;
  235. printf("\ndigita piano");
  236. do{
  237. scanf("%u",&i);
  238. FLUSH;
  239. }while(i>dim && i<0);
  240. return(i);
  241. }
  242.  
  243. void Insord(list *l,tipobaseList x){
  244. position p,u;
  245. tipobaseList tmp;
  246. if(EmptyList(*l)) InsertList(l,First(*l),x);
  247. else {
  248. p=First(*l);
  249. u=End(*l);
  250. while(p!=u){
  251. tmp=Retrieve(*l,p);
  252. if(Confronta(tmp,x)<0) p=Next(*l,p);
  253. else break;
  254. }InsertList(l,p,x);
  255. }
  256.  
  257. }
  258. void InserisciCondomine(struct nodoVect *v,tipobaseList y,unsigned int dim){
  259. unsigned int i;
  260. position p;
  261. char scala;
  262. i=Index(dim);
  263. do{
  264. printf("\n inserisci scala a\b \n");
  265. scanf("%c",&scala);
  266. FLUSH;
  267.  
  268. switch(scala){
  269. case 'a':
  270. if(!EmptyList(v[i].ListaA)) p=Locate(v[i].ListaA,y);
  271. if(!EmptyList(v[i].ListaA) && p!=End(v[i].ListaA))
  272. printf("\nelemento già presente");
  273. else{
  274. y=Retrieve(v[i].ListaA,p);
  275. AzzeraSpese(&y);
  276. Insord(&(v[i].ListaA),y);
  277. }
  278. break;
  279. case 'b':
  280. if(!EmptyList(v[i].ListaB)) p=Locate(v[i].ListaB,y);
  281. if(!EmptyList(v[i].ListaB) && p!=End(v[i].ListaB))
  282. printf("\nelemento già presente");
  283. else{
  284. y=Retrieve(v[i].ListaB,p);
  285. AzzeraSpese(&y);
  286. Insord(&(v[i].ListaB),y);
  287. }
  288.  
  289. break;
  290. }
  291. }while(scala!='a' && scala!='b');
  292. printf("\n\napposto\n\n");
  293. }
  294.  
  295.  
  296. void AggiornaSpese(struct nodoVect *v,tipobaseList y,unsigned int dim){
  297. unsigned int i;
  298. position p;
  299.  
  300. char scala;
  301. i=Index(dim);
  302. do{
  303. printf("\n inserisci scala a\b \n");
  304. scanf("%c",&scala);
  305. switch(scala){
  306. case 'a':
  307. if(!EmptyList(v[i].ListaA)) p=Locate(v[i].ListaA,y);
  308. if(EmptyList(v[i].ListaA) || p==End(v[i].ListaA))
  309. printf("condomine non presente");
  310. else{
  311. x=Retrieve(v[i].ListaA,p);
  312. DeleteList(&(v[i].ListaA),p);
  313. InsertList(&(v[i].ListaA),p,y); printf("\nappostooooo\n");
  314. }
  315. break;
  316. case 'b':
  317. if(!EmptyList(v[i].ListaB)) p=Locate(v[i].ListaB,y);
  318. if(EmptyList(v[i].ListaB) || p==End(v[i].ListaB))
  319. printf("condomine non presente");
  320. else{
  321. x=Retrieve(v[i].ListaB,p);
  322. DeleteList(&(v[i].ListaB),p);
  323. InsertList(&(v[i].ListaB),p,y); printf("\nappostooooo\n");
  324. }
  325.  
  326. break;
  327. }
  328. }while(scala!='a' && scala!='b');
  329. printf("\n\napposto\n\n");
  330.  
  331. }
Add Comment
Please, Sign In to add comment