Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <codecvt>
- #include <fstream>
- #include <locale.h>
- using namespace std;
- const int LEN = 40;
- //MORSE
- string morse[LEN] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
- "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".-.-",
- "-.-..", "..-..", ".-..-", "--.--", "---.", "...-...", "--..-.", "--..-", ".-.-.-", "--..--",
- "..--..", "-.-.--","/"};
- //ALFABET
- wstring alpha = L"abcdefghijklmnopqrstuvwxyząćęłńóśżź.,?! ";
- wstring alphaB = L"ABCDEFGHIJKLMNOPQRSTUVWXYZĄĆĘŁŃÓŚŻŹ.,?! ";
- /**
- *
- * @param val znak
- * @return Zwraca indeks znaku z alfabetu
- */
- int getID(wchar_t val) {
- for (int i = 0; i < LEN; i++) {
- if (alpha[i] == val)return i;
- if (alphaB[i] == val)return i;
- }
- return -1;
- }
- /**
- *
- * @param val sekwencja morsea
- * @return Zwraca indeks znaku z morsea
- */
- int getID(string val) {
- for (int i = 0; i < LEN; i++) {
- if (morse[i] == val)return i;
- }
- return -1;
- }
- /**
- *
- * @param str tekst
- * @param c znak
- * @return zwraca ilosc wystapien znaku c ze stringa str
- */
- int getAmountOfChar(string str, char c) {
- int tmp = 0;
- for (int i = 0; i < str.length(); i++)
- if (str[i] == c)tmp++;
- return tmp;
- }
- /**
- *
- * @param str tekst do rozdzielenia
- * @param len ilosc elementow w tablicy
- * @return rozdziela stringa na tablice po spacji
- */
- string *split(string str, int &len) {
- char delimiter = ' ';
- len = getAmountOfChar(str, delimiter) + 1;
- string *tmp = new string[len];
- stringstream ss;
- int id = 0;
- for (int i = 0; i < str.length(); i++) {
- if (str[i] == delimiter) {
- tmp[id++] = ss.str();
- ss.str("");
- } else {
- ss << str[i];
- }
- }
- tmp[id] = ss.str();
- return tmp;
- }
- /**
- *
- * @param str kod morsea
- * @return zwraca tekst w normalnym alfabecie
- */
- wstring decode(string str) {
- wstringstream ss;
- int len = 0;
- string *s = split(str, len);
- for (int i = 0; i < len; i++) {
- int id = getID(s[i]);
- if (id != -1) {
- ss << alpha[id];
- }
- }
- return ss.str();
- }
- /**
- *
- * @param str tekst
- * @return zwraca zakodowany kod morsea
- */
- string encode(wstring str) {
- stringstream ss;
- for (int i = 0; i < str.size(); i++) {
- int id = getID((str[i]));
- if (id != -1) {
- ss << morse[id] << " ";
- }
- }
- return ss.str();
- }
- /**
- *
- * @param file nazwa pliku
- * @return zwraca tresc pliku z uwzglednieniem kodowania utf8
- */
- wstring readWString(string file) {
- locale loc(locale(), new codecvt_utf8<wchar_t>);
- wifstream ifs(file);
- if(ifs.fail())cout<<"Nie znaleziono pliku"<<endl;
- ifs.imbue(loc);
- wstring ws;
- getline(ifs, ws);
- return ws;
- }
- /**
- *
- * @param file nazwa pliku
- * @param val tresc
- * @return zapisuje val do pliku z uwzglednieniem kodowania utf8
- */
- void writeWString(string file, wstring val) {
- locale loc(locale(), new codecvt_utf8<wchar_t>);
- wofstream ofs(file);
- ofs.imbue(loc);
- ofs << val;
- }
- /**
- *
- * @param file nazwa pliku
- * @param val tresc
- * @return zapisuje val do pliku
- */
- void writeString(string file, string val) {
- ofstream ofs(file);
- ofs << val;
- }
- /**
- *
- * @param file nazwa pliku
- * @return zwraca tresc pliku
- */
- string readString(string file) {
- ifstream ifs(file);
- if(ifs.fail())cout<<"Nie znaleziono pliku"<<endl;
- string ws;
- getline(ifs, ws);
- return ws;
- }
- int main() {
- setlocale(LC_ALL,"Polish");
- int c;
- string f1, f2;
- cout<<"Podaj nazwe pliku tlumaczonego"<<endl;
- cin>>f1;
- cout<<"Podaj nazwe pliku do ktorego chcesz przetlumaczyc"<<endl;
- cin>>f2;
- cout<<"Wybierz opcje"<<endl<<"1.Z morse'a na alfabet"<<endl<<"2.Z alfabetu na morse'a"<<endl;
- cin>>c;
- switch (c) {
- case 1:
- writeWString(f2, decode(readString(f1)));
- break;
- case 2:
- writeString(f2, encode(readWString(f1)));
- break;
- default:
- cout<<"Zła wartosc"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement