Facebook
From Reliable Cockroach, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 130
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <algorithm>
  7.  
  8. #include "rapidxml-1.13/rapidxml.hpp"
  9. #include "rapidxml-1.13/rapidxml_print.hpp"
  10. #include "rapidxml-1.13/rapidxml_utils.hpp"
  11. #include <vector>
  12.  
  13. using namespace rapidxml;
  14. using namespace std;
  15.  
  16. string IntToString (int a)
  17. {
  18.     ostringstream temp;
  19.     temp<<a;
  20.     return temp.str();
  21. }
  22.  
  23. bool pobierz(xml_document <> *doc,char * Nazwa, char * rozszezenie,vector<char> ** buffer)
  24. {
  25.     string nazwa_doc = string(Nazwa) + string(rozszezenie);
  26.     char *cstr = new char[nazwa_doc.length() + 1];
  27.     strcpy(cstr, nazwa_doc.c_str());
  28.  
  29.     ifstream XmlF (cstr);
  30.     if (!XmlF.good()){cout << "file no exists"; exit(1);}
  31.         vector<char> *B =new vector<char>((istreambuf_iterator<char>(XmlF)), istreambuf_iterator<char>());
  32.         B->push_back('\0');
  33.         try{
  34.           doc->parse<0>(&(*B)[0]);
  35.         }
  36.      catch( parse_error p )
  37.     {
  38.         cout <<"|" <<p.what()<<"|";
  39.         cout << p.where<char>();
  40.         exit(1);
  41.         return false;
  42.     }
  43.     (*buffer) = B;
  44.     delete cstr;
  45.     return true;
  46. }
  47.  
  48. class MojXML
  49. {
  50. public :
  51.     xml_document<> * XML;
  52.     vector<char> * XMLvect;
  53.     MojXML(char * nazwa,char * rozszezenie)
  54.     {
  55.        XML = new xml_document<>();
  56.        if (!pobierz(XML,nazwa,rozszezenie,&XMLvect))
  57.             {cout << "cos poszlo nie tak" << nazwa<< ".xml";exit(1);}
  58.     }
  59.     ~MojXML()
  60.     {
  61.         XML->clear();
  62.         delete XML;
  63.         delete XMLvect;
  64.     }
  65.     xml_node <>* utworz_wierzcholek(string name)
  66.       {
  67.           char *Name = XML->allocate_string(name.c_str());
  68.           return XML->allocate_node( node_element,Name);
  69.       }
  70.     void dodaj_atrybut(xml_node<>* W,string attr, string value)
  71.       {
  72.           char *Attr = XML->allocate_string(attr.c_str());
  73.           char *Value = XML->allocate_string(value.c_str());
  74.           W->append_attribute(XML->allocate_attribute(Attr,Value));
  75.       }
  76.     void Zapisz_do_pliku(char * nazwa,char * rozszezenie)
  77.     {
  78.         string nazwa_doc = string(nazwa) + string(rozszezenie);
  79.         char *cstr = new char[nazwa_doc.length() + 1];
  80.         strcpy(cstr, nazwa_doc.c_str());
  81.         ofstream File;
  82.         File.open(cstr);
  83.         File << *XML;
  84.         File.flush();
  85.         File.close();
  86.         delete cstr;
  87.     }
  88.     void drukuj()
  89.     {
  90.         xml_node<>* temp = XML->first_node();
  91.        if (!temp) {cout << 1;exit(1);}
  92.        temp = temp->first_node();
  93.        while (temp)
  94.          {
  95.            cout << * temp;
  96.            temp = temp->next_sibling();
  97.          }
  98.     }
  99. };
  100.  
  101. void WyswietlAtrybuty(xml_node <> * N )
  102. {
  103.     cout << "<" << N->name() <<">";
  104.     for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() ) // (7)
  105.     {
  106.         string name (atrybut->name(),atrybut->name_size());
  107.         string value (atrybut->value(),atrybut->value_size());
  108.         cout << "|" << name << "=" << value << "| ";
  109.     }
  110. }
  111.  
  112. void WyswietlPotomkow(xml_node <> * N)
  113. {
  114.     for( xml_node <>* node = N->first_node(); node; node = node->next_sibling() ) // (7)
  115.     {
  116.         cout << "<" << node->name() <<" ";
  117.         WyswietlAtrybuty(node);
  118.         cout << ">" << endl;
  119.     }
  120. }
  121. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  122.  
  123.  
  124. //wyswietlanie atrybutu
  125. void wyswietl_wartosc_atrybutu(xml_node<>*N, string nazwa)
  126. {
  127.      for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
  128.      {
  129.         string name (atrybut->name(),atrybut->name_size());
  130.         string value (atrybut->value(),atrybut->value_size());
  131.         if(name == nazwa)
  132.         {
  133.             cout<<value;
  134.             break;
  135.         }
  136.      }
  137. }
  138. //zczytywanie atrybutu
  139. string pobierz_wartosc_atrybutu(xml_node <> * N, string nazwa)
  140. {
  141.     for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
  142.     {
  143.         string name (atrybut->name(),atrybut->name_size());
  144.         string value (atrybut->value(),atrybut->value_size());
  145.         if( name == nazwa )
  146.         {
  147.             return value;
  148.         }
  149.     }
  150. }
  151.  
  152. //ladne wyswietlanie atrybytów pojazdu
  153. void wyswietl_ladnie_samochod(xml_node<> *N)
  154. {
  155.      for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
  156.      {
  157.         string name (atrybut->name(),atrybut->name_size());
  158.         string value (atrybut->value(),atrybut->value_size());
  159.         if (name == "rodzaj")
  160.         {
  161.             cout<<" i nalezy do rodzaju:"<<value;
  162.         }
  163.         else if (name == "marka")
  164.         {
  165.             cout<<" jest marki " << value <<" ";
  166.         }
  167.         else if(name == "rocznik")
  168.         {
  169.             cout<<"Rocznik pojazdu to " << value <<",";
  170.         }
  171.  
  172.      }
  173. cout<<endl;
  174. }
  175. //wyswietlanie calosci
  176. void wyswietl_ladnie_wszystko(xml_node<> *N)
  177. {
  178.     for( xml_node <>* node = N->first_node(); node; node = node->next_sibling() )
  179.     {
  180.         cout<<"Rodzaj: ";
  181.         cout<<node->name()<<" "<<endl;
  182.         for( xml_node <>* node_pojazd = node->first_node(); node_pojazd; node_pojazd = node_pojazd->next_sibling() )
  183.         {
  184.             wyswietl_ladnie_samochod(node_pojazd);
  185.         }
  186.         cout<<endl;
  187.     }
  188. }
  189.  
  190. //wyswietlanie po rodzaju
  191. void wyswietl_ladnie_po_rodzaju(xml_node <> *N, string rodzaj)
  192. {
  193.     int czy_istnieje = 0;
  194.     cout<<"Podaj rodzaj pojazdu (ciezarowe/osobowe/...):  ";
  195.     cin>>rodzaj;
  196.     for(xml_node <> *node = N->first_node() ; node ; node = node->next_sibling())
  197.     {
  198.         if(pobierz_wartosc_atrybutu(node , "name") == rodzaj)
  199.         {
  200.             czy_istnieje = 1;
  201.             for(xml_node<> *node_pojazd = node->first_node() ; node_pojazd ; node_pojazd = node_pojazd->next_sibling())
  202.             {
  203.                 cout<< " ";
  204.                 wyswietl_ladnie_samochod(node_pojazd);
  205.             }
  206.             cout<<endl;
  207.             break;
  208.         }
  209.     }
  210.     if(czy_istnieje == 0){ cout<<"Nie ma takiego rodzaju"<<endl;}
  211. }
  212.  
  213. //wyswietlanie po marce
  214. void wyswietl_ladnie_po_marce(xml_node <> *N)
  215. {
  216.     cout<<"Podaj marke szukanego pojazdu:  ";
  217.     string marka;cin>>marka;
  218.     int czy_istnieje=0;
  219.  
  220.     for(xml_node<>*node=N->first_node();node;node=node->next_sibling())
  221.     {
  222.     for(xml_node<>*pomocniczy = node->first_node();pomocniczy;pomocniczy=pomocniczy->next_sibling())
  223.     {
  224.             if (pobierz_wartosc_atrybutu(pomocniczy,"marka") == marka)
  225.             {
  226.                 wyswietl_ladnie_samochod(pomocniczy);
  227.                 czy_istnieje = 1;
  228.                 continue;
  229.             }
  230.     }
  231.     //if(czy_istnieje==1)break;
  232.     }
  233.     if(czy_istnieje==0)
  234.      {cout<<"Nie posiadamy pojazdu podanej marki. Upewnij sie co do wielkosci wpisywanych liter.";}
  235.  
  236. }
  237. //funkcja pomocnicza do dodawania
  238. xml_node<> * gdzie_dodac(xml_node<>* Salon, string name)
  239. {
  240.     int czy_istnieje=0;
  241.     for(xml_node <> * node = Salon->first_node(); node ; node = node->next_sibling())
  242.     {
  243.         if(pobierz_wartosc_atrybutu(node,"name") == name)
  244.         {
  245.             return node;
  246.             czy_istnieje=1;
  247.         }
  248.     }
  249.     if(czy_istnieje==0)
  250.     {
  251.         cout<<"Nie ma takiego rodzaju pojazdu"<<endl;
  252.         return NULL;
  253.     }
  254.  
  255. }
  256. //dodawanie samochodu
  257. void dodaj_samochod(MojXML *Salon, string gdzie_dodac_ , string rodzaj, string marka, string rocznik)
  258. {
  259.  
  260.     xml_node<> * Samochody = Salon->XML->first_node();
  261.     xml_node<> * nowy_samochod = Salon->utworz_wierzcholek(rodzaj);
  262.     Salon->dodaj_atrybut(nowy_samochod, (string)("rocznik"), (string)(rocznik));
  263.     Salon->dodaj_atrybut(nowy_samochod, (string)("marka"), (string)marka);
  264.     Salon->dodaj_atrybut(nowy_samochod, (string)("rodzaj"), (string)(rodzaj));
  265.  
  266.     if(gdzie_dodac(Samochody,gdzie_dodac_)!=NULL)
  267.     {
  268.     xml_node<>* tutaj_dodaj = gdzie_dodac(Samochody,gdzie_dodac_);
  269.     tutaj_dodaj->append_node(nowy_samochod);
  270.     Salon->Zapisz_do_pliku((char*)"salon_samochodowy",(char*)".xml");
  271.     }
  272. }
  273. //tworzenie nowego rodzaju
  274. void dodaj_rodzaj(MojXML *Salon, xml_node<> *N, string rodzaj)
  275. {
  276.     xml_node<> *ostatni_rodzaj = N->last_node();
  277.     xml_node<> *nowy_rodzaj = Salon->utworz_wierzcholek(rodzaj);
  278.     Salon->dodaj_atrybut(nowy_rodzaj,(string)"name",(string)rodzaj);
  279.     N->append_node(nowy_rodzaj);
  280.     Salon->Zapisz_do_pliku((char*)"salon_samochodowy",(char*)".xml");
  281. }
  282.  
  283.  
  284. ////////////////////////////////////MENU//////////////////////////////////////////
  285. int menu()
  286. {   int wybor;
  287. while(true)
  288. {
  289.         cout<<"1.Wyswietl wszystkie pojazdy"<<endl;
  290.         cout<<"2.Wyswietl wszystkie pojazdy z danego rodzaju"<<endl;
  291.         cout<<"3.Wyszukaj pojazd danej marki"<<endl;
  292.         cout<<"4.Dodaj pojazd do istniejacego rodzaju"<<endl;
  293.         cout<<"5.Stworz nowy rodzaj"<<endl;
  294.         cout<<"6.Wyjdz"<<endl;
  295.         cout<<"Wybierz numer i zatwierdz enterem:";
  296.     cin>>wybor;
  297.     system("cls");
  298.     if(wybor==1||wybor==2||wybor==3||wybor==4||wybor==5||wybor==6){break;}
  299.  
  300.     if(wybor!=1&&wybor!=2&&wybor!=3&&wybor!=4&&wybor!=5&&wybor!=6)
  301.     {
  302.         //cout<<"Nie ma takiego numeru, sprobuj ponownie"<<endl<<endl;
  303.         cout<<"1.Wyswietl wszystkie pojazdy"<<endl;
  304.         cout<<"2.Wyswietl wszystkie pojazdy z danego rodzaju"<<endl;
  305.         cout<<"3.Wyszukaj pojazd danej marki"<<endl;
  306.         cout<<"4.Dodaj pojazd do istniejacego rodzaju"<<endl;
  307.         cout<<"5.Stworz nowy rodzaj"<<endl;
  308.         cout<<"6.Wyjdz"<<endl;
  309.         cout<<"Wybierz numer i zatwierdz enterem:";
  310.         cin>>wybor;
  311.         system("cls");
  312.         if(wybor==1||wybor==2||wybor==3||wybor==4||wybor==5||wybor==6){break;}
  313.     }
  314. }
  315.     return wybor;
  316. }
  317.  
  318. int main()
  319. {
  320.  
  321.  
  322. MojXML Salon_samochodowy((char*)"salon_samochodowy",(char*)".xml");
  323. xml_node<> * salon_samochodowy = Salon_samochodowy.XML->first_node();
  324. poczatek:
  325. int powrot;
  326. int wybor;
  327. wybor = menu();
  328.     if(wybor == 1)
  329.     {
  330.         wyswietl_ladnie_wszystko(salon_samochodowy);
  331.         cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  332.         cin>>powrot;
  333.         while(true)
  334.         {
  335.             if (powrot!=0)
  336.             {
  337.                 system("cls");
  338.                 wyswietl_ladnie_wszystko(salon_samochodowy);
  339.                 cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  340.                 cin>>powrot;
  341.             }
  342.         else
  343.             {
  344.                 system("cls");
  345.                 goto poczatek;
  346.             }
  347.         }
  348.     }
  349.     if (wybor == 2)
  350.     {
  351.         wyswietl_ladnie_po_rodzaju(salon_samochodowy,"przyklad");
  352.         cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  353.         cin>>powrot;
  354.         while(true)
  355.         {
  356.             if (powrot!=0)
  357.             {
  358.                 cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  359.                 cin>>powrot;
  360.             }
  361.         else
  362.             {
  363.                 system("cls");
  364.                 goto poczatek;
  365.             }
  366.         }
  367.     }
  368.     if (wybor == 3)
  369.     {
  370.         wyswietl_ladnie_po_marce(salon_samochodowy);
  371.          cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  372.         cin>>powrot;
  373.         while(true)
  374.         {
  375.             if (powrot!=0)
  376.             {
  377.                 cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  378.                 cin>>powrot;
  379.             }
  380.         else
  381.             {
  382.                 system("cls");
  383.                 goto poczatek;
  384.             }
  385.         }
  386.     }
  387.     if (wybor == 4)
  388.     {
  389.         cout<<"Do jakiego rodzaju (osobowe/ciezarowe/...)pojazd chcesz dodac :   ";
  390.         string rodzaj;cin>>rodzaj;
  391.         cout<<endl<<"Podaj marke pojazdu:  ";
  392.         string marka;cin>>marka;
  393.         cout<<endl<<"Podaj rocznik pojazdu:  ";
  394.         string rocznik;cin>>rocznik;
  395.         string pomocnicze;
  396.         if (rodzaj=="osobowy") rodzaj ="osobowe";
  397.         else if (rodzaj=="ciezarowy") rodzaj ="ciezarowe";
  398.         else{ dodaj_samochod(&Salon_samochodowy,rodzaj,rodzaj,marka,rocznik);}
  399.  
  400.          cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  401.         cin>>powrot;
  402.         while(true)
  403.         {
  404.             if (powrot!=0)
  405.             {
  406.                 cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  407.                 cin>>powrot;
  408.             }
  409.         else
  410.             {
  411.                 system("cls");
  412.                 goto poczatek;
  413.             }
  414.         }
  415.  
  416.     }
  417.     if (wybor==5)
  418.     {
  419.         cout<<"Podaj rodzaj:  ";
  420.         string rodzaj;cin>>rodzaj;
  421.         dodaj_rodzaj(&Salon_samochodowy,salon_samochodowy,rodzaj);
  422.          cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  423.         cin>>powrot;
  424.         while(true)
  425.         {
  426.             if (powrot!=0)
  427.             {
  428.                 cout<<endl<<endl<<"Wpisz 0 zeby powrocic:  ";
  429.                 cin>>powrot;
  430.             }
  431.         else
  432.             {
  433.                 system("cls");
  434.                 goto poczatek;
  435.             }
  436.         }
  437.     }
  438.     if (wybor == 6) return 0;
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447. Salon_samochodowy.Zapisz_do_pliku((char*)"salon_samochodowy",(char*)".xml");
  448.  
  449.  
  450. return 0;
  451. }
  452.  
  453.