Binary Search Trees II Morse Code.

Slides:



Advertisements
Similar presentations
1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)
Advertisements

1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Binary Search Trees Chapter 6.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
11 Introduction to Object Oriented Programming (Continued) Cats.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
1 Modeling CDs (Continued). 2 Getting Started Download: 2011_02_07_CD_Collection_Incomplete/
1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
STAT 598W Lecture 16 Templates. Templates can be used in function definitions and in class definitions. Roughly, templates are a way to (apparently) overcome.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 Huffman Codes Drozdek Chapter Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
1 Modeling CDs. 2 Objectives You will be able to: Sketch a diagram showing the relationship between a class and classes that it contains a members. Define.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1 The Standard Template Library Drozdek Section 3.7.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
1 Implementing Ticket Printer. Download project from last class Downloads/2016_02_12_In_Class/
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
1 Using an XML Parser. 2 Objective You will be able to use a publically available open source parser to convert an XML file into an internal data structure.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
1 Ugly Realities The Dark Side of C++ Chapter 12.
CSCE 210 Data Structures and Algorithms
Strings: C-strings vs. Strings as Objects
Overloading Operator MySting Example
group work #hifiTeam
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Strings: C-strings vs. Strings as Objects
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
C++ Templates L03 - Iterator 10 – Iterator.
C++ Programming Lecture 20 Strings
Presentation transcript:

Binary Search Trees II Morse Code

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

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; }

Initial Program Running

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: http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/2011_03_07_Morse_Code_BST/

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; } };

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);

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; }

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; }

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;

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); }

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');

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'); }

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

The Morse_Pair BST

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;

Program in Action End of Presentation