Alx09

Ex proiect

May 16th, 2020
1,431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. unsigned combustibil, k, nrPersImport,nrPersInDrum;
  5. double distParcurs;
  6. typedef struct Calls
  7. {
  8.     unsigned short x, y, prioritate;
  9.     struct Calls * urm;
  10.  
  11. }nevoiasi;
  12. typedef enum{Very_Low, Low, Medium, High, Very_High} prioritate;
  13. prioritate prio[5];
  14. nevoiasi *persImport[100], *persInDrum[100];
  15.  
  16. nevoiasi * ADD_Call(nevoiasi *prim)  {
  17.     nevoiasi *q, *p;
  18.     p = (nevoiasi *)malloc(sizeof(nevoiasi));
  19.     printf("x= "); scanf("%hu", &p->x);
  20.     printf("y= "); scanf("%hu", &p->y);
  21.     printf("prioritate= "); scanf("%hu", &p->prioritate);
  22.     prio[p->prioritate]++;
  23.     if (p->prioritate == Very_High)
  24.         persImport[nrPersImport++] = p;
  25.     p->urm = NULL;
  26.     if (prim == NULL)
  27.         return p;
  28.     if (prim->x >= p->x && prim->y > p->y) {
  29.         p->urm = prim;
  30.         return p;
  31.     }
  32.     q = prim;
  33.     while (q->urm != NULL && q->urm->x <= p->x &&  q->urm->y < p->y)
  34.         q = q->urm;
  35.     p->urm = q->urm;
  36.     q->urm = p;
  37.     return prim;
  38.  
  39. }
  40. void Afisare(nevoiasi *prim) {
  41.     nevoiasi *q;
  42.     q = prim;
  43.     while (q != NULL) {
  44.         printf("\n%d %d\n", q->x, q->y);
  45.         q = q->urm;
  46.     }
  47. }
  48.  
  49. double Dist(nevoiasi *p, nevoiasi *q) {
  50.     return sqrt(pow(q->x - p->x, 2) + pow(q->y - p->y, 2));
  51. }
  52. double DistPersImport() {
  53.     unsigned i;
  54.     double suma = 0;
  55.     for (i = 0; i < nrPersImport - 1; i++)
  56.         suma += Dist(persImport[i], persImport[i + 1]);
  57.     return suma;
  58. }
  59.  
  60. void SetPriority( nevoiasi *prim) {
  61.     nevoiasi *q = prim;
  62.     nrPersImport = 0;
  63.     int i;
  64.     for (i = 0; i < 5; i++)
  65.         prio[i] = 0;
  66.     while (q)
  67.     {
  68.         q->prioritate++;
  69.         prio[q->prioritate]++;
  70.         if (q->prioritate == Very_High)
  71.             persImport[nrPersImport++] = q;
  72.         q = q->urm;
  73.     }
  74.  
  75. }
  76. void PersInDrum() {
  77.     nevoiasi *q;
  78.     int i = nrPersImport - 2;
  79.     while (i >= 0) {
  80.         q = persImport[i];
  81.         q = q->urm;
  82.         if (k - nrPersImport - nrPersInDrum < 0)
  83.             return;
  84.         while (q != persImport[i+1])
  85.         {
  86.             if (k - nrPersImport - nrPersInDrum < 0)
  87.                 return;
  88.             if ((persImport[i + 1]->x - persImport[i]->x)*(q->y - persImport[i]->y) == (q->x - persImport[i]->x) *(persImport[i + 1]->y - persImport[i]->y)) {
  89.                 persInDrum[nrPersInDrum++] = q;
  90.             }
  91.         }
  92.     }
  93.  
  94. }
  95.  
  96. int main() {
  97.     unsigned days = 1, i, n;
  98.     nevoiasi *prim = NULL;
  99.  
  100.     printf("Numarul de pachete ce pot fi livrate: "); scanf("%u", &k);
  101.     printf("Numarul de apeluri: "); scanf("%u", &n);
  102.     for (i = 1; i <= n; i++)
  103.         prim = ADD_Call(prim);
  104.     do {
  105.         printf("Ziua %u:\n", days);
  106.         combustibil += 10000;
  107.         if (prio[Very_High]) {
  108.             distParcurs = DistPersImport();
  109.             if ((unsigned)prio[Very_High] > k || (unsigned)distParcurs > combustibil) {
  110.                 printf("Nu se poate ajunge la toate persoanlele");
  111.                 return 0;
  112.             }
  113.            
  114.            
  115.         }
  116.         SetPriority(prim);
  117.         days++;
  118.     } while (1);
  119.     system("pause");
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment