Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- #include <fstream>
- #include <sstream>
- #include <string.h>
- #include <iomanip>
- #include <math.h>
- using namespace std;
- void writeToFile(int** tab,int size) {
- ofstream ofs;
- ofs.open("punkty.txt");
- if(ofs.good()){
- for (int i = 0; i < size; i++) {
- ofs << tab[i][0] << " " << tab[i][1] << endl;
- }
- }
- ofs.close();
- }
- void readFromFile(int** tab,int count, string name) {
- char buff[255];
- ifstream ifs;
- ifs.open(name);
- if (ifs.is_open()) {
- for (int i = 0; i < count; i++) {
- ifs.getline(buff, 255);
- string temp = string(buff);
- string x = temp.substr(0,temp.find(" "));
- string y = temp.substr(temp.find(" "));
- tab[i][0] = stoi(x);
- tab[i][1] = stoi(y);
- }
- }
- ifs.close();
- }
- void wypisz(int** tab, int ilosc) {
- for (int i = 0; i < ilosc; i++) cout << "Punkt " << i <<setw(5)<< "x: " << tab[i][0] << "\ty: " << tab[i][1] << endl;
- }
- void wypisz2d(double** tab, int ilosc) {
- for (int i = -1; i < ilosc; i++) {
- for (int j = -1; j < ilosc; j++) {
- if (j < 0) cout << " " << i;
- else if(i<0) cout << setw(9) << " " << j;
- else cout << " | " <<setw(7)<< tab[i][j];
- }
- cout << endl;
- }
- }
- void liczDlg(int** tab, int ilosc,double** wynik) {
- for (int i = 0; i < ilosc; i++) {
- for (int j = 0; j < ilosc; j++) {
- double x = (tab[i][0] - tab[j][0]);
- double y = (tab[i][1] - tab[j][1]);
- wynik[i][j] = sqrt(x*x+y*y );
- }
- }
- }
- void min(int* wynik, double** wyniki,int ilosc) {
- int min = wyniki[0][1];
- for (int i = 0; i < ilosc; i++) {
- for (int j = 0; j < ilosc; j++) {
- if (i != j) {
- if (min > wyniki[i][j]) {
- min = wyniki[i][j];
- wynik[0] = i;
- wynik[1] = j;
- }
- }
- }
- }
- }
- int main() {
- srand(time(NULL));
- int ilosc;
- cout << "Podaj ilosc punktow: ";
- cin >> ilosc;
- int **tab;
- tab= new int*[ilosc];
- for (int i = 0; i < ilosc; i++) tab[i] = new int[2];
- for (int i = 0; i < ilosc; i++) {
- tab[i][0] = rand() % 100;
- tab[i][1] = rand() % 100;
- }
- wypisz(tab, ilosc);
- writeToFile(tab,ilosc);
- int **tab2;
- tab2 = new int*[ilosc];
- for (int i = 0; i < ilosc; i++) tab2[i] = new int[2];
- cout << "\nWypisz: \n";
- readFromFile(tab2,ilosc,"punkty.txt");
- wypisz(tab, ilosc);
- double** dlugosci;
- dlugosci = new double*[ilosc];
- for (int i = 0; i < ilosc; i++) dlugosci[i] = new double[ilosc];
- liczDlg(tab2,ilosc,dlugosci);
- cout << "Macierz dlugosci:\n\n";
- wypisz2d(dlugosci, ilosc);
- int tabWynikow[2];
- min(tabWynikow, dlugosci,ilosc);
- cout << "Najmniejsza odleglosc to: " << dlugosci[tabWynikow[0]][ tabWynikow[1]] << "\nSa to punkty o indeksach: " << tabWynikow[0] << " oraz " << tabWynikow[1]<<endl;
- for (int i = 0; i < ilosc; i++) delete[] tab[i];
- delete[] tab;
- for (int i = 0; i < ilosc; i++) delete[] tab2[i];
- delete[] tab2;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement