Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //napisz program majac strukture który stworzy i zwróci tablicê dwóch wektorów, punkt (0.0) pomiezy promieniem r, ma znajdowac sie w tym okrêgu, sciezka w parametrze do pliku i promien tez.
- struct punkt
- {
- double x,y;
- };
- struct punkt* funkcja (char *s,int r, int *ilosc_w)
- {
- FILE *p;
- p = fopen(s,"r");
- if(!p){ printf("blad"); exit(-1);}
- double x,z;
- int ilosc =0;
- while(fscanf(p,"%lf %lf",&x,&z)!=EOF) ilosc++;
- fclose(p);
- struct punkt* tmp=NULL;
- tmp=(struct punkt*)malloc(ilosc*sizeof(struct punkt));
- p = fopen(s,"r");
- *ilosc_w=0;int i;
- for (i=0;i<ilosc;i++)
- {
- fscanf(p,"%lf %lf",&tmp[i].x,&tmp[i].y);
- if( (tmp[i].x*tmp[i].x+(tmp[i].y*tmp[i].y)<=(r*r))) (*ilosc_w)++;
- }
- fclose(p);
- struct punkt* wynik=NULL;
- wynik=(struct punkt*)malloc((*ilosc_w)*sizeof(struct punkt));
- int y=0;
- for(i=0;i<ilosc;i++)
- {
- if( (tmp[i].x*tmp[i].x+(tmp[i].y*tmp[i].y)<=(r*r)))
- {
- wynik[y].x=tmp[i].x;
- wynik[y++].y=tmp[i].y;
- }
- }
- free(tmp); tmp=NULL;
- return wynik;
- }
- int main()
- {
- char s[] = "plik.txt";
- int ilosc_w;
- struct punkt* p1;
- p1=funkcja(s,3,&ilosc_w);
- int i;
- for(i=0;i<ilosc_w;i++)
- {
- printf("%lf %lf\n",p1[i].x,p1[i].y);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement