Advertisement
Guest User

Untitled

a guest
May 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.90 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 = " ", UserName, Password, _UserName, _Password;
  65. ifstream readFile("userandpassword.txt");
  66.  
  67. cout << "\t\t\tEnter UserName: ";
  68. cin >> UserName;
  69.  
  70. cout << "\t\t\tEnter Password: ";
  71. cin >> Password;
  72. bool found = false;
  73. while (getline(readFile,line))
  74. {
  75. stringstream iss(line);
  76. iss >> _UserName >> _Password;
  77.  
  78. if (UserName == _UserName && Password == _Password)
  79. {
  80. cout<<"\t\t\t\t\t\t";
  81. for(int i=0;i<5;i++)
  82. {
  83. cout<<".";
  84. // Sleep(100);
  85. }
  86.  
  87. cout<<endl<<endl<<endl<<"\t\t\t\t\tLogin Successful!"<<endl;
  88. found = true;
  89. break;
  90. }
  91. }
  92.  
  93. if (!found)
  94. {
  95. cout<<"\t\t\t\t\tInvalid Username And Password"<< endl<<"\t\t\t\tTo Register press 1 and for trying Logging again press 2"<<endl;
  96. int choice;
  97. cout<<"\t\t\t\t\t";
  98. cin>>choice;
  99. switch(choice)
  100. {
  101. case 1:
  102. register_user();
  103. break;
  104. case 2:
  105. login();
  106. break;
  107. }
  108. }
  109.  
  110. }
  111.  
  112. void main_menu()
  113. {
  114.  
  115. int choice = get_input <int>("\n\n\n\n\n\n\n"
  116. "\t\t\t\tHello, Would you like to Log in or Register?" "\n"
  117. "\t\t\t\t[1] Login" "\n"
  118. "\t\t\t\t[2] Register" "\n"
  119. "\t\t\t\t[3] Exit");
  120. switch (choice)
  121. {
  122. case 1:
  123. login();
  124. break;
  125. case 2:
  126. register_user();
  127. cout<<"\t\t\t\t\t\t";
  128. for(int i=0;i<5;i++)
  129. {
  130. cout<<".";
  131. //Sleep(100);
  132. }
  133. cout<<endl<<endl<<endl<<"\t\t\t\t\tRegistered Successfully!"<<endl;
  134. break;
  135. default:
  136. exit(0);
  137. }
  138. }
  139.  
  140. void register_user()
  141. {
  142. string username = get_username();
  143. string password = get_password();
  144. save_user(username, password);
  145. }
  146.  
  147. void save_user(const string &username, const string &password)
  148. {
  149. ofstream myfile;
  150. myfile.open ("user_and_password.txt",ios::app);
  151. myfile<<username<<" "<<password<<'\n';
  152. myfile.close();
  153. }
  154.  
  155. void Welcome_window()
  156. {
  157. cout<<"\t\t\t\t\t\tWelcome to our Travel Agency\n\n\n\n\n\n\n\n";
  158. cout<<"\t\t\t\t\t\tThe Agency is managed by: \n\n\n\n\n";
  159. cout<<"\t\t\t\t\t\t\t\tHari Dutt Parashar\n\n";
  160. cout<<"\t\t\t\t\t\t\t\tDivya Singh\n\n";
  161. cout<<"\t\t\t\t\t\t\t\tLakshya Kumar\n\n";
  162. cout<<"\t\t\t\t\t\t\t\tSonam Dema\n\n";
  163. }
  164.  
  165. class Pair
  166. {
  167. public:
  168. string destination;
  169. float dist;
  170. int condition;
  171. Pair(string dest, float weight,int cond=1)
  172. {
  173. destination=dest;
  174. dist=weight;
  175. condition=cond;
  176. }
  177. };
  178. class myComparator
  179. {
  180. public:
  181. int operator() (Pair p1, Pair p2)
  182. {
  183. return p1.dist > p2.dist;
  184. }
  185. };
  186.  
  187. class Graph
  188. {
  189. int V;
  190. map<string, list< Pair > > adjList;
  191. public:
  192. Graph(int v)
  193. {
  194. V=v;
  195. }
  196.  
  197. void addEdge(string src, string dest,float wt,int condition)
  198. {
  199. Pair p (dest,wt,condition);
  200. Pair k (src,wt,condition);
  201. adjList[src].push_back(p);
  202. adjList[dest].push_back(k);
  203. }
  204.  
  205. void print()
  206. {
  207. /*to print the source and destination the place is connected to*/
  208. for(auto it=adjList.begin();it!=adjList.end();it++)
  209. {
  210. cout<<"\t\t\t"<<(it)->first<<"-->\t\t";
  211. for(auto lit=((it)->second).begin();lit!=((it)->second).end();lit++)
  212. {
  213. cout<<(lit)->destination<<",";
  214. }
  215. cout<<endl;
  216. }
  217. cout<<endl;
  218.  
  219. }
  220. void printAllPathsUtil(string u, string d, map<string,bool> &visited, string path[], int &path_index,int cond,float wt,int rate)
  221. {
  222. visited[u] = true;
  223. path[path_index] = u;
  224. path_index++;
  225.  
  226. if (u.compare(d)==0)
  227. {
  228. cout<<"\t\t\t";
  229. for (int i = 0; i<path_index; i++)
  230. cout << path[i] << "-->";
  231. cout << endl;
  232. cout << endl;
  233. if(cond<=-1)
  234. cout<<"\t\t\tThe travel conditions are Bad "<<endl;
  235. else if(cond==0)
  236. cout<<"\t\t\tThe travel conditions are Normal "<<endl;
  237. else cout<<"\t\t\tThe travel conditions are Good "<<endl;
  238. cout << endl;
  239.  
  240. cout<< "\t\t\tThe distance for the route being "<< wt<<" KM"<<endl<<endl;
  241. cout<<"\t\t\tThe cost of Travel Inclusive of Taxes : Rs "<<wt*rate<<endl<<endl<<endl<<endl;
  242. }
  243. else
  244. {
  245. for (auto i = adjList[u].begin(); i != adjList[u].end(); ++i)
  246. if (!visited[i->destination])
  247. printAllPathsUtil(i->destination,d, visited, path, path_index,cond+i->condition,wt+i->dist,rate);
  248. }
  249.  
  250. path_index--;
  251. visited[u] = false;
  252. }
  253. void Validate_input(string &src, string &des)
  254. {
  255. if(adjList.find(src) == adjList.end() && adjList.find(des) == adjList.end())
  256. {
  257. cout<<"\t\t\t\tSource and destination Could Not be Found, Renter\n";
  258. string src1;
  259. string des1;
  260. cin>>src1;
  261. cin>>des1;
  262. src=src1;
  263. des=des1;
  264. }
  265. }
  266.  
  267. void printAllPaths(string s, string d,int rate)
  268. {
  269. map<string,bool> visited;
  270. for(auto it=adjList.begin();it!=adjList.end();it++)
  271. {
  272. visited[it->first]=false;
  273. }
  274. string *path = new string[V];
  275. int path_index = 0;
  276. printAllPathsUtil(s, d, visited, path, path_index,0,0,rate);
  277. }
  278.  
  279. void Dijkstras(string src, string des, int rate)
  280. {
  281. /*to print the path having the shortest distance*/
  282. map<string ,float > distance;
  283. map<string, int> conditions;
  284. map<string,string> parent;
  285. for(auto it=adjList.begin();it!=adjList.end();it++)
  286. {
  287. distance[it->first]=INT_MAX;
  288. }
  289. distance[src]=0;
  290. conditions[src]=0;
  291. priority_queue<Pair,vector<Pair>, myComparator> Q;
  292. Pair P(src,distance[src],conditions[src]);
  293. Q.push(P);
  294. string last;
  295.  
  296. while(!Q.empty())
  297. {
  298. Pair Temp=Q.top();
  299. Q.pop();
  300. string u=Temp.destination;last=Temp.destination;
  301. for(auto it=adjList[u].begin();it!=adjList[u].end();it++)
  302. {
  303. Pair f = *it;
  304. string v = f.destination;
  305. float w = f.dist;
  306. int cond=f.condition;
  307. if(distance[u]+w<distance[v])
  308. {
  309. parent[v]=u;
  310. distance[v]=distance[u]+w;
  311. conditions[v]=conditions[u]+cond;
  312. Pair L(v,distance[v],conditions[v]);
  313. Q.push(L);
  314. }
  315. }
  316. }
  317.  
  318. if(distance[des]==INT_MAX)
  319. {
  320. cout<<"\n\n\n\n\n\n\n";
  321. cout<<"\t\t\t\t\tNo Path between the source and the destination Exists, Sorry for the Inconvenience";
  322. exit(0);
  323. }
  324.  
  325. cout<<"-----------------------------------SHORTEST ROUTE BETWEEN THE SORCE AND DESTINATION-----------------------------------"<<endl<<endl<<endl;
  326. cout<< "\t\t\tThe shortest distance between "<<src<<" and "<<des<< " is : "<<distance[des]<<" KM"<<endl<<endl<<endl;
  327. cout<<"\t\t\tThe cost of Travel Inclusive of Taxes : Rs "<<distance[des]*rate<<endl<<endl<<endl;
  328. cout<<"\t\t\tThe path Of the Shortest Route is: "<<endl<<endl<<endl<<"\t\t\t";
  329. string k=des;
  330. cout<<k<<"<--";
  331.  
  332. while(parent[k].compare(src)!=0)
  333. {
  334. k=parent[k];
  335. }
  336. cout<<parent[k]<<" <--";
  337.  
  338. cout<<src<<endl<<endl;
  339. if(conditions[des]<=-1)
  340. cout<<"\t\t\tThe travel conditions are Bad"<<endl;
  341. else if(conditions[des]==0)
  342. cout<<"\t\t\tThe travel conditions are Normal"<<endl;
  343. else cout<<"\t\t\tThe travel conditions are Good"<<endl;
  344. }
  345.  
  346. void traverse(string src, string dest, int rate)
  347. {
  348. Dijkstras(src, dest, rate);
  349. cout<<"-----------------------------------ALL ROUTES FROM SOURCE TO DESTINATION--------------------------------------"<<endl<<endl<<endl<<endl;
  350. printAllPaths(src, dest, rate);
  351. }
  352. };
  353.  
  354. void print_pyramid()
  355. {
  356. cout<<"\n\n\n\n\n\n\n\n\n\n\n\n";
  357. for(int i=0;i<10;i++)
  358. {
  359. cout<<"\t\t\t\t\t\t\t\t";
  360. for(int k=9-i;k>=0;k--)
  361. cout<<" ";
  362. for(int j=1;j<=i;j++)
  363. {
  364. cout<<"*"<<" ";
  365. // Sleep(50);
  366. }
  367. cout<<endl;
  368. }
  369. system("clear");
  370. }
  371.  
  372. int main()
  373. {
  374. Welcome_window();
  375. //Sleep(5000);
  376. system("clear");
  377. Graph g(60);
  378. main_menu();
  379. // Sleep(2000);
  380.  
  381. system("clear");
  382. string line = " ";
  383. ifstream readFile("Distance-From-City.txt");
  384. string source, destination, distance, condition;
  385.  
  386. while (getline(readFile,line))
  387. {
  388. stringstream iss(line);
  389. iss >> source >> destination >> distance >> condition;
  390. float D= strtof((distance).c_str(),0);
  391. int C= atoi((condition).c_str());
  392. g.addEdge(source,destination,D,C);
  393. }
  394.  
  395. print_pyramid();
  396. cout<<endl<<endl<<endl<<endl<<"-----------------------THE LIST OF CITIES THAT THE AGENCY MANAGES ARE---------------------------";
  397. // Sleep(1000);
  398. cout<<endl<<endl<<endl;
  399. g.print();
  400. cout<<"\t\t\tEnter the City from the list above:\t";
  401. string src;
  402. cin>>src;
  403.  
  404. cin.ignore();
  405. cout<<"\t\t\tEnter Your Destination:\t";
  406. string dest;
  407. cin>>dest;
  408. g.Validate_input(src,dest);
  409. system("clear");
  410. print_pyramid();
  411. cout<<endl<<endl<<endl<<endl<<"\t\t\t\t\tSelect The Mode of Transport You Want to Travel By: "<<endl;
  412. cout<<"---------------------------------------------------------------------------------------------------------"<<endl;
  413. 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;
  414. int choice;
  415. cout<<"\t\t\t\t\t";
  416. cin>>choice;
  417. print_pyramid();
  418.  
  419. switch(choice)
  420. {
  421. case 1:
  422. {
  423. g.traverse(src, dest, 10);
  424. break;
  425. }
  426. case 2:
  427. {
  428. g.traverse(src, dest, 5);
  429. break;
  430. }
  431. case 3:
  432. {
  433. g.traverse(src, dest, 7);
  434. break;
  435. }
  436.  
  437. case 4:
  438. {
  439. g.traverse(src, dest, 8);
  440. break;
  441. }
  442. default:
  443. {
  444. cout<<"Invalid Input";
  445. break;
  446. }
  447. }
  448.  
  449. return 0;
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement