davemx_5

Untitled

May 16th, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 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[], const char s[])
  34. {
  35. int len = strlen(s);
  36. int i=0;
  37. do{out_s[i] ='\0';i++;}while(i<MAX_WORD_LEN);
  38. char temp[MAX_WORD_LEN] = {'\0'};
  39. char returned[MAX_WORD_LEN] = {'\0'};
  40. string str2;
  41.  
  42.  
  43. for (int a = 0; a < len; a++)
  44. {
  45. // if it has punctuation should put it into the outer array
  46.  
  47. int b = 0;
  48.  
  49. /* if(temp[b] ='.'||','||'!')
  50.  
  51. { temp[b] = '.'||','||'!';
  52.  
  53. }
  54. b = 0;*/
  55.  
  56.  
  57. while(s[a] != ' '&&s[a] != ','&&s[a] !='.'&&s[a] !='-')// Check is this is a cpitals if so store infro in Boolen
  58. {// is alpha sa pr sa you -
  59.  
  60. temp[b] = s[a];
  61.  
  62. // check words to see if they
  63.  
  64. if (temp[0]>='A' && temp[0]<='Z')
  65. {temp[0]= temp[0]-'A' + 'a';}
  66.  
  67. b++;
  68. a++;
  69.  
  70.  
  71. }
  72. // cycle through the letters and check for caps first letter / after .
  73.  
  74. temp[b] = '\0';
  75.  
  76.  
  77. dict.translate(returned, temp);
  78.  
  79. str2 = str2 + returned;
  80.  
  81. if(s[a] == ' ' || s[a] == ','||s[a] == '.'||s[a] == '-') // Did we have captial at point in word if so make cpital this postion
  82. {
  83.  
  84. cout << s[a]<<endl;
  85. str2 = str2 + s[a];
  86. cout << str2<<endl;}
  87.  
  88. strcpy(out_s, str2.c_str());
  89. // cout << <<endl;
  90. }
  91.  
  92. }
  93.  
  94.  
  95. void Translator::toEnglish(char out_s[], const char s[])
  96. {
  97. int len = strlen(s);
  98. int i=0;
  99. do{out_s[i] ='\0';i++;}while(i<MAX_WORD_LEN);
  100. char temp[MAX_WORD_LEN] = {'\0'};
  101. char returned[MAX_WORD_LEN] = {'\0'};
  102. string str2;
  103. //const char *cs;
  104.  
  105. for (int a = 0; a < len; a++)
  106. {
  107. if(s[a]!=' ')
  108. {
  109. int b = 0;
  110.  
  111. while(s[a] != ' '&&s[a] != ','&&s[a] !='.'&&s[a] !='-')
  112.  
  113.  
  114. {
  115.  
  116. if (temp[0]>='A' && temp[0]<='Z')
  117. {temp[0]= temp[0]-'A' + 'a';}
  118.  
  119. temp[b] = s[a];
  120. b++;
  121. a++;
  122. }
  123. temp[b] = '\0';
  124.  
  125. dict.translate2(returned, temp);
  126.  
  127. str2 = str2 + returned;
  128.  
  129. if(s[a] == ' ' || s[a] == ','||s[a] == '.'||s[a] == '-')
  130. str2 = str2 + s[a];
  131.  
  132.  
  133. strcpy(out_s, str2.c_str());
  134. }
  135. }
  136. }
  137.  
  138.  
  139. void Dictionary::translate(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  140. {
  141. int i = 0;
  142. for (i=0;i<numEntries;i++)
  143. {
  144. if (strcmp(englishWord[i], s)==0)
  145. break;
  146.  
  147.  
  148.  
  149. if(i<numEntries)
  150. {
  151. strcpy(out_s, elvishWord[i]);
  152. }
  153.  
  154. while(i<numEntries && strcmp(englishWord[i],s)!=0)
  155. // else if(strcmp(englishWord[i],s)!=0)
  156. {
  157. char *not_found = {'\0'};
  158. char *input = {'\0'};
  159. strcpy(input,s);
  160. not_found = '*' + input + '*';
  161. strcpy(out_s, not_found);
  162. break;
  163. }
  164. // what do I do if I didn't find the word?
  165. // might want another parameter or return value what
  166. // indicates that the word wasn't found
  167. }
  168. }
  169.  
  170.  
  171.  
  172. void Dictionary::translate2(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  173. {
  174. int i = 0;
  175. for (i=0;i<numEntries;i++)
  176. {
  177. if (strcmp(elvishWord[i], s)==0)
  178. break;
  179. }
  180. /* for(i = 0; i < numEntries; i++)
  181. {
  182.  
  183. char *not_found;
  184. char *input = {'\0'};
  185. if(strcmp(elvishWord[i],s)!=0)
  186. {
  187. strcpy(input,s);
  188. not_found = '*' + input + '*';
  189. strcpy(out_s, not_found);
  190. break;
  191. }
  192. }*/
  193. if ( i<numEntries)
  194. {
  195. strcpy(out_s, englishWord[i]);
  196. // what do I do if I didn't find the word?
  197. // might want another parameter or return value what
  198. // indicates that the word wasn't found
  199. }
  200. while(i<numEntries && strcmp(elvishWord[i], s)!=0)
  201. // else if(strcmp(elvishWord[i],s)!=0)
  202. {
  203. char *not_found = {'\0'};
  204. char *input = {'\0'};
  205. strcpy(input,s);
  206. not_found = '*' + input + '*';
  207. strcpy(out_s, not_found);
  208. break;
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment