Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_W 25
- struct point
- {
- int x, y;
- };
- void czytaj_do_tablicy(char* tab[])
- {
- char buffer[100];
- int i;
- for(i = 0; i < MAX_W; i++)
- {
- if(fgets(buffer,sizeof(buffer),stdin))
- {
- tab[i] = malloc((strlen(buffer) + 1)*sizeof(char));
- strcpy(tab[i],buffer);
- }
- else
- {
- return;
- }
- }
- }
- void inicjuj_punkty(struct point* points[])
- {
- int i;
- for(i = 0; i < MAX_W; i++)
- {
- points[i] = (struct point*)malloc((i+1)*sizeof(struct point));
- }
- }
- void wypiszArgument( char* arg )
- {
- printf("%s", arg);
- }
- void wypiszArgumentV2( char* arg, void (*f)(char*))
- {
- f(arg);
- }
- int comparePoints(const void* e1, const void* e2)
- {
- struct point* p1 = (struct point*)e1;
- struct point* p2 = (struct point*)e2;
- float d1 = sqrt(p1->x*p1->x + p1->y*p1->y);
- float d2 = sqrt(p2->x*p2->x + p2->y*p2->y);
- if(d1 > d2) return 1;
- if(d1 < d2) return -1;
- if(d1 == d2) return 0;
- }
- int main ( int argc, char *argv[] )
- {
- /* zadanie 1
- char *tab[MAX_W];
- czytaj_do_tablicy(tab);
- */
- /* zadanie 2
- struct point *points[MAX_W];
- inicjuj_punkty(points);
- points[10][5].x = 0;
- (*(*(points + 10) + 5)).x = 0;
- */
- /* zadanie 3
- int i;
- for(i = 1; i < argc; ++i)
- {
- printf("%s\n",argv[i]);
- }
- */
- /* zadanie 4
- wypiszArgumentV2("blabla",wypiszArgument);
- */
- /* zadanie 5 */
- int ilosc = 100;
- struct point* points = (struct point*)malloc(ilosc*sizeof(struct point));
- int i;
- for(i = 0; i < ilosc; ++i)
- {
- int a = points[i].x = rand() % 101;
- int b = points[i].y = rand() % 101;
- }
- puts("przed sortowaniem");
- for(i = 0; i < ilosc; ++i)
- {
- struct point c = points[i];
- float d = sqrt(c.x*c.x + c.y*c.y);
- printf("%f\n",d);
- }
- qsort(points,ilosc,sizeof(struct point),comparePoints);
- puts("po sortowaniu");
- for(i = 0; i < ilosc; ++i)
- {
- struct point c = points[i];
- float d = sqrt(c.x*c.x + c.y*c.y);
- printf("%f\n",d);
- }
- free(points);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement