Advertisement
Enter121

Untitled

Jan 8th, 2021
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <codecvt>
  5. #include <fstream>
  6. #include <locale.h>
  7.  
  8. using namespace std;
  9.  
  10. const int LEN = 40;
  11. //MORSE
  12. string morse[LEN] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
  13. "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".-.-",
  14. "-.-..", "..-..", ".-..-", "--.--", "---.", "...-...", "--..-.", "--..-", ".-.-.-", "--..--",
  15. "..--..", "-.-.--","/"};
  16. //ALFABET
  17. wstring alpha = L"abcdefghijklmnopqrstuvwxyząćęłńóśżź.,?! ";
  18. wstring alphaB = L"ABCDEFGHIJKLMNOPQRSTUVWXYZĄĆĘŁŃÓŚŻŹ.,?! ";
  19.  
  20. /**
  21. *
  22. * @param val znak
  23. * @return Zwraca indeks znaku z alfabetu
  24. */
  25. int getID(wchar_t val) {
  26. for (int i = 0; i < LEN; i++) {
  27. if (alpha[i] == val)return i;
  28. if (alphaB[i] == val)return i;
  29. }
  30. return -1;
  31. }
  32.  
  33. /**
  34. *
  35. * @param val sekwencja morsea
  36. * @return Zwraca indeks znaku z morsea
  37. */
  38. int getID(string val) {
  39. for (int i = 0; i < LEN; i++) {
  40. if (morse[i] == val)return i;
  41. }
  42. return -1;
  43. }
  44.  
  45. /**
  46. *
  47. * @param str tekst
  48. * @param c znak
  49. * @return zwraca ilosc wystapien znaku c ze stringa str
  50. */
  51. int getAmountOfChar(string str, char c) {
  52. int tmp = 0;
  53. for (int i = 0; i < str.length(); i++)
  54. if (str[i] == c)tmp++;
  55. return tmp;
  56. }
  57. /**
  58. *
  59. * @param str tekst do rozdzielenia
  60. * @param len ilosc elementow w tablicy
  61. * @return rozdziela stringa na tablice po spacji
  62. */
  63. string *split(string str, int &len) {
  64. char delimiter = ' ';
  65. len = getAmountOfChar(str, delimiter) + 1;
  66. string *tmp = new string[len];
  67. stringstream ss;
  68. int id = 0;
  69. for (int i = 0; i < str.length(); i++) {
  70. if (str[i] == delimiter) {
  71. tmp[id++] = ss.str();
  72. ss.str("");
  73. } else {
  74. ss << str[i];
  75. }
  76. }
  77. tmp[id] = ss.str();
  78. return tmp;
  79. }
  80.  
  81. /**
  82. *
  83. * @param str kod morsea
  84. * @return zwraca tekst w normalnym alfabecie
  85. */
  86. wstring decode(string str) {
  87. wstringstream ss;
  88. int len = 0;
  89. string *s = split(str, len);
  90. for (int i = 0; i < len; i++) {
  91. int id = getID(s[i]);
  92.  
  93. if (id != -1) {
  94. ss << alpha[id];
  95. }
  96. }
  97. return ss.str();
  98. }
  99.  
  100. /**
  101. *
  102. * @param str tekst
  103. * @return zwraca zakodowany kod morsea
  104. */
  105. string encode(wstring str) {
  106. stringstream ss;
  107. for (int i = 0; i < str.size(); i++) {
  108. int id = getID((str[i]));
  109. if (id != -1) {
  110. ss << morse[id] << " ";
  111. }
  112. }
  113. return ss.str();
  114. }
  115. /**
  116. *
  117. * @param file nazwa pliku
  118. * @return zwraca tresc pliku z uwzglednieniem kodowania utf8
  119. */
  120. wstring readWString(string file) {
  121. locale loc(locale(), new codecvt_utf8<wchar_t>);
  122. wifstream ifs(file);
  123. if(ifs.fail())cout<<"Nie znaleziono pliku"<<endl;
  124. ifs.imbue(loc);
  125. wstring ws;
  126. getline(ifs, ws);
  127. return ws;
  128. }
  129.  
  130. /**
  131. *
  132. * @param file nazwa pliku
  133. * @param val tresc
  134. * @return zapisuje val do pliku z uwzglednieniem kodowania utf8
  135. */
  136. void writeWString(string file, wstring val) {
  137. locale loc(locale(), new codecvt_utf8<wchar_t>);
  138. wofstream ofs(file);
  139. ofs.imbue(loc);
  140. ofs << val;
  141. }
  142. /**
  143. *
  144. * @param file nazwa pliku
  145. * @param val tresc
  146. * @return zapisuje val do pliku
  147. */
  148. void writeString(string file, string val) {
  149. ofstream ofs(file);
  150. ofs << val;
  151. }
  152. /**
  153. *
  154. * @param file nazwa pliku
  155. * @return zwraca tresc pliku
  156. */
  157. string readString(string file) {
  158. ifstream ifs(file);
  159. if(ifs.fail())cout<<"Nie znaleziono pliku"<<endl;
  160. string ws;
  161. getline(ifs, ws);
  162. return ws;
  163. }
  164.  
  165.  
  166. int main() {
  167. setlocale(LC_ALL,"Polish");
  168. int c;
  169. string f1, f2;
  170. cout<<"Podaj nazwe pliku tlumaczonego"<<endl;
  171. cin>>f1;
  172. cout<<"Podaj nazwe pliku do ktorego chcesz przetlumaczyc"<<endl;
  173. cin>>f2;
  174. cout<<"Wybierz opcje"<<endl<<"1.Z morse'a na alfabet"<<endl<<"2.Z alfabetu na morse'a"<<endl;
  175. cin>>c;
  176. switch (c) {
  177. case 1:
  178. writeWString(f2, decode(readString(f1)));
  179. break;
  180. case 2:
  181. writeString(f2, encode(readWString(f1)));
  182. break;
  183. default:
  184. cout<<"Zła wartosc"<<endl;
  185. }
  186. return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement