Advertisement
Guest User

mehedi

a guest
Aug 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<stdio.h>
  4. #include<utility>
  5. #include<string>
  6. #include<vector>
  7.  
  8. using namespace std;
  9. void insert_contact();
  10. void search_contact();
  11. void upORdel(string x);
  12. void update_contact(string outputName, string outputLine, vector<string>output);
  13. void delete_contact(string outputName, string outputLine);
  14. void view_all_contact();
  15. char f1[]="file1.txt", f2[]="file2.txt";
  16.  
  17.  
  18. int main()
  19. {
  20. //while(1)
  21. //{
  22. // system("cls");
  23. int c;
  24. cout<<":::Choice your option:::"<<endl<<endl<<endl;
  25. cout<<"Type 1 for insert contact."<<endl;
  26. cout<<"Type 2 for search contact."<<endl;
  27. cout<<"Type 3 for view all contact."<<endl;
  28. cin>>c;
  29. switch(c)
  30. {
  31. case 1:
  32. insert_contact();
  33. break;
  34. case 2:
  35. search_contact();
  36. break;
  37. case 3:
  38. view_all_contact();
  39. break;
  40. default:
  41. cout<<"Entered wrong key"<<endl;
  42. break;
  43. }
  44. //}
  45. return 0;
  46. }
  47. void insert_contact()
  48. {
  49.  
  50. ofstream FILE;
  51. FILE.open(f1, ios::app);
  52. cout<<"Enter User name: ";
  53. string x;
  54. getchar();
  55. getline(cin, x);
  56. FILE<<endl<<"#"<<x;
  57.  
  58. cout<<"Phone number: ";
  59. cin>>x;
  60. FILE<<endl<<x<<"^";
  61. cout<<endl;
  62.  
  63. cout<<"Email address: ";
  64. cin>>x;
  65. FILE<<x<<"^";
  66. cout<<endl;
  67.  
  68. cout<<"Address: ";
  69. cin>>x;
  70. FILE<<x<<"^"<<endl;
  71.  
  72. x.clear();
  73. FILE.close();
  74. }
  75. void search_contact()
  76. {
  77. ifstream FILE;
  78. FILE.open(f1);
  79. cout<<"User Name: ";
  80. string x="#", data, extra;
  81. getchar();
  82. getline(cin, extra);
  83. x.append(extra);
  84. cout<<x<<endl;
  85. while(!FILE.eof())
  86. {
  87. getline(FILE, data);
  88. if(x==data)
  89. {
  90. cout<<"found "<<x<<endl;
  91. upORdel(x);
  92. break;
  93. }
  94. }
  95. cout<<"Contact not found"<<endl;
  96. FILE.close();
  97. FILE.close();
  98. }
  99. void upORdel(string x)
  100. {
  101. cout<<"UPorDEl function"<<x<<endl;
  102.  
  103. ifstream FILE;
  104. FILE.open(f1);
  105. string data, extra, ex, exx;
  106. vector<string>output;
  107. while(!FILE.eof())
  108. {
  109. getline(FILE, data);
  110. if(x==data)
  111. {
  112. extra.assign(x, 1, x.size());
  113. output.push_back(extra);
  114. cout<<"User Name: "<<extra<<endl;
  115. getline(FILE, extra);
  116.  
  117. int s=0, k=0;
  118. for(int i=0;i<extra.size();i++)
  119. {
  120. if(k==0) k++;
  121. else if(k==2) k++;
  122. else if(k==4) k++;
  123.  
  124. if(extra[i]=='^')
  125. {
  126. k++;
  127. output.push_back(ex);
  128. ex.clear();
  129. }
  130. else
  131. {
  132. cout<<extra[i];
  133. exx=extra[i];
  134. ex.append(exx);
  135.  
  136. }
  137. }
  138. break;
  139. }
  140. }
  141. FILE.close();
  142. int j=0;
  143. for(auto&i:output)
  144. {
  145. if(j==0) cout<<"User Name: ", j++;
  146. else if(j==1) cout<<"Contact Number: ", j++;
  147. else if(j==2) cout<<"Email Number: ", j++;
  148. else if(j==3)cout<<"Address: ", j++;
  149. cout<<i<<endl;
  150. }
  151. string outputName='#'+output[0];
  152. output.erase(output.begin());
  153. string outputLine;
  154. for(auto&i:output)
  155. outputLine.append(i+'^');
  156. cout<<endl<<endl<<":::Choice your option:::"<<endl;
  157. cout<<"Press 1 for update"<<endl;
  158. cout<<"Press 2 for delete the contact"<<endl;
  159. cout<<"Choose: ";
  160. int c;
  161. cin>>c;
  162. switch(c)
  163. {
  164. case 1:
  165. update_contact(outputName, outputLine, output);
  166. break;
  167. case 2:
  168. delete_contact(outputName, outputLine);
  169. }
  170.  
  171.  
  172. }
  173. void update_contact(string outputName, string outputLine, vector<string>output)
  174. {
  175. vector<string>updatevec(3);
  176. string x, updateName;
  177.  
  178. cout<<":::Update:::"<<endl;
  179. cout<<"To not change a specific filed type null"<<endl;
  180. for(int i=0;i<4;i++)
  181. {
  182. if(i==0)
  183. {
  184. cout<<"New name: ";
  185. getchar();
  186. getline(cin, x);
  187. if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
  188. updateName='#'+x;
  189. else updateName=outputName;
  190. }
  191. else if(i==1)
  192. {
  193. cout<<"New Contact Number: ";
  194. getline(cin, x);
  195. if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
  196. updatevec[0]=x+'^';
  197. else updatevec[0]=output[0]+'^';
  198. }
  199. else if(i==2)
  200. {
  201. cout<<"New Email Address: ";
  202. getline(cin, x);
  203. if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
  204. updatevec[1]=x+'^';
  205. else updatevec[1]=output[1]+'^';
  206. }
  207. else if(i==3)
  208. {
  209. cout<<"New Address: ";
  210. getline(cin, x);
  211. if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
  212. updatevec[2]=x+'^';
  213. else updatevec[2]=output[2]+'^';
  214. }
  215. }
  216. string updateInfo;
  217. for(auto&i:updatevec)
  218. updateInfo.append(i);
  219.  
  220.  
  221. cout<<"Make change?"<<endl;
  222. cout<<"Type yes/no"<<endl;
  223. cout<<"Choose: ";
  224. getline(cin, x);
  225. cout<<"Change able Name: "<<updateName<<endl;
  226. cout<<"Change able info: "<<updateInfo<<endl;
  227. if(x.compare("YES")==0||x.compare("yes")==0||x.compare("Yes")==0)
  228. {
  229.  
  230. fstream FILE(f1);
  231. ofstream FILEx(f2);
  232. //temp.open(f2, ios::out);
  233. string line;
  234. while(!FILE.eof())
  235. {
  236.  
  237. if(line==outputName)
  238. {
  239. FILEx<<updateName<<endl;
  240. getline(FILE, line);
  241. FILEx<<updateInfo<<endl;
  242. }
  243. else FILEx<<line<<endl;
  244. getline(FILE, line);
  245. }
  246.  
  247. FILE.close();
  248.  
  249.  
  250.  
  251. ofstream file(f1, ios::trunc);
  252. ifstream reRight(f2);
  253. string extra;
  254. while(!reRight.eof())
  255. {
  256. file<<extra<<endl;
  257. getline(reRight, extra);
  258. }
  259. reRight.close();
  260. FILEx.close();
  261. ofstream file2(f2, ios::trunc);
  262. file2.close();
  263.  
  264. }
  265.  
  266. }
  267. void delete_contact(string outputName, string outputLine)
  268. {
  269. cout<<"Are you sure to delete this contact?"<<endl;
  270. cout<<"Type yes/no."<<endl;
  271. cout<<"Choose: ";
  272. string x;
  273. cin>>x;
  274. if(x=="yes")
  275. {
  276. fstream FILE(f1);
  277. ofstream FILEx(f2);
  278.  
  279. string line;
  280. while(!FILE.eof())
  281. {
  282.  
  283. if(line==outputName)
  284. {
  285. FILEx<<""<<endl;
  286. getline(FILE, line);
  287. FILEx<<""<<endl;
  288. }
  289. else FILEx<<line<<endl;
  290. getline(FILE, line);
  291. }
  292.  
  293. FILE.close();
  294.  
  295.  
  296.  
  297. ofstream file(f1, ios::trunc);
  298. ifstream reRight(f2);
  299. string extra;
  300. while(!reRight.eof())
  301. {
  302. file<<extra<<endl;
  303. getline(reRight, extra);
  304. }
  305. reRight.close();
  306. FILEx.close();
  307. ofstream file2(f2, ios::trunc);
  308. file2.close();
  309. }
  310. }
  311. void view_all_contact()
  312. {
  313. vector<pair<string, vector<string> > >OutputData;
  314. ifstream FILE(f1);
  315. string str, x;
  316. vector<string>vec;
  317. string ex1, ex2;
  318. while(!FILE.eof())
  319. {
  320. if(str[0]=='#')
  321. {
  322. //str.erase(str.begin(), str.begin()+0);
  323. ex1.assign(str, 1, str.size());
  324. cout<<"User Name: "<<ex1<<endl;
  325. getline(FILE, str);
  326. for(int i=0;i<str.size();i++)
  327. {
  328. if(str[i]=='^') vec.push_back(x), x.clear();
  329. else x+=str[i];
  330. }
  331.  
  332. OutputData.push_back(make_pair(ex1, vec));
  333. ex1.clear();
  334.  
  335. vec.clear();
  336. }
  337. getline(FILE, str);
  338. }
  339. FILE.close();
  340. //sorting
  341. string extra;
  342. vector<string>xvec;
  343. cout<<":::Unsorted:::"<<endl;
  344. for(auto&i:OutputData)
  345. {
  346. cout<<endl<<"User Name: "<<i.first<<endl;
  347. vec=i.second;
  348. for(auto&j:vec)
  349. cout<<"Info: "<<j<<" ";
  350. cout<<endl;
  351. }
  352. for(int i=1;i<OutputData.size();i++)
  353. {
  354. for(int j=1;j<OutputData.size();j++)
  355. {
  356. if((OutputData[j-1].first).compare(OutputData[j].first)>0)
  357. {
  358. extra=OutputData[j-1].first;
  359. xvec=OutputData[j-1].second;
  360.  
  361. OutputData[j-1]=OutputData[j];
  362.  
  363. OutputData[j].first=extra;
  364. OutputData[j].second=xvec;
  365.  
  366. }
  367.  
  368. }
  369. }
  370. cout<<":::Sorted:::"<<endl;
  371. for(auto&i:OutputData)
  372. {
  373. cout<<endl<<"User Name: "<<i.first<<endl;
  374. vec=i.second;
  375. for(auto&j:vec)
  376. cout<<"Info: "<<j<<" ";
  377. cout<<endl;
  378. }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement