Advertisement
elica123

Untitled

Aug 13th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4.  
  5. typedef struct _stanica{
  6. char ime[61];
  7. double x,y;
  8. int br;
  9. struct _stanica *next;
  10. }stanica;
  11.  
  12. double trosak(stanica* pom1, stanica* pom2){
  13. double rje;
  14. rje=pom1->br/sqrt((((pom2->x)-(pom1->x))*((pom2->x)-(pom1->x)))+(((pom2->y)-(pom1->y))*((pom2->y)-(pom1->y))));
  15. return rje;
  16. }
  17.  
  18. stanica* dodaj_stanicu(stanica* first, stanica* nova){
  19. if(first==NULL) //ekstremni slucajevi
  20. { nova->next=NULL;
  21. return nova;
  22. }
  23. if((first->next)==NULL){
  24. first->next=nova;
  25. nova->next=NULL;
  26. return first;
  27. }
  28. double suma, sumamin=trosak(first, nova)+trosak(nova, first->next);
  29. stanica* pom;
  30. stanica* pom2;
  31. for(pom=first; pom->next!=NULL; pom=pom->next){
  32. suma=trosak(pom, nova)+trosak(nova, pom->next);
  33. if(suma<sumamin){
  34. sumamin=suma;
  35. pom2=pom; // da znamo na koje mjesto ga treba ubacit
  36. }
  37. }
  38. nova->next=(pom2->next);
  39. pom2->next=nova;
  40. return first;
  41. }
  42.  
  43. int izvan(stanica* pom2, stanica* pom1, double r){
  44. double udaljenost=sqrt((((pom2->x)-(pom1->x))*((pom2->x)-(pom1->x)))+(((pom2->y)-(pom1->y))*((pom2->y)-(pom1->y))));
  45. if(udaljenost>r)return 1;
  46. return 0;
  47. }
  48.  
  49. stanica* izdvoji(stanica** first, char *naziv, double rad){
  50. stanica* pom;
  51. stanica* nova;
  52. stanica* pom2=NULL;
  53. for(pom=*first; pom!=NULL; pom=pom->next)
  54. if(!strcmp(naziv, pom->ime)){
  55. pom2=pom;
  56. break;
  57. }
  58. if(pom2==NULL)return NULL;
  59. for(pom=*first; pom->next!=NULL; pom=pom->next){
  60. if(izvan(pom->next, pom2, rad)){
  61. stanica* temp=pom->next;
  62. pom->next=temp->next;
  63. nova=dodaj_stanicu(nova, pom->next);
  64. free(temp);
  65. }
  66. }
  67. if(izvan(pom2, (*first), rad)==1){ //posebna provjera prvog
  68. stanica* temp=(*first);
  69. nova=dodaj_stanicu(nova, (*first));
  70. free(*first);
  71. return temp->next;
  72. }
  73. return nova;
  74. }
  75.  
  76. void ispisi(stanica *first) {
  77. while(first != NULL) {
  78. printf("%s x=%lf y=%lf broj=%d\n",
  79. first->ime, first->x, first->y, first->br);
  80. first = first->next;
  81. }
  82. }
  83.  
  84. int main(void) {
  85. stanica *izbaceni;
  86. stanica s1 = {"prvi", 0, 0, 10, NULL};
  87. stanica s2 = {"drugi", 1, 1, 5, NULL};
  88. stanica s3 = {"treci", -1, 1, 6, NULL};
  89. stanica s4 = {"cetvrti", 1, -1, 7, NULL};
  90. stanica s5 = {"peti", -1, -1, 12, NULL};
  91. stanica s6 = {"sesti", 4, 3, 13, NULL};
  92. stanica s7 = {"sedmi", 3, 2, 14, NULL};
  93.  
  94. stanica *lista = NULL;
  95. lista = dodaj_stanicu(lista, &s1);
  96. lista = dodaj_stanicu(lista, &s2);
  97. lista = dodaj_stanicu(lista, &s3);
  98. lista = dodaj_stanicu(lista, &s4);
  99. lista = dodaj_stanicu(lista, &s5);
  100. lista = dodaj_stanicu(lista, &s6);
  101. lista = dodaj_stanicu(lista, &s7);
  102. printf("originalna lista:\n");
  103. ispisi(lista);
  104.  
  105. izbaceni = izdvoji(&lista, "sedmi", 3.0);
  106. printf("izbacene stanice:\n");
  107. ispisi(izbaceni);
  108.  
  109. printf("preostale stanice:\n");
  110. ispisi(lista);
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement