Advertisement
Guest User

Untitled

a guest
May 10th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.92 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <limits>
  4. #include <string>
  5. //#include <windows.h>
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9. template <typename T>
  10.  
  11. T get_input(const string &strQuery);
  12. string get_username();
  13. string get_password();
  14. void save_user(const string &username, const string &password);
  15. void login();
  16. void register_user();
  17. void main_menu();
  18.  
  19. template <typename T>
  20. T get_input(const string &strQuery)
  21. {
  22. cout << strQuery << "\n> ";
  23. T out = T();
  24.  
  25. while (!(cin >> out)) {
  26. cin.clear();
  27. cin.ignore(std::numeric_limits <std::streamsize>::max(), '\n');
  28. cout << "\t\t\t\t\tError!" "\n";
  29.  
  30. }
  31.  
  32. return out;
  33. }
  34.  
  35. string get_password()
  36. {
  37. string password1 = get_input <string> ("\t\t\tPlease enter your password.");
  38. string password2 = get_input <string> ("\t\t\tPlease re-enter your password.");
  39.  
  40. while (password1 != password2) {
  41. cout << "\t\t\t\t\tError! Passwords do not match." "\n";
  42. password1 = get_input <string>("\t\t\tPlease enter your password.");
  43. password2 = get_input <string>("\t\t\tPlease re-enter your password.");
  44. }
  45.  
  46. return password1;
  47. }
  48.  
  49. string get_username()
  50. {
  51. string username = get_input <string>("\t\t\tPlease enter a Username.");
  52. cout << "\t\t\tUsername: \"" << username << "\t\t";
  53.  
  54. while (get_input <int>("\t\t\tConfirm your Username? [0|1]") != 1) {
  55. username = get_input <string>("\t\t\tPlease enter a Username.");
  56. cout << "\t\t\tUsername: \"" << username << "\t\t";
  57. }
  58.  
  59. return username;
  60. }
  61.  
  62. void login()
  63. {
  64. string line = " ";
  65. ifstream readFile("userandpassword.txt");
  66. string UserName;
  67. string Password;
  68. string _UserName;
  69. string _Password;
  70.  
  71. cout << "\t\t\tEnter UserName: ";
  72. cin >> UserName;
  73.  
  74. cout << "\t\t\tEnter Password: ";
  75. cin >> Password;
  76. bool found = false;
  77. while (getline(readFile,line)) {
  78.  
  79. stringstream iss(line);
  80. iss >> _UserName >> _Password;
  81.  
  82. if (UserName == _UserName && Password == _Password) {
  83. cout<<"\t\t\t\t\t\t";
  84. for(int i=0;i<5;i++)
  85. {
  86. cout<<".";
  87. // Sleep(100);
  88. }
  89. cout<<endl;
  90. cout<<endl;
  91. cout<<endl;
  92. cout << "\t\t\t\t\tLogin Successful!"<< endl;
  93. found = true;
  94. break;
  95. }
  96. }
  97.  
  98. if (!found) {
  99. cout << "\t\t\t\t\tInvalid Username And Password"<< endl;
  100. cout<<"\t\t\t\tTo Register press 1 and for trying Logging again press 2"<<endl;
  101. int choice;
  102. cout<<"\t\t\t\t\t";
  103. cin>>choice;
  104. switch(choice)
  105. {
  106. case 1:
  107. register_user();
  108. break;
  109. case 2:
  110. login();
  111. break;
  112. }
  113. }
  114.  
  115. }
  116.  
  117. void main_menu()
  118. {
  119.  
  120. int choice = get_input <int>("\n\n\n\n\n\n\n"
  121. "\t\t\t\tHello, Would you like to Log in or Register?" "\n"
  122. "\t\t\t\t[1] Login" "\n"
  123. "\t\t\t\t[2] Register" "\n"
  124. "\t\t\t\t[3] Exit");
  125. switch (choice)
  126. {
  127. case 1:
  128. login();
  129. break;
  130. case 2:
  131. register_user();
  132. cout<<"\t\t\t\t\t\t";
  133. for(int i=0;i<5;i++)
  134. {
  135. cout<<".";
  136. //Sleep(100);
  137. }
  138. cout<<endl;
  139. cout<<endl;
  140. cout<<endl;
  141. cout << "\t\t\t\t\tRegistered Successfully!"<< endl;
  142.  
  143. break;
  144. case 3:
  145. exit(0);
  146. }
  147. }
  148.  
  149. void register_user()
  150. {
  151. string username = get_username();
  152. string password = get_password();
  153. save_user(username, password);
  154. }
  155.  
  156. void save_user(const string &username, const string &password)
  157. {
  158. ofstream myfile;
  159. myfile.open ("userandpassword.txt",ios::app);
  160. myfile << username<< " "<< password;
  161. myfile<<'\n';
  162. myfile.close();
  163. }
  164. void Welcome_window()
  165. {
  166. cout<<"\t\t\t\t\t\tWelcome to our Travel Agency\n\n\n\n\n\n\n\n";
  167. cout<<"\t\t\t\t\t\tThe Agency is managed by: \n\n\n\n\n";
  168. cout<<"\t\t\t\t\t\t\t\tHari Dutt Parashar\n\n";
  169. cout<<"\t\t\t\t\t\t\t\tDivya Singh\n\n";
  170. cout<<"\t\t\t\t\t\t\t\tLakshya Kumar\n\n";
  171. cout<<"\t\t\t\t\t\t\t\tSonam Dema\n\n";
  172. }
  173.  
  174. class Pair
  175. {
  176. public:
  177. string destination;
  178. float dist;
  179. int condition;
  180. Pair(string dest, float weight,int cond=1)
  181. {
  182. destination=dest;
  183. dist=weight;
  184. condition=cond;
  185. }
  186. };
  187. class myComparator
  188. {
  189. public:
  190. int operator() (Pair p1, Pair p2)
  191. {
  192. return p1.dist > p2.dist;
  193. }
  194. };
  195.  
  196. class Graph
  197. {
  198. int V;
  199. map<string, list< Pair > > adjList;
  200. public:
  201. Graph(int v)
  202. {
  203. V=v;
  204. }
  205. void addEdge(string src, string dest,float wt,int condition)
  206. {
  207. Pair p (dest,wt,condition);
  208. Pair k (src,wt,condition);
  209. adjList[src].push_back(p);
  210. adjList[dest].push_back(k);
  211. }
  212. void print()
  213.  
  214. {
  215. /*to print the source and destination the place is connected to*/
  216. for(auto it=adjList.begin();it!=adjList.end();it++)
  217. {
  218. cout<<"\t\t\t"<<(it)->first<<"-->\t\t";
  219. for(auto lit=((it)->second).begin();lit!=((it)->second).end();lit++)
  220. {
  221. cout<<(lit)->destination<<",";
  222. }
  223. cout<<endl;
  224. }
  225. cout<<endl;
  226.  
  227. }
  228. void printAllPathsUtil(string u, string d, map<string,bool> &visited, string path[], int &path_index,int cond,float wt,int rate)
  229. {
  230. visited[u] = true;
  231. path[path_index] = u;
  232. path_index++;
  233.  
  234. if (u.compare(d)==0)
  235. {
  236. cout<<"\t\t\t";
  237. for (int i = 0; i<path_index; i++)
  238. cout << path[i] << "-->";
  239. cout << endl;
  240. cout << endl;
  241. if(cond<=-1)
  242. cout<<"\t\t\tThe travel conditions are Bad "<<endl;
  243. else if(cond==0)
  244. cout<<"\t\t\tThe travel conditions are Normal "<<endl;
  245. else cout<<"\t\t\tThe travel conditions are Good "<<endl;
  246. cout << endl;
  247.  
  248. cout<< "\t\t\tThe distance for the route being "<< wt<<" KM"<<endl;
  249. cout << endl;
  250. cout<<"\t\t\tThe cost of Travel Inclusive of Taxes : Rs "<<wt*rate<<endl;
  251. cout << endl;cout << endl;cout << endl;
  252. }
  253. else
  254. {
  255. for (auto i = adjList[u].begin(); i != adjList[u].end(); ++i)
  256. if (!visited[i->destination])
  257. printAllPathsUtil(i->destination,d, visited, path, path_index,cond+i->condition,wt+i->dist,rate);
  258. }
  259.  
  260. path_index--;
  261. visited[u] = false;
  262. }
  263. void Validate_input(string &src,string &des)
  264. {
  265. if(( adjList.find(src) == adjList.end() )) {
  266. cout<<"\t\t\t\tSource Could Not be Found, Renter\n";
  267. string src1;
  268. cin>>src1;
  269. src=src1;
  270. }
  271. if(( adjList.find(des) == adjList.end() )) {
  272. cout<<"\t\t\t\tDestination Could Not be Found, Renter\n";
  273. string des1;
  274. cin>>des1;
  275. des=des1;
  276.  
  277. }
  278.  
  279.  
  280. }
  281.  
  282. void printAllPaths(string s, string d,int rate)
  283. {
  284. map<string,bool> visited;
  285. for(auto it=adjList.begin();it!=adjList.end();it++)
  286. {
  287. visited[it->first]=false;
  288. }
  289. string *path = new string[V];
  290. int path_index = 0;
  291. printAllPathsUtil(s, d, visited, path, path_index,0,0,rate);
  292. }
  293.  
  294. void Dijkstras(string src,string des,int rate)
  295. {
  296.  
  297. /*to print the path having the shortest distance*/
  298. map<string ,float > distance;
  299. map<string, int> conditions;
  300. map<string,string> parent;
  301. for(auto it=adjList.begin();it!=adjList.end();it++)
  302. {
  303. distance[it->first]=INT_MAX;
  304. }
  305. distance[src]=0;
  306. conditions[src]=0;
  307. priority_queue<Pair,vector<Pair>, myComparator> Q;
  308. Pair P(src,distance[src],conditions[src]);
  309. Q.push(P);
  310. string last;
  311.  
  312. while(!Q.empty())
  313. {
  314. Pair Temp=Q.top();
  315. Q.pop();
  316. string u=Temp.destination;last=Temp.destination;
  317. for(auto it=adjList[u].begin();it!=adjList[u].end();it++)
  318. {
  319. Pair f = *it;
  320. string v = f.destination;
  321. float w = f.dist;
  322. int cond=f.condition;
  323. if(distance[u]+w<distance[v])
  324. {
  325. parent[v]=u;
  326. distance[v]=distance[u]+w;
  327. conditions[v]=conditions[u]+cond;
  328. Pair L(v,distance[v],conditions[v]);
  329. Q.push(L);
  330. }
  331.  
  332. }
  333.  
  334. }
  335. if(distance[des]==INT_MAX){
  336. cout<<"\n\n\n\n\n\n\n";
  337. cout<<"\t\t\t\t\tNo Path between the source and the destination Exists, Sorry for the Inconvenience";
  338. exit(0);
  339.  
  340. }
  341. cout<<"-----------------------------------SHORTEST ROUTE BETWEEN THE SORCE AND DESTINATION-----------------------------------"<<endl;
  342. cout<<endl;
  343. cout<<endl;
  344. cout<< "\t\t\tThe shortest distance between "<<src<<" and "<<des<< " is : "<<distance[des]<<" KM"<<endl;
  345. cout<<endl;
  346. cout<<endl;
  347. cout<<"\t\t\tThe cost of Travel Inclusive of Taxes : Rs "<<distance[des]*rate<<endl;
  348. cout<<endl;
  349. cout<<endl;
  350. cout<<"\t\t\tThe path Of the Shortest Route is: "<<endl;
  351. cout<<endl;
  352. cout<<endl;
  353. cout<<"\t\t\t";
  354. string k=des;
  355. cout<<k<<"<--";
  356. while(parent[k].compare(src)!=0)
  357. {
  358. cout<<parent[k]<<" <--";
  359. k=parent[k];
  360. }
  361. cout<<src;
  362. cout<<endl;
  363. cout<<endl;
  364. if(conditions[des]<=-1)
  365. cout<<"\t\t\tThe travel conditions are Bad"<<endl;
  366. else if(conditions[des]==0)
  367. cout<<"\t\t\tThe travel conditions are Normal"<<endl;
  368. else cout<<"\t\t\tThe travel conditions are Good"<<endl;
  369. }
  370.  
  371.  
  372. };
  373. int main()
  374. {
  375. Welcome_window();
  376. //Sleep(5000);
  377. system("cls");
  378. Graph g(60);
  379. main_menu();
  380. // Sleep(2000);
  381.  
  382. system("cls");
  383. string line = " ";
  384. ifstream readFile("Distance-From-City.txt");
  385. string source;
  386. string destination;
  387. string distance;
  388. string condition;
  389. while (getline(readFile,line)) {
  390.  
  391. stringstream iss(line);
  392. iss >> source >> destination >> distance >> condition;
  393. float D= strtof((distance).c_str(),0);
  394. int C= atoi((condition).c_str());
  395. g.addEdge(source,destination,D,C);
  396. }
  397. cout<<"\n\n\n\n\n\n\n\n\n\n\n\n";
  398. for(int i=0;i<10;i++)
  399. {
  400. cout<<"\t\t\t\t\t\t\t\t";
  401. for(int k=9-i;k>=0;k--)
  402. cout<<" ";
  403. for(int j=1;j<=i;j++)
  404. {
  405. cout<<"*"<<" ";
  406. // Sleep(50);
  407. }
  408. cout<<endl;
  409. }
  410. system("cls");
  411. cout<<endl;
  412. cout<<endl;
  413. cout<<endl;
  414. cout<<endl;
  415.  
  416. cout<<"-----------------------THE LIST OF CITIES THAT THE AGENCY MANAGES ARE---------------------------";
  417. // Sleep(1000);
  418. cout<<endl;
  419. cout<<endl;
  420. cout<<endl;
  421. g.print();
  422. cout<<"\t\t\tEnter the City from the list above:\t";
  423. string src;
  424. cin>>src;
  425.  
  426. cin.ignore();
  427. cout<<"\t\t\tEnter Your Destination:\t";
  428. string dest;
  429. cin>>dest;
  430. g.Validate_input(src,dest);
  431. system("cls");
  432. cout<<"\n\n\n\n\n\n\n\n\n\n\n\n";
  433. for(int i=0;i<10;i++)
  434. {
  435. cout<<"\t\t\t\t\t\t\t\t";
  436. for(int k=9-i;k>=0;k--)
  437. cout<<" ";
  438. for(int j=1;j<=i;j++)
  439. {
  440. cout<<"*"<<" ";
  441. // Sleep(50);
  442. }
  443. cout<<endl;
  444.  
  445. }
  446. system("cls");
  447. cout<<endl;
  448. cout<<endl;
  449. cout<<endl;
  450. cout<<endl;
  451.  
  452. cout<<"\t\t\t\t\tSelect The Mode of Transport You Want to Travel By: "<<endl;
  453. cout<<"---------------------------------------------------------------------------------------------------------"<<endl;
  454. cout<<"\t\t\t\t\t[1] Aeroplane \n \t\t\t\t\t[2] Train \n \t\t\t\t\t[3] Bus \n\t\t\t\t\t[4] Taxi"<<endl;
  455. int choice;
  456. cout<<"\t\t\t\t\t";
  457. cin>>choice;
  458. cout<<"\n\n\n\n\n\n\n\n\n\n\n\n";
  459. for(int i=0;i<10;i++)
  460. {
  461. cout<<"\t\t\t\t\t\t\t\t";
  462. for(int k=9-i;k>=0;k--)
  463. cout<<" ";
  464. for(int j=1;j<=i;j++)
  465. {
  466. cout<<"*"<<" ";
  467. // Sleep(50);
  468. }
  469. cout<<endl;
  470. }
  471. system("cls");
  472.  
  473. switch(choice)
  474. {
  475. case 1:
  476.  
  477. {
  478. g.Dijkstras(src, dest, 10);
  479. cout<<"-----------------------------------ALL ROUTES FROM SOURCE TO DESTINATION--------------------------------------"<<endl;
  480. cout<<endl;
  481. cout<<endl;
  482. cout<<endl;
  483. g.printAllPaths(src,dest,10);
  484. break;
  485. }
  486. case 2:
  487. {
  488. g.Dijkstras(src, dest, 5);
  489. cout<<"-----------------------------------ALL ROUTES FROM SOURCE TO DESTINATION--------------------------------------"<<endl;
  490. cout<<endl;
  491. cout<<endl;
  492. cout<<endl;
  493. g.printAllPaths(src,dest,5);
  494. break;
  495. }
  496. case 3:
  497. {
  498. g.Dijkstras(src, dest, 7);
  499. cout<<"-----------------------------------ALL ROUTES FROM SOURCE TO DESTINATION--------------------------------------"<<endl;
  500. cout<<endl;
  501. cout<<endl;
  502. cout<<endl;
  503. g.printAllPaths(src,dest,7);
  504. break;
  505. }
  506.  
  507. case 4:
  508. {
  509. g.Dijkstras(src, dest, 8);
  510. cout<<"-----------------------------------ALL ROUTES FROM SOURCE TO DESTINATION--------------------------------------"<<endl;
  511. cout<<endl;
  512. cout<<endl;
  513. cout<<endl;
  514. g.printAllPaths(src,dest,8);
  515. break;
  516. }
  517. }
  518.  
  519. return 0;
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement