Advertisement
fabbe680

Untitled

Jan 14th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. #include <iomanip>
  7. #include <algorithm>
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include "constants.h"
  11.  
  12. using namespace std;
  13.  
  14. //----------------------------------------------------------------------------------------------------------------------
  15. // Function List
  16. //----------------------------------------------------------------------------------------------------------------------
  17.  
  18. void mainMenu();
  19. int menuValidate(int menuLimit, int numSelect);
  20. void addEntry();
  21. void printList();
  22. void searchList();
  23. void sortList();
  24. void randomOrder();
  25. void saveFile();
  26. void readFile();
  27. string toLower(string s);
  28.  
  29. //----------------------------------------------------------------------------------------------------------------------
  30. // Values
  31. //----------------------------------------------------------------------------------------------------------------------
  32.  
  33. vector<list> nameList;
  34.  
  35. //----------------------------------------------------------------------------------------------------------------------
  36. // Main program
  37. //----------------------------------------------------------------------------------------------------------------------
  38.  
  39. int main() {
  40.  
  41. mainMenu();
  42.  
  43. return 0;
  44. }
  45.  
  46. //----------------------------------------------------------------------------------------------------------------------
  47. // mainMenu
  48. //----------------------------------------------------------------------------------------------------------------------
  49.  
  50. void mainMenu() {
  51.  
  52. string menuSelect;
  53. int numSelect;
  54.  
  55. cout << "==== Main Menu ====" << endl;
  56. cout << endl;
  57. cout << "1. Add Entry" << endl;
  58. cout << "2. Print List" << endl;
  59. cout << "3. Search" << endl;
  60. cout << "4. Sort" << endl;
  61. cout << "5. Randomize Order" << endl;
  62. cout << "6. Save to File" << endl;
  63. cout << "7. Read from File" << endl;
  64. cout << "8. Exit" << endl;
  65. cout << endl;
  66.  
  67. cout << "Make Selection [1-8]: ";
  68. cin >> menuSelect;
  69. istringstream iss(menuSelect);
  70. iss >> numSelect;
  71. numSelect = menuValidate(8, numSelect);
  72.  
  73. switch(numSelect) {
  74. case 1 : addEntry();
  75. break;
  76. case 2 : printList();
  77. break;
  78. case 3 : searchList();
  79. break;
  80. case 4 : sortList();
  81. break;
  82. case 5 : randomOrder();
  83. break;
  84. case 6 : saveFile();
  85. break;
  86. case 7 : readFile();
  87. break;
  88. case 8 : return;
  89. }
  90. }
  91.  
  92. //----------------------------------------------------------------------------------------------------------------------
  93. // menuValidate
  94. //----------------------------------------------------------------------------------------------------------------------
  95.  
  96. int menuValidate(int menuLimit, int numSelect) {
  97.  
  98. string menuSelect;
  99. bool cinFail = true;
  100.  
  101. while(cinFail == true) {
  102. if (cin.fail() || numSelect < 1 || numSelect > menuLimit) {
  103. numSelect = 0;
  104. cout << "Invalid Choice!" << endl;
  105. cout << "Make Selection [1-8]: ";
  106. cin >> menuSelect;
  107. istringstream iss(menuSelect);
  108. iss >> numSelect;
  109. } else {
  110. cinFail = false;
  111. }
  112. }
  113.  
  114. return numSelect;
  115. }
  116.  
  117. //----------------------------------------------------------------------------------------------------------------------
  118. // addEntry
  119. //----------------------------------------------------------------------------------------------------------------------
  120.  
  121. void addEntry() {
  122.  
  123. string s1;
  124. string s2;
  125. string sign = "xxxxxx";
  126. double d1;
  127.  
  128. //New Entry
  129.  
  130. int size = nameList.size();
  131. nameList.push_back(list());
  132.  
  133. cout << "Name: ";
  134. cin >> s1;
  135. nameList[size].first = s1;
  136.  
  137. cout << "Last: ";
  138. cin >> s2;
  139. nameList[size].last = s2;
  140.  
  141. cout << "Height: ";
  142. cin >> d1;
  143. nameList[size].height = d1;
  144.  
  145. //Signature
  146.  
  147. for(int i = 0; i < 3; i++) {
  148. if((i + 1) > s1.length() || s1[i] == ' ') {
  149. sign[i] = 'x';
  150. }
  151. else {
  152. sign[i] = s1[i];
  153. }
  154. }
  155.  
  156. for(int i = 0; i < 3; i++) {
  157. if((i + 1) > s2.length() || s1[i] == ' ') {
  158. sign[i + 3] = 'x';
  159. }
  160. else {
  161. sign[i + 3] = s2[i];
  162. }
  163. }
  164.  
  165. sign = toLower(sign);
  166. nameList[size].sign = sign;
  167.  
  168. cout << endl;
  169. mainMenu();
  170.  
  171. return;
  172. }
  173.  
  174. //----------------------------------------------------------------------------------------------------------------------
  175. // printList
  176. //----------------------------------------------------------------------------------------------------------------------
  177.  
  178. void printList() {
  179.  
  180. cout << setw(4) << left << "Nr";
  181. cout << setw(8) << left << "Sign";
  182. cout << setw(32) << left << "Name";
  183. cout << setw(16) << left << "Height [m]";
  184. cout << endl;
  185.  
  186. for(int i = 0; i < nameList.size(); i++){
  187. cout << setw(4) << left << (i + 1);
  188. cout << setw(8) << left << nameList[i].sign;
  189. cout << setw(32) << left << nameList[i].first + " " + nameList[i].last;
  190. cout << setw(16) << left << nameList[i].height << endl;
  191. }
  192.  
  193. cout << endl;
  194. mainMenu();
  195.  
  196. return;
  197. }
  198.  
  199. //----------------------------------------------------------------------------------------------------------------------
  200. // searchList
  201. //----------------------------------------------------------------------------------------------------------------------
  202.  
  203. void searchList() {
  204.  
  205. string target;
  206. bool found = false;
  207. int pos;
  208. char choice;
  209.  
  210. cout << "Enter a signature: " << endl;
  211. cin >> target;
  212.  
  213. for(int i = 0; i < nameList.size(); i++) {
  214. if(target == nameList[i].sign) {
  215. found = true;
  216. pos = i;
  217. cout << "Entry found!" << endl;
  218. cout << " " << endl;
  219. cout << "Nr: " << (i + 1) << " ";
  220. cout << "Sign: " << nameList[i].sign << " ";
  221. cout << "Name: " << nameList[i].first << " ";
  222. cout << nameList[i].last << " ";
  223. cout << "Height: " << nameList[i].height << " m" << endl;
  224. }
  225. }
  226.  
  227. if(found == true) {
  228. cout << "Remove entry [y/n]: ";
  229. cin >> choice;
  230. if(choice == 'y') {
  231. nameList.erase(nameList.begin() + pos);
  232. cout << "Entry removed!" << endl;
  233. }
  234. }
  235.  
  236. if(found == false) {
  237. cout << "Nope!" << endl;
  238. }
  239.  
  240. cout << endl;
  241. mainMenu();
  242.  
  243. return;
  244. }
  245.  
  246. //----------------------------------------------------------------------------------------------------------------------
  247. // sortList
  248. //----------------------------------------------------------------------------------------------------------------------
  249.  
  250. void sortList() {
  251.  
  252. cout << "fml" << endl;
  253. mainMenu();
  254.  
  255. return;
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------------------------------------
  259. // randomOrder
  260. //----------------------------------------------------------------------------------------------------------------------
  261.  
  262. void randomOrder() {
  263.  
  264. random_shuffle(nameList.begin(), nameList.end());
  265. cout << "List has been shuffled!" << endl;
  266. cout << endl;
  267. mainMenu();
  268.  
  269. return;
  270. }
  271.  
  272. //----------------------------------------------------------------------------------------------------------------------
  273. // saveFile
  274. //----------------------------------------------------------------------------------------------------------------------
  275.  
  276. void saveFile() {
  277.  
  278. string fileName;
  279.  
  280. cout << "Enter filename: ";
  281. cin >> fileName;
  282.  
  283. string outputFile = fileName + ".txt";
  284. ofstream outFile(outputFile);
  285.  
  286. for(int i = 0; i < nameList.size(); i++) {
  287. outFile << nameList[i].first << delim;
  288. outFile << nameList[i].last << delim;
  289. outFile << nameList[i].sign << delim;
  290. outFile << nameList[i].height << endl;
  291. }
  292.  
  293. cout << endl;
  294. mainMenu();
  295.  
  296. return;
  297. }
  298.  
  299. //----------------------------------------------------------------------------------------------------------------------
  300. // toLower
  301. //----------------------------------------------------------------------------------------------------------------------
  302.  
  303. string toLower(string s) {
  304.  
  305. char letter;
  306. string result;
  307.  
  308. for(int i = 0; i < s.size(); i++) {
  309. letter = s[i];
  310. tolower(letter);
  311. result[i] = letter;
  312. cout << letter << endl;
  313. }
  314.  
  315. return result;
  316. }
  317.  
  318. //----------------------------------------------------------------------------------------------------------------------
  319. // readFile
  320. //----------------------------------------------------------------------------------------------------------------------
  321.  
  322. void readFile() {
  323.  
  324. int i = 0;
  325. int pos = 0;
  326. int pos1 = 0, pos2 = 0;
  327. int length;
  328. int nextPos;
  329. double height;
  330. string fileName;
  331. string line;
  332.  
  333. cout << "Enter filename: ";
  334. cin >> fileName;
  335.  
  336. string inputFile = fileName + ".txt";
  337. ifstream inFile(inputFile);
  338.  
  339. while(getline(inFile, line)) {
  340. nameList.push_back(list());
  341. pos2 = line.find(delim, pos1);
  342. length = (pos2 - pos1);
  343. nameList[i].first = line.substr(pos1, length);
  344. pos1 = (pos2 + 1);
  345. pos2 = line.find(delim, pos1);
  346. length = (pos2 - pos1);
  347. nameList[i].last = line.substr(pos1, length);
  348. pos1 = (pos2 + 1);
  349. pos2 = line.find(delim, pos1 + 1);
  350. length = (pos2 - pos1);
  351. nameList[i].sign = line.substr(pos1, length);
  352. pos1 = (pos2 + 1);
  353. pos2 = line.find(delim, pos1 +1);
  354. length = (pos2 - pos1);
  355. nameList[i].height = stod(line.substr(pos1, length));
  356. pos1 = 0;
  357. pos2 = 0;
  358. i++;
  359. }
  360.  
  361. inFile.close();
  362.  
  363. cout << endl;
  364. mainMenu();
  365.  
  366. return;
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement