Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <algorithm>
- #include <cstdio>
- #include <limits>
- #include <cstdlib>
- #include <iomanip>
- using namespace std;
- const int imax = INT_MAX;
- struct eshop {
- int id; /**< struct value ID. */
- string jmeno; /**< struct value Jmeno. */
- string popis; /**< struct value Popis. */
- double cena; /**< struct value Cena. */
- int pocet_kusu; /**< struct value Pocet_kusu. */
- };
- void tisk_knihovny(eshop *p)
- {
- for (unsigned int i=0; i<15;i++)
- {
- cout << left<< p[i].id <<" | " << p[i].jmeno <<" | " <<
- p[i].popis <<" | " << p[i].cena << p[i].pocet_kusu << endl;
- }
- }
- void nacti_soubor(string cesta, eshop *p)
- {
- ifstream muj_soubor (cesta);
- string obsah;
- string ukladani;
- string prevod;
- int j=0;
- while (muj_soubor.good())
- {
- int pocitac_stredniku=0;
- int id;
- double cena;
- int pocet_kusu;
- getline(muj_soubor,obsah);
- obsah+=';';
- for(unsigned int i=0; i<obsah.size(); i++)
- {
- if(obsah[i]==',')
- {
- continue;
- }
- if(obsah[i] ==';')
- {
- if(pocitac_stredniku==0)
- {
- id= std::atoi( ukladani.c_str() );
- p[j].id=id;
- id=0;
- }
- else if(pocitac_stredniku==1)
- {
- p[j].jmeno=ukladani;
- }
- else if (pocitac_stredniku==2)
- {
- p[j].popis=ukladani;
- }
- else if (pocitac_stredniku==3)
- {
- // převod stringu na double
- cena= std::strtod( ukladani.c_str(), NULL );
- p[j].cena=cena;
- cena=0;
- }
- else if(pocitac_stredniku==4)
- {
- // prevod stringu na double
- pocet_kusu= std::atoi( ukladani.c_str() );
- p[j].pocet_kusu=pocet_kusu;
- pocet_kusu=0;
- j++;
- }
- else
- {
- pocitac_stredniku=0;
- }
- ukladani="";
- pocitac_stredniku++;
- }
- else
- {
- ukladani=ukladani+obsah[i];
- }
- } /// for řádek
- }
- muj_soubor.close();
- }
- void vypis_menu()
- {
- cout<<"Vyberte z nasledujiciho menu co chcete se souborem provest:"<<endl;
- cout<<endl;
- cout<<"[1] Setridit polozky podle ceny a vypsat vsechno zbozi"<<endl;
- cout<<"[2] Vypsat zbozi podle zadaneho cenoveho intervalu"<<endl;
- cout<<"[3] Vypsat zbozi pro zadanou castku"<<endl;
- cout<<"[4] Ukoncit program"<<endl;
- }
- void bubble(eshop *p, int pocet)
- {
- bool doMore;
- do
- {
- doMore = false;
- for (int i=0; i<pocet-1; i++)
- {
- if (p[i].cena > p[i+1].cena)
- {
- // vymena prvku
- eshop temp = p[i];
- p[i] = p[i+1];
- p[i+1] = temp;
- doMore = true;
- }
- }} while (doMore);
- cout<<"Polozky byly uspesne sezareny."<<endl;
- }
- void vystup_html(eshop *p, int HM, int DM, int pocet, bool na_skladu)
- {
- cout<<"Zadejte jmeno vystupni souboru."<<endl;
- string nazev_souboru_out;
- cin>>nazev_souboru_out;
- while(true)
- {
- string str2=".html";
- int found = nazev_souboru_out.find(str2);
- if (found ==string::npos)
- {
- cout<<"Neplatny nazev vystupniho souboru."<<endl;
- cout<<"Format vystupu --> Nazev_soubor.html"<<endl;
- cout<<"Zadejte nazev souboru:"<<endl;
- cin>>nazev_souboru_out;
- found = nazev_souboru_out.find(str2);
- cout<<endl;
- }
- else {
- break;
- }
- }
- string cesta_out_2=string("vystupnidata\\")+ nazev_souboru_out;
- ofstream muj_soubor_out(cesta_out_2);
- if(muj_soubor_out.is_open())
- {
- muj_soubor_out<< "<!DOCTYPE html><html><head></head><body>"<<endl;
- muj_soubor_out<<"<table border=""1"" bordercolor=""#CC3300"" style=""background-color:#FFFFCC"" width=""100%"" cellpadding=""3"" cellspacing=""3"">"<<endl;
- muj_soubor_out<<"<tr><td>"<<"ID"<<"</td><td>"<<"Nazev"<<"</td><td>"<<"Popis"<<"</td><td>"<<"Cena"<<"</td><td>"<<"Pocet_kusu"<<"</td></tr>"<<endl;
- for(int i=0; i<pocet;i++){
- if(p[i].cena>=DM && p[i].cena<=HM)
- {
- if (na_skladu)
- {
- if (p[i].pocet_kusu>0)
- {
- 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;
- }
- }
- else
- {
- 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;
- }
- }
- }
- muj_soubor_out << "</table></body></html>"<<endl;
- cout<<"Vas soubor byl uspesne vytvoren."<<endl;
- muj_soubor_out.flush();
- muj_soubor_out.close();
- }
- }
- int get_horni_mez()
- {
- int zadana_castka;
- cout<<"Zadejte maximalni castku:"<<endl;
- cin>>zadana_castka;
- if (!cin.good())
- {
- while(!cin.good())
- {
- cin.clear();
- cin.sync();
- cout<<"Neni ciselna hodnota, zkuste to znovu"<<endl;
- cin>>zadana_castka;
- }
- }
- return zadana_castka;
- }
- int get_dolni_mez()
- {
- int dolni_hranice;
- cout<<"Zadejte minimalni castku:"<<endl;
- cin>>dolni_hranice;
- if (!cin.good())
- {
- while(!cin.good())
- {
- cin.clear();
- cin.sync();
- cout<<"Neni ciselna hodnota, zkuste to znovu"<<endl;
- cin>>dolni_hranice;
- }
- }
- return dolni_hranice;
- }
- int get_pocet_radku(string &cesta)
- {
- string nazev_souboru_in;
- cout<<"Zadejte soubor, ktery chcete nacist"<<endl;
- cin>> nazev_souboru_in;
- cesta=string("vstupnidata\\")+ nazev_souboru_in;
- ifstream muj_soubor (cesta);
- if (muj_soubor.fail() ) // pokud soubor neni v poradku vyskoci hlaseni o chybe
- {
- while ( muj_soubor.fail() )
- {
- cerr<<"Chyba pri nacitani dat, zkuste to znovu."<<endl;
- cin>>nazev_souboru_in;
- string cesta=string("vstupnidata\\")+ nazev_souboru_in;
- muj_soubor.open(cesta); // abychom zjistili, zda je porad chybny, musime soubor zkusit znova otevrit
- }
- if( muj_soubor.good())
- {
- cout<<"Vas soubor byl uspesne nahran."<<endl;
- cout<<endl;
- }
- }
- else // pokud je soubor v poradku, otevre se.
- {
- cout<<"Vas soubor byl uspesne nahran."<<endl;
- cout<<endl;
- }
- cesta=string("vstupnidata\\")+ nazev_souboru_in;
- string obsah;
- string ukladani;
- string prevod;
- int pocitac = 0;
- while (muj_soubor.good())
- {
- getline(muj_soubor,obsah);
- pocitac++;
- }
- muj_soubor.clear();
- muj_soubor.close();
- return pocitac;
- }
- int main()
- {
- string cesta;
- int pocitac=get_pocet_radku(cesta);
- eshop *p=new eshop[pocitac];
- nacti_soubor(cesta,p);
- int volba = 0;
- vypis_menu();
- cin >> volba;
- if (!cin.good())
- {
- while(!cin.good())
- {
- cin.clear();
- cin.sync();
- cout<<"Neni ciselna hodnota, zkuste to znovu"<<endl;
- cin>>volba;
- }
- }
- while (volba >4 || volba <0)
- {
- cout << "spatna volba, nove zadani" << endl;
- cin >> volba;
- }
- switch(volba)
- {
- case 1:
- {
- bubble(p,pocitac);
- vystup_html(p,imax,0,pocitac,false);
- break;
- }
- case 2:
- {
- vystup_html(p,get_horni_mez(),get_dolni_mez(),pocitac,false);
- break;
- }
- case 3:
- {
- vystup_html(p,get_horni_mez(),0,pocitac,true);
- break;
- }
- case 4:
- {
- if (p != 0)
- {
- delete[] p;
- p = 0;
- }
- cout << "program byl ukoncen " << endl;
- return 0;
- }
- if (p != 0)
- {
- delete[] p;
- p = 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment