Advertisement
Guest User

Untitled

a guest
May 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1.  
  2.  
  3. // Translator.cpp: определяет точку входа для консольного приложения.
  4. //
  5.  
  6. #include "stdafx.h"
  7. #include <string>
  8. #include <map>
  9. #include "fstream"
  10. #include <iostream>
  11.  
  12. using namespace std;
  13.  
  14. map<string, string> ed;
  15. map<string, string> dr;
  16.  
  17. void tolow(string* str)
  18. {
  19. for (string::iterator it = (*str).begin(); it != (*str).end(); it++)
  20. {
  21. *it = tolower(*it);
  22. }
  23. }
  24.  
  25. string* split(string s, string delimiter)
  26. {
  27. size_t pos = 0;
  28. int k = 0;
  29. for (int i = 0; i < s.length(); i++)
  30. {
  31. if (s[i] == *" ")
  32. {
  33. k++;
  34. }
  35. }
  36. string* mass = new string[k+1];
  37. int kPos = 0;
  38. while ((pos = s.find(delimiter)) != string::npos) {
  39. string token = s.substr(0, pos);
  40. mass[kPos] = token;
  41. s.erase(0, pos + delimiter.length());
  42.  
  43. kPos++;
  44. }
  45. mass[kPos] = s;
  46. return mass;
  47. }
  48. void addDict()
  49. {
  50. struct stat sb;
  51. if (stat("edDict.txt", &sb) == 0)
  52. {
  53. ofstream ofs("edDict.txt", ios_base::app);
  54. ofs.close();
  55. }
  56. ifstream fin("edDict.txt");
  57. string enWord;
  58. fin » enWord;
  59. while (fin.is_open() && !fin.eof())
  60. {
  61. tolow(&enWord);
  62. string deWord;
  63. fin » deWord;
  64. tolow(&deWord);
  65. ed.insert(make_pair(enWord, deWord));
  66. fin » enWord;
  67. }
  68. fin.close();
  69. if (stat("drDict.txt", &sb) == 0)
  70. {
  71. ofstream ofs("drDict.txt", ios_base::app);
  72. ofs.close();
  73. }
  74. fin.open("drDict.txt");
  75. string deWord;
  76. fin » deWord;
  77. while (fin.is_open() && !fin.eof())
  78. {
  79. tolow(&deWord);
  80. string rusWord;
  81. fin » rusWord;
  82. tolow(&rusWord);
  83. dr.insert(make_pair(deWord, rusWord));
  84. fin » deWord;
  85. }
  86. fin.close();
  87. }
  88.  
  89. void addToDict(string firstWord, string secWord, string dict)
  90. {
  91.  
  92. tolow(&firstWord);
  93. tolow(&secWord);
  94. ofstream fout(dict, ios_base::app);
  95. fout « firstWord « endl « secWord « endl;
  96. fout.close();
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. string* translate(string enPhrase)
  117. {
  118. string* str = split(enPhrase, " ");
  119. string* trans=new string[2];
  120. int k = 0;
  121. for (int i = 0; i < enPhrase.length(); i++)
  122. {
  123. if (enPhrase[i] == *" ")
  124. {
  125. k++;
  126. }
  127. }
  128. for (int i = 0; i<=k; i++) {
  129. string enWord = str[i];
  130. tolow(&enWord);
  131. try {
  132. string deutchWord;
  133. deutchWord = ed.at(enWord);
  134. trans[0] += deutchWord+" ";
  135. try {
  136. string rusWord = dr.at(deutchWord);
  137. trans[1] += rusWord+" ";
  138. }
  139. catch (out_of_range e)
  140. {
  141. trans[0] += " ... ";
  142. }
  143. }
  144. catch (out_of_range e)
  145. {
  146. trans[1] += " ... ";
  147. }
  148. }
  149. return trans;
  150. }
  151. void insertED(string enWord, string deutchWord)
  152. {
  153. try {
  154. ed.at(enWord);
  155. //слово уже существует в словаре
  156. }
  157. catch (out_of_range e)
  158. {
  159. addToDict(enWord, deutchWord, "edDict.txt");
  160. ed.insert(make_pair(enWord, deutchWord));
  161. }
  162. }
  163. void insertDR(string deutchWord, string rusWord)
  164. {
  165. try {
  166. dr.at(deutchWord);
  167. //слово уже существует в словаре
  168. }
  169. catch (out_of_range e)
  170. {
  171. addToDict(deutchWord, rusWord, "drDict.txt");
  172. dr.insert(make_pair(deutchWord, rusWord));
  173. }
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. int main(int argc, char *argv[])
  183. {
  184. addDict();
  185. while (true)
  186. {
  187. string com;
  188. cin » com;
  189. if (com == "t")
  190. {
  191. string phrase;
  192. cin » phrase;
  193. string* trans=translate(phrase);
  194. cout « " translated russ: " « trans[1] « endl;
  195. }
  196. else if (com=="aed"){
  197. string fw;
  198. string sw;
  199. cin » fw;
  200. cin » sw;
  201. insertED(fw, sw);
  202. cout « "inserted";
  203. }
  204. else if (com == "adr")
  205. {
  206. string fw;
  207. string sw;
  208. cin » fw;
  209. cin » sw;
  210. insertDR(fw, sw);
  211. cout « "inserted";
  212.  
  213. }
  214. }
  215. return 0;
  216. }
  217.  
  218. 01:49:19
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228. Денис
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement