davemx_5

Untitled

May 19th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 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]!=' ')
  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' && s[a] != '\0' && s[a]!= '"')// 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' || s[a] == '\0' || s[a] == '"') // 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]!='?' && 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. str2 = str2 + returned;
  142. cout << returned << endl;
  143.  
  144. if(s[a] == ' ' || s[a]== '\n' || s[a] == ','||s[a] == '.' ||s[a] == '-' || s[a]== '!' || s[a]== '?' || s[a] == '*' || s[a] == '\0' || s[a] == '"')
  145. {
  146. str2 = str2 + s[a];
  147. strcpy(out_s, str2.c_str());
  148. }
  149. }
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. void Dictionary::translate(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  161. {
  162. int i = 0;
  163. int len = 0;
  164. bool notfound = false;
  165. for (i=0;i<numEntries;i++)
  166. {
  167. if (strcmp(englishWord[i], s)==0)
  168. {
  169.  
  170. notfound=true;
  171. break;}
  172. }
  173. if(i<numEntries)
  174. {
  175. strcpy(out_s, elvishWord[i]);
  176. }
  177. if((i==numEntries) && (!notfound))
  178. {
  179.  
  180. len = strlen(s);
  181. out_s[0] = '*';
  182. out_s[len+1] = '*';
  183. out_s[len+2] = '\0';
  184. for(int j=0; j<len;j++)
  185. {
  186. out_s[j+1] = s[j];
  187. }
  188.  
  189. }
  190. }
  191.  
  192.  
  193.  
  194. void Dictionary::translate2(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  195. {
  196. int len = 0;
  197. bool notfound = false;
  198. int i = 0;
  199. for (i=0;i<numEntries;i++)
  200. {
  201. if (strcmp(elvishWord[i], s)==0)
  202. {
  203. notfound = true;
  204. break;
  205. }
  206. }
  207. if ( i<numEntries)
  208. {
  209. strcpy(out_s, englishWord[i]);
  210.  
  211. }
  212.  
  213. if((i==numEntries) && (!notfound))
  214. {
  215.  
  216. len = strlen(s);
  217. for( int j=0; j < len-2;j++)
  218. {
  219.  
  220. out_s[j] = s[j+1];
  221. }
  222. out_s[len-2] = '\0';
  223. }
  224.  
  225.  
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment