Advertisement
Guest User

Untitled

a guest
May 17th, 2017
67
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(src) == adjList.end()) //chhaya
  256. {
  257. cout<<"\t\t\t\tSource and Destination Could Not be Found, Renter \n";
  258. string src1,des1;
  259. cin>>src1>>des1;
  260. src=src1;
  261. des=des1; //chhaya
  262. }
  263. }
  264.  
  265. void printAllPaths(string s, string d,int rate)
  266. {
  267. map<string,bool> visited;
  268. for(auto it=adjList.begin();it!=adjList.end();it++)
  269. {
  270. visited[it->first]=false;
  271. }
  272. string *path = new string[V];
  273. int path_index = 0;
  274. printAllPathsUtil(s, d, visited, path, path_index,0,0,rate);
  275. }
  276.  
  277. void Dijkstras(string src, string des, int rate)
  278. {
  279. /*to print the path having the shortest distance*/
  280. map<string ,float > distance;
  281. map<string, int> conditions;
  282. map<string,string> parent;
  283. for(auto it=adjList.begin();it!=adjList.end();it++)
  284. {
  285. distance[it->first]=INT_MAX;
  286. }
  287. distance[src]=0;
  288. conditions[src]=0;
  289. priority_queue<Pair,vector<Pair>, myComparator> Q;
  290. Pair P(src,distance[src],conditions[src]);
  291. Q.push(P);
  292. string last;
  293.  
  294. while(!Q.empty())
  295. {
  296. Pair Temp=Q.top();
  297. Q.pop();
  298. string u=Temp.destination;last=Temp.destination;
  299. for(auto it=adjList[u].begin();it!=adjList[u].end();it++)
  300. {
  301. Pair f = *it;
  302. string v = f.destination;
  303. float w = f.dist;
  304. int cond=f.condition;
  305. if(distance[u]+w<distance[v])
  306. {
  307. parent[v]=u;
  308. distance[v]=distance[u]+w;
  309. conditions[v]=conditions[u]+cond;
  310. Pair L(v,distance[v],conditions[v]);
  311. Q.push(L);
  312. }
  313. }
  314. }
  315.  
  316. if(distance[des]==INT_MAX)
  317. {
  318. cout<<"\n\n\n\n\n\n\n";
  319. cout<<"\t\t\t\t\tNo Path between the source and the destination Exists, Sorry for the Inconvenience";
  320. exit(0);
  321. }
  322.  
  323. cout<<"-----------------------------------SHORTEST ROUTE BETWEEN THE SORCE AND DESTINATION-----------------------------------"<<endl<<endl<<endl;
  324. cout<< "\t\t\tThe shortest distance between "<<src<<" and "<<des<< " is : "<<distance[des]<<" KM"<<endl<<endl<<endl;
  325. cout<<"\t\t\tThe cost of Travel Inclusive of Taxes : Rs "<<distance[des]*rate<<endl<<endl<<endl;
  326. cout<<"\t\t\tThe path Of the Shortest Route is: "<<endl<<endl<<endl<<"\t\t\t";
  327. string k=des;
  328. cout<<k<<"<--";
  329.  
  330. while(parent[k].compare(src)!=0) //chhaya
  331. {
  332. k=parent[k];
  333. }
  334. cout<<parent[k]<<" <--"; //chhaya
  335.  
  336. cout<<src<<endl<<endl;
  337. if(conditions[des]<=-1)
  338. cout<<"\t\t\tThe travel conditions are Bad"<<endl;
  339. else if(conditions[des]==0)
  340. cout<<"\t\t\tThe travel conditions are Normal"<<endl;
  341. else cout<<"\t\t\tThe travel conditions are Good"<<endl;
  342. }
  343.  
  344. void traverse(string src, string dest, int rate)
  345. {
  346. Dijkstras(src, dest, rate);
  347. cout<<"-----------------------------------ALL ROUTES FROM SOURCE TO DESTINATION--------------------------------------"<<endl<<endl<<endl<<endl;
  348. printAllPaths(src, dest, rate);
  349. }
  350. };
  351.  
  352. void print_pyramid()
  353. {
  354. cout<<"\n\n\n\n\n\n\n\n\n\n\n\n";
  355. for(int i=0;i<10;i++)
  356. {
  357. cout<<"\t\t\t\t\t\t\t\t";
  358. for(int k=9-i;k>=0;k--)
  359. cout<<" ";
  360. for(int j=1;j<=i;j++)
  361. {
  362. cout<<"*"<<" ";
  363. // Sleep(50);
  364. }
  365. cout<<endl;
  366. }
  367. system("clear");
  368. }
  369.  
  370. int main()
  371. {
  372. Welcome_window();
  373. //Sleep(5000);
  374. system("clear");
  375. Graph g(60);
  376. main_menu();
  377. // Sleep(2000);
  378.  
  379. system("clear");
  380. string line = " ";
  381. ifstream readFile("Distance-From-City.txt");
  382. string source, destination, distance, condition;
  383.  
  384. while (getline(readFile,line))
  385. {
  386. stringstream iss(line);
  387. iss >> source >> destination >> distance >> condition;
  388. float D= strtof((distance).c_str(),0);
  389. int C= atoi((condition).c_str());
  390. g.addEdge(source,destination,D,C);
  391. }
  392.  
  393. print_pyramid();
  394. cout<<endl<<endl<<endl<<endl<<"-----------------------THE LIST OF CITIES THAT THE AGENCY MANAGES ARE---------------------------";
  395. // Sleep(1000);
  396. cout<<endl<<endl<<endl;
  397. g.print();
  398. cout<<"\t\t\tEnter the City from the list above:\t";
  399. string src;
  400. cin>>src;
  401.  
  402. cin.ignore();
  403. cout<<"\t\t\tEnter Your Destination:\t";
  404. string dest;
  405. cin>>dest;
  406. g.Validate_input(src,dest);
  407. system("clear");
  408. print_pyramid();
  409. cout<<endl<<endl<<endl<<endl<<"\t\t\t\t\tSelect The Mode of Transport You Want to Travel By: "<<endl;
  410. cout<<"---------------------------------------------------------------------------------------------------------"<<endl;
  411. 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;
  412. int choice;
  413. cout<<"\t\t\t\t\t";
  414. cin>>choice;
  415. print_pyramid();
  416.  
  417. switch(choice)
  418. {
  419. case 1:
  420. {
  421. g.traverse(src, dest, 10);
  422. break;
  423. }
  424. case 2:
  425. {
  426. g.traverse(src, dest, 5);
  427. break;
  428. }
  429. case 3:
  430. {
  431. g.traverse(src, dest, 7);
  432. break;
  433. }
  434.  
  435. case 4:
  436. {
  437. g.traverse(src, dest, 8);
  438. break;
  439. }
  440. default:
  441. {
  442. cout<<"Invalid Input";
  443. break;
  444. }
  445. }
  446.  
  447. return 0;
  448. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement