davemx_5

Untitled

May 17th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 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] = '\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. if(s[a]!=' ' || s[a]!='\n')
  52. {
  53.  
  54.  
  55. int b = 0;
  56.  
  57. /* if(temp[b] ='.'||','||'!')
  58.  
  59. { temp[b] = '.'||','||'!';
  60.  
  61. }
  62. b = 0;*/
  63.  
  64.  
  65. 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
  66. {// is alpha sa pr sa you -
  67.  
  68. temp[b] = s[a];
  69.  
  70. // check words to see if they
  71.  
  72. if (temp[0]>='A' && temp[0]<='Z')
  73. {temp[0]= temp[0]-'A' + 'a';}
  74.  
  75. b++;
  76. a++;
  77.  
  78.  
  79. }
  80. // cycle through the letters and check for caps first letter / after .
  81.  
  82. temp[b] = '\0';
  83.  
  84.  
  85. dict.translate(returned, temp);
  86.  
  87. cout << returned << endl;
  88. str2 = str2 + returned;
  89.  
  90. 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
  91. {
  92.  
  93. //cout << s[a]<<endl;
  94. str2 = str2 + s[a];
  95. //cout << str2<<endl;}
  96.  
  97. strcpy(out_s, str2.c_str());
  98. // cout << <<endl;
  99. }
  100. }
  101.  
  102. }
  103.  
  104. }
  105.  
  106. void Translator::toEnglish(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  107. {
  108. char s_edit[MAX_WORD_LEN] = {'\0'};
  109. strcpy(s_edit,s);
  110. int len = strlen(s_edit);
  111. s_edit[len] = '\0';
  112. int i=0;
  113. do{s_edit[i] ='\0';i++;}while(i<MAX_WORD_LEN);
  114. char temp[MAX_WORD_LEN] = {'\0'};
  115. char returned[MAX_WORD_LEN] = {'\0'};
  116.  
  117. string str2;
  118. //const char *cs;
  119.  
  120. for (int a = 0; a < len; a++)
  121. {
  122. if(s[a]!=' ' || s[a]!='\n')
  123. {
  124. int b = 0;
  125.  
  126. while(s[a] != ' ' && s[a] != '\n' &&s[a] != ','&&s[a] !='.' && s[a]!='-' && s[a]!='!' && s[a]!='?')
  127.  
  128.  
  129. {
  130.  
  131. if (temp[0]>='A' && temp[0]<='Z')
  132. {temp[0]= temp[0]-'A' + 'a';}
  133.  
  134. temp[b] = s[a];
  135. b++;
  136. a++;
  137. }
  138. temp[b] = '\0';
  139.  
  140. dict.translate2(returned, temp);
  141.  
  142. str2 = str2 + returned;
  143. cout << returned << endl;
  144.  
  145. if(s[a] == ' ' || s[a]=='\n' || s[a] == ','||s[a] == '.'||s[a] == '-' || s[a]=='!' || s[a]=='?')
  146. str2 = str2 + s[a];
  147.  
  148. //cout << str2 << endl;
  149. strcpy(out_s, str2.c_str());
  150. }
  151. }
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. void Dictionary::translate(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  162. {
  163. cout << s << endl;
  164. int i = 0;
  165. int len = 0;
  166. bool notfound = false;
  167. for (i=0;i<numEntries;i++)
  168. {
  169. // cout << "1 "<< endl;
  170. if (strcmp(englishWord[i], s)==0)
  171. {
  172.  
  173. notfound=true;
  174. break;}
  175. // cout << 1 << endl;
  176. }
  177. if(i<numEntries)
  178. {
  179. strcpy(out_s, elvishWord[i]);
  180. //cout << 2 << endl;
  181. }
  182. if((i==numEntries) && (!notfound))// if(strcmp(englishWord[i],s)!=0)
  183. {
  184. cout << s << endl;
  185. //char not_found[MAX_WORD_LEN];
  186. len = strlen(s);
  187. out_s[0] = '*';
  188. out_s[len+1] = '*';
  189. out_s[len+2] = '\0';
  190. for(int j=0; j<len;j++)
  191. {
  192. out_s[j+1] = s[j];
  193. }
  194.  
  195. }
  196.  
  197. /* else{
  198. string not_found;
  199. // cout << 3 << endl;
  200. not_found = '*' + s + '*';
  201. // cout << not_found << endl;
  202. strcpy(out_s, not_found.c_str());
  203.  
  204.  
  205.  
  206. }*/
  207. // what do I do if I didn't find the word?
  208. // might want another parameter or return value what
  209. // indicates that the word wasn't found
  210.  
  211. }
  212.  
  213.  
  214.  
  215. void Dictionary::translate2(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  216. {
  217. int len = 0;
  218. bool notfound = false;
  219. int i = 0;
  220. for (i=0;i<numEntries;i++)
  221. {
  222. if (strcmp(elvishWord[i], s)==0)
  223. {
  224. notfound = true;
  225. break;
  226. }
  227. }
  228. if ( i<numEntries)
  229. {
  230. strcpy(out_s, englishWord[i]);
  231. // what do I do if I didn't find the word?
  232. // might want another parameter or return value what
  233. // indicates that the word wasn't found
  234. }
  235.  
  236. if((i==numEntries) && (!notfound))// if(strcmp(englishWord[i],s)!=0)
  237. {
  238. cout << s << endl;
  239. //char not_found[MAX_WORD_LEN];
  240. len = strlen(s);
  241. for( int j=0; j < len-2;j++)
  242. {
  243.  
  244. out_s[j] = s[j+1];
  245. }
  246. out_s[len-2] = '\0';
  247. }
  248.  
  249.  
  250. /* else{
  251. string not_found;
  252. cout << 3 << endl;
  253. not_found = '*' + s + '*';
  254. cout << not_found << endl;
  255. strcpy(out_s, not_found.c_str());
  256. cout << 5 << endl;
  257. }*/
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment