Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees II Morse Code.

Similar presentations


Presentation on theme: "Binary Search Trees II Morse Code."— Presentation transcript:

1 Binary Search Trees II Morse Code

2 An Application: Morse Code Table
Let's create a BST to translate ASCII characters into Morse Code symbols. Use genBST1.h template. Will create a new class to be the template parameter. New empty C++ project ASCII_to_Morse_Code

3 Initial main.cpp #include <iostream> using namespace std;
int main() { cout << "Enter an ASCII string to be translated into Morse Code\n"; char buffer[1000]; cin.getline(buffer, 1000); cout << "You entered:\n"; cout << buffer; cin.get(); return 0; }

4 Initial Program Running

5 Class Morse_Code_Pair
Add class Morse_Code_Pair Each object will specify the Morse Code symbol for one ASCII character. We will build a BST of these objects. Use the BST to translate ASCII into Morse Code. Copy source files from:

6 Morse_Code_Pair.h #pragma once #include <iostream>
#include <string> class Morse_Code_Pair { public: std::string symbol; // A Morse code symbol. char value; // The letter or digit represented // by this symbol // Default constructor Morse_Code_Pair(); // Normal constructor Morse_Code_Pair(std::string symbol_, char value_); // Accessor functions std::string get_symbol() const {return symbol; } char get_value() const {return value; } };

7 Morse_Code_Pair.h // Less than operator
bool operator<(const Morse_Code_Pair& lhs, const Morse_Code_Pair& rhs); // Equals operator bool operator==(const Morse_Code_Pair& lhs, // Output operator std::ostream& operator<<(std::ostream& out, const Morse_Code_Pair& s);

8 Morse_Code_Pair.cpp #include "Morse_Code_Pair.h" using namespace std;
// Default constructor Morse_Code_Pair::Morse_Code_Pair() : symbol("", ' ') {} // Normal constructor Morse_Code_Pair::Morse_Code_Pair(string symbol_, char value_) : symbol(symbol_), value(value_) // Less then operator bool operator< (const Morse_Code_Pair& lhs, const Morse_Code_Pair& rhs) { char c_lhs = lhs.get_value(); char c_rhs = rhs.get_value(); return c_lhs < c_rhs; }

9 Morse_Code_Pair.cpp // Equals operator
bool operator==(const Morse_Code_Pair& lhs, const Morse_Code_Pair& rhs) { char c_lhs = lhs.get_value(); char c_rhs = rhs.get_value(); return c_lhs == c_rhs; }

10 Morse_Code_Pair.cpp ostream& operator<<(ostream& out,
const Morse_Code_Pair& s) { string symbol_string = s.get_symbol(); for (size_t i = 0; i < symbol_string.length(); ++i) if (symbol_string[i] == '_') symbol_string[i] = '-'; } out << s.get_value() << " " << symbol_string; return out;

11 main.cpp #include <iostream> #include "genBST1.h"
#include "Morse_Code_Pair.h" using namespace std; // Conservative upper bound on length of a single Morse code symbol const int MAX_SYMBOL_LEN = 10; // Binary Search Tree to map Morse code symbols to characters BST<Morse_Code_Pair> morse_map; void add_symbol_to_map(string symbol, char value) { Morse_Code_Pair sym(symbol, value); morse_map.insert(sym); }

12 build_map In main.cpp: void build_map() {
add_symbol_to_map("....", 'H'); add_symbol_to_map("___..", '8'); add_symbol_to_map("._.", 'R'); add_symbol_to_map("...__", '3'); add_symbol_to_map("_._.", 'C'); add_symbol_to_map("__", 'M'); add_symbol_to_map("..._", 'V'); add_symbol_to_map(".____", '1'); add_symbol_to_map(".....", '5'); add_symbol_to_map("._", 'A'); add_symbol_to_map(".", 'E'); add_symbol_to_map(".___", 'J'); add_symbol_to_map("___", 'O'); add_symbol_to_map("_", 'T'); add_symbol_to_map("_.._", 'X'); add_symbol_to_map(".__", 'W');

13 build_map add_symbol_to_map("_____", '0');
add_symbol_to_map("_...", 'B'); add_symbol_to_map("_..", 'D'); add_symbol_to_map(".._.", 'F'); add_symbol_to_map("..", 'I'); add_symbol_to_map("_._", 'K'); add_symbol_to_map(".__.", 'P'); add_symbol_to_map("_.__", 'Y'); add_symbol_to_map("__...", '7'); add_symbol_to_map("__.", 'G'); add_symbol_to_map("._..", 'L'); add_symbol_to_map("_.", 'N'); add_symbol_to_map("__._", 'Q'); add_symbol_to_map("...", 'S'); add_symbol_to_map(".._", 'U'); add_symbol_to_map("__..", 'Z'); }

14 main.cpp int main() { build_map(); morse_map.display(cout);

15 The Morse_Pair BST

16 main.cpp Add at end of main.cpp:
cout << "\n\nHere is the string in Morse Code:\n"; for (int i = 0; i < (int) strlen(buffer); ++i) { char c = toupper(buffer[i]); Morse_Code_Pair* search_target = new Morse_Code_Pair("", c); Morse_Code_Pair* mcp = morse_map.search(*search_target); if (mcp == 0) cout << " "; } else string next_symbol = mcp->get_symbol(); cout << next_symbol << " "; cout << endl;

17 Program in Action End of Presentation


Download ppt "Binary Search Trees II Morse Code."

Similar presentations


Ads by Google