davemx_5

Untitled

May 17th, 2013
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1.  
  2. #include "translator.h"
  3. #include <iostream>
  4. using namespace std;
  5. #include <fstream>
  6. #include <cstring>
  7. #include <string>
  8. #include <algorithm>
  9.  
  10.  
  11. Dictionary::Dictionary(const char dictFileName[])
  12. {
  13. fstream str;
  14. str.open(dictFileName, ios::in);
  15.  
  16. int i=0;
  17. while (!str.eof())
  18. {
  19. str >> englishWord[i];// Here says take first word slot into english array
  20. str >> elvishWord[i];
  21. i++;
  22. }
  23. numEntries=i;
  24. }
  25.  
  26. Translator::Translator(const char dictFileName[]) : dict(dictFileName)// This is the constructor with an initalizer list
  27. {
  28.  
  29.  
  30.  
  31. }
  32.  
  33. void Translator::toElvish(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  34. {
  35.  
  36. char s_edit[MAX_WORD_LEN] = {'\0'};
  37. strcpy(s_edit,s);
  38. int len = strlen(s_edit);
  39. s_edit[len-2] = '\0';
  40. int i=0;
  41. do{s_edit[i] ='\0';i++;}while(i<MAX_WORD_LEN);
  42. char temp[MAX_WORD_LEN] = {'\0'};
  43. char returned[MAX_WORD_LEN] = {'\0'};
  44.  
  45. string str2;
  46.  
  47.  
  48. for (int a = 0; a < len; a++)
  49. {
  50. // if it has punctuation should put it into the outer array
  51.  
  52. int b = 0;
  53.  
  54. /* if(temp[b] ='.'||','||'!')
  55.  
  56. { temp[b] = '.'||','||'!';
  57.  
  58. }
  59. b = 0;*/
  60.  
  61.  
  62. while(s[a] != ' '&& s[a] != ','&& s[a] !='.' && s[a]!='-' && s[a]!='!' && s[a]!='?' && s[a]!='\n')// Check is this is a cpitals if so store infro in Boolen
  63. {// is alpha sa pr sa you -
  64.  
  65. temp[b] = s[a];
  66.  
  67. // check words to see if they
  68.  
  69. if (temp[0]>='A' && temp[0]<='Z')
  70. {temp[0]= temp[0]-'A' + 'a';}
  71.  
  72. b++;
  73. a++;
  74.  
  75.  
  76. }
  77. // cycle through the letters and check for caps first letter / after .
  78.  
  79. temp[b] = '\0';
  80.  
  81.  
  82. dict.translate(returned, temp);
  83.  
  84. str2 = str2 + returned;
  85.  
  86. if(s[a] == ' ' || s[a] == ','||s[a] == '.'||s[a] == '-' || s[a] == '*' || s[a]=='!' || s[a]=='?' || s[a] == '\n') // Did we have captial at point in word if so make cpital this postion
  87. {
  88.  
  89. //cout << s[a]<<endl;
  90. str2 = str2 + s[a];
  91. //cout << str2<<endl;}
  92.  
  93. strcpy(out_s, str2.c_str());
  94. // cout << <<endl;
  95. }
  96.  
  97. }
  98.  
  99. }
  100.  
  101. void Translator::toEnglish(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  102. {
  103. char s_edit[MAX_WORD_LEN] = {'\0'};
  104. strcpy(s_edit,s);
  105. int len = strlen(s_edit);
  106. s_edit[len-2] = '\0';
  107. int i=0;
  108. do{s_edit[i] ='\0';i++;}while(i<MAX_WORD_LEN);
  109. char temp[MAX_WORD_LEN] = {'\0'};
  110. char returned[MAX_WORD_LEN] = {'\0'};
  111.  
  112. string str2;
  113. //const char *cs;
  114.  
  115. for (int a = 0; a < len; a++)
  116. {
  117. if(s[a]!=' ')
  118. {
  119. int b = 0;
  120.  
  121. while(s[a] != ' '&&s[a] != ','&&s[a] !='.' && s[a]!='-' && s[a]!='!' && s[a]!='?' &&s[a]!='\n')
  122.  
  123.  
  124. {
  125.  
  126. if (temp[0]>='A' && temp[0]<='Z')
  127. {temp[0]= temp[0]-'A' + 'a';}
  128.  
  129. temp[b] = s[a];
  130. b++;
  131. a++;
  132. }
  133. temp[b] = '\0';
  134.  
  135. dict.translate2(returned, temp);
  136.  
  137. str2 = str2 + returned;
  138. cout << returned << endl;
  139.  
  140. if(s[a] == ' ' || s[a] == ','||s[a] == '.'||s[a] == '-' || s[a]=='!' || s[a]=='?' || s[a] == '\n')
  141. str2 = str2 + s[a];
  142.  
  143. //cout << str2 << endl;
  144. strcpy(out_s, str2.c_str());
  145. }
  146. }
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. void Dictionary::translate(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  157. {
  158. cout << s << endl;
  159. int i = 0;
  160. int len = 0;
  161. bool notfound = false;
  162. for (i=0;i<numEntries;i++)
  163. {
  164. // cout << "1 "<< endl;
  165. if (strcmp(englishWord[i], s)==0)
  166. {
  167.  
  168. notfound=true;
  169. break;}
  170. // cout << 1 << endl;
  171. }
  172. if(i<numEntries)
  173. {
  174. strcpy(out_s, elvishWord[i]);
  175. //cout << 2 << endl;
  176. }
  177. if((i==numEntries) && (!notfound))// if(strcmp(englishWord[i],s)!=0)
  178. {
  179. cout << s << endl;
  180. //char not_found[MAX_WORD_LEN];
  181. len = strlen(s);
  182. out_s[0] = '*';
  183. out_s[len+1] = '*';
  184. out_s[len+2] = '\0';
  185. for(int j=0; j<len;j++)
  186. {
  187. out_s[j+1] = s[j];
  188. }
  189.  
  190. }
  191.  
  192. /* else{
  193. string not_found;
  194. // cout << 3 << endl;
  195. not_found = '*' + s + '*';
  196. // cout << not_found << endl;
  197. strcpy(out_s, not_found.c_str());
  198.  
  199.  
  200.  
  201. }*/
  202. // what do I do if I didn't find the word?
  203. // might want another parameter or return value what
  204. // indicates that the word wasn't found
  205.  
  206. }
  207.  
  208.  
  209.  
  210. void Dictionary::translate2(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  211. {
  212. int len = 0;
  213. bool notfound = false;
  214. int i = 0;
  215. for (i=0;i<numEntries;i++)
  216. {
  217. if (strcmp(elvishWord[i], s)==0)
  218. {
  219. notfound = true;
  220. break;
  221. }
  222. }
  223. if ( i<numEntries)
  224. {
  225. strcpy(out_s, englishWord[i]);
  226. // what do I do if I didn't find the word?
  227. // might want another parameter or return value what
  228. // indicates that the word wasn't found
  229. }
  230.  
  231. if((i==numEntries) && (!notfound))// if(strcmp(englishWord[i],s)!=0)
  232. {
  233. cout << s << endl;
  234. //char not_found[MAX_WORD_LEN];
  235. len = strlen(s);
  236. for( int j=0; j < len-2;j++)
  237. {
  238.  
  239. out_s[j] = s[j+1];
  240. }
  241. out_s[len-2] = '\0';
  242. }
  243.  
  244.  
  245. /* else{
  246. string not_found;
  247. cout << 3 << endl;
  248. not_found = '*' + s + '*';
  249. cout << not_found << endl;
  250. strcpy(out_s, not_found.c_str());
  251. cout << 5 << endl;
  252. }*/
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment