Advertisement
Dzaco

ZPOlab2 - try-catch

Oct 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. // ZPOlab2.1.cpp: definiuje punkt wejścia dla aplikacji konsolowej.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. class MyError
  11. {
  12. public:
  13.     MyError(string x)
  14.     {
  15.         cout << x << endl;
  16.         exit(0);
  17.     }
  18.     MyError(int i, string x)
  19.     {
  20.         cout << "Blad w linii " << i << ". " << x << endl;
  21.         exit(0);
  22.     }
  23. };
  24.  
  25. template <typename T , int rozm>
  26. class SzablonStosu
  27. {
  28.     T stos[rozm];
  29.     int top;
  30. public:
  31.     SzablonStosu() : top(0) {}
  32.     void push(const T &i)
  33.     {
  34.         stos[top++] = i;
  35.     }
  36.     T pop()
  37.     {
  38.         return stos[--top];
  39.     }
  40. };
  41.  
  42. void CheckMath(fstream &stream)
  43. {
  44.     string pom;
  45.     stream >> pom;
  46.     if ( pom != "<math" && pom != "<math>" ) throw MyError("Brak tagu <math>");
  47.  
  48.     while (pom != "</math>" && !stream.eof() )
  49.     {
  50.         stream >> pom;
  51.     }
  52.  
  53.     if (pom != "</math>") throw MyError("Brak tagu </math>");
  54.  
  55.     stream.seekg(0);
  56. }
  57.  
  58. void CheckTag(fstream &stream)
  59. {
  60.     string str;
  61.     int pom;
  62.  
  63.     for (int i = 1; !stream.eof(); i++)
  64.     {
  65.         getline(stream, str);
  66.        
  67.         pom = str.find('>') + 1;
  68.         if (str.substr(0,5) == "<math" || str.substr(0,pom) == "</math>" ){ } // ok
  69.         else if (str.substr(0, pom) == "<mi>" && str.substr(pom + 1, 5) != "</mi>")
  70.         {
  71.             throw MyError( i , "Brak tagu zamykajacego </mi>.");
  72.         }
  73.         else if (str.substr(0, pom) == "<mo>" && str.substr(pom + 1, 5) != "</mo>")
  74.         {
  75.             throw MyError(i, "Brak tagu zamykajacego </mo>.");
  76.         }
  77.         else if (str.substr(0, pom) == "<mn>" && str.substr(pom + 1, 5) != "</mn>")
  78.         {
  79.             throw MyError(i, "Brak tagu zamykajacego </mn>.");
  80.         }
  81.         else if (str.substr(0, pom) == "<msup>")
  82.         {
  83.             // tu nie wiem co..
  84.         }
  85.         else if (str.substr(0, pom) != "<mi>" && str.substr(0, pom) != "<mo>" && str.substr(0, pom) != "<mn>" && str.substr(0, pom) != "<msup>")
  86.         {
  87.             throw MyError( i , "Nie rozpoznany tag." );
  88.         }
  89.        
  90.     }
  91.  
  92.     stream.seekg(0);
  93. }
  94.  
  95. void Check( fstream &stream )
  96. {
  97.     try
  98.     {
  99.         CheckMath(stream);
  100.         CheckTag(stream);
  101.     }
  102.     catch (MyError) { }
  103.    
  104. }
  105.  
  106.  
  107. int main()
  108. {
  109.     // strumien do pliku
  110.     fstream stream;
  111.  
  112.     stream.open( "dane.txt" , ios::in );
  113.     if ( !stream.good() )
  114.     {
  115.         cout << "Blad otwarcia pliku" << endl;
  116.         exit(0);
  117.     }
  118.  
  119.     Check(stream);
  120.  
  121.  
  122.     stream.close();
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement