Guest User

Untitled

a guest
May 20th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <string>
  3. #include <fstream>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. struct node {
  8.     /* vrchol stromu  */
  9.     char letter;  /* pismeno vo vrchole alebo '?' */
  10.     node * dot;   /* kod predlzeny o bodku */
  11.     node * dash;  /* kod predlzeny o ciarku */
  12. };
  13.  
  14. void addCode(char letter, string code, node *root) {
  15.   /* Do stromu s koreňom root pridá kód uložený v reťazci code
  16.    * ktorý zodpovedá písmenu letter. */
  17.     node *current;
  18.     current = root;
  19.     for (int i = 0; i < code.length(); i++){
  20.         if (code[i] == '.') {
  21.             if( current->dot != NULL) {
  22.                 current = current->dot;            
  23.             } else {
  24.                 current->dot = new node;
  25.                 current = current->dot;
  26.                 current->dot = NULL;
  27.                 current->dash = NULL;
  28.             }      
  29.        
  30.         }
  31.                 if (code[i] == '-') {
  32.             if( current->dash != NULL) {
  33.                 current = current->dash;            
  34.             } else {
  35.                 current->dash = new node;
  36.                 current = current->dash;
  37.                 current->dot = NULL;
  38.                 current->dash = NULL;
  39.             }      
  40.        
  41.         }
  42.    
  43.    
  44.     }
  45.     current->letter = letter;
  46. }
  47.  
  48. char findLetter(string message, int &pos, node *root) {
  49.   /* V strome s koreňom root nájde písmeno zodpovedajúce kódu,
  50.    * ktorý v texte message začína na pozícii pos.
  51.    * Toto písmeno vráti ako výsledok funkcie.
  52.    * Ak takéto písmeno neexistuje, vráti znak '?'.
  53.    * Pozíciu pos posunie na najbližšiu medzeru za kódom alebo za
  54.    * posledné písmeno textu, ak za kódom je už koniec textu. */
  55.     string code;
  56.     node *current;
  57.     int pom;
  58.     char temp;
  59.     int counter = 0;
  60.  
  61.     while ((message[pos] = ' ') && ( pos < message.length() - 1)){
  62.         counter++;
  63.         pos++;
  64.         temp = message[pos];  
  65.        
  66.         if (counter == 2){return ' ';}
  67.     }
  68.     while ((temp != ' ') && ( pos < message.length() - 1)){
  69.         code += temp;
  70.         pos++;
  71.         temp = message[pos];    
  72.     }
  73.    
  74.     for (int i=0; i < code.length(); i++){
  75.         if (code[i] == '.') {
  76.            if (current->dot != NULL){
  77.                current= current->dot;
  78.            } else {
  79.                return '?';
  80.            }        
  81.         }
  82.         if (code[i] == '-') {
  83.            if (current->dash != NULL){
  84.                current= current->dash;
  85.            } else {
  86.                return '?';
  87.            }        
  88.         }      
  89.     }
  90.     if (pos == message.length() -1){pos++;};
  91.    
  92.     return current->letter;
  93.    
  94. }
  95. int main(int argc, char** argv) {
  96.     node *root = new node;
  97.     root->dash = NULL;
  98.     root->dot = NULL;
  99.     int pos = 0;
  100.     string tempstr;
  101.     char letter;
  102.     string code;
  103.     string zadanie;
  104.     string vysledok;
  105.     ifstream vstup;
  106.     vstup.open("morse.txt");
  107.     if(vstup.is_open()){
  108.         while (vstup.good()){
  109.            
  110.             vstup>>letter;
  111.  
  112.             vstup>>code;
  113.             addCode(letter, code, root);                
  114.         }    
  115.     }
  116.    
  117.     getline(cin, zadanie);
  118.    
  119.     while (pos < zadanie.length()) {
  120.         vysledok += findLetter(zadanie, pos, root);  
  121.         cout << vysledok << endl;
  122.     }
  123.    
  124.    
  125.     return 0;
  126. }
Add Comment
Please, Sign In to add comment