Advertisement
Alx09

Untitled

Jul 7th, 2020
1,207
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, occ;
  15. };
  16.  
  17. struct node *create(struct node *head, unsigned int n1, unsigned int n2)
  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->next = NULL;
  28.     if (head == NULL) {
  29.         head = p;
  30.         head->occ = 1;
  31.    
  32.         return head;
  33.     }
  34.     if (p->nr < head->nr) {
  35.         p->next = head;
  36.         return head;
  37.     }
  38.     if (p->nr == head->nr) {
  39.         head->nr++;
  40.         free(p);
  41.        
  42.         return head;
  43.     }
  44.     q = head;
  45.     while (p->nr > q->next->nr)q = q->next;
  46.     if (q->next->nr == p->nr) {
  47.    
  48.         q->next->nr++;
  49.         free(p);
  50.         return head;
  51.     }
  52.     else {
  53.         p->next = q->next;
  54.         q = p;
  55.         return head;
  56.     }
  57.        
  58.    
  59. }
  60.  
  61.  
  62.  
  63.  
  64. void displaylist(struct node *head, int nr)
  65. {
  66.     struct node *p;
  67.  
  68.     p = head;
  69.     if (p == NULL)
  70.         printf("lIST IS EMPTY!");
  71.     while (p != NULL)
  72.     {
  73.         double percent = (double)((p->occ) * 100) / nr;
  74.         fprintf(file2, "%u : %u : %lf \n", p->nr, p->occ, percent);
  75.         p = p->next;
  76.     }
  77. }
  78.  
  79. int main(int argc, char* argv[])
  80. {
  81.     struct node *head= NULL;
  82.     int nr = 0;
  83.     if (argc < 3)
  84.     {
  85.         printf("Not enough arguments!");
  86.         exit(0);
  87.     }
  88.     file1 = fopen(argv[1], "rb");
  89.     if (!file1)
  90.     {
  91.         printf("File input not open!");
  92.         exit(0);
  93.     }
  94.     file2 = fopen(argv[2], "w");
  95.     if (!file2)
  96.     {
  97.         printf("File output not open!");
  98.         exit(0);
  99.     }
  100.     struct s input;
  101.     while (fread(&input, sizeof(struct s), 1, file1))
  102.     {  
  103.             head = create(head, input.nr, input.occ);
  104.             nr++;
  105.    
  106.     }
  107.     displaylist(head, nr);
  108.     fclose(file1);
  109.     fclose(file2);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement