Advertisement
Guest User

Untitled

a guest
May 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include<math.h>
  5. struct punkt
  6. {
  7.     double x,y,z;
  8. };
  9.  
  10. void wypisz( struct punkt t[],int x)
  11. {
  12.     int i;
  13.     for(i=0;i<x;i++)
  14.     {
  15.         printf("Punkt ");
  16.         printf("%d", i+1);
  17.         printf(": ");
  18.         printf("%lf", t[i].x);
  19.         printf(", ");
  20.         printf("%lf", t[i].y);
  21.         printf(", ");
  22.         printf("%lf", t[i].z);
  23.         printf(", ");
  24.         printf("\n");
  25.     }
  26. }
  27. void wypelnijrecznie( struct punkt tab[], int i)
  28. {
  29.     int x;
  30.     printf("Wypelnij tablice swoimi danymi: \n");
  31.     for(x=0;x<i;x++)
  32.     {
  33.         printf("Kolejny punkt\n");
  34.         printf("X: ");
  35.         scanf("%lf", &tab[x].x);
  36.         printf("Y: ");
  37.         scanf("%lf", &tab[x].y);
  38.         printf("Z: ");
  39.         scanf("%lf", &tab[x].z);
  40.     }
  41. }
  42. void wypelnijlosowo( struct punkt tab[], int i)
  43. {
  44.     int x;
  45.     for(x=0;x<=i;x++)
  46.     {
  47.         tab[x].x=((rand() % 10) + 0)+0.1*(rand() % 10)+0.01*(rand() % 10)+0.001*(rand() % 10)+0.0001*(rand() % 10)+0.00001*(rand() % 10)+0.000001*(rand() % 10);
  48.         tab[x].y=((rand() % 10) + 0)+0.1*(rand() % 10)+0.01*(rand() % 10)+0.001*(rand() % 10)+0.0001*(rand() % 10)+0.00001*(rand() % 10)+0.000001*(rand() % 10);
  49.         tab[x].z=((rand() % 10) + 0)+0.1*(rand() % 10)+0.01*(rand() % 10)+0.001*(rand() % 10)+0.0001*(rand() % 10)+0.00001*(rand() % 10)+0.000001*(rand() % 10);
  50.     }
  51. }
  52. void odleglosc2pkt()
  53. {
  54.     double x,y,z,x1,y1,z1,odl;
  55.         printf("Punkt 1:\n");
  56.         printf("X: ");
  57.         scanf("%lf", &x);
  58.         printf("Y: ");
  59.         scanf("%lf", &y);
  60.         printf("Z: ");
  61.         scanf("%lf", &z);
  62.         printf("Punkt 2:\n");
  63.         printf("X: ");
  64.         scanf("%lf", &x1);
  65.         printf("Y: ");
  66.         scanf("%lf", &y1);
  67.         printf("Z: ");
  68.         scanf("%lf", &z1);
  69.         odl=sqrt(pow(x1-x,2) + pow(y1-y,2) + pow(z1-z,2));
  70.         printf("Odleglosc miedzy podanymi punktami to: ");
  71.         printf("%lf", odl);
  72.         printf("\n");
  73. }
  74. void sortowanie(struct punkt tab[], int i)
  75. {
  76.     int a,b=0,c;
  77.     double max,akt,pom;
  78.     for(b=0;b<i;b++)
  79.     {
  80.         max=sqrt(pow(tab[b].x,2) + pow(tab[b].y,2) + pow(tab[b].z,2));
  81.         for(a=0;a<=i;a++)
  82.         {
  83.             akt=sqrt(pow(tab[a].x,2) + pow(tab[a].y,2) + pow(tab[a].z,2));
  84.             if(akt>max)
  85.             {
  86.                 max=akt;
  87.                 c=a;
  88.             }
  89.         }
  90.         if((c=!b))
  91.         {
  92.             pom=tab[c].x;
  93.             tab[b].x=tab[c].x;
  94.             tab[b].x=pom;
  95.             pom=tab[c].y;
  96.             tab[b].y=tab[c].y;
  97.             tab[b].y=pom;
  98.             pom=tab[c].z;
  99.             tab[b].z=tab[c].z;
  100.             tab[b].z=pom;
  101.         }
  102.     }
  103. }
  104. void dodaj(struct punkt *tab, int rozm)
  105. {
  106.     tab =(struct punkt*) realloc(tab, (rozm+1)*sizeof(struct punkt));
  107. }
  108.  
  109. int main()
  110. {
  111.     srand(time(NULL));
  112.     int size,a;
  113.     printf("Jakiej wielkosci tablice chcesz stworzyc? \n");
  114.     scanf("%d", &size);
  115.     struct punkt * tablica = malloc(size*sizeof(struct punkt));
  116.     while (1)
  117.     {
  118.         printf("'1' - Wypelnij zadana tablice punktow wartosciami podanymi przez uzytkownika\n");
  119.         printf("'2' - Wypelnij zadana tablice punktow losowymi wartosciami [0.00;10.00]\n");
  120.         printf("'3' - Wyznaczanie odleglosci miedzy dwoma punktami podanymi przez uzytkownika\n");
  121.         printf("'4' - Sortuje zadana tablice wedlug odleglosci punktow od punktu (0,0,0)\n");
  122.         printf("'5' - Dodaj element na koniec tablicy\n");
  123.         printf("'6' - Usuwa pierwszy element tablicy\n");
  124.         printf("'7' - Dodaje element na podane miejsce w tablicy\n");
  125.         printf("'8' - Wyswietl tablice na ekranie\n");
  126.         printf("'0' - Zakoncz dzialanie programu\n");
  127.         printf("Podaj opcje, ktora chcesz wykonac: ");
  128.         scanf("%d", &a);
  129.             switch (a)
  130.             {
  131.                 case 0:
  132.                     printf("\n");
  133.                     printf("Wylaczam sie! Milego Dnia!");
  134.                     exit(0);
  135.                 case 1:
  136.                     wypelnijrecznie(tablica,size);
  137.                     break;
  138.                 case 2:
  139.                     wypelnijlosowo(tablica,size);
  140.                     break;
  141.                 case 3:
  142.                     odleglosc2pkt();
  143.                     break;
  144.                 case 4:
  145.                     sortowanie(tablica,size);
  146.                     break;
  147.                 case 5:
  148.                     dodaj(tablica,size++);
  149.                     break;
  150.                 case 6:
  151.                     break;
  152.                 case 7:
  153.                     break;
  154.                 case 8:
  155.                     wypisz(tablica, size);
  156.                     break;
  157.                 default: printf("Nie ma takiego polecenia do wyboru\n");
  158.             }
  159.     }
  160.     free(tablica);
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement