Guest User

Untitled

a guest
Feb 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. #include<vector>
  2. #include<string>
  3. #include<sstream>
  4. #include<fstream>
  5. #include<iostream>
  6. #include<bitset>
  7. #include<stdlib.h>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. class Personnel
  13. {
  14. protected:
  15. int ssn;
  16. int nameLen;
  17. int cityLen;
  18. int yob;
  19. string salary;
  20. char* name;
  21. char* city;
  22. public:
  23. Personnel() : nameLen(10), cityLen(10)
  24. {
  25. name = new char[nameLen + 1];
  26. city = new char[cityLen + 1];
  27. }
  28. Personnel(int nssn, int nyob, int nsalary, char* nname, char* ncity) : ssn(nssn), yob(nyob), salary(bitset<32>(nsalary).to_string()), nameLen(20), cityLen(20)
  29. {
  30. name = nname;
  31. city = ncity;
  32. }
  33. //for testing
  34. int getSsn() { return ssn; }
  35. int getYob() { return yob; }
  36. string getSalary() { return salary; }
  37. char* getName() { return name; }
  38. char* getCity() { return city; }
  39.  
  40. //writes attributes of Personnel obj to file
  41. void writeToFile(fstream& out) const
  42. {
  43. out.write(name, nameLen);
  44. out.write(city, cityLen);
  45. out << ssn;
  46. stringstream salstream(salary);
  47. string temp;
  48. while (salstream.good())
  49. {
  50. bitset<8> bits;
  51. salstream >> bits;
  52. char c = char(bits.to_ulong());
  53. temp += c;
  54. }
  55. out << temp;
  56. out << yob;
  57. out << '\n';
  58. }
  59. //reads file and assigns stored values to Personnel obj
  60. void readFromFile(fstream& in)
  61. {
  62. char temp[5];
  63. in.read(name, nameLen);
  64. in.read(city, cityLen);
  65. char ssntemp[10];
  66. in.read(ssntemp, 9);
  67. ssn = atoi(ssntemp);
  68. in.read(temp, 4);
  69. salary = bitset<8>(temp[0]).to_string();
  70. salary = salary + bitset<8>(temp[1]).to_string();
  71. salary = salary + bitset<8>(temp[2]).to_string();
  72. salary = salary + bitset<8>(temp[3]).to_string();
  73. in.seekp(+1, ios::cur);
  74. char yobtemp[5];
  75. in.read(yobtemp, 4);
  76. yob = atoi(yobtemp);
  77. in.seekp(+1, ios::cur);
  78.  
  79. }
  80. bool operator==(const Personnel &newP)
  81. {
  82. return this->ssn == newP.ssn;
  83. }
  84. int size()
  85. {
  86. return nameLen + cityLen + 4 + 9 + 4 + 2;
  87. }
  88. friend istream &operator >>(istream &in, Personnel &p)
  89. {
  90. int nsalary;
  91. cout << "Enter SSN:";
  92. in >> p.ssn;
  93. cout << "Enter year of birth: ";
  94. in >> p.yob;
  95. cout << "Enter Name: ";
  96. in.ignore(numeric_limits<streamsize>::max(), '\n');
  97. in.get(p.name, sizeof(p.name), '\n');
  98. cout << "Enter City: ";
  99. in.ignore(numeric_limits<streamsize>::max(), '\n');
  100. in.get(p.city, sizeof(p.city), '\n');
  101. cout << "Enter salary: ";
  102. in.ignore(numeric_limits<streamsize>::max(), '\n');
  103. in >> nsalary;
  104. p.salary = bitset<32>(nsalary).to_string();
  105. return in;
  106. }
  107. };
  108.  
  109. class Student : public Personnel
  110. {
  111. private:
  112. char* major;//
  113. int majorLen;
  114. public:
  115. char* getMajor() { return major; }//
  116. Student() :majorLen(30)
  117. {
  118. major = new char[majorLen];
  119. }
  120. void writeToFile(fstream& out) const
  121. {
  122. out.write(name, nameLen);
  123. out.write(city, cityLen);
  124. out << ssn;
  125. stringstream salstream(salary);
  126. string temp;
  127. while (salstream.good())
  128. {
  129. bitset<8> bits;
  130. salstream >> bits;
  131. char c = char(bits.to_ulong());
  132. temp += c;
  133. }
  134. out << temp;
  135. out << yob;
  136. out.write(major, majorLen);
  137. out << '\n';
  138. }
  139. void readFromFile(fstream& in)
  140. {
  141. char temp[5];
  142. in.read(name, nameLen);
  143. in.read(city, cityLen);
  144. char ssntemp[10];
  145. in.read(ssntemp, 9);
  146. ssn = atoi(ssntemp);
  147. in.read(temp, 4);
  148. salary = bitset<8>(temp[0]).to_string();
  149. salary = salary + bitset<8>(temp[1]).to_string();
  150. salary = salary + bitset<8>(temp[2]).to_string();
  151. salary = salary + bitset<8>(temp[3]).to_string();
  152. in.seekp(+1, ios::cur);
  153. char yobtemp[5];
  154. in.read(yobtemp, 4);
  155. yob = atoi(yobtemp);
  156. in.read(major, majorLen);
  157. in.seekp(+1, ios::cur);
  158. }
  159. int size()
  160. {
  161. return Personnel::size() + majorLen;
  162. }
  163. friend istream &operator >>(istream &in, Student &p)
  164. {
  165. int nsalary;
  166.  
  167. cout << "Enter SSN:";
  168. in >> p.ssn;
  169. cout << "Enter year of birth: ";
  170. in >> p.yob;
  171. cout << "Enter Name: ";
  172. in.ignore(numeric_limits<streamsize>::max(), '\n');
  173. in.get(p.name, sizeof(p.name), '\n');
  174. cout << "Enter City: ";
  175. in.ignore(numeric_limits<streamsize>::max(), '\n');
  176. in.get(p.city, sizeof(p.city), '\n');
  177. cout << "Enter salary: ";
  178. in.ignore(numeric_limits<streamsize>::max(), '\n');
  179. in >> nsalary;
  180. p.salary = bitset<32>(nsalary).to_string();
  181. cout << "Enter major: ";
  182. in.ignore(numeric_limits<streamsize>::max(), '\n');
  183. in.get(p.major, sizeof(p.major), '\n');
  184. in.ignore(numeric_limits<streamsize>::max(), '\n');
  185. return in;
  186.  
  187. }
  188. };
  189.  
  190. template<class T>
  191. class Database
  192. {
  193. private:
  194. fstream database;
  195. public:
  196. Database()
  197. {}
  198. Database(string filename)
  199. {
  200. database.open(filename, fstream::out | fstream::in);
  201. if (!database.is_open())
  202. {
  203. database.open(filename, fstream::out | fstream::in | fstream::trunc);
  204. }
  205. }
  206. void print()
  207. {
  208. database.clear();
  209. database.seekp(0, ios::beg);
  210. string s;
  211. Personnel obj;
  212. int num = 0;
  213. cout << " = DATA = " << endl;
  214. do
  215. {
  216. obj.readFromFile(database);
  217. if (obj.getSsn() != 0)
  218. {
  219. cout << "Name: " << obj.getName() << endl;
  220. cout << "YOB: " << obj.getYob() << endl;
  221. cout << "SSN: " << obj.getSsn() << endl;
  222. cout << "City: " << obj.getCity() << endl;
  223. for (int i = 0; i < 32; i++)
  224. {
  225. if (obj.getSalary()[i] == '1') {
  226. num += pow(2, 31 - i);
  227. }
  228. }
  229. cout << "Salary: " << num << endl << endl;
  230. num = 0;
  231. }
  232. } while (getline(database, s));
  233. database.seekp(0, ios::beg);
  234. }
  235. //Student print
  236. void printS()
  237. {
  238. database.clear();
  239. database.seekp(0, ios::beg);
  240. string s;
  241. Student obj;
  242. int num = 0;
  243. cout << " = DATA = " << endl;
  244. do
  245. {
  246. obj.readFromFile(database);
  247. if (obj.getSsn() != 0)
  248. {
  249. cout << "Name: " << obj.getName() << endl;
  250. cout << "YOB: " << obj.getYob() << endl;
  251. cout << "SSN: " << obj.getSsn() << endl;
  252. cout << "City: " << obj.getCity() << endl;
  253. for (int i = 0; i < 32; i++)
  254. {
  255. if (obj.getSalary()[i] == '1') {
  256. num += pow(2, 31 - i);
  257. }
  258. }
  259. cout << "Salary: " << num << endl;
  260. cout << "Major: "<< obj.getMajor()<<endl<<endl;
  261. num = 0;
  262. }
  263.  
  264. } while (getline(database, s));
  265. database.seekp(0, ios::beg);
  266. }
  267. //add file to end of database
  268. void add(T file)
  269. {
  270. database.seekp(0, ios::beg);
  271. if (!(this->find(file)))
  272. {
  273. database.clear();
  274. database.seekg(0, ios::end);
  275. file.writeToFile(database);
  276. }
  277. database.seekp(0, ios::beg);
  278. }
  279. bool find(T file)
  280. {
  281. database.clear();
  282. database.seekp(0, ios::beg);
  283. T p;
  284. string s;
  285.  
  286. do
  287. {
  288. p.readFromFile(database);
  289. if (p == file)
  290. {
  291. return true;
  292. }
  293. } while (getline(database, s));
  294. return false;
  295.  
  296. }
  297. //not done
  298. void modify(T file)
  299. {
  300. database.clear();
  301. database.seekp(0, ios::beg);
  302. if (this->find(file))
  303. {
  304. T editP;
  305.  
  306. cout << "Please enter new information" << endl;
  307. cin >> editP;
  308. if (file.getSsn() == editP.getSsn())
  309. {
  310. database.seekp(-file.size(), ios::cur);
  311. editP.writeToFile(database);
  312. }
  313. else
  314. {
  315. cout << "Error. SSN does not match." << endl;
  316. }
  317. }
  318. }
  319. };
  320.  
  321. template<class T>
  322. void menu(string filename)
  323. {
  324. Database<T> database(filename);
  325. int choice = 0;
  326. T newPerson;
  327.  
  328. cout << "--- " << filename << " ---";
  329. do {
  330. cout << endl << "Options" << endl;
  331. cout << "1. New entry" << endl;
  332. cout << "2. Print database" << endl;
  333. cout << "3. Modify entry" << endl;
  334. cout << "4. Exit" << endl;
  335. cout << "Select: ";
  336. cin >> choice;
  337. cout << endl;
  338. bool isStudent = is_same<T, Student>::value;
  339. switch (choice) {
  340. case 1:
  341. cin >> newPerson;
  342. database.add(newPerson);
  343. break;
  344. case 2:
  345. if (!isStudent) {
  346. database.print();
  347. break;
  348. }
  349. else if (isStudent) {
  350. database.printS();
  351. break;
  352. }
  353. case 3:
  354. cout << "Please enter information (at least SSN) for Personnel to modify. Enter 0 if you don't know." << endl;
  355. cin >> newPerson;
  356. database.modify(newPerson);
  357. break;
  358. default: break;
  359. }
  360. } while (choice != 4);
  361. }
  362.  
  363. //need menu
  364. int main()
  365. {
  366. /* Database<Personnel> database("database.txt");
  367. char n[10] = { 'r','o','s','e' };
  368. char c[10] = { 'h','o' };
  369. char n2[10] = { 'q','w' };
  370. char c2[10] = { 's','d' };
  371. Personnel p(123456789, 1111, 50000, n, c);
  372. Personnel q(123456790, 1111, 50000, n2, c2);
  373.  
  374. database.add(p);
  375. database.add(q);
  376. database.add(Personnel(111111111, 2222, 400, n, c));
  377. database.add(Personnel(333333333, 3333, 80, n2, c2));
  378. */
  379. int option;
  380. string filename;
  381. do
  382. {
  383. cout << "Options " << endl;
  384. cout << "1. Create/Open Student database" << endl;
  385. cout << "2. Create/Open Personnel database" << endl;
  386. cout << "3. Exit" << endl << "Select: ";
  387. cin >> option;
  388.  
  389. if (option == 1)
  390. {
  391. cout << "Enter database filename: ";
  392. cin >> filename;
  393. menu<Student>(filename);
  394. }
  395. else if (option == 2)
  396. {
  397. cout << "Enter database filename: ";
  398. cin >> filename;
  399. menu<Personnel>(filename);
  400. }
  401.  
  402.  
  403. } while (option != 3);
  404.  
  405.  
  406. return 0;
  407. }
Add Comment
Please, Sign In to add comment