Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <sstream>
- #define _VEC(name,type,array) vector<type> name(array,array+sizeof(array)/sizeof(array[0]))
- #define _foreach(var,container) for(unsigned var=0;var<container.size();var++)
- #define _SEP "*x*x*x*"
- using namespace std;
- //========================================================
- int ID = 0;
- //--------------------------------------------------------
- struct ct{int marka = -1,model = -1, data = -1, wlasciciel = -1, kontakt = -1, komentarz = -1, id = -1;};
- class baseFramework
- {
- public:
- string marka, model, wlasciciel, data, kontakt,komentarz;
- int _ID;
- baseFramework()
- {
- marka=model=wlasciciel=data=kontakt=komentarz=" ";
- _ID = ID++;
- }
- void nul()
- {
- marka=model=wlasciciel=data=kontakt=komentarz="";
- }
- };
- //--------------------------------------------------------------------------------
- static const string _cmdlist[] = {"/help","/edit","/add","/sort","/exit","/dump","/load","/print"};
- _VEC(cmdlist,string,_cmdlist); //vec w cmdlist
- vector<baseFramework> database; //database
- //------------------------------------------------
- void parseInput(string &);
- void write(string);
- void exit();
- void help();
- void add();
- void _sort();
- void print();
- void edit();
- void dump();
- void load();
- void find();
- ct count();
- bool textComparatorM(baseFramework,baseFramework);
- bool textComparatorMo(baseFramework,baseFramework);
- bool textComparatorD(baseFramework,baseFramework);
- bool textComparatorW(baseFramework,baseFramework);
- bool textComparatorK(baseFramework,baseFramework);
- bool textComparatorKo(baseFramework,baseFramework);
- bool idComparator(baseFramework,baseFramework);
- //==================================================================
- int main()
- {
- help();
- string n;
- for(;;)
- {
- getline(cin,n);
- parseInput(n);
- if(n=="/exit")break;
- write("");
- }
- return 0;
- }
- //==================================================================
- void write(string in)
- {cout<<"> "<<in;}
- void help(){
- sort(cmdlist.begin(),cmdlist.end());
- write("Dostepne komendy: \n");
- write("");
- _foreach(i,cmdlist)
- cout<<cmdlist[i]<<" ";
- cout<<endl;
- write("");
- }
- void add()
- {
- string in="";
- char choice;
- baseFramework bf;
- write("Podaj marke samochodu: ");
- getline(cin,in);
- bf.marka = in;
- write("Podaj model samochodu: ");
- getline(cin,in);
- bf.model = in;
- write("Podaj wlasciciela: ");
- getline(cin,in);
- bf.wlasciciel = in;
- write("Podaj date kradziezy: ");
- getline(cin,in);
- bf.data = in;
- write("Podaj kontakt do wlasciciela: ");
- getline(cin,in);
- bf.kontakt = in;
- write("Dodatkowe komentarze: ");
- getline(cin,in);
- bf.komentarz = in;
- database.push_back(bf);
- write("Rekord pomyslnie dodany do bazy danych.\n");
- }
- void _sort()
- {
- write("Wedlug ktorej tabeli chcesz posortowac baze danych?\n");
- write("__________________________________________________________________\n");
- write("| Marka | Model | Wlasciciel | Data | Kontakt | Komentarz | ID |\n");
- write("| 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n");
- write("");
- int n;
- cin>>n;
- switch(n)
- {
- case 1:
- sort(database.begin(),database.end(),&textComparatorM);
- break;
- case 2:
- sort(database.begin(),database.end(),&textComparatorMo);
- break;
- case 3:
- sort(database.begin(),database.end(),&textComparatorW);
- break;
- case 4:
- sort(database.begin(),database.end(),&textComparatorD);
- break;
- case 5:
- sort(database.begin(),database.end(),&textComparatorK);
- break;
- case 6:
- sort(database.begin(),database.end(),&textComparatorKo);
- break;
- case 7:
- sort(database.begin(),database.end(),&idComparator);
- break;
- }
- write("Podaj spoosob sortowania bazy danych: a-z [AZ] | z-a [ZA]: ");
- string tmp;
- cin>>tmp;
- if(tmp=="AZ"||tmp=="az")
- write("Baza posortowana od A do Z!\n");
- else if(tmp=="ZA"||tmp=="za")
- {
- reverse(database.begin(),database.end());
- write("Baza posortowana od Z do A!\n");
- }
- else write("Baza danych zostala posortowana domyslnie [a-z]\n");
- cin.ignore();
- }
- void print()
- {
- cout<<"_________________________________________________________________________________\n| Marka | Model | Wlasciciel | Data | Kontakt | Komentarz | ID |\n";
- _foreach(i,database)
- {
- ostringstream ss;
- ss<<i;
- string str = ss.str();
- printf("|%10.10s|%9.9s|%14.14s|%9.9s|%12.12s|%14.14s|%6.6s|\n",database[i].marka.c_str(),database[i].model.c_str(),database[i].wlasciciel.c_str(),database[i].data.c_str(),database[i].kontakt.c_str(),database[i].komentarz.c_str(),str.c_str());
- }
- }
- void dump()
- {
- fstream plik;
- plik.open("data.base",ios::out| ios::trunc);
- if(plik.good()==false)
- write("Nie mozna uzyskac dostepu do pliku!\n");
- else
- {
- _foreach(i,database)
- plik<<database[i].marka<<_SEP<<database[i].model<<_SEP<<database[i].wlasciciel
- <<_SEP<<database[i].data<<_SEP<<database[i].kontakt
- <<_SEP<<database[i].komentarz<<_SEP<<database[i]._ID<<endl;
- write("Baza danych zrzucona do pliku data.base!\n");
- plik.close();
- }
- }
- void load()
- {
- fstream plik;
- string line;
- baseFramework bf;
- plik.open("data.base",ios::in);
- if(plik.good()==false)
- write("Nie mozna uzyskac dostepu do pliku!\n");
- else
- {
- database.clear();
- while(getline(plik,line))
- {
- bf.nul();
- size_t found = line.find(_SEP);
- for(int i=0;i<found;i++)
- bf.marka+=line[i];
- line.erase(line.begin(),line.begin()+found+7);
- found = line.find(_SEP);
- for(int i=0;i<found;i++)
- bf.model+=line[i];
- line.erase(line.begin(),line.begin()+found+7);
- found = line.find(_SEP);
- for(int i=0;i<found;i++)
- bf.wlasciciel+=line[i];
- line.erase(line.begin(),line.begin()+found+7);
- found = line.find(_SEP);
- for(int i=0;i<found;i++)
- bf.data+=line[i];
- line.erase(line.begin(),line.begin()+found+7);
- found = line.find(_SEP);
- for(int i=0;i<found;i++)
- bf.kontakt+=line[i];
- line.erase(line.begin(),line.begin()+found+7);
- found = line.find(_SEP);
- for(int i=0;i<found;i++)
- bf.komentarz+=line[i];
- line.erase(line.begin(),line.begin()+found+7);
- bf._ID =ID++;
- database.push_back(bf);
- }
- write("Baza pomyslnie zaladowana!\n");
- plik.close();
- }
- }
- void exit()
- {
- dump();
- write("Dziekuje za prace");
- }
- void edit()
- {
- sort(database.begin(),database.end(),idComparator);
- write("Czy chcesz wyswietlic baze? [t/n] \n");
- write("");
- char choice;
- cin>>choice;
- cin.ignore();
- if(choice == 't')
- print();
- write("Podaj ID wpisu, ktory chcesz edytowac: ");
- int n;
- cin>>n;
- string in;
- cout<<"Marka: "<<database[n].marka<<"\nPodaj nowa wartosc dla tego pola: \n";
- cin.ignore();
- getline(cin,in);
- database[n].marka = in;
- cout<<"Model: "<<database[n].model<<"\nPodaj nowa wartosc dla tego pola: \n";
- getline(cin,in);
- database[n].model = in;
- cout<<"Wlasciciel: "<<database[n].wlasciciel<<"\nPodaj nowa wartosc dla tego pola: \n";
- getline(cin,in);
- database[n].wlasciciel = in;
- cout<<"Data: "<<database[n].data<<"\nPodaj nowa wartosc dla tego pola: \n";
- getline(cin,in);
- database[n].data = in;
- cout<<"Kontakt: "<<database[n].kontakt<<"\nPodaj nowa wartosc dla tego pola: \n";
- getline(cin,in);
- database[n].kontakt = in;
- cout<<"Komentarz: "<<database[n].komentarz<<"\nPodaj nowa wartosc dla tego pola: \n";
- getline(cin,in);
- database[n].komentarz = in;
- }
- bool idComparator(baseFramework left, baseFramework right)
- {
- return left._ID>right._ID;
- }
- bool textComparatorM(baseFramework left, baseFramework right)
- {
- int i=0;
- while(left.marka[i]==right.marka[i])
- i++;
- if(i==left.marka.size()&&i<right.marka.size())
- return true;
- if(left.marka[i]<right.marka[i])
- return true;
- return false;
- }
- bool textComparatorMo(baseFramework left, baseFramework right)
- {
- int i=0;
- while(left.model[i]==right.model[i])
- i++;
- if(i==left.model.size()&&i<right.model.size())
- return true;
- if(left.model[i]<right.model[i])
- return true;
- return false;
- }
- bool textComparatorK(baseFramework left, baseFramework right)
- {
- int i=0;
- while(left.kontakt[i]==right.kontakt[i])
- i++;
- if(i==left.kontakt.size()&&i<right.kontakt.size())
- return true;
- if(left.kontakt[i]<right.kontakt[i])
- return true;
- return false;
- }
- bool textComparatorKo(baseFramework left, baseFramework right)
- {
- int i=0;
- while(left.komentarz[i]==right.komentarz[i])
- i++;
- if(i==left.komentarz.size()&&i<right.komentarz.size())
- return true;
- if(left.komentarz[i]<right.komentarz[i])
- return true;
- return false;
- }
- bool textComparatorW(baseFramework left, baseFramework right)
- {
- int i=0;
- while(left.wlasciciel[i]==right.wlasciciel[i])
- i++;
- if(i==left.wlasciciel.size()&&i<right.wlasciciel.size())
- return true;
- if(left.wlasciciel[i]<right.wlasciciel[i])
- return true;
- return false;
- }
- bool textComparatorD(baseFramework left, baseFramework right)
- {
- int i=0;
- while(left.data[i]==right.data[i])
- i++;
- if(i==left.data.size()&&i<right.data.size())
- return true;
- if(left.data[i]<right.data[i])
- return true;
- return false;
- }
- void parseInput(string &in){
- (in=="/exit"?exit():(in=="/help"?help():(in=="/add"?add():(in=="/sort"?_sort():(in=="/print"?print():(in=="/dump"?dump():(in=="/load"?load():(in=="/edit"?edit():(in=="/search"?find():write("Unknown command!\n")))))))))); //not complete
- }
- void find()
- {
- vector<int> pos;
- string tmp;
- size_t found;
- cout<<"Marka: ";
- getline(cin,tmp);
- _foreach(i,database)
- {
- found = database[i].marka.find(tmp);
- if(found!=string::npos)
- pos.push_back(i);
- }
- cout<<"\nModel: ";
- getline(cin,tmp);
- _foreach(i,pos)
- {
- if(pos.empty())
- {
- cout<<"BRAK WYNIKOW!\n";
- break;
- }
- found = database[pos[i]].model.find(tmp);
- if(found==string::npos)
- {
- pos.erase(pos.begin()+i);
- i--;
- }
- }
- cout<<"\nWlasciciel: ";
- getline(cin,tmp);
- _foreach(i,pos)
- {
- if(pos.empty())
- {
- cout<<"BRAK WYNIKOW!\n";
- break;
- }
- found = database[pos[i]].wlasciciel.find(tmp);
- if(found==string::npos)
- {
- pos.erase(pos.begin()+i);
- i--;
- }
- }
- cout<<"\nData: ";
- getline(cin,tmp);
- _foreach(i,pos)
- {
- if(pos.empty())
- {
- cout<<"BRAK WYNIKOW!\n";
- break;
- }
- found = database[pos[i]].data.find(tmp);
- if(found==string::npos)
- {
- pos.erase(pos.begin()+i);
- i--;
- }
- }
- cout<<"\nKontakt: ";
- getline(cin,tmp);
- _foreach(i,pos)
- {
- if(pos.empty())
- {
- cout<<"BRAK WYNIKOW!\n";
- break;
- }
- found = database[pos[i]].kontakt.find(tmp);
- if(found==string::npos)
- {
- pos.erase(pos.begin()+i);
- i--;
- }
- }
- cout<<"\nKomentarz: ";
- getline(cin,tmp);
- _foreach(i,pos)
- {
- if(pos.empty())
- {
- cout<<"BRAK WYNIKOW!\n";
- break;
- }
- found = database[pos[i]].komentarz.find(tmp);
- if(found==string::npos)
- {
- pos.erase(pos.begin()+i);
- i--;
- }
- }
- if(pos.empty())
- {
- cout<<"Brak wynikow podczas wyszukiwania wedle zadanego kryterium.\n";
- }
- else
- {
- cout<<"___________________________________________________________________________\n| Marka | Model | Wlasciciel | Data | Kontakt | Komentarz |\n";
- _foreach(i,pos)
- printf("|%10.10s|%9.9s|%14.14s|%9.9s|%12.12s|%14.14s|\n",database[pos[i]].marka.c_str(),database[pos[i]].model.c_str(),database[pos[i]].wlasciciel.c_str(),database[pos[i]].data.c_str(),database[pos[i]].kontakt.c_str(),database[pos[i]].komentarz.c_str());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement