macrofish

lukas

Dec 20th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <cstdio>
  7. #include <limits>
  8. #include <cstdlib>
  9. #include <iomanip>
  10. using namespace std;
  11.  
  12. const int imax = INT_MAX;
  13.  
  14.  
  15. struct eshop {
  16.         int id; /**< struct value ID. */
  17.         string jmeno; /**< struct value Jmeno. */
  18.         string popis; /**< struct value Popis. */
  19.         double cena; /**< struct value Cena. */
  20.         int pocet_kusu; /**< struct value Pocet_kusu. */
  21.     };
  22.  
  23.  
  24. void tisk_knihovny(eshop *p)
  25. {
  26.         for (unsigned int i=0; i<15;i++)
  27.         {
  28.                 cout << left<< p[i].id <<" | " << p[i].jmeno <<" | " <<
  29.                     p[i].popis <<" | " << p[i].cena << p[i].pocet_kusu << endl;
  30.  
  31.         }
  32. }
  33.  
  34.  
  35. void nacti_soubor(string cesta, eshop *p)
  36. {
  37.     ifstream muj_soubor (cesta);
  38.    
  39.    
  40.     string obsah;
  41.     string ukladani;
  42.     string prevod;
  43.  
  44.     int j=0;
  45.  
  46.     while (muj_soubor.good())
  47.     {
  48.        
  49.         int pocitac_stredniku=0;
  50.         int id;
  51.         double cena;
  52.         int pocet_kusu;
  53.  
  54.         getline(muj_soubor,obsah);
  55.         obsah+=';';
  56.        
  57.     for(unsigned int i=0; i<obsah.size(); i++)
  58.         {
  59.                
  60.             if(obsah[i]==',')
  61.             {
  62.             continue;
  63.             }
  64.            
  65.             if(obsah[i] ==';')
  66.             {
  67.                 if(pocitac_stredniku==0)
  68.  
  69.                 {
  70.                    
  71.                     id= std::atoi( ukladani.c_str() );
  72.  
  73.                     p[j].id=id;
  74.                     id=0;  
  75.                    
  76.                 }
  77.  
  78.                     else if(pocitac_stredniku==1)
  79.                     {
  80.                             p[j].jmeno=ukladani;
  81.                            
  82.                     }
  83.        
  84.  
  85.                         else if (pocitac_stredniku==2)
  86.                         {
  87.                             p[j].popis=ukladani;
  88.                         }
  89.                                 else if (pocitac_stredniku==3)
  90.                                 {
  91.                                     // převod stringu na double
  92.                                    
  93.                                
  94.                                      cena=  std::strtod( ukladani.c_str(), NULL );
  95.                                
  96.                                     p[j].cena=cena;
  97.                                     cena=0;
  98.                                 }
  99.                                     else if(pocitac_stredniku==4)
  100.                                         {  
  101.                                            
  102.                                             // prevod stringu na double
  103.                                             pocet_kusu= std::atoi( ukladani.c_str() );
  104.                                             p[j].pocet_kusu=pocet_kusu;
  105.                                             pocet_kusu=0;  
  106.                                             j++;
  107.                                                
  108.                                         }
  109.  
  110.                                     else
  111.                                    
  112.                                     {
  113.                                         pocitac_stredniku=0;
  114.                                     }                                                              
  115.                 ukladani="";
  116.                 pocitac_stredniku++;           
  117.             }  
  118.             else
  119.             {
  120.                 ukladani=ukladani+obsah[i];
  121.             }
  122.        
  123.         } /// for řádek
  124.     }
  125.     muj_soubor.close();
  126. }
  127.  
  128.  
  129. void vypis_menu()
  130. {
  131.     cout<<"Vyberte z nasledujiciho menu co chcete se souborem provest:"<<endl;
  132.     cout<<endl;
  133.     cout<<"[1] Setridit polozky podle ceny a vypsat vsechno zbozi"<<endl;
  134.     cout<<"[2] Vypsat zbozi podle zadaneho cenoveho intervalu"<<endl;
  135.     cout<<"[3] Vypsat zbozi pro zadanou castku"<<endl;
  136.     cout<<"[4] Ukoncit program"<<endl;
  137. }
  138.  
  139.  
  140.  
  141. void bubble(eshop *p, int pocet)
  142. {
  143.     bool doMore;
  144.         do
  145.         {
  146.             doMore = false;
  147.            
  148.             for (int i=0; i<pocet-1; i++)
  149.             {
  150.                 if (p[i].cena > p[i+1].cena)
  151.                 {
  152.                     // vymena prvku
  153.                     eshop temp = p[i];
  154.                     p[i] = p[i+1];
  155.                     p[i+1] = temp;
  156.                     doMore = true;
  157.                 }
  158.             }} while (doMore);
  159.             cout<<"Polozky byly uspesne sezareny."<<endl;
  160. }
  161.  
  162.  
  163. void vystup_html(eshop *p, int HM, int DM, int pocet, bool na_skladu)
  164. {
  165.     cout<<"Zadejte jmeno vystupni souboru."<<endl;
  166.                 string nazev_souboru_out;
  167.                 cin>>nazev_souboru_out;
  168.  
  169.                 while(true)
  170.             {  
  171.    
  172.             string str2=".html";
  173.  
  174.             int found = nazev_souboru_out.find(str2);
  175.                
  176.                
  177.                 if (found ==string::npos)  
  178.                     {
  179.                     cout<<"Neplatny nazev vystupniho souboru."<<endl;
  180.                     cout<<"Format vystupu --> Nazev_soubor.html"<<endl;
  181.                     cout<<"Zadejte nazev souboru:"<<endl;
  182.                     cin>>nazev_souboru_out;
  183.                     found = nazev_souboru_out.find(str2);
  184.                     cout<<endl;
  185.                     }
  186.  
  187.                
  188.                 else    {
  189.                     break;
  190.                
  191.                 }
  192.             }
  193.  
  194.  
  195.                 string cesta_out_2=string("vystupnidata\\")+ nazev_souboru_out;
  196.             ofstream muj_soubor_out(cesta_out_2);
  197.     if(muj_soubor_out.is_open())
  198.             {
  199.            
  200.  
  201.             muj_soubor_out<< "<!DOCTYPE html><html><head></head><body>"<<endl;
  202.             muj_soubor_out<<"<table border=""1"" bordercolor=""#CC3300"" style=""background-color:#FFFFCC"" width=""100%"" cellpadding=""3"" cellspacing=""3"">"<<endl;
  203.             muj_soubor_out<<"<tr><td>"<<"ID"<<"</td><td>"<<"Nazev"<<"</td><td>"<<"Popis"<<"</td><td>"<<"Cena"<<"</td><td>"<<"Pocet_kusu"<<"</td></tr>"<<endl;
  204.             for(int i=0; i<pocet;i++){
  205.  
  206.                 if(p[i].cena>=DM && p[i].cena<=HM)
  207.                 {
  208.                     if (na_skladu)
  209.                     {
  210.                         if (p[i].pocet_kusu>0)
  211.                         {
  212.                             muj_soubor_out<<"<tr><td>"<<p[i].id<<"</td><td>"<<p[i].jmeno<<"</td><td>"<<p[i].popis<<"</td><td>"<<p[i].cena<<"</td><td>"<<p[i].pocet_kusu<<"</td></tr>"<<endl;
  213.                         }
  214.                     }
  215.                     else
  216.                     {
  217.                         muj_soubor_out<<"<tr><td>"<<p[i].id<<"</td><td>"<<p[i].jmeno<<"</td><td>"<<p[i].popis<<"</td><td>"<<p[i].cena<<"</td><td>"<<p[i].pocet_kusu<<"</td></tr>"<<endl;
  218.                     }
  219.                 }
  220.             }
  221.            
  222.              muj_soubor_out << "</table></body></html>"<<endl;
  223.             cout<<"Vas soubor byl uspesne vytvoren."<<endl;
  224.        
  225.             muj_soubor_out.flush();
  226.             muj_soubor_out.close();
  227.         }
  228. }
  229.  
  230.  
  231. int get_horni_mez()
  232. {
  233.     int zadana_castka;
  234.             cout<<"Zadejte maximalni castku:"<<endl;
  235.             cin>>zadana_castka;
  236.  
  237.         if (!cin.good())
  238.             {
  239.  
  240.                     while(!cin.good())
  241.                     {
  242.                         cin.clear();
  243.                         cin.sync();
  244.                         cout<<"Neni ciselna hodnota, zkuste to znovu"<<endl;
  245.                         cin>>zadana_castka;
  246.                     }
  247.             }
  248.  
  249.         return zadana_castka;
  250. }
  251.  
  252. int get_dolni_mez()
  253. {
  254.         int dolni_hranice;
  255.  
  256.             cout<<"Zadejte minimalni castku:"<<endl;
  257.             cin>>dolni_hranice;
  258.  
  259.             if (!cin.good())
  260.             {
  261.  
  262.                     while(!cin.good())
  263.                     {
  264.                         cin.clear();
  265.                         cin.sync();
  266.                         cout<<"Neni ciselna hodnota, zkuste to znovu"<<endl;
  267.                         cin>>dolni_hranice;
  268.                     }
  269.             }
  270.             return dolni_hranice;
  271. }
  272.  
  273.  
  274. int get_pocet_radku(string &cesta)
  275. {
  276.     string nazev_souboru_in;
  277.     cout<<"Zadejte soubor, ktery chcete nacist"<<endl;
  278.     cin>> nazev_souboru_in;
  279.    
  280.     cesta=string("vstupnidata\\")+ nazev_souboru_in;
  281.     ifstream muj_soubor (cesta);
  282.     if (muj_soubor.fail() )  // pokud soubor neni v poradku vyskoci hlaseni o chybe
  283.     {
  284.         while ( muj_soubor.fail() )
  285.             {
  286.              cerr<<"Chyba pri nacitani dat, zkuste to znovu."<<endl;
  287.              cin>>nazev_souboru_in;
  288.              string cesta=string("vstupnidata\\")+ nazev_souboru_in;
  289.              muj_soubor.open(cesta); // abychom zjistili, zda je porad chybny, musime soubor zkusit znova otevrit
  290.             }
  291.        
  292.                 if( muj_soubor.good())
  293.                 {
  294.                     cout<<"Vas soubor byl uspesne nahran."<<endl;
  295.                     cout<<endl;
  296.  
  297.                 }  
  298.     }
  299.  
  300.     else  // pokud je soubor v poradku, otevre se.
  301.     {
  302.        
  303.         cout<<"Vas soubor byl uspesne nahran."<<endl;
  304.         cout<<endl;
  305.     }
  306.  
  307.    
  308.     cesta=string("vstupnidata\\")+ nazev_souboru_in;
  309.    
  310.     string obsah;
  311.     string ukladani;
  312.     string prevod;
  313.     int pocitac = 0;
  314.     while (muj_soubor.good())
  315.     {
  316.         getline(muj_soubor,obsah);
  317.         pocitac++;
  318.     }
  319.     muj_soubor.clear();
  320.     muj_soubor.close();
  321.  
  322.     return pocitac;
  323. }
  324.  
  325. int main()
  326. {
  327.    
  328.     string cesta;
  329.     int pocitac=get_pocet_radku(cesta);
  330.     eshop *p=new eshop[pocitac];
  331.     nacti_soubor(cesta,p);
  332.    
  333.     int volba = 0;
  334.     vypis_menu();
  335.     cin >> volba;
  336.     if (!cin.good())
  337.             {
  338.  
  339.                     while(!cin.good())
  340.                     {
  341.                         cin.clear();
  342.                         cin.sync();
  343.                         cout<<"Neni ciselna hodnota, zkuste to znovu"<<endl;
  344.                         cin>>volba;
  345.                     }
  346.             }
  347.  
  348.  
  349.  
  350.     while (volba >4 || volba <0)
  351.     {
  352.         cout << "spatna volba, nove zadani" << endl;
  353.         cin >> volba;
  354.     }
  355.  
  356.  
  357.         switch(volba)
  358.         {
  359.         case 1:
  360.             {
  361.                 bubble(p,pocitac);
  362.                 vystup_html(p,imax,0,pocitac,false);
  363.  
  364.                 break;
  365.             }
  366.  
  367.         case 2:
  368.             {
  369.                 vystup_html(p,get_horni_mez(),get_dolni_mez(),pocitac,false);
  370.                 break;
  371.             }
  372.         case 3:
  373.             {
  374.                 vystup_html(p,get_horni_mez(),0,pocitac,true);
  375.                 break;
  376.             }
  377.  
  378.         case 4:
  379.             {
  380.                 if (p != 0)
  381.                     {
  382.                     delete[] p;
  383.                     p = 0;
  384.                     }
  385.                 cout << "program byl ukoncen " << endl;
  386.                 return 0;
  387.             }
  388.  
  389.     if (p != 0)
  390.     {
  391.         delete[] p;
  392.         p = 0;
  393.     }
  394. }
  395.  
  396.     return 0;
  397. }
Advertisement
Add Comment
Please, Sign In to add comment