Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NMAX 100000
  5.  
  6. struct Video
  7. {
  8. int ok;
  9. int size;
  10. };
  11.  
  12. struct Request
  13. {
  14. int id;
  15. int request;
  16. float ratio;
  17. };
  18.  
  19. struct Cache
  20. {
  21. int id;
  22. int lungime;
  23. int nrvideo;
  24. int *video;
  25. };
  26.  
  27. struct Endpoint {
  28. int DataCenterLatency;
  29. int NrCaches;
  30. struct Cache Caches[NMAX];
  31. int NrRequests;
  32. // float Requests[NMAX][3];
  33. struct Request Requests[NMAX];
  34. };
  35.  
  36. int main(int argc, char *argv[]) {
  37.  
  38. FILE* f;
  39. FILE *fout = fopen(argv[2], "w");
  40. f = fopen(argv[1], "r");
  41. if (f == NULL)
  42. return 0;
  43.  
  44. int R,C,X;
  45. int V,E;
  46.  
  47. fscanf(f,"%d %d %d %d %d ", &V, &E, &R, &C, &X);
  48.  
  49.  
  50. struct Endpoint *endpoint;
  51. struct Video *videoclip;
  52. endpoint = (struct Endpoint *) malloc ((E + 1) * sizeof(struct Endpoint));
  53. videoclip = (struct Video *) malloc ((V + 1) * sizeof(struct Video));
  54.  
  55.  
  56. for(int i = 0; i < V; ++i)
  57. fscanf(f, "%d ", &videoclip[i].size);
  58.  
  59.  
  60.  
  61. /* for(int i = 0; i < V; ++i)
  62. printf("%d ", videoclip[i].size);*/
  63.  
  64. for(int i = 0; i < E; i++)
  65. {
  66. endpoint[i].DataCenterLatency = 0;
  67. endpoint[i].NrCaches = 0;
  68. fscanf(f, "%d ", &endpoint[i].DataCenterLatency);
  69. fscanf(f, "%d ", &endpoint[i].NrCaches);
  70. for(int j = 0; j < endpoint[i].NrCaches; ++j)
  71. fscanf(f,"%d %d ", &endpoint[i].Caches[j].id, &endpoint[i].Caches[j].lungime);
  72. }
  73.  
  74. int idVid , endId;
  75. float req;
  76.  
  77. for(int i = 0; i < E; ++i)
  78. endpoint[i].NrRequests = 0;
  79.  
  80. for(int i = 0; i < R; ++i)
  81. {
  82.  
  83. fscanf(f, "%d %d %f ", &idVid, &endId, &req);
  84. // printf("%d %d %d\n", idVid, endId, req);
  85. endpoint[(int)endId].Requests[endpoint[(int)endId].NrRequests].id = idVid;
  86. endpoint[(int)endId].Requests[endpoint[(int)endId].NrRequests].request = req;
  87. endpoint[(int)endId].Requests[endpoint[(int)endId].NrRequests].ratio = (float) req / videoclip[(int)idVid].size;
  88. endpoint[(int)endId].NrRequests++;
  89. }
  90. // printf("test");
  91. /////////AFISARE///////////////
  92.  
  93. /*
  94.  
  95. for(int i = 0; i < E; ++i)
  96. {
  97. printf("%d ", endpoint[i].DataCenterLatency);
  98. printf("%d\n", endpoint[i].NrCaches);
  99.  
  100. for(int j = 0; j < endpoint[i].NrCaches; ++j)
  101. printf("%d %d\n", endpoint[i].Caches[j][0], endpoint[i].Caches[j][1]);
  102. }
  103.  
  104. for(int i = 0; i < E; ++i) {
  105.  
  106. printf("%d %d %d\n",endpoint[i].Requests[endpoint[i].NrRequests][0],
  107. endpoint[i].Requests[endpoint[i].NrRequests][1],
  108. endpoint[i].NrRequests);
  109.  
  110. }
  111. */
  112.  
  113. // sortare
  114. // printf("\n");
  115. int aux, i, j, k;
  116. float auxf;
  117. /*
  118. for (i = 0; i < E; i++)
  119. for (j = 0; j < endpoint[i].NrRequests; j++)
  120. */
  121.  
  122.  
  123.  
  124. for (i = 0; i < E; i++)
  125. for (j = 0; j < endpoint[i].NrCaches - 1; j++)
  126. for (k = j; k < endpoint[i].NrCaches; k++)
  127. if (endpoint[i].Caches[j].lungime > endpoint[i].Caches[k].lungime)
  128. {
  129. aux = endpoint[i].Caches[j].lungime;
  130. endpoint[i].Caches[j].lungime = endpoint[i].Caches[k].lungime;
  131. endpoint[i].Caches[k].lungime = aux;
  132.  
  133. aux = endpoint[i].Caches[j].id;
  134. endpoint[i].Caches[j].id = endpoint[i].Caches[k].id;
  135. endpoint[i].Caches[k].id = aux;
  136. }
  137.  
  138.  
  139. for (i = 0; i < E; i++)
  140. for (j = 0; j < endpoint[i].NrRequests - 1; j++)
  141. for (k = j; k < endpoint[i].NrRequests; k++)
  142. if (endpoint[i].Requests[j].ratio < endpoint[i].Requests[k].ratio)
  143. {
  144. auxf = endpoint[i].Requests[j].ratio;
  145. endpoint[i].Requests[j].ratio = endpoint[i].Requests[k].ratio;
  146. endpoint[i].Requests[k].ratio = auxf;
  147.  
  148. aux = endpoint[i].Requests[j].request;
  149. endpoint[i].Requests[j].request = endpoint[i].Requests[k].request;
  150. endpoint[i].Requests[k].request = aux;
  151.  
  152. aux = endpoint[i].Requests[j].id;
  153. endpoint[i].Requests[j].id = endpoint[i].Requests[k].id;
  154. endpoint[i].Requests[k].id = aux;
  155. }
  156.  
  157. //AFISARE
  158. /* for (i = 0; i < E; i++)
  159. {
  160. printf("-------------- %d -----------\n", i);
  161. printf("REQUESTS --- RATIO\n");
  162. for (j = 0; j < endpoint[i].NrRequests; j++)
  163. {
  164. printf("%d --- %d --- %f\n", endpoint[i].Requests[j].id, endpoint[i].Requests[j].request, endpoint[i].Requests[j].ratio);
  165. }
  166. printf("CACHES\n");
  167. for (j = 0; j < endpoint[i].NrCaches; j++)
  168. {
  169. printf("%d %d\n", endpoint[i].Caches[j].id, endpoint[i].Caches[j].lungime);
  170. }
  171. }
  172. */
  173.  
  174.  
  175.  
  176. int total;
  177. for (i = 0; i < E; i++)
  178. {
  179. total = 0;
  180. j = 0;
  181. //endpoint[i].Requests[j].id sau .ratio
  182. //videoclip[endpoint[i].Requests[j].id].size
  183. endpoint[i].Caches[j].video = (int *) malloc (1000000 * sizeof(int));
  184. total = videoclip[endpoint[i].Requests[j].id].size;
  185. endpoint[i].Caches[j].nrvideo = 1;
  186. endpoint[i].Caches[j].video[0] = endpoint[i].Requests[j].id;
  187. j++;
  188. while(total < X && j < endpoint[i].NrRequests)
  189. {
  190. endpoint[i].Caches[j].video = (int *) malloc (1000000 * sizeof(int));
  191. if(videoclip[endpoint[i].Requests[j].id].ok==0){
  192. total += videoclip[endpoint[i].Requests[j].id].size;
  193. endpoint[i].Caches[j].nrvideo++;
  194. endpoint[i].Caches[j].video[endpoint[i].Caches[j].nrvideo - 1] = endpoint[i].Requests[j].id;
  195. videoclip[endpoint[i].Requests[j].id].ok = 1;
  196. }
  197. j++;
  198. }
  199. if (total > X)
  200. {
  201. j--;
  202. endpoint[i].Caches[j].nrvideo--;
  203. videoclip[endpoint[i].Requests[j].id].ok = 0;
  204. }
  205.  
  206. }
  207.  
  208.  
  209. int m;
  210. fprintf(fout, "%d\n", C);
  211. for (i = 0; i < C; i++)
  212. {
  213. fprintf(fout, "%d ", i);
  214. for (j = 0; j < E; j++)
  215. {
  216. for (k = 0; k < endpoint[j].NrCaches; k++)
  217. if (endpoint[j].Caches[k].id == i)
  218. for (m = 0; m < endpoint[j].Caches[k].nrvideo; m++)
  219. fprintf(fout, "%d ", endpoint[j].Caches[k].video[m]);
  220. }
  221. fprintf(fout, "\n");
  222. }
  223.  
  224.  
  225.  
  226. fclose(f);
  227. fclose(fout);
  228.  
  229.  
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement