Guest User

Untitled

a guest
May 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. typedef struct tacka { int x;
  5. int y;
  6. }ttacka;
  7. struct tacke{ struct tacka inf;
  8. struct tacke *sledeci;
  9. }ttacke;
  10. void prikazizfajla(FILE *f){ rewind(f);
  11. int d,d1;
  12. while((fscanf(f,"%d",&d)==1)&&(fscanf(f,"%d",&d1)==1)){
  13. printf("x:%d, y:%d",d,d1);
  14. }
  15. }
  16. void prikaz(struct tacke *a) {
  17. struct tacke *temp;
  18. temp=a;
  19. while(temp!=NULL) { printf("x:%d, y:%d\n", temp->inf.x,temp->inf.y);
  20. temp=temp->sledeci;}
  21. }
  22. struct tacke* unosustr(struct tacke *b, struct tacka a){ struct tacke *c;
  23. c=(struct tacke *)malloc(sizeof(struct tacke));
  24. c->inf=a;
  25. c->sledeci=b;
  26. return c;
  27. }
  28. struct tacka najbliza(struct tacke *b, struct tacka a){
  29. if (b!=NULL){
  30. struct tacke *temp;
  31. struct tacka x1;
  32. x1=b->inf;
  33. temp=b;
  34. double min;
  35. min=sqrt((a.x-x1.x)*(a.x-x1.x)+(a.y-x1.y)*(a.y-x1.y));
  36. while (temp!=NULL){ if (sqrt((a.x-temp->inf.x)*(a.x-temp->inf.x)+(a.y-temp->inf.y)*(a.y-temp->inf.y))<min)
  37. {min=sqrt((a.x-temp->inf.x)*(a.x-temp->inf.x)+(a.y-temp->inf.y)*(a.y-temp->inf.y));
  38. x1=temp->inf;}
  39. temp=temp->sledeci;
  40. }
  41. return x1;}
  42. else return a;
  43. }
  44. int main(){
  45. int d,d1,c;
  46. FILE *f;
  47. struct tacke *a,*b,*g;
  48. struct tacka s,k;
  49. f=fopen("tacke.txt","r+");
  50. if ((fscanf(f,"%d",&d)==1)&&(fscanf(f,"%d",&d1)==1)) { a=(struct tacke*)malloc(sizeof(struct tacke));
  51. a->inf.x=d;
  52. a->inf.y=d1;}
  53. g=a;
  54. b=g;
  55. while((fscanf(f,"%d",&d)==1)&&(fscanf(f,"%d",&d1)==1)){ a=(struct tacke*)malloc(sizeof(struct tacke));
  56. b->sledeci=a;
  57. a->inf.x=d;
  58. a->inf.y=d1;
  59. b=a; };
  60. a->sledeci=NULL;
  61. while (c!=4){
  62. printf("za unos:1, za priaz:2,za najblizu:3, za kraj:4\n");
  63. scanf("%d",&c);
  64. switch (c){
  65. case 1: { printf("unesi x\n");
  66. scanf("%d", &s.x);
  67. printf("unesi y\n");
  68. scanf("%d", &s.y);
  69. g=unosustr(g,s);
  70. break;
  71. }
  72. case 2: { prikaz(g);
  73. break;
  74. }
  75. case 3: { printf("unesi x\n");
  76. scanf("%d", &s.x);
  77. printf("unesi y\n");
  78. scanf("%d", &s.y);
  79. k=najbliza(g,s);
  80. printf("x:%d, y:%d\n", k.x,k.y);
  81. break;
  82. }
  83. case 4: break;
  84. default: {printf("ponovo\n");
  85. break;
  86. }
  87.  
  88. }
  89. }
  90. fclose(f);
  91. return 0;
  92. }
Add Comment
Please, Sign In to add comment