Alx09

Proiect v2

May 21st, 2020
1,506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.17 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("\nNevoiasul %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. double SetPriority(nevoiasi *prim, double *distTotala) {
  52.     nevoiasi *q = prim->urm, *p = prim, *t = prim;
  53.     nrPersImport = 0;
  54.     int i;
  55.     double dist = 0;
  56.     *distTotala = 0;
  57.     for (i = Very_Low; i <= Very_High; i++)
  58.         prio[i] = 0;
  59.     persImport[nrPersImport++] = prim;
  60.     while (q)
  61.     {
  62.         *distTotala = *distTotala + Dist(t, q);
  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.     *distTotala += Dist(t, prim);
  76.     return dist + Dist(prim, p);
  77. }
  78. void GrowPriority(nevoiasi *prim) {
  79.     nevoiasi *q = prim->urm;
  80.     while (q)
  81.     {
  82.         if (q->prioritate < Very_High)
  83.             q->prioritate ++;
  84.         q = q->urm;
  85.     }
  86. }
  87.  
  88. void DeleteCall(nevoiasi *p) {
  89.     nevoiasi *q = p->urm;
  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. void SortID(unsigned *v, unsigned n) {
  131.     unsigned i, ok, aux;
  132.     do {
  133.         ok = 0;
  134.         for(i = 0;i < n - 1;i++)
  135.             if (v[i] > v[i + 1]) {
  136.                 aux = v[i];
  137.                 v[i] = v[i + 1];
  138.                 v[i + 1] = aux ;
  139.                 ok = 1;
  140.             }
  141.     } while (ok);
  142. }
  143.  
  144. int main() {
  145.     unsigned days = 1, i, nrCalls = 0, n, *v;
  146.     double distTotala;
  147.     nevoiasi *prim = (nevoiasi *)malloc(sizeof(nevoiasi));
  148.     prim->x = 0;
  149.     prim->y = 0;
  150.     prim->prioritate = 0;
  151.     prim->urm = NULL;
  152.     printf("Numarul de pachete ce pot fi livrate: "); scanf("%u", &k);
  153.     system("cls");
  154.     do {
  155.         printf("Ziua %u\n", days);
  156.         printf("Numarul de apeluri: "); scanf("%u", &n);
  157.         for (i = 1; i <= n; i++)
  158.             ADD_Call(prim);
  159.         system("cls");
  160.         nrCalls += n;
  161.         combustibil += 10000;
  162.         distParcurs = SetPriority(prim, &distTotala);
  163.         if (distTotala <= combustibil && nrCalls <= k) {
  164.             printf("\nRezumat sfarsit de zi %u:", days);
  165.             printf("\nAu fost sterse apelurile: ");
  166.             combustibil -= distTotala;
  167.             i = 0;
  168.             v = (unsigned *)malloc(sizeof(unsigned) * nrCalls);
  169.             while (prim->urm) {
  170.                 v[i] = prim->urm->id;
  171.                 i++;
  172.                 DeleteCall(prim);
  173.             }
  174.             SortID(v, nrCalls);
  175.             for (i = 0; i < nrCalls; i++)
  176.                 printf("%u ", v[i]);
  177.             free(v);
  178.             printf("\nNu se mai afla nici-un apel in lista!");
  179.             printf("\nCombustibil ramas %f L\n", combustibil);
  180.         }
  181.         else if (prio[Very_High]) {
  182.             if (k - prio[Very_High])
  183.                 PersInDrum();
  184.             combustibil -= distParcurs;
  185.             for (i = nrPersInDrum + nrPersImport - k -1; i < nrPersInDrum; i++)
  186.                 persImport[nrPersImport++] = persInDrum[i];
  187.             Sort();
  188.             printf("\nRezumat sfarsit de zi %u:", days);
  189.             printf("\nAu fost sterse apelurile: ");
  190.             v = (unsigned *)malloc(sizeof(unsigned) * nrPersImport);
  191.             for (i = nrPersImport - 1; i; i--) {
  192.                 v[i] = persImport[i]->urm->id;
  193.                 DeleteCall(persImport[i]);
  194.             }
  195.             SortID(v, nrPersImport);
  196.             for (i = 0; i < nrPersImport; i++)
  197.                 printf("%u ", v[i]);
  198.             free(v);
  199.             printf("\nCombustibil ramas %f L\n", combustibil);
  200.             printf("Au fost livrate %u pachete\n", nrPersImport - 1);
  201.  
  202.         }
  203.         else
  204.             printf("test\n");
  205.         system("pause");
  206.         system("cls");
  207.         GrowPriority(prim);
  208.         days++;
  209.         nrCalls = nrCalls - nrPersImport + 1;
  210.     } while (prim->urm);
  211.     system("pause");
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment