fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <sstream>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. struct Date{
  11. Date():
  12. month(0),
  13. day(0),
  14. mday(0),
  15. year(0),
  16. time(0)
  17. {}
  18. int month;
  19. int day;
  20. int mday;
  21. int year;
  22. int time;
  23. };
  24.  
  25. void _getCurrentDate(Date &date);
  26. int _getTime(string sentence);
  27. int _getYear(string sentence);
  28. int _getMday(string sentence);
  29. int _getDay(string sentence);
  30. int _getMonth(string sentence);
  31. bool _getDayIndex(string a_day, int &index);
  32. bool _getMonthIndex(string aMonth, int &index);
  33. void _changeToLower(string &str);
  34. bool _containsAlpha(string &str);
  35. void _split(string sentence, vector<string>& toSplit);
  36.  
  37. void getDateFromSentence(string sentence, Date &date);
  38. bool getTense(Date &givenDate);
  39. void processDateless(string sentence);
  40.  
  41. enum States{CheckYear, CheckMonth, CheckMDay, CheckDay, CheckTime};
  42. #define MAX_INPUT 1000
  43.  
  44. int main()
  45. {
  46. Date a_date;
  47. char str[MAX_INPUT];
  48. string a_string;
  49.  
  50. cout << "Please input the text you want to process" << endl;
  51. cin.getline(str, MAX_INPUT -1);
  52.  
  53. a_string.append(str);
  54. getDateFromSentence(a_string, a_date);
  55. if(!getTense(a_date))
  56. processDateless(a_string);
  57.  
  58. return 0;
  59. }
  60.  
  61. void _getCurrentDate(Date &date)
  62. {
  63. time_t t = time(0);
  64. tm *now = localtime(&t);
  65.  
  66. date.year = (now->tm_year + 1900);
  67. date.day = now->tm_wday;
  68. date.month = now->tm_mon;
  69. date.mday = now->tm_mday;
  70. date.time = now->tm_hour;
  71. }
  72.  
  73. void getDateFromSentence(string sentence, Date &date)
  74. {
  75. if(_getYear(sentence))
  76. date.year = (_getYear(sentence));
  77.  
  78. date.day = _getDay(sentence);
  79. date.month = _getMonth(sentence);
  80. date.mday = _getMday(sentence);
  81. date.time = _getTime(sentence);
  82. }
  83.  
  84. bool getTense(Date &givenDate)
  85. {
  86. States state = CheckYear;
  87. Date currentDate;
  88. bool checked = true;
  89.  
  90. _getCurrentDate(currentDate);
  91.  
  92. switch(state)
  93. {
  94. case CheckYear:
  95. if(givenDate.year)
  96. {
  97. if(currentDate.year > givenDate.year)
  98. {
  99. cout << "Past" << endl;
  100. break;
  101. }
  102. if(currentDate.year < givenDate.year)
  103. {
  104. cout << "Future" << endl;
  105. break;
  106. }
  107. }
  108. case CheckMonth:
  109. if(givenDate.month)
  110. {
  111. if(currentDate.month > (givenDate.month - 1))
  112. {
  113. cout << "Past" << endl;
  114. break;
  115. }
  116. if(currentDate.month < (givenDate.month - 1))
  117. {
  118. cout << "Future" << endl;
  119. break;
  120. }
  121. }
  122. case CheckMDay:
  123. if(givenDate.mday)
  124. {
  125. if(currentDate.mday > givenDate.mday)
  126. {
  127. cout << "Past" << endl;
  128. break;
  129. }
  130. if(currentDate.mday < givenDate.mday)
  131. {
  132. cout << "Future" << endl;
  133. break;
  134. }
  135. }
  136. case CheckDay:
  137. if(givenDate.day)
  138. {
  139. if(currentDate.day > (givenDate.day - 1))
  140. {
  141. cout << "Past" << endl;
  142. break;
  143. }
  144. if(currentDate.day < (givenDate.day - 1))
  145. {
  146. cout << "Future" << endl;
  147. break;
  148. }
  149. }
  150. case CheckTime:
  151. if(givenDate.time)
  152. {
  153. if(currentDate.time > givenDate.time)
  154. {
  155. cout << "Past" << endl;
  156. break;
  157. }
  158. if(currentDate.time < givenDate.time)
  159. {
  160. cout << "Future" << endl;
  161. break;
  162. }
  163. }
  164. default:
  165. checked = false;
  166. }
  167.  
  168. return checked;
  169. }
  170.  
  171. #define AUX_VERB "will"
  172. void processDateless(string sentence)
  173. {
  174. if(sentence.find(AUX_VERB) != string::npos)
  175. cout << "Future" << endl;
  176. else
  177. cout << "Could not determine the tense" << endl;
  178. }
  179.  
  180. int _getTime(string sentence)
  181. {
  182. int time = 0;
  183. string temp;
  184. vector<string> stringList;
  185.  
  186. _split(sentence, stringList);
  187.  
  188. for(unsigned int i = 0; i < stringList.size(); i++)
  189. {
  190. temp = stringList[i];
  191. time = atoi(temp.c_str());
  192. if(time)
  193. {
  194. if(temp.find("am") != string::npos)
  195. return time;
  196. if(temp.find("pm") != string::npos)
  197. return (time + 12);
  198. }
  199. }
  200.  
  201. return 0;
  202. }
  203.  
  204. void _split(string sentence, vector<string> &toSplit)
  205. {
  206. stringstream stream(sentence);
  207. string temp;
  208.  
  209. while(stream)
  210. {
  211. stream >> temp;
  212. toSplit.push_back(temp);
  213. temp.clear();
  214. }
  215. }
  216.  
  217. int _getMday(string sentence)
  218. {
  219. int mday = 0;
  220. vector<string> stringList;
  221. string temp;
  222.  
  223. _split(sentence, stringList);
  224.  
  225. for(unsigned int i = 0; i < stringList.size(); i++)
  226. {
  227. temp = stringList[i];
  228. mday = atoi(temp.c_str());
  229.  
  230. if(mday)
  231. {
  232. if(temp.find("th") != string::npos)
  233. return mday;
  234. }
  235. }
  236.  
  237. return 0;
  238. }
  239.  
  240. int _getMonth(string sentence)
  241. {
  242. int month = 0;
  243. string temp;
  244. vector<string> stringList;
  245.  
  246. _split(sentence, stringList);
  247.  
  248. for(unsigned int i =0; i< stringList.size(); i++)
  249. {
  250. temp = stringList[i];
  251. if(_getMonthIndex(temp, month))
  252. return (month + 1);
  253. }
  254.  
  255. return 0;
  256. }
  257.  
  258. #define MONTH_NUM 12
  259. bool _getMonthIndex(string aMonth, int &index)
  260. {
  261.  
  262. string months[MONTH_NUM] = {"january", "february", "march", "april", "may", "june", "july", "august", "september",
  263. "october", "november", "december"};
  264.  
  265. _changeToLower(aMonth);
  266. for(int i = 0; i < MONTH_NUM; i++)
  267. {
  268. if(aMonth == months[i])
  269. {
  270. index = i;
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276.  
  277. int _getYear(string sentence)
  278. {
  279. int year = 0;
  280. vector<string> stringList;
  281.  
  282. _split(sentence, stringList);
  283.  
  284. for(unsigned int i = 0; i< stringList.size(); i++)
  285. {
  286. istringstream stream(stringList[i]);
  287. stream >> year;
  288.  
  289. if(year)
  290. {
  291. if(!_containsAlpha(stringList[i]))
  292. return year;
  293. }
  294. }
  295.  
  296. return 0;
  297. }
  298.  
  299. int _getDay(string sentence)
  300. {
  301. int day = 0;
  302. vector<string> stringList;
  303.  
  304. _split(sentence, stringList);
  305.  
  306. for(unsigned int i =0; i< stringList.size(); i++)
  307. {
  308. if(_getDayIndex(stringList[i], day))
  309. return (day + 1);
  310. }
  311.  
  312. return 0;
  313. }
  314.  
  315. #define DAY_SIZE 7
  316. bool _getDayIndex(string a_day, int &index)
  317. {
  318. string days[DAY_SIZE] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
  319.  
  320. _changeToLower(a_day);
  321. for(int i = 0; i < DAY_SIZE; i++)
  322. {
  323. if(a_day == days[i])
  324. {
  325. index = i;
  326. return true;
  327. }
  328. }
  329.  
  330. return false;
  331. }
  332.  
  333. void _changeToLower(string& str)
  334. {
  335. string temp;
  336. for(unsigned int i = 0; i < str.size(); i++)
  337. {
  338. if(::isupper(str.at(i)))
  339. temp.append(sizeof(char), ::tolower(str.at(i)));
  340. else
  341. temp.append(sizeof(char), str.at(i));
  342.  
  343. }
  344. str.clear();
  345. str = temp;
  346. }
  347.  
  348. bool _containsAlpha(string &str)
  349. {
  350. for(unsigned int i = 0; i< str.size(); i++)
  351. {
  352. if(::isalpha(str.at(i)))
  353. return true;
  354. }
  355. return false;
  356. }
  357.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty