Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <time.h>
  4. #include <fstream>
  5. #include <assert.h>
  6. #define R 4 //Rozmiar tabeli to 4
  7. using namespace std;
  8. template <typename T>
  9. class Element {
  10. public:
  11.     T wartosc;
  12.     Element<T> * nast;
  13.     Element<T> * poprz;
  14.     Element(T wartosc) {
  15.         nast = NULL;
  16.         this->wartosc = wartosc;
  17.     }
  18. };
  19. bool czy_liczba(string text){
  20.     for (int i = 0; i<text.length(); i++){
  21.         if (!isdigit(text[i])){
  22.             return false;
  23.         }
  24.     }
  25.     return true;
  26. }
  27.  
  28. template <typename T>
  29. class Kolejka {
  30.     Element<T> * glowa;
  31.     Element<T> * ogon;
  32.     int rozmiar;
  33. public:
  34.     Kolejka() {
  35.         glowa = NULL;
  36.         ogon = NULL;
  37.         rozmiar = 0;
  38.     }
  39.     void push(T wartosc) {
  40.         Element<T> * element = new Element<T>(wartosc);
  41.         element->poprz = ogon;
  42.         if (ogon) {
  43.             ogon->nast = element;
  44.             ogon = element;
  45.         }
  46.         else {
  47.             glowa = ogon = element;
  48.         }
  49.         rozmiar++;
  50.     }
  51.     void wypisz(){
  52.         cout << glowa->wartosc<<endl;
  53.     }
  54.     T pop() {
  55.         assert(glowa);
  56.         T wynik = glowa->wartosc;
  57.         Element<T> * usun = glowa;
  58.         if (glowa->nast) {
  59.             glowa->nast->poprz = NULL;
  60.             glowa = glowa->nast;
  61.             if (glowa->nast == NULL){
  62.                 ogon = glowa;
  63.             }
  64.         }
  65.         else {
  66.             glowa = ogon = NULL;
  67.         }
  68.         delete usun;
  69.         rozmiar--;
  70.         return wynik;
  71.     }
  72.     T co_jest(){
  73.         return ogon->wartosc;
  74.     }
  75.     T co_jest2(){
  76.         return glowa->wartosc;
  77.     }
  78.     bool czy_istnieje(T do_sprawdzenia,T do_kad){
  79.         Element<T> * element;
  80.         element = ogon;
  81.         element = element->poprz;
  82.         while (element->poprz != NULL)
  83.         {
  84.             if (element->wartosc == do_kad){
  85.                 return false;
  86.             }
  87.             else if (element->wartosc==do_sprawdzenia)
  88.             {
  89.                 return true;
  90.             }
  91.             else
  92.             {
  93.                 element = element->poprz;
  94.             }
  95.         }
  96.         return false;
  97.     }
  98.     bool czy_istnieje2(T do_sprawdzenia, T do_kad){
  99.         Element<T> * element;
  100.         element = glowa;
  101.         element = element->nast;
  102.         while (element->nast != NULL)
  103.         {
  104.             if (element->wartosc == do_kad){
  105.                 return false;
  106.             }
  107.             else if (element->wartosc == do_sprawdzenia)
  108.             {
  109.                 return true;
  110.             }
  111.             else
  112.             {
  113.                 element = element->nast;
  114.             }
  115.         }
  116.         return false;
  117.     }
  118.    
  119.     int size() {
  120.         return rozmiar;
  121.     }
  122. };
  123. int Odczyt_z_pliku(){
  124.     ifstream in("a.txt");
  125.     string slowo;
  126.     string slowo2;
  127.     string sprawdzenie;
  128.     Kolejka<string> Stos;
  129.     in >> slowo;
  130.     int ktora_linijka = 0;
  131.    
  132.     if (slowo != "\\begin{enumerate}")
  133.     {
  134.        
  135.         system("Pause");
  136.         exit(0);
  137.     }
  138.     else
  139.     {
  140.         Stos.push(slowo);
  141.         ktora_linijka++;
  142.     }
  143.     while (!in.eof()){
  144.         sprawdzenie = Stos.co_jest() ;
  145.         in >> slowo;
  146.         getline(in, slowo2);
  147.         if (slowo != "\\item" && slowo != "\\end{enumerate}"&& slowo != "\\begin{itemize}" && slowo != "\\end{itemize}"){
  148.             cout << "wystapienie nieoczekiwanej wartości: " << slowo << endl << "linia: " << ktora_linijka << endl;
  149.         }
  150.         else if (slowo == "\\begin{itemize}"&&sprawdzenie != "\\item"){
  151.             cout << "zle wpisanie wielopoziomowej listy : \\item powinno byc przed \\begin" << endl << "linia: " << ktora_linijka << endl;
  152.         }
  153.         else if (slowo == "\\end{itemize}"&&Stos.czy_istnieje("\\begin{itemize}", "\\end{itemize}") == false)
  154.         {
  155.             cout << "zle wpisanie wielopoziomowej listy : \\end{itemize} powinno byc poprzedzone \\begin" << endl<<"linia: "<<ktora_linijka<<endl;
  156.         }
  157.        
  158.        
  159.         else
  160.         {
  161.  
  162.  
  163.             Stos.push(slowo);
  164.         }
  165.             if (slowo == "\\end{enumerate}\r" || slowo == "\\end{enumerate}")
  166.             {
  167.                 while (Stos.size() != 0)
  168.                 {
  169.                     Stos.co_jest2() = slowo;
  170.                     try{
  171.                         if (slowo != "\\item" && slowo != "\\end{enumerate}"&& slowo != "\\begin{itemize}" && slowo != "\\end{itemize}"){
  172.                             throw "wystapienie nieoczekiwanej wartości: " ;
  173.                         }
  174.                         else if (slowo == "\\begin{itemize}"&&sprawdzenie != "\\item"){
  175.                             throw "zle wpisanie wielopoziomowej listy : \\item powinno byc przed \\begin" ;
  176.                         }
  177.                         else if (slowo == "\\end{itemize}"&&Stos.czy_istnieje("\\begin{itemize}", "\\end{itemize}") == false)
  178.                         {
  179.                             throw "zle wpisanie wielopoziomowej listy : \\end{itemize} powinno byc poprzedzone \\begin";
  180.                         }
  181.                         else if (Stos.co_jest2() == "\\begin{itemize}"&&Stos.czy_istnieje2("\\end{itemize}", "\\begin{itemize}") == false){
  182.                             throw "zle wpisanie wielopoziomowej listy : \\begin{itemize} powinno byc zakonczone \\end" ;
  183.                         }
  184.                     }
  185.                     catch (const char * blad){
  186.                         cout << ktora_linijka - Stos.size() << ": " << blad << endl;
  187.                     }
  188.                     Stos.wypisz();
  189.                     Stos.pop();
  190.                 }
  191.                 return 0;
  192.             }
  193.             ktora_linijka++;
  194.     }
  195.     return 0;
  196. }
  197. int main(){
  198.     Odczyt_z_pliku();
  199.     system("pause");
  200.     return 0;
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement