Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <windows.h>
  4. using namespace std;
  5.  
  6. fstream plik, config;
  7. string nazin, fileKey, keyConfig, ynConfig, newTab, newTabFS;
  8. int x, y, ynCheck;
  9.  
  10. int showTables()
  11. {
  12. string keyConfig, coutConfig = "\n Dostępne tabele w bazie:";
  13. config.open("tablelist.cfg", ios::in);
  14. config >> ynConfig >> keyConfig;
  15. if (ynConfig != "1")
  16. {
  17. cout << "\n ERROR: Konfiguracja zablokowana do odczytu!\n";
  18. system("PAUSE");
  19. return 0;
  20. }
  21. if (keyConfig != ".DKTABCFGCPP")
  22. {
  23. cout << "\n ERROR: Niepoprawny format!\n";
  24. system("PAUSE");
  25. return 0;
  26. }
  27. while(!config.eof())
  28. {
  29. cout << " " << coutConfig << endl;
  30. config >> coutConfig;
  31. }
  32. cout << " #EOF\n";
  33. config.close();
  34. }
  35.  
  36. int loadData()
  37. {
  38. plik.open(nazin.c_str(), ios::in);
  39. plik >> ynCheck;
  40. plik >> fileKey;
  41.  
  42. if (ynCheck == 1)
  43. {
  44. if (fileKey == ".DKTABCPP")
  45. {
  46. plik >> x >> y;
  47. int tab[x][y];
  48. system("PAUSE");
  49. system("CLS");
  50. cout << "\n Loading data...\n";
  51. for (int i = 0; i<x; i++)
  52. {
  53. for (int j = 0; j<y; j++)
  54. {
  55. plik >> tab[i][j];
  56. cout << "Position: [" << i+1 << "][" << j+1 << "] loaded!\n";
  57. }
  58. }
  59. cout << " Data loaded.\n";
  60. for (;;)
  61. {
  62. cout << "\n 1.Edytuj otwartą tablicę.\n 2.Wyświetl otwartą tablicę.\n 3.Zapisz tablice.\n 4.Przejdz do głównego menu.\n$$: ";
  63. char pickOne;
  64. while (cin >> pickOne && pickOne != '1' && pickOne != '2' && pickOne != '3' && pickOne != '4');
  65. if (pickOne == '1')
  66. {
  67. int x1, y1, nValue, n;
  68. cout << "\n Podaj: N(ILOŚĆ ZMIAN)\n KOLEJNO: X Y NOWA_WARTOŚĆ. \n$$: ";
  69. cin >> n;
  70. if (n<=0)
  71. {
  72. cout << "\n ERROR: Nieprawidłowa wartość dla N. (N!<=0). \n";
  73. } else {
  74. for (int u = 0; u<n; u++)
  75. {
  76. cout << "$" << -(u-n) << ": ";
  77. cin >> x1 >> y1 >> nValue;
  78. x1 -= 1;
  79. y1 -= 1;
  80. if (x1 < 0 || y1 < 0 )
  81. {
  82. cout << "\n ERROR: X/Y nie mogą być mniejsze/równe \"0\"!\n";
  83. u--;
  84. } else {
  85. int oldValue = tab[x1][y1];
  86. tab[x1][y1] = nValue;
  87. cout << "\n INFO: Komórka: [" << x1+1 << "][" << y1+1 << "] zmieniła wartość z: \"" << oldValue << "\" na: \"" << nValue << "\".\n";
  88. }
  89. }
  90. }
  91. }
  92. else if (pickOne == '2')
  93. {
  94. if(plik.is_open())
  95. {
  96. cout << endl;
  97. for (int i = 0; i<x; i++)
  98. {
  99. for (int j = 0; j<y; j++)
  100. {
  101. cout << " " << tab[i][j];
  102. if (j==y-1)
  103. {
  104. cout << endl;
  105. }
  106. }
  107. }
  108. } else {
  109. cout << "ERROR: Żadena tablica nie jest otwarta!\n";
  110. }
  111. }
  112. else if (pickOne == '3')
  113. {
  114. system("CLS");
  115. cout << "\n Zapisywanie...\n";
  116. plik.close();
  117. plik.open(nazin.c_str(), ios::out);
  118. plik << ynCheck << " " << fileKey << endl << x << " "<< y << endl;
  119. for (int i = 0; i<x; i++)
  120. {
  121. for (int j = 0; j<y; j++)
  122. {
  123. plik << tab[i][j] << " ";
  124. cout << "Position: [" << i+1 << "][" << j+1 << "] with score: \"" << tab[i][j] << "\" saved!\n";
  125. }
  126. }
  127. plik.close();
  128. plik.open(nazin.c_str(), ios::in);
  129. cout << " INFO: Dane zapisane do pliku.\n";
  130. system("PAUSE");
  131. }
  132. else if (pickOne == '4')
  133. {
  134. plik.close();
  135. break;
  136. }
  137. }
  138. } else {
  139. cout << "\n ERROR! Niepoprawne rozszerzenie pliku!\n";
  140. system("PAUSE");
  141. }
  142. } else {
  143. cout << "\n ERROR: Wyłączona edycja. Włącz spowrotem w pliku!\n";
  144. system("PAUSE");
  145. }
  146. }
  147.  
  148. int openTable()
  149. {
  150. system("CLS");
  151. showTables();
  152. cout << "\nPodaj nazwe tablicy [By wyjść - exit]:\n$$: ";
  153. cin >> nazin;
  154. if (nazin == "exit") {
  155. return 0;
  156. }
  157. for (;;) {
  158. nazin += ".txt";
  159. fstream exist;
  160. exist.open(nazin.c_str(), ios::in);
  161. if (exist.is_open()) {
  162. exist.close();
  163. loadData();
  164. break;
  165. } else {
  166. cout << " ERROR: Tablica o podanej nazwie nie istnieje. Spr\242buj ponownie:\n$$: ";
  167. cin >> nazin;
  168. }
  169. }
  170. }
  171.  
  172. int createTable()
  173. {
  174. config.open("tablelist.cfg", ios::in);
  175. config >> ynConfig >> keyConfig;
  176. if (ynConfig != "1")
  177. {
  178. cout << "\n ERROR: Konfiguracja zablokowana do odczytu!\n";
  179. system("PAUSE");
  180. config.close();
  181. return 0;
  182. }
  183. if (keyConfig != ".DKTABCFGCPP")
  184. {
  185. cout << "\n ERROR: Niepoprawny format!\n";
  186. system("PAUSE");
  187. config.close();
  188. return 0;
  189. }
  190. config.close();
  191. config.open("tablelist.cfg", ios::app);
  192. system("CLS");
  193. cout << "Podaj nazwe tablicy:\n$$: ";
  194. cin >> newTab;
  195. for(;;)
  196. {
  197. fstream exist;
  198. newTabFS = newTab;
  199. newTab += ".txt";
  200. exist.open(newTab.c_str(), ios::in);
  201. if (exist.is_open()) {
  202. cout << "Plik o podanej nazwie już istnieje (TXT ONLY ALLOWED). Spr\242buj ponownie:\n$$: ";
  203. exist.close();
  204. cin >> newTab;
  205. } else {
  206. config << newTabFS << endl;
  207. config.close();
  208. int X, Y, xxx;
  209. fstream newTabb;
  210. newTabb.open(newTab.c_str(), ios::out);
  211. newTabb << "1 .DKTABCPP" << endl;
  212. cout << "\n Podaj: X Y i w kolejnch liniach kolejne wartości tabeli.\n$$: ";
  213. cin >> X >> Y;
  214. newTabb << X << " " << Y << endl;
  215. for (int i = 0; i<X; i++)
  216. {
  217. for (int j = 0; j<Y; j++)
  218. {
  219. cout << "[" << i+1 << "]" << "[" << j+1 << "]\n$$: ";
  220. cin >> xxx;
  221. newTabb << xxx << " ";
  222. }
  223. }
  224. system("CLS");
  225. cout << "\n INFO: Tablica \"" << newTab << "\" została utworzona!\n";
  226. system("PAUSE");
  227. newTabb.close();
  228. break;
  229. }
  230. }
  231. }
  232.  
  233. int deleteTab()
  234. {
  235. system("CLS");
  236. showTables();
  237. string deletetab;
  238. cout << "\n Podaj nazwę tablicy do usunięcia:\n$$: ";
  239. cin >> deletetab;
  240. int change = 0;
  241. string keyConfig, output, read, ynConfig;
  242. config.open("tablelist.cfg", ios::in);
  243. config >> ynConfig >> keyConfig;
  244. if (ynConfig != "1")
  245. {
  246. cout << "\n ERROR: Konfiguracja zablokowana do odczytu!\n";
  247. system("PAUSE");
  248. return 0;
  249. }
  250. if (keyConfig != ".DKTABCFGCPP")
  251. {
  252. cout << "\n ERROR: Niepoprawny format!\n";
  253. system("PAUSE");
  254. return 0;
  255. }
  256. output += ynConfig + " " + keyConfig;
  257. while(!config.eof())
  258. {
  259. if (read != deletetab)
  260. {
  261. output += read + "\n";
  262. } else {
  263. change++;
  264. }
  265. config >> read;
  266. }
  267. config.close();
  268. if (change == 1)
  269. {
  270. deletetab += ".txt";
  271. config.open("tablelist.cfg", ios::in);
  272. config >> ynConfig >> keyConfig;
  273. if (ynConfig != "1")
  274. {
  275. cout << "\n ERROR: Konfiguracja zablokowana do odczytu!\n";
  276. system("PAUSE");
  277. config.close();
  278. return 0;
  279. }
  280. if (keyConfig != ".DKTABCFGCPP")
  281. {
  282. cout << "\n ERROR: Niepoprawny format!\n";
  283. system("PAUSE");
  284. config.close();
  285. return 0;
  286. }
  287. config.close();
  288. config.open("tablelist.cfg", ios::out);
  289. config << output;
  290. config.close();
  291. cout << "\n INFO: Pomyślnie usunięto tablicę: \"" << deletetab << "\".\n";
  292. cout << " Usunąć też plik z tablicą? [y/n]\n$$: ";
  293. char yndelete;
  294. while (cin >> yndelete && yndelete != 'y' && yndelete != 'n')
  295. {
  296. cout << "\n [y/n] Ponów próbę:\n$$: ";
  297. }
  298. if (yndelete == 'y')
  299. {
  300. if ( remove( deletetab.c_str() ) != 0 )
  301. {
  302. perror( "\n ERROR: Plik nie istnieje!" );
  303. } else {
  304. puts( "\n INFO: Plik został usunięty." );
  305. }
  306. }
  307. } else {
  308. cout << "\n ERROR: Podana tablica nie istnieje! Nie wprowadzono zmian.\n";
  309. }
  310. system("PAUSE");
  311. }
  312.  
  313. int addTable()
  314. {
  315. system("CLS");
  316. cout << "\nPodaj nazwe tablicy do dodania [By wyjść - exit]:\n$$: ";
  317. cin >> nazin;
  318. if (nazin == "exit") {
  319. return 0;
  320. }
  321. string nazinFS = nazin;
  322. for (;;) {
  323. int X = 0, Y = 0;
  324. nazin += ".txt";
  325. fstream exist;
  326. exist.open(nazin.c_str(), ios::in);
  327. if (exist.is_open()) {
  328. exist.close();
  329. plik.open(nazin.c_str(), ios::in);
  330. plik >> ynCheck >> fileKey >> X >> Y;
  331. if (ynCheck == 1 && fileKey == ".DKTABCPP" && X != 0 && Y != 0)
  332. {
  333. config.open("tablelist.cfg", ios::in);
  334. config >> ynConfig >> keyConfig;
  335. if (ynConfig != "1")
  336. {
  337. cout << "\n ERROR: Konfiguracja zablokowana do odczytu!\n";
  338. system("PAUSE");
  339. config.close();
  340. return 0;
  341. }
  342. if (keyConfig != ".DKTABCFGCPP")
  343. {
  344. cout << "\n ERROR: Niepoprawny format!\n";
  345. system("PAUSE");
  346. config.close();
  347. return 0;
  348. }
  349. config.close();
  350. config.open("tablelist.cfg", ios::app);
  351. config << nazinFS << endl;
  352. config.close();
  353. cout << "\n INFO: Pomyślnie dodano tablice do bazy!\n";
  354. break;
  355. } else {
  356. cout << "\n ERROR: Tablica nie może być dodana! Zły format.\n";
  357. }
  358. } else {
  359. cout << " ERROR: Tablica o podanej nazwie nie istnieje. Spr\242buj ponownie:\n$$: ";
  360. cin >> nazin;
  361. }
  362. }
  363. system("PAUSE");
  364. }
  365.  
  366. int main()
  367. {
  368. setlocale(LC_ALL, "polish");
  369. fstream exist;
  370. exist.open("tablelist.cfg", ios::in);
  371. if (exist.is_open()) {
  372. exist.close();
  373. } else {
  374. config.open("tablelist.cfg", ios::out);
  375. config << "1 .DKTABCFGCPP" << endl;
  376. config.close();
  377. }
  378. for (;;)
  379. {
  380. system("CLS");
  381. cout << "Witaj w menedzerze tablicą z danymi. Wybierz opcje:\n 1.Otwórz istniejącą tablice.\n 2.Stwórz nową tablice.\n 3.Usuń tablicę.\n 4.Wyświetl zapisane tablice.\n 5.Dodaj tablicę z pliku.\nCreated By Damian Kreński\n$$: ";
  382. char chose;
  383. while ( cin >> chose && chose != '1' && chose != '2' && chose != '3' && chose != '4' && chose != '5' );
  384. if (chose == '1')
  385. {
  386. openTable();
  387. }
  388. else if (chose == '2')
  389. {
  390. createTable();
  391. }
  392. else if (chose == '3')
  393. {
  394. deleteTab();
  395. }
  396. else if (chose == '4')
  397. {
  398. showTables();
  399. system("PAUSE");
  400. }
  401. else if (chose == '5')
  402. {
  403. addTable();
  404. }
  405. }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement