Advertisement
Guest User

morse

a guest
Jan 29th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctype.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void Skaityti(string& zinute)
  9. {
  10.     string temps;
  11.     ifstream fin("input.txt");
  12.     while (getline(fin, temps))
  13.     {
  14.         zinute += temps;
  15.     }
  16. }
  17. void Koduoti(const string& zinute, string& uzsifruota1, string& uzsifruota2)
  18. {
  19.     string uzsifruota = "";
  20.     char symbols[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'Y', 'J', 'K', 'L',
  21.                        'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z',
  22.                        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '•', ',', '?', '!', '-', '"', ';', ':' };
  23.     string morze[] = { "•-", "-•••", "-•-•", "-••", "•", "••-•", "--•", "••••", "••",
  24.                       "-•--", "•---", "-•-", "•-••", "--", "-•", "---", "•--•", "--•-", "•-•", "•••",
  25.                       "-", "••-", "•••-", "•--", "-••-", "--••", "-----", "•----",
  26.                       "••---", "•••--", "••••-", "•••••", "-••••", "--•••", "---••", "----•", "••••••", "•-•-•",
  27.                       "••-••", "--••--", "-••••-", "•-••-•", "-•-•-•", "---•••" };
  28.     for (auto x : zinute) //koduoti paprastai
  29.     {
  30.         for (int i = 0; i < 45; i++)
  31.         {
  32.             if (x == symbols[i] || (isalpha(x) && toupper(x) == symbols[i]))
  33.             {
  34.                 uzsifruota1 += morze[i] + " ";
  35.                 break;
  36.             }
  37.             if (x == ' ')
  38.             {
  39.                 uzsifruota1 += " ";
  40.                 break;
  41.             }
  42.         }
  43.     }
  44.     bool pora = false;
  45.     string temps = "";
  46.     for (int j = 0; j < zinute.length(); j++) //koduoti sukeiciant salia raides
  47.     {
  48.         for (int i = 0; i < 45; i++)
  49.         {
  50.             if (zinute[j] == symbols[i] || (isalpha(zinute[j]) && toupper(zinute[j]) == symbols[i]))
  51.             {
  52.                 if (j + 1 < zinute.length())
  53.                 {
  54.                     uzsifruota2 += morze[i];
  55.                 }
  56.                 else
  57.                 {
  58.                     temps = morze[i] + " " + temps;
  59.                     if (pora)
  60.                     {
  61.                         uzsifruota2 += temps;
  62.                         pora = false;
  63.                         temps = "";
  64.                     }
  65.                 }
  66.                 break;
  67.             }
  68.             if (zinute[j] == ' ')
  69.             {
  70.                 uzsifruota1 += " ";
  71.                 pora = false;
  72.                 break;
  73.             }
  74.         }
  75.     }
  76. }
  77. void Spausdinti(const string& zinute)
  78. {
  79.     ofstream fout("output.txt", ios::app);
  80.     fout << zinute << endl << endl;
  81. }
  82.  
  83. int main()
  84. {
  85.     string zinute = "";
  86.     string uzsifruota1, uzsifruota2;
  87.     Skaityti(zinute);
  88.     Koduoti(zinute, uzsifruota1, uzsifruota2);
  89.     Spausdinti(uzsifruota1); //paprastai
  90.     Spausdinti(uzsifruota2); //sukeiciant
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement