Advertisement
Alx09

Untitled

Jul 7th, 2020
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. FILE *file1, *file2;
  4. int valh = 0, valt = 0, total = 0;;
  5. struct node
  6. {
  7.     unsigned int nr, occ;
  8.     struct node *next;
  9.  
  10. };
  11.  
  12. struct s
  13. {
  14.     unsigned int nr;
  15. };
  16.  
  17. struct node *create(struct node *head, unsigned int n1)
  18. {  
  19.     struct  node *p, *q;
  20.     p = (struct node*)malloc(sizeof(struct node));
  21.     if (!p)
  22.     {
  23.         printf("Error at allocating the memory");
  24.         exit(0);
  25.     }
  26.     p->nr = n1;
  27.     p->occ = 1;
  28.     p->next = NULL;
  29.     if (head == NULL) {
  30.         head = p;
  31.         head->occ = 1;
  32.    
  33.         return head;
  34.     }
  35.     if (p->nr < head->nr) {
  36.         p->next = head;
  37.         return p;
  38.     }
  39.     if (p->nr == head->nr) {
  40.         head->nr++;
  41.         free(p);
  42.        
  43.         return head;
  44.     }
  45.     q = head;
  46.     while (q ->next != NULL && p->nr > q->next->nr)q = q->next;
  47.     if (q ->next != NULL && q->next->nr == p->nr) {
  48.    
  49.         q->next->nr++;
  50.         free(p);
  51.         return head;
  52.     }
  53.     else {
  54.         p->next = q->next;
  55.         q ->next = p;
  56.        
  57.         return head;
  58.     }
  59.        
  60.    
  61. }
  62.  
  63.  
  64.  
  65.  
  66. void displaylist(struct node *head, int nr)
  67. {
  68.     struct node *p;
  69.  
  70.     p = head;
  71.     if (p == NULL)
  72.         printf("lIST IS EMPTY!");
  73.     while (p != NULL)
  74.     {
  75.         double percent = (double)((p->occ) * 100) / nr;
  76.         fprintf(file2, "%u : %u : %lf \n", p->nr, p->occ, percent);
  77.         p = p->next;
  78.     }
  79. }
  80.  
  81. int main(int argc, char* argv[])
  82. {
  83.     struct node *head= NULL;
  84.     int nr = 0;
  85.    
  86.     file1 = fopen("file2.bin", "rb");
  87.     if (!file1)
  88.     {
  89.         printf("File input not open!");
  90.         exit(0);
  91.     }
  92.     file2 = fopen("out.txt", "w");
  93.     if (!file2)
  94.     {
  95.         printf("File output not open!");
  96.         exit(0);
  97.     }
  98.     struct s input;
  99.     while (fread(&input, sizeof(struct s), 1, file1))
  100.     {  
  101.             head = create(head, input.nr);
  102.             nr++;
  103.    
  104.     }
  105.     displaylist(head, nr);
  106.     fclose(file1);
  107.     fclose(file2);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement