Advertisement
Wojtekd

LAB 9

May 5th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_W 25
  5.  
  6. struct point
  7. {
  8.     int x, y;
  9. };
  10.  
  11. void czytaj_do_tablicy(char* tab[])
  12. {
  13.     char buffer[100];
  14.     int i;
  15.     for(i = 0; i < MAX_W; i++) 
  16.     {
  17.         if(fgets(buffer,sizeof(buffer),stdin))
  18.         {
  19.             tab[i] = malloc((strlen(buffer) + 1)*sizeof(char));
  20.             strcpy(tab[i],buffer);
  21.         }
  22.         else
  23.         {
  24.             return;
  25.         }      
  26.     }
  27. }
  28. void inicjuj_punkty(struct point* points[])
  29. {
  30.     int i;
  31.     for(i = 0; i < MAX_W; i++)
  32.     {
  33.         points[i] = (struct point*)malloc((i+1)*sizeof(struct point));
  34.     }
  35. }
  36. void wypiszArgument( char* arg )
  37. {
  38.     printf("%s", arg);
  39. }
  40. void wypiszArgumentV2( char* arg, void (*f)(char*))
  41. {
  42.     f(arg);
  43. }
  44. int comparePoints(const void* e1, const void* e2)
  45. {
  46.     struct point* p1 = (struct point*)e1;
  47.     struct point* p2 = (struct point*)e2;
  48.    
  49.     float d1 = sqrt(p1->x*p1->x + p1->y*p1->y);
  50.     float d2 = sqrt(p2->x*p2->x + p2->y*p2->y);
  51.    
  52.     if(d1 > d2) return 1;
  53.     if(d1 < d2) return -1;
  54.     if(d1 == d2) return 0; 
  55. }
  56. int main ( int argc, char *argv[] )
  57. {
  58.    
  59.     /* zadanie 1
  60.     char *tab[MAX_W];
  61.     czytaj_do_tablicy(tab);
  62.     */
  63.    
  64.     /* zadanie 2
  65.     struct point *points[MAX_W];
  66.    
  67.     inicjuj_punkty(points);
  68.    
  69.     points[10][5].x = 0;
  70.     (*(*(points + 10) + 5)).x = 0;
  71.     */
  72.    
  73.     /* zadanie 3
  74.     int i;
  75.     for(i = 1; i < argc; ++i)
  76.     {
  77.         printf("%s\n",argv[i]);
  78.     }
  79.     */
  80.     /* zadanie 4    
  81.     wypiszArgumentV2("blabla",wypiszArgument);
  82.     */
  83.    
  84.     /* zadanie 5 */
  85.    
  86.     int ilosc = 100;
  87.     struct point* points = (struct point*)malloc(ilosc*sizeof(struct point));
  88.     int i;
  89.     for(i = 0; i < ilosc; ++i)
  90.     {
  91.         int a = points[i].x = rand() % 101;
  92.         int b = points[i].y = rand() % 101;    
  93.     }
  94.    
  95.    
  96.     puts("przed sortowaniem");
  97.     for(i = 0; i < ilosc; ++i)
  98.     {
  99.         struct point c = points[i];
  100.        
  101.         float d = sqrt(c.x*c.x + c.y*c.y);
  102.         printf("%f\n",d);
  103.     }
  104.    
  105.     qsort(points,ilosc,sizeof(struct point),comparePoints);
  106.    
  107.     puts("po sortowaniu"); 
  108.     for(i = 0; i < ilosc; ++i)
  109.     {
  110.         struct point c = points[i];
  111.        
  112.         float d = sqrt(c.x*c.x + c.y*c.y);
  113.         printf("%f\n",d);
  114.     }
  115.        
  116.     free(points);
  117.    
  118.    
  119.     system("pause");
  120.    
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement