Advertisement
razvanth21

Untitled

May 24th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct nod
  6. {
  7.     char *denumire;
  8.     float pu;
  9.     int num;
  10.     struct nod *urm;
  11. } NOD;
  12.  
  13. NOD *r = NULL;
  14.  
  15. void sortare()
  16. {
  17.     NOD *h = r, *i, *j;
  18.  
  19.     for (i = h; i != NULL && i->urm != NULL; i=i->urm)
  20.     {
  21.         NOD *c = i;
  22.         for (j = i->urm; j != NULL; j=j->urm)
  23.         {
  24.             if ((float)(j->pu * j->num) > (float)(c->pu * c->num))
  25.                 c = j;
  26.         }
  27.            
  28.         if (c != i)
  29.         {
  30.             float aux;
  31.             int x;
  32.  
  33.             aux = c->pu;
  34.             x = c->num;
  35.  
  36.             c->pu = i->pu;
  37.             c->num = i->num;
  38.             i->pu = aux;
  39.             i->num = x;
  40.         }
  41.     }
  42.     r = h;
  43. }
  44.  
  45. int main(void)
  46. {
  47.     int n, i, x;
  48.     float f;
  49.     char s[20];
  50.     NOD *p, *u;
  51.  
  52.     scanf("%d", &n);
  53.  
  54.     for (i = 0; i < n; i++)
  55.     {
  56.         p = (NOD *)malloc(sizeof(NOD));
  57.  
  58.         if (p == NULL)
  59.         {
  60.             printf("Memorie insuficienta");
  61.             exit(1);
  62.         }
  63.  
  64.         if (r == NULL)
  65.             r = u = p;
  66.  
  67.         else
  68.             u->urm = p;
  69.  
  70.         //scanf("%s", s);
  71.         fgets(s, 20, stdin);
  72.         s[strlen(s) - 1] = '\0';
  73.  
  74.         p->denumire = (char *)malloc(strlen(s) + 1);
  75.         strcpy(p->denumire, s);
  76.  
  77.         scanf("%f", &f);
  78.         p->pu = f;
  79.  
  80.         scanf("%d", &x);
  81.         p->num = x;
  82.  
  83.         p->urm = NULL;
  84.         u = p;
  85.     }
  86.  
  87.     sortare();
  88.  
  89.     for(p = r; p != NULL; p = p->urm)
  90.         printf("%s, %f, %d\n", p->denumire, p->pu, p->num);
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement