Advertisement
Guest User

Untitled

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