davemx_5

Untitled

May 16th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 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] !='.')// 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.  
  96. void Translator::toEnglish(char out_s[], const char s[])
  97. {
  98. int len = strlen(s);
  99. int i=0;
  100. do{out_s[i] ='\0';i++;}while(i<MAX_WORD_LEN);
  101. char temp[MAX_WORD_LEN] = {'\0'};
  102. char returned[MAX_WORD_LEN] = {'\0'};
  103. string str2;
  104. //const char *cs;
  105.  
  106. for (int a = 0; a < len; a++)
  107. {
  108. if(s[a]!=' ')
  109. {
  110. int b = 0;
  111.  
  112. while(s[a] != ' '&&s[a] != ','&&s[a] !='.')
  113.  
  114.  
  115. {
  116.  
  117. if (temp[0]>='A' && temp[0]<='Z')
  118. {temp[0]= temp[0]-'A' + 'a';}
  119.  
  120. temp[b] = s[a];
  121. b++;
  122. a++;
  123. }
  124. temp[b] = '\0';
  125.  
  126. dict.translate2(returned, temp);
  127.  
  128. str2 = str2 + returned;
  129.  
  130. if(s[a] == ' ' || s[a] == ','||s[a] == '.'||s[a] == '-')
  131. str2 = str2 + s[a];
  132.  
  133.  
  134. strcpy(out_s, str2.c_str());
  135. }
  136. }
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. void Dictionary::translate(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  147. {
  148. int i = 0;
  149. for (i=0;i<numEntries;i++)
  150. {
  151. if (strcmp(englishWord[i], s)==0)
  152. break;
  153. // cout << 1 << endl;
  154. }
  155.  
  156. if(i<numEntries)
  157. {
  158. strcpy(out_s, elvishWord[i]);
  159. //cout << 2 << endl;
  160. }
  161.  
  162. else{
  163. string not_found;
  164. cout << 3 << endl;
  165. not_found = '*' + s + '*';
  166. cout << 4 << endl;
  167. strcpy(out_s, not_found.c_str());
  168. cout << 5 << endl;
  169.  
  170.  
  171. }
  172. // what do I do if I didn't find the word?
  173. // might want another parameter or return value what
  174. // indicates that the word wasn't found
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. void Dictionary::translate2(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
  182. {
  183. int i = 0;
  184. for (i=0;i<numEntries;i++)
  185. {
  186. if (strcmp(elvishWord[i], s)==0)
  187. break;
  188. }
  189. if ( i<numEntries)
  190. {
  191. strcpy(out_s, elvishWord[i]);
  192. // what do I do if I didn't find the word?
  193. // might want another parameter or return value what
  194. // indicates that the word wasn't found
  195. }
  196. else{
  197. string not_found;
  198. cout << 3 << endl;
  199. not_found = '*' + s + '*';
  200. cout << 4 << endl;
  201. strcpy(out_s, not_found.c_str());
  202. cout << 5 << endl;
  203. }
  204.  
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment