Advertisement
Guest User

EsercizioLista1

a guest
Jan 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. struct el
  2. {
  3. char info;
  4. struct el *next;
  5. };
  6. typedef struct el *t_lista;
  7.  
  8. t_lista riempiLista(FILE *fp)
  9. {
  10. t_lista first, attuale;
  11. char carattere;
  12. fscanf(fp, "%c", &carattere);
  13. first = malloc(sizeof(struct el));
  14. first->info = carattere;
  15. first->next = attuale;
  16. while(!feof(fp))
  17. {
  18. fscanf(fp, "%c", &carattere);
  19. attuale = malloc(sizeof(struct el));
  20. attuale->info = carattere;
  21. attuale->next = NULL;
  22. attuale = attuale->next;
  23. }
  24. attuale->next = NULL;
  25. return first;
  26. }
  27.  
  28. int verificaLista(t_lista testa)
  29. {
  30. int countTonda = 0, countQuadra = 0, countGraffa = 0;
  31. t_lista temp = testa;
  32. while(temp != NULL)
  33. {
  34. if(temp->info == '(')
  35. countTonda++;
  36. if(temp->info == ')')
  37. countTonda--;
  38. if(temp->info == '[')
  39. countQuadra++;
  40. if(temp->info == ']')
  41. countQuadra--;
  42. if(temp->info == '{')
  43. countGraffa++;
  44. if(temp->info == '}')
  45. countGraffa--;
  46. temp = temp->next;
  47. }
  48. if(countTonda != 0 || countQuadra != 0 || countGraffa != 0)
  49. {
  50. printf("Errore, almeno una stringa non e' ben parentesizzata\n");
  51. return -1;
  52. }
  53. else
  54. {
  55. printf("Il file e' ben parentesizzato");
  56. return 0;
  57. }
  58. }
  59.  
  60. int main()
  61. {
  62. FILE *fp;
  63. t_lista testa;
  64. fp = fopen("dati.txt", "r");
  65. if(fp == NULL)
  66. {
  67. printf("Errore nell'apertura del file\n");
  68. return -1;
  69. }
  70. else
  71. {
  72. testa = riempiLista(fp);
  73. verificaLista(testa);
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement