Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. //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.
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. struct punkt
  40. {
  41.     double x,y;
  42. };
  43.  
  44. struct punkt* funkcja (char *s,int r, int *ilosc_w)
  45. {
  46.     FILE *p;
  47.     p = fopen(s,"r");
  48.     if(!p){ printf("blad"); exit(-1);}
  49.     double x,z;
  50.     int ilosc =0;
  51.     while(fscanf(p,"%lf %lf",&x,&z)!=EOF) ilosc++;
  52.     fclose(p);
  53.  
  54.     struct punkt* tmp=NULL;
  55.     tmp=(struct punkt*)malloc(ilosc*sizeof(struct punkt));
  56.     p = fopen(s,"r");
  57.  
  58.     *ilosc_w=0;int i;
  59.     for (i=0;i<ilosc;i++)
  60.     {
  61.         fscanf(p,"%lf %lf",&tmp[i].x,&tmp[i].y);
  62.         if( (tmp[i].x*tmp[i].x+(tmp[i].y*tmp[i].y)<=(r*r))) (*ilosc_w)++;
  63.     }
  64.  
  65.     fclose(p);
  66.  
  67.     struct punkt* wynik=NULL;
  68.     wynik=(struct punkt*)malloc((*ilosc_w)*sizeof(struct punkt));
  69.  
  70.     int y=0;
  71.  
  72.     for(i=0;i<ilosc;i++)
  73.     {
  74.         if( (tmp[i].x*tmp[i].x+(tmp[i].y*tmp[i].y)<=(r*r)))
  75.         {
  76.             wynik[y].x=tmp[i].x;
  77.             wynik[y++].y=tmp[i].y;
  78.         }
  79.     }
  80.     free(tmp); tmp=NULL;
  81.     return wynik;
  82. }
  83.  
  84. int main()
  85. {
  86.     char s[] = "plik.txt";
  87.     int ilosc_w;
  88.     struct punkt* p1;
  89.     p1=funkcja(s,3,&ilosc_w);
  90.  
  91.     int i;
  92.  
  93.     for(i=0;i<ilosc_w;i++)
  94.     {
  95.         printf("%lf %lf\n",p1[i].x,p1[i].y);
  96.     }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement