Advertisement
Guest User

6

a guest
Nov 20th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1.  #include <iostream>
  2.  #include <fstream>
  3.  using namespace std;
  4.  
  5.  
  6. class blad
  7. {
  8.     int a;
  9.     string s;
  10.  
  11.   public:
  12.  
  13.     blad(): a(0), s("...") {}
  14.  
  15.     blad(const int &b, const string &w): a(b), s(w) {}
  16.  
  17.  
  18.     friend ostream & operator << (ostream& out, const blad & r);
  19. };
  20. ostream & operator << (ostream& out, const blad & r)
  21. {
  22.   return out << "LINIA " << r.a << ": " << r.s << endl;
  23. }
  24.  
  25. class polygon;
  26. class punkt
  27. {
  28.   double x,y;
  29.  
  30. public:
  31.  
  32.   punkt(): x(0) , y(0) {}
  33.  
  34.   punkt(const double & a, const double &b): x(a), y(b) {}
  35.  
  36.  
  37.   friend class polygon;
  38.   //friend <<
  39.   //firend >>
  40. };
  41.  
  42.  
  43.  
  44. class polygon
  45. {
  46.     unsigned roz;
  47.     punkt * tp; // tablica punktow
  48.     unsigned * tk; // tablica kolejnosci
  49.  
  50.   public:
  51.  
  52.     polygon(): roz(0), tp(0), tk(0) {};
  53.  
  54.  
  55.     polygon(const polygon &r); // konstruktor kopiujacy
  56.     polygon & operator = (const polygon &r);  // operator przypisania
  57.  
  58.  
  59.     ~polygon()
  60.     {
  61.       delete tk;
  62.       delete tp;
  63.     }
  64.  
  65.  
  66.     void wczytaj (ifstream &) throw (blad);
  67. };
  68.  
  69.  
  70. void polygon:: wczytaj (ifstream & plik) throw(blad)
  71. {
  72.   // zdefiniowac  dwiie dodatkowe zmiennie
  73.   int line(1);
  74.   string txt;
  75.  
  76.  
  77.   plik >> txt;
  78.   if(txt != "[POLYGON]") throw blad(line, " [POLYGON] ERROR ");
  79.  
  80.  
  81.   line++;
  82.   getline(plik,txt); // zeby przejsc wiersz nizej
  83.   getline(plik,txt);
  84.   if(txt != "[NUMBER OF NODES]") throw blad(line, " [NUMBER OF NODES] ERROR " );
  85.  
  86.  
  87.   line++;
  88.   plik >> roz;
  89.   if (!plik) throw blad(line, "ROZMIAR ERROR");
  90.  
  91.  
  92.   line++;
  93.   getline(plik,txt); // zeby przejsc wiersz nizej
  94.   getline(plik,txt);
  95.   if(txt != "[NODES]") throw blad(line, " [NODES] ERROR");
  96.  
  97.  
  98.   tp = new punkt[roz];
  99.  
  100.     for (unsigned i=0; i<roz; i++)
  101.     {
  102.         unsigned nr;
  103.         line++;
  104.         plik >> nr;
  105.         if (nr>roz || nr<=0) throw blad(line, "NR WEZLA POZA TABLICA ERROR");
  106.  
  107.         plik >> tp[nr-1].x;
  108.         plik >> tp[nr-1].y;
  109.  
  110.         if (!plik) throw blad(line, "WEZEL ERROR");
  111.     }
  112.  
  113.   line++;
  114.   plik >> txt;
  115.   if(txt != "[POLYGON]") throw blad(line, " [POLYGON] ERROR ");
  116.  
  117.  
  118.     tk = new unsigned [roz];
  119. line++;
  120.     for (unsigned i=0; i<roz; i++)
  121.     {
  122.         plik >> tk[i];
  123.         if (tk[i]>roz || tk[i]<=0) throw blad(line," NR WEZLA POZA TABLICA ERROR");
  124.         tk[i]--;
  125.         if (!plik) throw blad(line, "TABLICA KOLEJNOSCI ERROR");
  126.     }
  127. }
  128.  
  129.  
  130.  
  131.  int main(int argc, char * argv[])
  132.  try
  133.  {
  134.    if (argc!=2) throw string("Zla liczba parametrow");
  135.    if (string(argv[1]) != "wej.txt") throw string ("Niewlasciwa nazwa pliku");
  136.  
  137.    ifstream plik(argv[1]);
  138.  
  139.    if (!plik) throw string ("blad otwarcia pliku");
  140.  
  141.    try
  142.    {
  143.       polygon ob1;
  144.       ob1.wczytaj(plik);
  145.       //cout << ob1;
  146.    }
  147.     catch (const blad & e) {cout << e;}
  148.  
  149.     plik.close();
  150.  
  151.  
  152.       return 0;
  153.  }
  154.  catch(const string & e) {cout << e << endl;}
  155.  catch(...){cout << " wyjatki nieznanego typu";}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement