salron3

All In

May 30th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void psn(lst *head, FILE *fp) //b)
  5. {
  6.     if (head == NULL)
  7.     {
  8.         printf("The list is empty");
  9.         return;
  10.     }
  11.  
  12.     int err = 0;
  13.     while (head != NULL)
  14.     {
  15.         if (err = fprintf(fp, "%s\n", head->data.name) != 1)
  16.             printf("Greshka pri zapis\n");
  17.  
  18.         if (err = fprintf(fp, "%d\n", head->data.num) != 1)
  19.             printf("Greshka pri zapis\n");
  20.  
  21.         if (err = fprintf(fp, "%f\n", head->data.cht) != 1)
  22.             printf("Greshka pri zapis\n");
  23.  
  24.         head = head->next;
  25.     }
  26.  
  27.     if (fp)
  28.         fclose(fp);
  29. }
  30.  
  31. int ct(lst *head) //в)
  32. {
  33.     if (head == NULL)
  34.     {
  35.         printf("The list is empty");
  36.         return;
  37.     }
  38.     int countOfNode = 0;
  39.  
  40.     while (head != NULL)
  41.     {
  42.         if (head->data.num < 10)
  43.         {
  44.             countOfNode++;
  45.         }
  46.         head = head->next;
  47.     }
  48.     return countOfNode;
  49. }
  50.  
  51. int main()
  52. {
  53.     lst *head = NULL;
  54.     int countNodes;
  55.  
  56.     countNodes = ct(head);
  57.     printf("Broq na vuzlite: %d", countNodes);
  58.  
  59.     char fileName[64];
  60.     printf("Vuvedete ime na faila za zapis: ");
  61.     scanf("%63s", fileName);
  62.  
  63.     FILE *fp = fopen(fileName, "wt");
  64.     if (fp == NULL)
  65.     {
  66.         printf("Greshka pri otvarqne");
  67.         return;
  68.     }
  69.  
  70.     psn(head, fp); //записваме списъка във файл
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment