fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <string>
  6. #include <unordered_map>
  7.  
  8. using namespace std;
  9.  
  10. class Time
  11. {
  12. private:
  13. int hour;
  14. int minute;
  15. int second;
  16. string midday;
  17.  
  18. public:
  19.  
  20. Time()
  21. {
  22. }
  23.  
  24. Time(int h, int m, int s)
  25. {
  26. hour = h;
  27. minute = m;
  28. second = s;
  29.  
  30. }
  31.  
  32. void setHour(int h)
  33. {
  34. if (h >= 0 && h <= 24)
  35. {
  36. if (h == 24)
  37. {
  38. hour = 0;
  39. midday = "AM";
  40. }
  41. else if (h > 12)
  42. {
  43. hour = h - 12;
  44. midday = "PM";
  45.  
  46. }
  47. else if (h == 0)
  48. {
  49. hour = 12;
  50. midday = "AM";
  51. }
  52. else if (h == 12)
  53. {
  54. hour = h;
  55. midday = "PM";
  56. }
  57.  
  58. else
  59. {
  60. hour = h;
  61. midday = "AM";
  62. }
  63. }
  64. }
  65.  
  66. string getmidday()
  67. {
  68. return midday;
  69. }
  70.  
  71. int getHour()
  72. {
  73. return hour;
  74. }
  75.  
  76. void setMinute(int m)
  77. {
  78. if (m >= 0 && m <= 59)
  79. {
  80. minute = m;
  81. }
  82. else
  83. {
  84. minute = 0;
  85. }
  86. }
  87.  
  88. int getMinute()
  89. {
  90. return minute;
  91. }
  92.  
  93. void setSecond(int s)
  94. {
  95. if (s >= 0 && s <= 59)
  96. {
  97. second = s;
  98. }
  99. else
  100. {
  101. second = 0;
  102. }
  103. }
  104.  
  105. int getSecond()
  106. {
  107. return second;
  108. }
  109.  
  110. void printTime_info()
  111. {
  112. cout << " the time is " << setfill('0') << setw(2) << hour << ":";
  113. cout << setfill('0') << setw(2) << minute << ":";
  114. cout << setfill('0') << setw(2) << second << " " << midday << endl;
  115. }
  116.  
  117. };
  118.  
  119.  
  120. class Country
  121. {
  122. Time timezone;
  123.  
  124. bool found;
  125.  
  126. string stadt;
  127.  
  128. unordered_map<string, int> country = {
  129. {"afghanistan", 4}, {"alaska", -9}, {"argentina", -3}, {"armenia", 4}, {"azores", -1},
  130. {"bangladesh", 6}, {"bhutan", 6}, {"brasilia", -3}, {"brazil", -3}, {"cambodia", 7},
  131. {"canada", -5}, {"cape verde island", -1}, {"caracas", -4}, {"central america", -6}, {"chihuahua", -7},
  132. {"china", 8}, {"colombia", -5}, {"cuba", -5}, {"egypt", 2}, {"fiji", 12},
  133. {"france", 1}, {"georgia", 4}, {"germany", 1}, {"ghana", 0}, {"greece", 2},
  134. {"greenland", -3}, {"guam", 10}, {"hawaii", -10}, {"iceland", 0}, {"india", 5},
  135. {"indonesia", 7}, {"iran", 3}, {"iraq", 3}, {"italy", 1}, {"jakarta", 7},
  136. {"japan", 9}, {"kazakhstan", 5}, {"kenya", 3}, {"korea", 9}, {"kuwait", 3},
  137. {"la paz", -4}, {"mali", 0}, {"malaysia", 8}, {"marshall island", 12}, {"mazatlan", -7},
  138. {"melbourne", 10}, {"mexico", -6}, {"midway island", -11}, {"moscow", 3}, {"myanmar", 6},
  139. {"nepal", 5}, {"new zealand", 12}, {"newfoundland", -3}, {"nigeria", 1}, {"oman", 4},
  140. {"pakistan", 5}, {"peru", -5}, {"philippines", 8}, {"poland", 1}, {"rio", -3},
  141. {"russia", 3}, {"samoa", -11}, {"santiago", -4}, {"saudi arabia", 3}, {"senegal", 0},
  142. {"singapore", 8}, {"solomon island", 11}, {"south africa", 2}, {"south korea", 9}, {"spain", 1},
  143. {"sri lanka", 5}, {"sweden", 1}, {"sydney", 10}, {"thailand", 7}, {"tijuana", -8},
  144. {"turkey", 3}, {"ukraine", 2}, {"united arab emirates", 4}, {"united kingdom", 0}, {"united states", -5},
  145. {"uzbekistan", 5}, {"vanuatu", 11}, {"vietnam", 7}, {"vladivostok", 10}};
  146.  
  147. int Levenstein_Distance(const string &word1, const string &word2)
  148. {
  149. int m = word1.length();
  150. int n = word2.length();
  151.  
  152. vector< vector < int>> dif(m +1, vector<int> (n+1));
  153.  
  154. for( int i = 0; i <= m; i++)
  155. {
  156. dif[i][0] = i;
  157. }
  158.  
  159. for(int j = 0; j <= n; j++)
  160. {
  161. dif[0][j] = j;
  162. }
  163.  
  164. for( int i = 1; i <= m; i++)
  165. {
  166. for(int j = 1; j <= n; j++)
  167. {
  168. if(word1[i - 1] == word2[j-1])
  169. {
  170. dif[i][j] = dif[i-1][j-1];
  171. }
  172. else{
  173.  
  174. dif[i][j] = min({dif[i-1][j-1]+1, /// substitute
  175. dif[i-1][j]+1, /// deletion
  176. dif[i][j-1]+1}); /// insertation
  177. }
  178. }
  179. }
  180. return dif[m][n];
  181. }
  182.  
  183. public:
  184.  
  185. Country(): found(false){}
  186.  
  187. void setTimeLine(string paese, Time UTC)
  188. {
  189. for (int i = 0; i < paese.size(); i++)
  190. {
  191. if (paese[i] >= 'A' && paese[i] <= 'Z')
  192. {
  193. paese[i] = paese[i] + 32;
  194. }
  195. }
  196.  
  197. int min_distance = 100;
  198. string best_match;
  199.  
  200. for( auto it = country.begin(); it != country.end(); it++)
  201. {
  202. int distance = Levenstein_Distance(paese,it->first);
  203.  
  204. if (distance == 0)
  205. {
  206. found = true;
  207. best_match = it->first;
  208. break;
  209. }
  210.  
  211. else if (distance < min_distance)
  212. {
  213. min_distance = distance;
  214. best_match = it->first;
  215. }
  216. }
  217.  
  218. if (min_distance <= 2)
  219. {
  220. auto it = country.find(best_match);
  221. found = true;
  222. stadt = best_match;
  223. int hour = it->second;
  224. int newHour = UTC.getHour()+hour;
  225. timezone = UTC;
  226. timezone.setHour(newHour);
  227. }
  228.  
  229. else
  230. {
  231. cout << " Not valid country name" << endl;
  232. found = false;
  233. }
  234. }
  235.  
  236. string getCountry()
  237. {
  238. return stadt;
  239. }
  240.  
  241.  
  242. Time gettimeZone()
  243. {
  244. return timezone;
  245. }
  246.  
  247.  
  248. void printnewTime()
  249. {
  250. if(found == true)
  251. {
  252. cout << " the local time in " << stadt << " is " << setfill('0') << setw(2) << timezone.getHour() << ":";
  253. cout << setfill('0') << setw(2) << timezone.getMinute() << ":";
  254. cout << setfill('0') << setw(2) << timezone.getSecond() << " " << timezone.getmidday() << endl;
  255.  
  256. cout<<endl;
  257.  
  258. cout<< "A fun fact: Airplane travelers can be considered time travelers since they journey through both space and time."<<endl;
  259. }
  260.  
  261. else{
  262.  
  263. return;
  264. }
  265. }
  266. };
  267.  
  268. int main()
  269. {
  270.  
  271. // int ora, minu, secondo;
  272. //
  273. // cout << "what's the time now in your country" << endl;
  274. // cout << endl;
  275. // cout << "enter hour then minutes then second" << endl;
  276. //
  277. // cin >> ora >> minu >> secondo;
  278. //
  279. // Time zone1(ora,minu,secondo);
  280. //
  281. Country c;
  282. //
  283. // cout << "enter the city that you are traveling to: ";
  284. // string city;
  285. //
  286. // cin.ignore();
  287. //
  288. // getline(cin,city);
  289. // cout<<endl;
  290. //
  291. //
  292. // c.setTimeLine(city,zone1);
  293. // c.printnewTime();
  294.  
  295. // cout<<endl;
  296. //
  297. // cout<<endl;
  298.  
  299. Time t1(20,30,0);
  300. c.setTimeLine("itly", t1); // Should auto-correct to "italy"
  301. c.printnewTime();
  302.  
  303. cout<<endl;
  304.  
  305. Time t2(10,0,0);
  306.  
  307. c.setTimeLine("z", t2);
  308. c.printnewTime();
  309.  
  310. cout<<endl;
  311.  
  312.  
  313. return 0;
  314. }
  315.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
 the local time in italy is 09:30:00 PM

A fun fact: Airplane travelers can be considered time travelers since they journey through both space and time.

 Not valid country name