Advertisement
encoree1996

db

May 17th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7.  
  8. #define _VEC(name,type,array) vector<type> name(array,array+sizeof(array)/sizeof(array[0]))
  9. #define _foreach(var,container) for(unsigned var=0;var<container.size();var++)
  10.  
  11.  
  12. using namespace std;
  13.  
  14. //========================================================
  15. int ID = 0;
  16. //--------------------------------------------------------
  17. class itemInfo
  18. {
  19. public:
  20. unsigned marka,model,wlasciciel,data,kontakt,komentarz;
  21. itemInfo()
  22. {
  23. marka=model=wlasciciel=data=kontakt=komentarz=-1;
  24. }
  25. };
  26. class baseFramework
  27. {
  28. public:
  29. string marka, model, wlasciciel, data, kontakt,komentarz;
  30. int _ID;
  31.  
  32. baseFramework()
  33. {
  34. marka=model=wlasciciel=data=kontakt=komentarz=" ";
  35. _ID = ID++;
  36. }
  37. itemInfo returnLen()
  38. {
  39. itemInfo ii;
  40. ii.marka=marka.size();
  41. ii.model=model.size();
  42. ii.wlasciciel=wlasciciel.size();
  43. ii.data==data.size();
  44. ii.kontakt==kontakt.size();
  45. ii.komentarz==komentarz.size();
  46.  
  47. return ii;
  48. }
  49.  
  50. };
  51.  
  52.  
  53.  
  54.  
  55.  
  56. //--------------------------------------------------------------------------------
  57.  
  58. static const string _cmdlist[] = {"/help","/edit","/add","/sort","/exit","/dump","/load"};
  59. _VEC(cmdlist,string,_cmdlist); //vec w cmdlist
  60. vector<baseFramework> database; //database
  61. itemInfo maxValues;
  62.  
  63.  
  64. //------------------------------------------------
  65. void parseInput(string &);
  66. void write(string);
  67. void exit();
  68. void help();
  69. void add();
  70. void _sort();
  71. void print();
  72. void edit();
  73. void dump();
  74. void load();
  75. bool idComparator(baseFramework,baseFramework);
  76.  
  77.  
  78. //==================================================================
  79.  
  80. int main()
  81. {
  82. help();
  83.  
  84. string n;
  85. for(;;)
  86. {
  87. getline(cin,n);
  88.  
  89. parseInput(n);
  90. if(n=="/exit")break;
  91. }
  92. return 0;
  93. }
  94.  
  95. //==================================================================
  96.  
  97. void write(string in)
  98. {cout<<"> "<<in;}
  99.  
  100. void help(){
  101. write("Dostepne komendy: \n");
  102. write("");
  103. _foreach(i,cmdlist)
  104. cout<<cmdlist[i]<<" ";
  105. cout<<endl;
  106. write("");
  107. }
  108.  
  109. void add()
  110. {
  111. string in="";
  112. char choice;
  113. baseFramework bf;
  114.  
  115. write("Podaj marke samochodu: ");
  116. getline(cin,in);
  117. bf.marka = in;
  118. write("Podaj model samochodu: ");
  119. getline(cin,in);
  120. bf.model = in;
  121. write("Podaj wlasciciela: ");
  122. getline(cin,in);
  123. bf.wlasciciel = in;
  124. write("Podaj date kradziezy: ");
  125. getline(cin,in);
  126. bf.data = in;
  127. write("Podaj kontakt do wlasciciela: ");
  128. getline(cin,in);
  129. bf.data = in;
  130. write("Dodatkowe komentarze: ");
  131. getline(cin,in);
  132. bf.komentarz = in;
  133. write("Chcesz dodac rekord na poczatku [B], koncu[E], czy na podanej pozycji[A]?");
  134. cin>>choice;
  135. if(choice == 'e' || choice == 'E')
  136. {
  137.  
  138. database.push_back(bf);
  139. }
  140. else
  141. if(choice=='B'||choice=='b')
  142. {
  143. database.insert(database.begin(),bf);
  144. }
  145. else
  146. if(choice=='A'||choice=='a')
  147. {
  148. write("Podaj pozycje na jakiej chcesz umiescic rekord: ");
  149. int n;
  150. cin>>n;
  151. database.insert(database.begin()+n-1,bf);
  152. }
  153.  
  154. itemInfo ii;
  155.  
  156. ii = bf.returnLen();
  157. maxValues.data = max(maxValues.data,ii.data);
  158. maxValues.model = max(maxValues.model,ii.model);
  159. maxValues.marka = max(maxValues.marka,ii.marka);
  160. maxValues.wlasciciel = max(maxValues.wlasciciel,ii.wlasciciel);
  161. maxValues.kontakt = max(maxValues.kontakt,ii.kontakt);
  162. maxValues.komentarz = max(maxValues.komentarz,ii.komentarz);
  163. }
  164.  
  165.  
  166.  
  167.  
  168. void _sort()
  169. {
  170. write("Wedlug ktorej tabeli chcesz posortowac baze danych?\n");
  171. write("__________________________________________________________________\n");
  172. write("| Marka | Model | Data | Wlasciciel | Kontakt | Komentarz | ID |\n");
  173. write("| 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n");
  174. write("");
  175. int n;
  176. cin>>n;
  177.  
  178. switch(n)
  179. {
  180. case 1:
  181. case 2:
  182. case 3:
  183. case 4:
  184. case 5:
  185. case 6:
  186. case 7:
  187. sort(database.begin(),database.end(),&idComparator);
  188. break;
  189. }
  190. write("Podaj spoosob sortowania bazy danych: a-z [AZ] | z-a [ZA]: ");
  191. string tmp;
  192. cin>>tmp;
  193. if(tmp=="AZ"||tmp=="az")
  194. write("Gotowe!\n");
  195. else if(tmp=="ZA"||tmp=="za")
  196. {
  197. reverse(database.begin(),database.end());
  198. write("Gotowe!\n");
  199. }
  200. else write("Baza danych zostala posortowana domyslnie [a-z]\n");
  201. }
  202. void print()
  203. {
  204.  
  205. }
  206. void dump()
  207. {
  208.  
  209. }
  210. void load()
  211. {
  212.  
  213. }
  214. void exit()
  215. {
  216. dump();
  217. write("Dziekuje za prace");
  218. }
  219. void edit()
  220. {
  221. write("Czy chcesz wyswietlic baze? [t/n] \n");
  222. write("");
  223. char choice;
  224. if(choice == 't')
  225. print();
  226.  
  227. write("Podaj ID wpisu, ktory chcesz edytowac: ");
  228. int n;
  229. cin>>n;
  230.  
  231.  
  232.  
  233. }
  234.  
  235. bool idComparator(baseFramework left, baseFramework right)
  236. {
  237. return left._ID>right._ID;
  238. }
  239.  
  240.  
  241. void parseInput(string &in){
  242. (in=="/exit"?exit():(in=="/help"?help():(in=="/add"?add():(in=="/sort"?_sort():(in=="/print"?print():(in=="/dump"?dump():(in=="/load"?load():(in=="/edit"?edit():write("Unknown command!\n"))))))))); //not complete
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement