Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <cmath>
  6.  
  7. // Zadanie Klasy 2.2
  8. using namespace std;
  9.  
  10. class Punkt{
  11. double x,y;
  12. public:
  13. Punkt();
  14. Punkt(double, double);
  15. void Ustaw(double, double);
  16. void Wyswietl()const; //format x.xxx
  17. double Odleglosc(Punkt p)const;
  18. };
  19. class Lamana{
  20. int ilePkt;
  21. Punkt *p = new Punkt[ilePkt]; // Punkt p[100]
  22. public:
  23. Lamana(int);
  24. void UstawIle(int);
  25. void UstawK(int, double, double);
  26. bool Wczytaj(string);
  27. void Wyswietl()const;
  28. double Dlugosc()const;
  29. };
  30. Punkt::Punkt(){};
  31. Punkt::Punkt(double x, double y) {
  32. this -> x = x;
  33. this -> y = y;
  34. }
  35. void Punkt::Ustaw(double x, double y) {
  36. this -> x = x;
  37. this -> y = y;
  38. }
  39. void Punkt::Wyswietl() const {
  40. cout << setprecision(3) << fixed << "[" << x << " , " << y << "]" << endl;
  41. }
  42. double Punkt::Odleglosc(Punkt p) const {
  43. return abs(sqrt((x-p.x)*(x-p.x) + (y-p.y)*(y-p.y)));
  44. }
  45. Lamana::Lamana(int ile) {
  46. ilePkt = ile;
  47. }
  48. void Lamana::UstawIle(int ile) {
  49. ilePkt = ile;
  50. }
  51.  
  52. void Lamana::UstawK(int nr, double x, double y) {
  53. Lamana::p[nr].Ustaw(x,y);
  54. }
  55. bool Lamana::Wczytaj(string nazwa) {
  56. fstream inFile;
  57. inFile.open(nazwa);
  58. if(!inFile.good()) return false;
  59.  
  60. double x,y;
  61. int i = 0;
  62. char tmp;
  63. while(inFile >> tmp && i < ilePkt){
  64. inFile >> tmp >> x >> tmp >> y >> tmp >> tmp;
  65. UstawK(i, x, y);
  66. i++;
  67. }
  68. inFile.close();
  69. return i = ilePkt;
  70.  
  71. }
  72. void Lamana::Wyswietl() const {
  73. for(int i = 0; i < ilePkt; i++) {
  74. p[i].Wyswietl();
  75. }
  76. }
  77. double Lamana::Dlugosc() const {
  78. double s = 0.0;
  79. for(int i = 1; i < ilePkt; i++)
  80. s += p[i].Odleglosc(p[i-1]);
  81. return s;
  82. }
  83.  
  84. int main() {
  85. Lamana l(5);
  86.  
  87. string nazwa = "F:\\Sem 3\\Programowanie Obiektowe\\Lekcja 6\\c2.2\\dane.txt"; //musze podac pelna sciezke do pliku
  88. l.Wczytaj(nazwa);
  89. l.Wyswietl();
  90.  
  91. cout << "Dlugosc " << l.Dlugosc();
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement