Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <pthread.h>
- #include <locale.h>
- #include <time.h>
- #include <string.h>
- typedef struct Sensor {
- int id;
- int intervalo;
- char local[30];
- char tipo[30];
- //bool active;
- int active;
- struct Sensor* anterior;
- struct Sensor* proximo;
- } Sensor;
- Sensor* novo2(int id, int tempo, char* l, char* t) {
- Sensor* novoSensor = (Sensor*)malloc(sizeof(struct Sensor));
- novoSensor->id = id;
- novoSensor->intervalo = tempo;
- strcpy(novoSensor->local, l);
- strcpy(novoSensor->tipo, t);
- novoSensor->active = 1;
- novoSensor->anterior = NULL;
- novoSensor->proximo = NULL;
- //gravaSensor(novoSensor, (*totalSens), 1);
- return novoSensor;
- }
- Sensor* novo(int *totalSens, int tempo, char* l, char* t) {
- Sensor* novoSensor = (Sensor*)malloc(sizeof(struct Sensor));
- novoSensor->id = ++(*totalSens);
- novoSensor->intervalo = tempo;
- strcpy(novoSensor->local, l);
- strcpy(novoSensor->tipo, t);
- novoSensor->active = 1;
- novoSensor->anterior = NULL;
- novoSensor->proximo = NULL;
- return novoSensor;
- }
- void insereSensorFim(Sensor** Lista, Sensor* novo) {
- Sensor* atual = *Lista;
- if ((*Lista == NULL))
- (*Lista = novo);
- else {
- while (atual->proximo != NULL) {
- atual = atual->proximo;
- }
- atual->proximo = novo;
- novo->anterior = atual;
- }
- }
- void deleteSensor(Sensor** l, Sensor* del) {
- /* base case */
- if (*l == NULL || del == NULL)
- return;
- //del->active = 0; WIP
- //gravaLista(l);
- /* If node to be deleted is head node */
- if(*l == del)
- *l = del->proximo;
- /* Change next only if node to be deleted is NOT the last node */
- if(del->proximo != NULL)
- del->proximo->anterior = del->anterior;
- /* Change prev only if node to be deleted is NOT the first node */
- if(del->anterior != NULL)
- del->anterior->proximo = del->proximo;
- /* Finally, free the memory occupied by del*/
- free(del);
- }
- Sensor* procura(Sensor* l, int x) {
- while (l != NULL && l->id != x) {
- l = l->proximo;
- }
- //if (head->value = NULL) return NULL;
- return l;
- }
- void print(Sensor* l) {
- //Sensor* temp = *Lista;
- printf("----- \n");
- while(l != NULL) {
- printf("id: %d \n",l->id);
- printf("%s \n", l->tipo);
- printf("%s \n", l->local);
- printf("%d s\n", l->intervalo);
- printf("-----\n");
- l = l->proximo;
- }
- }
- /*void alteraIntervalo(Sensor* s, int x) {
- s->intervalo = x;
- }
- void alteraLocal(Sensor* s, char* l) {
- strcpy(s->local, l);
- }
- void alteraTipo(Sensor* s, char* t) {
- strcpy(s->tipo, t);
- }*/
- void imprimePorTipo(Sensor* l, char* tipo) {
- while (l != NULL) {
- if (strcmp(l->tipo, tipo) == 0 && l->active)
- printf("%s\n", l->tipo);
- l = l->proximo;
- }
- }
- void imprimeAgrupado(Sensor* l) {
- Sensor* temp = l;
- printf("sensores de humidade\n");
- imprimePorTipo(temp, "humidade");
- printf("\n");
- temp = l;
- printf("sensores de temperatura\n");
- imprimePorTipo(temp, "temp");
- printf("\n");
- temp = l;
- printf("sensores de ruido\n");
- imprimePorTipo(temp, "ruido");
- }
- void gravaLista(Sensor* l) {
- FILE *ficheiro;
- Sensor* temp = l;
- ficheiro = fopen("sensores.txt", "r+t");
- if (ficheiro == NULL) {
- ficheiro = fopen("sensores.txt", "w+t");
- }
- while (temp != NULL) {
- fprintf(ficheiro, "%d\n%d\n%d\n%29s\n%29s\n", temp->id, temp->intervalo, temp->active,
- temp->local, temp->tipo);
- temp = temp->proximo;
- }
- fclose(ficheiro);
- }
- int CarregaTodos(Sensor** l, int *totalSens) {
- Sensor sens;
- FILE *ficheiro;
- int i = 0;
- ficheiro = fopen("sensores.txt", "r+t");
- if (ficheiro == NULL) {
- printf("no file\n", "sensores.txt");
- return i;
- }
- rewind(ficheiro);
- while (fscanf(ficheiro, "%d\n%d\n%d\n%29s\n%29s\n", &sens.id, &sens.intervalo, &sens.active,
- &sens.local, &sens.tipo) == 5) {
- if (sens.active == 1) {
- insereSensorFim(l, novo2(sens.id, sens.intervalo, sens.local, sens.tipo));
- }
- ++(*totalSens);
- }
- fclose(ficheiro);
- return i;
- }
- int main()
- {
- int totalSens = 0;
- Sensor* Lista = NULL;
- //deleteSensor(&Lista, procura(Lista, 2));
- /*insereSensorFim(&Lista, novo(&totalSens, 2, "braga", "temp"));
- insereSensorFim(&Lista, novo(&totalSens, 3, "porto", "humidade"));
- insereSensorFim(&Lista, novo(&totalSens, 4, "porto", "humidade"));
- insereSensorFim(&Lista, novo(&totalSens, 5, "porto", "humidade"));
- /*deleteSensor(&Lista, procura(Lista, 5));
- alteraIntervalo(procura(Lista, 2), 27);
- alteraTipo(procura(Lista, 2), "ruido");
- alteraLocal(procura(Lista, 2), "lisboa");*/
- /*insereSensorFim(&Lista, novo(&totalSens));
- insereSensorFim(&Lista, novo(&totalSens));*/
- //deleteSensor(&Lista, procura(Lista, 60));
- //imprimePorTipo(Lista, "humidade");
- //imprimeAgrupado(Lista);
- //gravaSensor(procura(Lista, 2), )
- CarregaTodos(&Lista, &totalSens);
- //insereSensorFim(&Lista, novo(&totalSens, 5, "guima", "ruido"));
- //gravaLista(Lista);
- print(Lista);
- printf("%d", totalSens);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement