Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.19 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. #include <iomanip>
  7. #include <sstream>
  8. #include <stdlib.h>
  9.  
  10. using namespace std;
  11.  
  12. struct Students
  13. {
  14. string Surname;
  15. string Name;
  16. string Patronymic;
  17. int Age;
  18. string Sex;
  19. int Course;
  20. double Progress;
  21.  
  22. Students(string Su, string Na, string Pa, int Ag, string Se, int Co, double Pr) : Surname(Su), Name(Na), Patronymic(Pa), Age(Ag), Sex(Se), Course(Co), Progress(Pr) {}
  23. };
  24.  
  25. void CreateFile(char FileName[50])
  26. {
  27. ofstream F(FileName);
  28.  
  29. F.close();
  30.  
  31. cout << "txt file to the data base of students ready!" << endl;
  32.  
  33. }
  34.  
  35. void Save(vector<Students*> &Stud, char FileName[50])
  36. {
  37. ofstream F(FileName);
  38.  
  39. for (int i = 0; i < Stud.size(); i++)
  40. {
  41. F << Stud[i]->Surname << "|";
  42. F << Stud[i]->Name << "|";
  43. F << Stud[i]->Patronymic << "|";
  44. F << Stud[i]->Age << "|";
  45. F << Stud[i]->Sex << "|";
  46. F << Stud[i]->Course << "|";
  47. F << Stud[i]->Progress << endl;
  48. }
  49. F.close();
  50. }
  51.  
  52. vector<string> explode(string const &s, char delim)
  53. {
  54. vector<string> result;
  55. istringstream iss(s);
  56.  
  57. for (string token; getline(iss, token, delim); )
  58. {
  59. result.push_back(move(token));
  60. }
  61.  
  62. return result;
  63. }
  64.  
  65. void Input(vector<Students*> &Stud, char FileName[50])
  66. {
  67. ifstream Student(FileName);
  68. if (!Student){}
  69. else
  70. {
  71. string s;
  72. vector<string> temp;
  73.  
  74. while (getline(Student, s))
  75. {
  76. temp = explode(s, '|');
  77. Stud.push_back(new Students(temp[0], temp[1], temp[2], atoi(temp[3].c_str()), temp[4], atoi(temp[5].c_str()), atof(temp[6].c_str())));
  78. }
  79. }
  80. }
  81.  
  82. void Output(vector<Students*> &Stud, char FileName[50])
  83. {
  84. int opinion;
  85. ifstream Student(FileName);
  86. if (!Student)
  87. {
  88. cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
  89. cin >> opinion;
  90. if(opinion == 1) CreateFile(FileName);
  91. }
  92. else
  93. {
  94. int count = 0;
  95. for (int i = 0; i < Stud.size(); i++)
  96. {
  97. count++;
  98. }
  99. if (count == 0) cout << "No entries yet!" << endl;
  100. else
  101. {
  102. for (int i = 0; i < Stud.size(); i++)
  103. {
  104. cout << i << " ";
  105. cout << Stud[i]->Surname << " ";
  106. cout << Stud[i]->Name << " ";
  107. cout << Stud[i]->Patronymic << " ";
  108. cout << Stud[i]->Age << " ";
  109. cout << Stud[i]->Sex << " ";
  110. cout << Stud[i]->Course << " ";
  111. cout << Stud[i]->Progress << endl;
  112. }
  113. }
  114. count = 0;
  115. }
  116. }
  117.  
  118. void AddStudent(vector<Students*> &Stud, char FileName[50])
  119. {
  120. int opinion;
  121.  
  122. ifstream F(FileName);
  123. if (!F)
  124. {
  125. cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
  126. cin >> opinion;
  127. if(opinion == 1)
  128. {
  129. CreateFile(FileName);
  130.  
  131. Stud.push_back(new Students("", "", "", 0, "", 0, 0));
  132.  
  133. cout << "Surname: "; cin >> Stud[Stud.size()-1]->Surname;
  134. cout << "Name: "; cin >> Stud[Stud.size()-1]->Name;
  135. cout << "Patronymic: "; cin >> Stud[Stud.size()-1]->Patronymic;
  136. cout << "Age: "; cin >> Stud[Stud.size()-1]->Age;
  137. cout << "Sex: "; cin >> Stud[Stud.size()-1]->Sex;
  138. cout << "Course: "; cin >> Stud[Stud.size()-1]->Course;
  139. cout << "Progress: "; cin >> Stud[Stud.size()-1]->Progress;
  140.  
  141. Save(Stud, FileName);
  142. }
  143. }
  144. else
  145. {
  146. Stud.push_back(new Students("", "", "", 0, "", 0, 0));
  147.  
  148. cout << "Surname: "; cin >> Stud[Stud.size()-1]->Surname;
  149. cout << "Name: "; cin >> Stud[Stud.size()-1]->Name;
  150. cout << "Patronymic: "; cin >> Stud[Stud.size()-1]->Patronymic;
  151. cout << "Age: "; cin >> Stud[Stud.size()-1]->Age;
  152. cout << "Sex: "; cin >> Stud[Stud.size()-1]->Sex;
  153. cout << "Course: "; cin >> Stud[Stud.size()-1]->Course;
  154. cout << "Progress: "; cin >> Stud[Stud.size()-1]->Progress;
  155.  
  156. Save(Stud, FileName);
  157. }
  158. }
  159.  
  160. void FindSttudentsNCourse(vector<Students*> &Stud, char FileName[50])
  161. {
  162. int opinion;
  163. ifstream Student(FileName);
  164. if (!Student)
  165. {
  166. cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
  167. cin >> opinion;
  168. if(opinion == 1) CreateFile(FileName);
  169. }
  170. else
  171. {
  172. int count = 0;
  173. for (int i = 0; i < Stud.size(); i++)
  174. {
  175. count++;
  176. }
  177. if (count == 0) cout << "No entries yet!" << endl;
  178. else
  179. {
  180.  
  181. char New[50] = "/Users/CoolGeek/Documents/SixStudent.txt";
  182.  
  183. ofstream F(New);
  184.  
  185. int temp;
  186. cout << "Input course: ";
  187. cin >> temp;
  188.  
  189. for (int i = 0; i < Stud.size(); i++)
  190. {
  191. if (Stud[i]->Course == temp)
  192. {
  193. F << i << " ";
  194. F << Stud[i]->Surname << " ";
  195. F << Stud[i]->Name << " ";
  196. F << Stud[i]->Patronymic << " ";
  197. F << Stud[i]->Age << " ";
  198. F << Stud[i]->Sex << " ";
  199. F << Stud[i]->Course << " ";
  200. F << Stud[i]->Progress << endl;
  201. }
  202. }
  203. cout << "txt file to the data base of students ready!" << endl;
  204. F.close();
  205. }
  206. count = 0;
  207. }
  208. }
  209.  
  210. void DeleteStudent(vector<Students*> &Stud, char FileName[50])
  211. {
  212. int opinion;
  213.  
  214. ifstream F(FileName);
  215. if(!F)
  216. {
  217. cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
  218. cin >> opinion;
  219. if(opinion == 1)
  220. {
  221. CreateFile(FileName);
  222. }
  223.  
  224. }
  225. else
  226. {
  227. int count = 0;
  228. for (int i = 0; i < Stud.size(); i++)
  229. {
  230. count++;
  231. }
  232.  
  233. if (count == 0) cout << "No entries yet!" << endl;
  234. else
  235. {
  236. int temp;
  237. cout << "Input ID: ";
  238. cin >> temp;
  239.  
  240. Stud.erase(Stud.begin() + temp);
  241. Save(Stud, FileName);
  242. }
  243. count = 0;
  244. }
  245. }
  246.  
  247. void EditStudent(vector<Students*> &Stud, char FileName[50])
  248. {
  249. int opinion;
  250.  
  251. ifstream F(FileName);
  252. if (!F)
  253. {
  254. cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
  255. cin >> opinion;
  256. if(opinion == 1)
  257. {
  258. CreateFile(FileName);
  259. }
  260. }
  261. else
  262. {
  263. int count = 0;
  264. for (int i = 0; i < Stud.size(); i++)
  265. {
  266. count++;
  267. }
  268. if (count == 0) cout << "No entries yet!" << endl;
  269. else
  270. {
  271. int temp;
  272. cout << "Input ID: ";
  273. cin >> temp;
  274.  
  275. cout << "Surname: "; cin >> Stud[temp]->Surname;
  276. cout << "Name: "; cin >> Stud[temp]->Name;
  277. cout << "Patronymic: "; cin >> Stud[temp]->Patronymic;
  278. cout << "Age: "; cin >> Stud[temp]->Age;
  279. cout << "Sex: "; cin >> Stud[temp]->Sex;
  280. cout << "Course: "; cin >> Stud[temp]->Course;
  281. cout << "Progress: "; cin >> Stud[temp]->Progress;
  282. }
  283. count = 0;
  284. }
  285. }
  286.  
  287. int main()
  288. {
  289. vector<Students*> Stud;
  290. char FileName[50] = "/Users/CoolGeek/Documents/students.txt";
  291.  
  292. int menu;
  293.  
  294. do
  295. {
  296. cout << endl << "1. Create a txt file to store database (required for first start)" << endl
  297. << "2. View the contents of a databaseĐ°." << endl
  298. << "3. Add an entry in the database." << endl
  299. << "4. Delete an entry from the database." << endl
  300. << "5. Edit entry in the database" << endl
  301. << "6. Students N rate" << endl
  302. << "0. Exit" << endl << endl;
  303.  
  304. cout << "Select action: "; cin >> menu;
  305.  
  306. switch (menu) {
  307.  
  308. case 1:
  309. {
  310. ifstream F(FileName);
  311. if (!F) CreateFile(FileName);
  312. else cout << "The file has already been created and is ready to use!" << endl;
  313. break;
  314. }
  315.  
  316. case 2:
  317. {
  318. Input(Stud, FileName);
  319. Output(Stud, FileName);
  320. Stud.clear();
  321. break;
  322. }
  323.  
  324. case 3:
  325. {
  326. Input(Stud, FileName);
  327. AddStudent(Stud, FileName);
  328. Stud.clear();
  329. break;
  330. }
  331.  
  332. case 4:
  333. {
  334. Input(Stud, FileName);
  335. DeleteStudent(Stud, FileName);
  336. Stud.clear();
  337. break;
  338. }
  339.  
  340. case 5:
  341. {
  342. Input(Stud, FileName);
  343. EditStudent(Stud, FileName);
  344. Stud.clear();
  345. break;
  346. }
  347.  
  348. case 6:
  349. {
  350. Input(Stud, FileName);
  351. FindSttudentsNCourse(Stud, FileName);
  352. Stud.clear();
  353. break;
  354. }
  355.  
  356. case 0:
  357. {
  358. cout << "Bye!" << endl;
  359. break;
  360. }
  361.  
  362. default:
  363. {
  364. cout << "ERROR!" << endl;
  365. break;
  366. }
  367. }
  368. }
  369. while (menu != 0);
  370.  
  371. return 0;
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement