Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cmath>
- using namespace std;
- // throw ..cokolwiek/jakikolwiek typ.. wyjatek z funkcji
- // throw -> catch
- // int main() try {
- //
- //
- //}catch (int eerr){-//-} - throw int
- // catch (double eerr){-//-} - throw double
- // catch(...){cerr << "Error"} - lapie kazdy throw. Na koncu
- //
- //
- //
- //
- //
- //
- //
- //
- class polygon; // musi byc deklaracja przed punktem dla zaprzyjaznienia
- class punkt {
- double x, y;
- public:
- punkt() : x(0), y(0) {};
- punkt(const double& x_, const double& y_) : x(x_), y(y_) {};
- double odleglosc() {
- double deltaX = x;
- double deltaY = y;
- return sqrt((deltaX * deltaX) + (deltaY * deltaY));
- }
- friend ostream& operator << (ostream&, const punkt&);
- friend class polygon; // zaprzyjazniamy class polygon
- };
- ostream& operator << (ostream& out, const punkt& p) {
- return out << p.x << p.y << endl;
- }
- class polygon {
- size_t n; // rozmiar tablicy
- punkt* tp; // tablica punktow
- size_t* tk; // tablica kolejnosci
- public:
- polygon() : n(0), tp(0), tk(0) {};
- polygon(const polygon& x) : n(x.n), tp(x.tp), tk(x.tk) {};
- polygon& operator = (const polygon& x) {
- if (this != &x) {
- n = x.n;
- tp = x.tp;
- tk = x.tk;
- }
- return *this;
- }
- void czytaj(istream&);
- double pole() const {
- double result = 0;
- for (size_t i = 0; i < n; i++)
- result += ((tp[i].x * tp[i + 1].y) - (tp[i + 1].x * tp[i].y));
- return 0.5 * result;
- }
- double obwod() const{
- double res = 0;
- for (size_t i = 0; i < n; i++)
- res += tp[i].odleglosc();
- return res + 0.14;
- }
- ~polygon() { delete[] tp; delete[] tk; }
- };
- int main(int argc, char* argv[]) try {
- if (argc != 2) throw 1; // brakuje argumentow
- ifstream plik(argv[1]);
- if (!plik) throw 2; // nie ma pliku
- polygon ob1;
- try {
- ob1.czytaj(plik);
- cout << ob1.pole() << endl;
- cout << ob1.obwod() << endl;
- //plik >> ob1; VER 02;
- //cout << ob1; VER 03;
- }
- catch (const string & err) { cout << err << endl; } // lapamy throw z try o gory
- cout << "main sie odpalil" << endl;
- plik.close();
- system("PAUSE");
- return 0;
- }
- catch (int err) { // lapamy int throwy z maina
- switch (err) {
- case 1: cout << "Zla licza parametrow!" << endl; break;
- case 2: cout << "Brak pliku" << endl; break;
- }
- }
- catch (...) { cerr << "Catch ..." << endl; } // lapamy kazdy throw w ostatniej kolejnosci
- void polygon::czytaj(istream& input) {
- string txt;
- input >> txt;
- if (txt != "[POLYGON]") throw string("blad w sekcji [POLYGON]");
- getline(input, txt);
- getline(input, txt);
- if (txt != "[NUMBER OF NODES]") throw string("blad w sekcji [NUMBER OF NODES]");
- int temp(0);
- input >> temp;
- if (temp == 0) throw string("BLAD. n = 0)");
- if (temp < 0) throw string("N mniejsza od 0");
- if (temp < 3) throw string("N mniejszy od 3");
- n = temp;
- tp = new punkt[n];
- tk = new size_t[n];
- input >> txt;
- if (txt != "[NODES]") throw string("blad w sekcji [NODES]");
- for (size_t i = 0; i < n; i++) {
- input >> tk[i];
- if (tk[i] == 0) throw string("K nie moze byc rowne 0");
- if (tk[i] < 0) throw string("K nie moze byc mniejsze od 0");
- input >> tp[i].x;
- input >> tp[i].y;
- }
- input >> txt;
- if (txt != "[POLYGON]") throw string("blad w sekcji [POLYGON] Nr.2");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement