Advertisement
Alx09

Untitled

May 21st, 2020
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. unsigned  k, nrPersImport, nrPersInDrum;
  6. unsigned short id = 0;
  7. double distParcurs, combustibil;
  8.  
  9. typedef struct Calls
  10. {
  11.     unsigned short x, y, prioritate, id;
  12.     struct Calls *urm;
  13.  
  14. }nevoiasi;
  15. typedef enum { Very_Low, Low, Medium, High, Very_High } prioritate;
  16. prioritate prio[5];
  17. nevoiasi *persImport[100], *persInDrum[100];
  18.  
  19. void ADD_Call(nevoiasi *prim) {
  20.     nevoiasi *q, *p;
  21.     id++;
  22.     system("cls");
  23.     printf("Nevoiasul %hu\n", id);
  24.     p = (nevoiasi *)malloc(sizeof(nevoiasi));
  25.     printf("x= "); scanf("%hu", &p->x);
  26.     printf("y= "); scanf("%hu", &p->y);
  27.     printf("prioritate= "); scanf("%hu", &p->prioritate);
  28.     p->urm = NULL;
  29.     p->id = id;
  30.     q = prim;
  31.     while (q->urm != NULL && q->urm->x < p->x)
  32.         q = q->urm;
  33.         while (q->urm != NULL && q->urm->x == p->x && q->urm->y < p->y)
  34.         q = q->urm;
  35.    
  36.     p->urm = q->urm;
  37.     q->urm = p;
  38. }
  39. void Afisare(nevoiasi *prim) {
  40.     nevoiasi *q;
  41.     q = prim->urm;
  42.     while (q != NULL) {
  43.         printf("\n%d %d\n", q->x, q->y);
  44.         q = q->urm;
  45.     }
  46. }
  47. double Dist(nevoiasi *p, nevoiasi *q) {
  48.     return sqrt(pow(q->x - p->x, 2) + pow(q->y - p->y, 2));
  49. }
  50.  
  51.  
  52. double SetPriority(nevoiasi *prim) {
  53.     nevoiasi *q = prim->urm, *p = prim, *t = prim;
  54.     nrPersImport = 0;
  55.     int i;
  56.     double dist = 0;
  57.     for (i = Very_Low; i <= Very_High; i++)
  58.         prio[i] = 0;
  59.     persImport[nrPersImport++] = prim;
  60.     while (q)
  61.     {
  62.  
  63.         prio[q->prioritate]++;
  64.         if (q->prioritate == Very_High && nrPersImport <= k) {
  65.             if (dist + Dist(p, q) + Dist(prim, q) <= combustibil) {
  66.                 dist += Dist(p, q);
  67.                 p = q;
  68.                 persImport[nrPersImport] = t;
  69.                 nrPersImport++;
  70.             }
  71.         }
  72.         t = q;
  73.         q = q->urm;
  74.     }
  75.     return dist + Dist(prim, p);
  76. }
  77. void GrowPriority(nevoiasi *prim) {
  78.     nevoiasi *q = prim->urm;
  79.     while (q)
  80.     {
  81.         if (q->prioritate < Very_High)
  82.             q->prioritate ++;
  83.         q = q->urm;
  84.     }
  85. }
  86.  
  87. void DeleteCall(nevoiasi *p) {
  88.     nevoiasi *q = p->urm;
  89.     printf(" %hu ", q->id);
  90.     p->urm = p->urm->urm;
  91.     free(q);
  92. }
  93.  
  94. void PersInDrum() {
  95.     nevoiasi *q, *t;
  96.     nrPersInDrum = 0;
  97.     int i, n = nrPersImport - 1;
  98.     for (i = 0; i < n; i++) {
  99.         q = persImport[i]->urm;
  100.         int a = persImport[i + 1]->urm->x - persImport[i]->urm->x;
  101.         int b = persImport[i + 1]->urm->y - persImport[i]->urm->y;
  102.         if(persImport[i] != persImport[i+1])
  103.         while (q->urm != NULL && q->urm != persImport[i+1]->urm)
  104.         {
  105.             t = q;
  106.             q = q->urm;
  107.             if (b * (q->x - persImport[i]->urm->x) == a * (q->y - persImport[i]->urm->y)) {
  108.                 persInDrum[nrPersInDrum] = t;
  109.                 nrPersInDrum++;
  110.             }
  111.         }
  112.     }
  113.  
  114. }
  115. void Sort() {
  116.     int i, ok;
  117.     nevoiasi *aux;
  118.     do {
  119.         ok = 0;
  120.         for (i = nrPersImport - 2; i; i--)
  121.             if (persImport[i]->x > persImport[i + 1]->x || (persImport[i]->x == persImport[i + 1]->x && persImport[i]->y > persImport[i + 1]->y)) {
  122.                 aux = persImport[i];
  123.                 persImport[i] = persImport[i + 1];
  124.                 persImport[i + 1] = aux;
  125.                 ok = 1;
  126.             }
  127.     } while (ok);
  128.        
  129. }
  130.  
  131. int main() {
  132.     unsigned days = 1, i, nrCalls;
  133.     nevoiasi *prim = (nevoiasi *)malloc(sizeof(nevoiasi));
  134.     prim->x = 0;
  135.     prim->y = 0;
  136.     prim->prioritate = 0;
  137.     prim->urm = NULL;
  138.     printf("Numarul de pachete ce pot fi livrate: "); scanf("%u", &k);
  139.     printf("Numarul de apeluri: "); scanf("%u", &nrCalls);
  140.     for (i = 1; i <= nrCalls; i++)
  141.         ADD_Call(prim);
  142.  
  143.     do {
  144.    
  145.         combustibil += 10000;
  146.         distParcurs = SetPriority(prim);
  147.  
  148.         if (prio[Very_High]) {
  149.             if (k - prio[Very_High])
  150.                 PersInDrum();
  151.             combustibil -= distParcurs;
  152.             for (i = nrPersInDrum + nrPersImport - k -1; i < nrPersInDrum; i++)
  153.                 persImport[nrPersImport++] = persInDrum[i];
  154.             Sort();
  155.             printf("\nRezumat sfarsit de zi %u:", days);
  156.             printf("\nAu fost sterse apelurile: ");
  157.             for (i = nrPersImport - 1; i; i--)
  158.                 DeleteCall(persImport[i]);
  159.             printf("\nCombustibil ramas %f L\n", combustibil);
  160.             printf("Au fost livrate %u pachete\n", nrPersImport - 1);
  161.  
  162.         }
  163.         else
  164.             printf("test\n");
  165.         GrowPriority(prim);
  166.         days++;
  167.         nrCalls = nrCalls - nrPersImport + 1;
  168.     } while (prim->urm);
  169.     system("pause");
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement