Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <float.h>
  4. #include <math.h>
  5.  
  6. typedef unsigned short int boolean;
  7. #define TRUE 1
  8. #define FALSE 0
  9.  
  10. struct list{
  11. float value;
  12. struct list * next;
  13. };
  14.  
  15. struct list_times{
  16. float value;
  17. int times;
  18. struct list_times * next;
  19. };
  20.  
  21. boolean isequal(float a, float b);
  22.  
  23.  
  24. void init(struct list ** ptr);
  25. void visit(struct list * ptr);
  26. boolean suf_insert(struct list **ptr, float value);
  27. boolean pre_insert(struct list **ptrptr, float value);
  28.  
  29. void init_times(struct list_times ** ptr);
  30. void visit_times(struct list_times * ptr);
  31. boolean search_times(struct list ** ptr, float *V, int n, struct list_times ** new );
  32. boolean suf_insert_times(struct list_times ** ptrptr, float value, int times);
  33. boolean pre_insert_times(struct list_times ** ptrptr, float value, int times);
  34.  
  35. int main() {
  36.  
  37. char answer; float value; struct list * ptr; struct list_times * new; float * V; int n;
  38.  
  39. printf("Programma che riceve in input i valori (float) di una lista e di un array, e li inserisce in una nuova lista nella quale verrà indicata la loro occorrenza nel vettore"
  40. "Durante l'inserimento dei valori nella nuova lista l'altra sarà cancellata\n");
  41.  
  42. init(&ptr);
  43. init_times(&new);
  44.  
  45.  
  46. do{
  47. printf("Inserisci un valore della lista:\n");
  48. scanf("%f", &value);
  49.  
  50. suf_insert(&ptr,value);
  51.  
  52. printf("Se vuoi inserire un altro valore digita ""y""\n");
  53. scanf("%s", &answer);
  54.  
  55. } while(answer=='y'||answer=='Y');
  56.  
  57.  
  58. do {
  59. printf("Inserisci la dimensione dell'array:\n");
  60. scanf("%d", &n);
  61.  
  62. if (n <= 0)
  63. printf("La dimensine deve essere maggiore di 0!");
  64.  
  65. } while(n<=0);
  66.  
  67. if((V=(float *)malloc(sizeof(float)*n))) {
  68.  
  69. for (int i = 0; i < n; i++) {
  70. printf("Inserisci un valore dell'array:\n");
  71. scanf("%f", &V[i]);
  72. }
  73.  
  74. printf("Lista inserita:\n");
  75. visit(ptr);
  76.  
  77. printf("\nVettore inserito:\n");
  78. for(int i=0;i<n;i++)
  79. printf("Valore %d: " "%f\n",i+1,V[i]);
  80.  
  81.  
  82. printf("Nuova lista:\n");
  83. search_times(&ptr, V, n, &new);
  84. visit_times(new);
  85.  
  86.  
  87. }//if
  88. else
  89. printf("Non è stato possibile allocare lo spazio di memoria per l'array\n");
  90.  
  91. return 0;
  92. }
  93.  
  94. boolean search_times(struct list ** ptr, float *V, int n, struct list_times ** new ){
  95.  
  96. struct list * tmp_ptr;
  97.  
  98. if( V!=NULL && n>=0 ){
  99.  
  100. while((*ptr)!=NULL) {
  101.  
  102. int times=0;
  103.  
  104. for (int i = 0; i < n; i++) {
  105. if (isequal((*ptr)->value, V[i]))
  106. times++;
  107. }
  108.  
  109. if((suf_insert_times(new, (*ptr)->value, times))==FALSE)
  110. return FALSE;
  111.  
  112. tmp_ptr=*ptr;
  113. ptr=&((*ptr)->next);
  114. free(tmp_ptr);
  115. }
  116. return TRUE;
  117. }
  118.  
  119. else
  120. return FALSE;
  121.  
  122. }
  123.  
  124.  
  125. boolean suf_insert_times(struct list_times ** ptrptr, float value, int times)
  126. {
  127. while( *ptrptr != NULL )
  128. ptrptr = &((*ptrptr)->next);
  129. if ( pre_insert_times(ptrptr,value,times) )
  130. return TRUE;
  131. else
  132. return FALSE;
  133. }
  134.  
  135. boolean pre_insert_times(struct list_times ** ptrptr, float value, int times){
  136.  
  137. struct list_times * tmp_new;
  138. tmp_new = (struct list_times *)malloc(sizeof(struct list_times));
  139. if(tmp_new!=NULL) {
  140. tmp_new->value = value;
  141. tmp_new->times=times;
  142. tmp_new->next = *ptrptr;
  143. *ptrptr = tmp_new;
  144. return TRUE;
  145. }
  146. else
  147. return FALSE;
  148.  
  149. }
  150.  
  151. void init_times(struct list_times ** ptr)
  152. {
  153. *ptr = NULL;
  154. }
  155.  
  156. void init(struct list ** ptr)
  157. {
  158. *ptr = NULL;
  159. }
  160.  
  161. boolean pre_insert(struct list **ptrptr, float value)
  162. {
  163. struct list * tmp_ptr;
  164. tmp_ptr = (struct list *)malloc(sizeof(struct list));
  165. if ( tmp_ptr != NULL ) {
  166. tmp_ptr->value = value;
  167. tmp_ptr->next = *ptrptr;
  168. *ptrptr = tmp_ptr;
  169. return TRUE;
  170. }
  171. else
  172. return FALSE;
  173.  
  174. }
  175.  
  176. boolean suf_insert(struct list **ptrptr, float value)
  177. {
  178. while( *ptrptr != NULL )
  179. ptrptr = &((*ptrptr)->next);
  180. if ( pre_insert(ptrptr,value) )
  181. return TRUE;
  182. else
  183. return FALSE;
  184. }
  185.  
  186. void visit(struct list * ptr)
  187. {
  188.  
  189. while ( ptr != NULL ) {
  190. printf("%f ", ptr->value);
  191. ptr = ptr->next;
  192. }
  193. }
  194.  
  195. void visit_times(struct list_times * ptr)
  196. {
  197.  
  198. while ( ptr != NULL ) {
  199. printf("%f ", ptr->value);
  200. printf("%d ", ptr->times);
  201. ptr = ptr->next;
  202. }
  203. }
  204.  
  205. boolean isequal(float a, float b) {
  206. float diff, max;
  207. diff = fabs(a-b);
  208. a = fabs(a);
  209. b = fabs(b);
  210. max = a > b ? a : b;
  211. if ( diff < FLT_EPSILON*max) return TRUE;
  212. else return FALSE;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement