Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- typedef struct tacka { int x;
- int y;
- }ttacka;
- struct tacke{ struct tacka inf;
- struct tacke *sledeci;
- }ttacke;
- void prikazizfajla(FILE *f){ rewind(f);
- int d,d1;
- while((fscanf(f,"%d",&d)==1)&&(fscanf(f,"%d",&d1)==1)){
- printf("x:%d, y:%d",d,d1);
- }
- }
- void prikaz(struct tacke *a) {
- struct tacke *temp;
- temp=a;
- while(temp!=NULL) { printf("x:%d, y:%d\n", temp->inf.x,temp->inf.y);
- temp=temp->sledeci;}
- }
- struct tacke* unosustr(struct tacke *b, struct tacka a){ struct tacke *c;
- c=(struct tacke *)malloc(sizeof(struct tacke));
- c->inf=a;
- c->sledeci=b;
- return c;
- }
- struct tacka najbliza(struct tacke *b, struct tacka a){
- if (b!=NULL){
- struct tacke *temp;
- struct tacka x1;
- x1=b->inf;
- temp=b;
- double min;
- min=sqrt((a.x-x1.x)*(a.x-x1.x)+(a.y-x1.y)*(a.y-x1.y));
- 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)
- {min=sqrt((a.x-temp->inf.x)*(a.x-temp->inf.x)+(a.y-temp->inf.y)*(a.y-temp->inf.y));
- x1=temp->inf;}
- temp=temp->sledeci;
- }
- return x1;}
- else return a;
- }
- int main(){
- int d,d1,c;
- FILE *f;
- struct tacke *a,*b,*g;
- struct tacka s,k;
- f=fopen("tacke.txt","r+");
- if ((fscanf(f,"%d",&d)==1)&&(fscanf(f,"%d",&d1)==1)) { a=(struct tacke*)malloc(sizeof(struct tacke));
- a->inf.x=d;
- a->inf.y=d1;}
- g=a;
- b=g;
- while((fscanf(f,"%d",&d)==1)&&(fscanf(f,"%d",&d1)==1)){ a=(struct tacke*)malloc(sizeof(struct tacke));
- b->sledeci=a;
- a->inf.x=d;
- a->inf.y=d1;
- b=a; };
- a->sledeci=NULL;
- while (c!=4){
- printf("za unos:1, za priaz:2,za najblizu:3, za kraj:4\n");
- scanf("%d",&c);
- switch (c){
- case 1: { printf("unesi x\n");
- scanf("%d", &s.x);
- printf("unesi y\n");
- scanf("%d", &s.y);
- g=unosustr(g,s);
- break;
- }
- case 2: { prikaz(g);
- break;
- }
- case 3: { printf("unesi x\n");
- scanf("%d", &s.x);
- printf("unesi y\n");
- scanf("%d", &s.y);
- k=najbliza(g,s);
- printf("x:%d, y:%d\n", k.x,k.y);
- break;
- }
- case 4: break;
- default: {printf("ponovo\n");
- break;
- }
- }
- }
- fclose(f);
- return 0;
- }
Add Comment
Please, Sign In to add comment