Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <iomanip>
- #include <cmath>
- // Zadanie Klasy 2.2
- using namespace std;
- class Punkt{
- double x,y;
- public:
- Punkt();
- Punkt(double, double);
- void Ustaw(double, double);
- void Wyswietl()const; //format x.xxx
- double Odleglosc(Punkt p)const;
- };
- class Lamana{
- int ilePkt;
- Punkt *p = new Punkt[ilePkt]; // Punkt p[100]
- public:
- Lamana(int);
- void UstawIle(int);
- void UstawK(int, double, double);
- bool Wczytaj(string);
- void Wyswietl()const;
- double Dlugosc()const;
- };
- Punkt::Punkt(){};
- Punkt::Punkt(double x, double y) {
- this -> x = x;
- this -> y = y;
- }
- void Punkt::Ustaw(double x, double y) {
- this -> x = x;
- this -> y = y;
- }
- void Punkt::Wyswietl() const {
- cout << setprecision(3) << fixed << "[" << x << " , " << y << "]" << endl;
- }
- double Punkt::Odleglosc(Punkt p) const {
- return abs(sqrt((x-p.x)*(x-p.x) + (y-p.y)*(y-p.y)));
- }
- Lamana::Lamana(int ile) {
- ilePkt = ile;
- }
- void Lamana::UstawIle(int ile) {
- ilePkt = ile;
- }
- void Lamana::UstawK(int nr, double x, double y) {
- Lamana::p[nr].Ustaw(x,y);
- }
- bool Lamana::Wczytaj(string nazwa) {
- fstream inFile;
- inFile.open(nazwa);
- if(!inFile.good()) return false;
- double x,y;
- int i = 0;
- char tmp;
- while(inFile >> tmp && i < ilePkt){
- inFile >> tmp >> x >> tmp >> y >> tmp >> tmp;
- UstawK(i, x, y);
- i++;
- }
- inFile.close();
- return i = ilePkt;
- }
- void Lamana::Wyswietl() const {
- for(int i = 0; i < ilePkt; i++) {
- p[i].Wyswietl();
- }
- }
- double Lamana::Dlugosc() const {
- double s = 0.0;
- for(int i = 1; i < ilePkt; i++)
- s += p[i].Odleglosc(p[i-1]);
- return s;
- }
- int main() {
- Lamana l(5);
- string nazwa = "F:\\Sem 3\\Programowanie Obiektowe\\Lekcja 6\\c2.2\\dane.txt"; //musze podac pelna sciezke do pliku
- l.Wczytaj(nazwa);
- l.Wyswietl();
- cout << "Dlugosc " << l.Dlugosc();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement