Advertisement
Guest User

Untitled

a guest
May 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <pthread.h>
  5. #include <locale.h>
  6. #include <time.h>
  7. #include <string.h>
  8.  
  9.  
  10. typedef struct Sensor {
  11. int id;
  12. int intervalo;
  13. char local[30];
  14. char tipo[30];
  15. //bool active;
  16. int active;
  17. struct Sensor* anterior;
  18. struct Sensor* proximo;
  19. } Sensor;
  20.  
  21. Sensor* novo2(int id, int tempo, char* l, char* t) {
  22.  
  23. Sensor* novoSensor = (Sensor*)malloc(sizeof(struct Sensor));
  24.  
  25. novoSensor->id = id;
  26. novoSensor->intervalo = tempo;
  27. strcpy(novoSensor->local, l);
  28. strcpy(novoSensor->tipo, t);
  29. novoSensor->active = 1;
  30. novoSensor->anterior = NULL;
  31. novoSensor->proximo = NULL;
  32.  
  33. //gravaSensor(novoSensor, (*totalSens), 1);
  34.  
  35. return novoSensor;
  36. }
  37.  
  38. Sensor* novo(int *totalSens, int tempo, char* l, char* t) {
  39.  
  40. Sensor* novoSensor = (Sensor*)malloc(sizeof(struct Sensor));
  41.  
  42. novoSensor->id = ++(*totalSens);
  43. novoSensor->intervalo = tempo;
  44. strcpy(novoSensor->local, l);
  45. strcpy(novoSensor->tipo, t);
  46. novoSensor->active = 1;
  47. novoSensor->anterior = NULL;
  48. novoSensor->proximo = NULL;
  49.  
  50. return novoSensor;
  51. }
  52.  
  53. void insereSensorFim(Sensor** Lista, Sensor* novo) {
  54.  
  55. Sensor* atual = *Lista;
  56.  
  57. if ((*Lista == NULL))
  58. (*Lista = novo);
  59.  
  60. else {
  61.  
  62. while (atual->proximo != NULL) {
  63. atual = atual->proximo;
  64. }
  65.  
  66. atual->proximo = novo;
  67. novo->anterior = atual;
  68. }
  69. }
  70.  
  71. void deleteSensor(Sensor** l, Sensor* del) {
  72. /* base case */
  73. if (*l == NULL || del == NULL)
  74. return;
  75.  
  76. //del->active = 0; WIP
  77. //gravaLista(l);
  78.  
  79. /* If node to be deleted is head node */
  80. if(*l == del)
  81. *l = del->proximo;
  82.  
  83. /* Change next only if node to be deleted is NOT the last node */
  84. if(del->proximo != NULL)
  85. del->proximo->anterior = del->anterior;
  86.  
  87. /* Change prev only if node to be deleted is NOT the first node */
  88. if(del->anterior != NULL)
  89. del->anterior->proximo = del->proximo;
  90.  
  91. /* Finally, free the memory occupied by del*/
  92. free(del);
  93. }
  94.  
  95. Sensor* procura(Sensor* l, int x) {
  96. while (l != NULL && l->id != x) {
  97. l = l->proximo;
  98. }
  99. //if (head->value = NULL) return NULL;
  100. return l;
  101. }
  102.  
  103. void print(Sensor* l) {
  104. //Sensor* temp = *Lista;
  105. printf("----- \n");
  106. while(l != NULL) {
  107. printf("id: %d \n",l->id);
  108. printf("%s \n", l->tipo);
  109. printf("%s \n", l->local);
  110. printf("%d s\n", l->intervalo);
  111. printf("-----\n");
  112. l = l->proximo;
  113. }
  114.  
  115. }
  116.  
  117. /*void alteraIntervalo(Sensor* s, int x) {
  118. s->intervalo = x;
  119. }
  120.  
  121. void alteraLocal(Sensor* s, char* l) {
  122. strcpy(s->local, l);
  123. }
  124.  
  125. void alteraTipo(Sensor* s, char* t) {
  126. strcpy(s->tipo, t);
  127. }*/
  128.  
  129. void imprimePorTipo(Sensor* l, char* tipo) {
  130. while (l != NULL) {
  131. if (strcmp(l->tipo, tipo) == 0 && l->active)
  132. printf("%s\n", l->tipo);
  133. l = l->proximo;
  134. }
  135. }
  136.  
  137.  
  138. void imprimeAgrupado(Sensor* l) {
  139. Sensor* temp = l;
  140.  
  141. printf("sensores de humidade\n");
  142. imprimePorTipo(temp, "humidade");
  143. printf("\n");
  144.  
  145. temp = l;
  146.  
  147. printf("sensores de temperatura\n");
  148. imprimePorTipo(temp, "temp");
  149. printf("\n");
  150.  
  151. temp = l;
  152.  
  153. printf("sensores de ruido\n");
  154. imprimePorTipo(temp, "ruido");
  155. }
  156.  
  157. void gravaLista(Sensor* l) {
  158. FILE *ficheiro;
  159. Sensor* temp = l;
  160. ficheiro = fopen("sensores.txt", "r+t");
  161.  
  162. if (ficheiro == NULL) {
  163. ficheiro = fopen("sensores.txt", "w+t");
  164. }
  165.  
  166. while (temp != NULL) {
  167. fprintf(ficheiro, "%d\n%d\n%d\n%29s\n%29s\n", temp->id, temp->intervalo, temp->active,
  168. temp->local, temp->tipo);
  169. temp = temp->proximo;
  170. }
  171.  
  172. fclose(ficheiro);
  173. }
  174.  
  175. int CarregaTodos(Sensor** l, int *totalSens) {
  176. Sensor sens;
  177. FILE *ficheiro;
  178. int i = 0;
  179.  
  180. ficheiro = fopen("sensores.txt", "r+t");
  181.  
  182. if (ficheiro == NULL) {
  183. printf("no file\n", "sensores.txt");
  184. return i;
  185. }
  186.  
  187. rewind(ficheiro);
  188.  
  189. while (fscanf(ficheiro, "%d\n%d\n%d\n%29s\n%29s\n", &sens.id, &sens.intervalo, &sens.active,
  190. &sens.local, &sens.tipo) == 5) {
  191.  
  192. if (sens.active == 1) {
  193. insereSensorFim(l, novo2(sens.id, sens.intervalo, sens.local, sens.tipo));
  194. }
  195.  
  196. ++(*totalSens);
  197. }
  198.  
  199. fclose(ficheiro);
  200. return i;
  201. }
  202.  
  203. int main()
  204. {
  205. int totalSens = 0;
  206. Sensor* Lista = NULL;
  207.  
  208. //deleteSensor(&Lista, procura(Lista, 2));
  209.  
  210. /*insereSensorFim(&Lista, novo(&totalSens, 2, "braga", "temp"));
  211. insereSensorFim(&Lista, novo(&totalSens, 3, "porto", "humidade"));
  212. insereSensorFim(&Lista, novo(&totalSens, 4, "porto", "humidade"));
  213. insereSensorFim(&Lista, novo(&totalSens, 5, "porto", "humidade"));
  214.  
  215. /*deleteSensor(&Lista, procura(Lista, 5));
  216.  
  217. alteraIntervalo(procura(Lista, 2), 27);
  218. alteraTipo(procura(Lista, 2), "ruido");
  219. alteraLocal(procura(Lista, 2), "lisboa");*/
  220.  
  221.  
  222. /*insereSensorFim(&Lista, novo(&totalSens));
  223. insereSensorFim(&Lista, novo(&totalSens));*/
  224.  
  225. //deleteSensor(&Lista, procura(Lista, 60));
  226.  
  227. //imprimePorTipo(Lista, "humidade");
  228.  
  229. //imprimeAgrupado(Lista);
  230.  
  231. //gravaSensor(procura(Lista, 2), )
  232.  
  233. CarregaTodos(&Lista, &totalSens);
  234.  
  235. //insereSensorFim(&Lista, novo(&totalSens, 5, "guima", "ruido"));
  236.  
  237. //gravaLista(Lista);
  238.  
  239. print(Lista);
  240. printf("%d", totalSens);
  241. return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement