Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- #include <string>
- #include <iostream>
- int main()
- {
- char buf[16], war = 0;
- srand( time( NULL ) );
- int n = 0;
- printf("podaj liczbe punktow do wygenerowania\n");
- scanf("%d", &n);
- while(1)
- {
- printf("wyswietlac wygenerowane punkty? [T/N]\n");
- std::cin >> war;
- if(war == 'T' || war == 't' || war == 'N' || war == 'n')
- break;
- }
- struct punkt
- {
- double x, y, z;
- std::string nazwa;
- double odleglosc(const punkt punkty)
- {
- return sqrt((punkty.x - x) * (punkty.x - x) + (punkty.y - y) * (punkty.y - y) + (punkty.z - z) * (punkty.z - z));
- }
- };
- punkt *punkty = new punkt[n];
- if(war == 'T' || war == 't')
- printf("%9s%9s%9s%9s\n", "x", "y", "z", "nazwa");
- for (int i = 0; i < n; i++)
- {
- punkty[i].nazwa = "0x";
- punkty[i].x = (rand() % 200) - 100;
- punkty[i].y = (rand() % 200) - 100;
- punkty[i].z = (rand() % 200) - 100;
- punkty[i].nazwa.append(itoa(i, buf, 16));
- if(war == 'T' || war == 't')
- printf("%9.3lf%9.3lf%9.3lf%9s\n", punkty[i].x, punkty[i].y, punkty[i].z, punkty[i].nazwa.c_str());
- }
- int licznik = 0;
- int *odleglosci = new int[(n * n - n) / 2]; //liczba odległości do przechowywania to kombinacja 2 elementowa ze zbioru n elementowego
- double x1_min = punkty[0].x, y1_min = punkty[0].y, z1_min = punkty[0].z, odleglosc_min = punkty[0].odleglosc(punkty[1]);
- double x2_min = punkty[0].x, y2_min = punkty[0].y, z2_min = punkty[0].z;
- std::string nazwa_min1 = punkty[0].nazwa.c_str();
- std::string nazwa_min2 = punkty[0].nazwa.c_str();
- double x1_max = punkty[0].x, y1_max = punkty[0].y, z1_max = punkty[0].z, odleglosc_max = punkty[0].odleglosc(punkty[1]);
- double x2_max = punkty[0].x, y2_max = punkty[0].y, z2_max = punkty[0].z;
- std::string nazwa_max1 = punkty[0].nazwa.c_str();
- std::string nazwa_max2 = punkty[0].nazwa.c_str();
- for (int indeks_poczatku = 0; indeks_poczatku < n - 1; indeks_poczatku++)
- {
- for (int i = indeks_poczatku + 1; i < n; i++,licznik++)
- {
- odleglosci[licznik] = punkty[indeks_poczatku].odleglosc(punkty[i]);
- if(punkty[indeks_poczatku].odleglosc(punkty[i]) < odleglosc_min)
- {
- x1_min = punkty[indeks_poczatku].x;
- x2_min = punkty[i].x;
- y1_min = punkty[indeks_poczatku].y;
- y2_min = punkty[i].y;
- z1_min = punkty[indeks_poczatku].z;
- z2_min = punkty[i].z;
- odleglosc_min = punkty[indeks_poczatku].odleglosc(punkty[i]);
- nazwa_min1 = punkty[indeks_poczatku].nazwa.c_str();
- nazwa_min2 = punkty[i].nazwa.c_str();
- }
- if(punkty[indeks_poczatku].odleglosc(punkty[i]) > odleglosc_max)
- {
- x1_max = punkty[indeks_poczatku].x;
- x2_max = punkty[i].x;
- y1_max = punkty[indeks_poczatku].y;
- y2_max = punkty[i].y;
- z1_max = punkty[indeks_poczatku].z;
- z2_max = punkty[i].z;
- odleglosc_max = punkty[indeks_poczatku].odleglosc(punkty[i]);
- nazwa_max1 = punkty[indeks_poczatku].nazwa.c_str();
- nazwa_max2 = punkty[i].nazwa.c_str();
- }
- }
- }
- printf("punkty polozone najblizej siebie\n\n");
- printf("%9s%9s%9s%9s%9s\n", "nazwa", "x", "y", "z","odleglosc");
- printf("%9s%9.3lf%9.3lf%9.3lf%9.3lf\n", nazwa_min1, x1_min, y1_min, z1_min, odleglosc_min);
- printf("%9s%9.3lf%9.3lf%9.3lf\n\n\n", nazwa_min2, x2_min, y2_min, z2_min);
- printf("punkty polozone najdalej od siebie\n\n");
- printf("%9s%9s%9s%9s%9s\n", "nazwa", "x", "y", "z","odleglosc");
- printf("%9s%9.3lf%9.3lf%9.3lf%9.3lf\n", nazwa_max1, x1_max, y1_max, z1_max, odleglosc_max);
- printf("%9s%9.3lf%9.3lf%9.3lf\n\n\n", nazwa_max2, x2_max, y2_max, z2_max);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement