Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.30 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Project 5
  4. //
  5. // Created by Andy Yu on 11/17/14.
  6. // Copyright (c) 2014 Hongseok (Andy) Yu. All rights reserved.
  7. //
  8.  
  9. #define CRT_SECURE_NO_WARNINGS
  10. #include <cstring>
  11. #include <iostream>
  12. #include <cassert>
  13. using namespace std;
  14.  
  15. const int MAX_WORD_LENGTH = 20;
  16.  
  17. void delChar(int pos, int max, char array[][MAX_WORD_LENGTH+1])
  18. {
  19. char temp[20];
  20. for (int i = pos; i < max-1; i++)
  21. {
  22. strcpy(temp, array[i]);
  23. strcpy(array[i], array[i+1]);
  24. strcpy(array[i+1], temp);
  25. }
  26. }
  27.  
  28. void delInt(int pos, int max, int array[])
  29. {
  30. int temp = -1;
  31. for (int i = pos; i < max-1; i++)
  32. {
  33. temp = array[i];
  34. array[i] = array[i+1];
  35. array[i+1] = temp;
  36. }
  37. }
  38.  
  39. void delRule(int pos, int max, int array[], char array2[][MAX_WORD_LENGTH+1], char array3[][MAX_WORD_LENGTH+1])
  40. {
  41. delInt(pos, max, array);
  42. delChar(pos, max, array2);
  43. delChar(pos, max, array3);
  44. }
  45.  
  46. int cleanRule(int length, char array2[], char array3[])
  47. {
  48. if (strlen(array2) == 0)
  49. return 1;
  50. int j = 0;
  51. while (array2[j] != '\0')
  52. {
  53. if (!(isalpha(array2[j])))
  54. return 1;
  55. if (isupper(array2[j]))
  56. array2[j] = tolower(array2[j]);
  57. j++;
  58. }
  59. if (strlen(array3) == 0)
  60. return 1;
  61. j = 0;
  62. while (array3[j] != '\0')
  63. {
  64. if (!(isalpha(array3[j])))
  65. return 1;
  66. if (isupper(array3[j]))
  67. array3[j] = tolower(array3[j]);
  68. j++;
  69. }
  70. return 2;
  71. }
  72.  
  73. int standardizeRules(int distance[],
  74. char word1[][MAX_WORD_LENGTH+1],
  75. char word2[][MAX_WORD_LENGTH+1],
  76. int nRules)
  77. {
  78. int counter = 0;
  79. int cleancounter = nRules;
  80. for (int i = 0; i < cleancounter; i++)
  81. {
  82. if (distance[i] < 0) // check that distance is positive
  83. {
  84. distance[i] = -1; // if not mark to remove respective values from the arrays
  85. }
  86. if (cleanRule(nRules, word1[i], word2[i]) == 1)
  87. {
  88. // delete match rule if it contains a nonalpha character
  89. distance[i] = -1;
  90. }
  91. for (int k = 0; k < nRules; k++) // check for any repeated w1 / w2 values and remove the smaller distanced one
  92. {
  93. if (k != i)
  94. {
  95. if (strcmp(word1[k],word2[k]) == 0)
  96. {
  97. if (strcmp(word1[i],word2[i])== 0)
  98. {
  99. if (!(distance[k] == -1|| distance [i] == -1))
  100. {
  101. if (distance[k] > distance[i])
  102. {
  103. distance[i] = -1;
  104. }
  105. else
  106. {
  107. distance[k] = -1;
  108. }
  109. }
  110. }
  111. }
  112. if (strcmp(word2[i],word1[k]) == 0)
  113. {
  114. if (strcmp(word1[i], word2[k]) == 0)
  115. {
  116. if (!(distance[k] == -1|| distance [i] == -1))
  117. {
  118. if (distance[k] > distance[i])
  119. {
  120. distance[i] = -1;
  121. }
  122. else
  123. {
  124. distance[k] = -1;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. for (int i = 0; i < nRules; i++)
  133. {
  134. if (distance[i] == -1)
  135. {
  136. delRule(i, nRules, distance, word1, word2);
  137. counter++;
  138. }
  139. }
  140. return nRules - counter;
  141. }
  142.  
  143. int determineQuality(const int distance[],
  144. const char word1[][MAX_WORD_LENGTH+1],
  145. const char word2[][MAX_WORD_LENGTH+1],
  146. int nRules,
  147. const char document[])
  148. {
  149. int counter = 0;
  150. char doccopy[200] = {}; // copy of document
  151. char arraycopy[][20] = {};
  152. int i = 0;
  153. int k = 0;
  154. int words = 0;
  155. char temp[20];
  156. int templ = 0;
  157. while (document[i]!= '\0') // copy all alphabetical characters to document
  158. {
  159. if (document[i] == ' ')
  160. {
  161. doccopy[k] = ' ';
  162. k++;
  163. }
  164. if (isalpha(document[i]))
  165. {
  166. doccopy[k] = document[i];
  167. toupper(doccopy[k]); // change case
  168. k++;
  169. }
  170. i++;
  171. }
  172. while (doccopy[i]!= '\0') // write document to char 2d array
  173. {
  174. int j = 0;
  175. if (!(doccopy[i] == ' '))
  176. {
  177. temp[templ] = doccopy[i];
  178. templ++;
  179. }
  180. else
  181. {
  182. strcpy(arraycopy[j],temp);
  183. j++;
  184. }
  185. words = j;
  186. }
  187. for (int m = 0; m < nRules; m++)
  188. {
  189. for (int n = 0; n < words; n++)
  190. {
  191. if (strcmp(word1[m], arraycopy[n]) == 0)
  192. {
  193. if (n >= distance[m])
  194. {
  195. for (int i = 1; i < distance[m]+1; i++)
  196. {
  197. if (strcmp(arraycopy[n-i], word2[m]))
  198. {
  199. counter++;
  200. }
  201. }
  202. }
  203. if (n <= words - distance[m] - 1)
  204. {
  205. for (int i = 1; i < distance[m]; i++)
  206. {
  207. if (strcmp(arraycopy[n+i], word2[m]))
  208. {
  209. counter++;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. return counter;
  217. }
  218.  
  219. int main(int argc, const char * argv[])
  220. {
  221. auto does_test1_pass = [](int distance[], char word1[][MAX_WORD_LENGTH + 1], char word2[][MAX_WORD_LENGTH + 1])
  222. {
  223. const int returnValue = 4;
  224. int match = 0;
  225. int y = 0;
  226. while (y < returnValue)
  227. {
  228. if (distance[y] == 2 && (strcmp(word1[y], "mad") == 0) && (strcmp(word2[y], "scientist") == 0))
  229. match++;
  230. y++;
  231. }
  232. if (match != 1)
  233. return false;
  234.  
  235. y = 0;
  236. match = 0;
  237. while (y < returnValue)
  238. {
  239. if (distance[y] == 4 && (strcmp(word1[y], "deranged") == 0) && (strcmp(word2[y], "robot") == 0))
  240. match++;
  241. y++;
  242. }
  243. if (match != 1)
  244. return false;
  245.  
  246. y = 0;
  247. match = 0;
  248. while (y < returnValue)
  249. {
  250. if (distance[y] == 1 && (strcmp(word1[y], "nefarious") == 0) && (strcmp(word2[y], "plot") == 0))
  251. match++;
  252. else if (distance[y] == 1 && (strcmp(word1[y], "plot") == 0) && (strcmp(word2[y], "nefarious") == 0))
  253. match++;
  254. y++;
  255. }
  256. if (match != 1)
  257. return false;
  258.  
  259. y = 0;
  260. match = 0;
  261. while (y < returnValue)
  262. {
  263. if (distance[y] == 13 && (strcmp(word1[y], "have") == 0) && (strcmp(word2[y], "mad") == 0))
  264. match++;
  265. y++;
  266. }
  267. if (match != 1)
  268. return false;
  269.  
  270. return true;
  271. };
  272.  
  273. auto does_test3_pass = [](int distance[], char word1[][MAX_WORD_LENGTH + 1], char word2[][MAX_WORD_LENGTH + 1])
  274. {
  275. const int returnValue = 2;
  276. int match = 0;
  277. int y = 0;
  278. while (y < returnValue)
  279. {
  280. if (distance[y] == 5 && (strcmp(word1[y], "sad") == 0) && (strcmp(word2[y], "happy") == 0))
  281. match++;
  282. y++;
  283. }
  284. if (match != 1)
  285. return false;
  286.  
  287. y = 0;
  288. match = 0;
  289. while (y < returnValue)
  290. {
  291. if (distance[y] == 13 && (strcmp(word1[y], "net") == 0) && (strcmp(word2[y], "ten") == 0))
  292. match++;
  293. y++;
  294. }
  295. if (match != 1)
  296. return false;
  297.  
  298. return true;
  299. };
  300.  
  301. int distance1[7] = { 2, 4, 1, 3, 2, 1, 13 };
  302. char word1_1[7][MAX_WORD_LENGTH + 1] = { "mad", "deranged", "NEFARIOUS", "half-witted", "robot", "plot", "have" };
  303. char word2_1[7][MAX_WORD_LENGTH + 1] = { "scientist", "robot", "plot", "assistant", "deranged", "Nefarious", "mad" };
  304.  
  305. int a = standardizeRules(distance1, word1_1, word2_1, 7);
  306. cout << a << endl;
  307. assert(does_test1_pass(distance1, word1_1, word2_1));
  308.  
  309. int distance2[3] = { -1, 0, 1 };
  310. char word1_2[3][MAX_WORD_LENGTH + 1] = { "mad", "deranged", "nefarious" };
  311. char word2_2[3][MAX_WORD_LENGTH + 1] = { "scientist", "robot", "" };
  312.  
  313. int b = standardizeRules(distance2, word1_2, word2_2, 3);
  314. assert(b == 0);
  315.  
  316. int distance3[5] = { 5, 1, 10, 5, 13 };
  317. char word1_3[5][MAX_WORD_LENGTH + 1] = { "sad", "happy", "ten", "net", "net" };
  318. char word2_3[5][MAX_WORD_LENGTH + 1] = { "happy", "sad", "net", "ten", "ten" };
  319.  
  320. int c = standardizeRules(distance3, word1_3, word2_3, 5);
  321. assert(does_test3_pass(distance3, word1_3, word2_3));
  322.  
  323. int distance4[1] = { 1 };
  324. char word1_4[1][MAX_WORD_LENGTH + 1] = { "mad" };
  325. char word2_4[1][MAX_WORD_LENGTH + 1] = { "mad" };
  326.  
  327. int d = standardizeRules(distance4, word1_4, word2_4, 1);
  328. assert(d == 1);
  329.  
  330. const int TEST1_NCRITERIA = 4;
  331. int test1dist[TEST1_NCRITERIA] = {
  332. 2, 4, 1, 13
  333. };
  334. char test1w1[TEST1_NCRITERIA][MAX_WORD_LENGTH + 1] = {
  335. "mad", "deranged", "nefarious", "have"
  336. };
  337. char test1w2[TEST1_NCRITERIA][MAX_WORD_LENGTH + 1] = {
  338. "scientist", "robot", "plot", "mad"
  339. };
  340. assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
  341. "The mad UCLA scientist unleashed a deranged evil giant robot.") == 2);
  342. assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
  343. "The mad UCLA scientist unleashed a deranged robot.") == 2);
  344. assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
  345. "**** 2014 ****") == 0);
  346. assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
  347. " That plot: NEFARIOUS!") == 1);
  348. assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
  349. "deranged deranged robot deranged robot robot") == 1);
  350. assert(determineQuality(test1dist, test1w1, test1w2, TEST1_NCRITERIA,
  351. "Two mad scientists suffer from deranged-robot fever.") == 0);
  352.  
  353. const int TEST2_NCRITERIA = 3;
  354. int test2dist[TEST2_NCRITERIA] = {
  355. 4, 1, 2
  356. };
  357. char test2w1[TEST2_NCRITERIA][MAX_WORD_LENGTH + 1] = {
  358. "mad", "micheal", "presents"
  359. };
  360. char test2w2[TEST2_NCRITERIA][MAX_WORD_LENGTH + 1] = {
  361. "mad", "bay", "explosions"
  362. };
  363. assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
  364. "Mad Micheal Bay presents mad explosions!") == 3);
  365. assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
  366. "Mad Micheal Bay presents explosions!") == 2);
  367. assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
  368. "Mad mad") == 1);
  369. assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
  370. "Mad") == 0);
  371. assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
  372. "Micheal presents explosions at the bay!") == 1);
  373. assert(determineQuality(test2dist, test2w1, test2w2, TEST2_NCRITERIA,
  374. "explosions presents bay micheal bay bay micheal mad") == 2);
  375. cout << "All tests succeed";
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement