1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#pragma comment(lib,"d3d9.lib")
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <string>
#include <cctype>
#include<stdlib.h>
using namespace std;
struct TREENODE {//constructor
char letter;//to be replaced by a letter
TREENODE* left;//declare pointer
TREENODE* right;//declare pointer
TREENODE() {
letter = ' ';
left = 0;
right = 0;
}
};
struct MORSECODE {//each morse code object has
char letter;//english letters
char code[100];//dots and dashed
};
class TELEGRAPH {//binary tree
private:
static MORSECODE table[100];
static TREENODE* root;
static void destroyTree(TREENODE *NODE);
public:
TELEGRAPH() {
root = NULL;
}
static void buildTree();
void Encode(char text[], char morse[]);
void Decode(char morse[], char text[]);
};
MORSECODE TELEGRAPH::table[100] = {
{'E', ". " }, { 'T', "- " }, { 'I', ".. " }, { 'A', ".- " }, { 'N', "-. " }, { 'M', "-- " },
{'S', "... "}, {'U', "..- "}, {'R', ".-. "}, {'W', ".-- "}, {'D', "-.. "}, {'K', "-.- "},
{'G', "--. "}, {'O', "--- "}, {'H', ".... "}, {'V', "...- "}, {'F', "..-. "}, {'L', ".-.. "},
{'P', ".--. "}, {'J', ".--- "}, {'B', "-... "}, {'X', "-..- "},
{'C', "-.-. "}, {'Y', "-.-- "}, {'Z', "--.. "}, {'Q', "--.- "}, {' ', "---- "}
};
TREENODE *TELEGRAPH::root = NULL;
void TELEGRAPH::Decode(char morse[], char text[]) {
char* morsePtr;//points to the dots and dashes in the table
TREENODE* node;//declare pointer
node = root;
cout << "Decode called" << endl;
for (morsePtr = morse; *morsePtr; morsePtr++) {//goes through the morse code that letter/symbol
if (*morsePtr != ' ') {
if (*morsePtr == '.') {
node = node->left;
}
else if (*morsePtr == '-') {
node = node->right;
}
}
else {
node = root;
}
continue;
}
*text++ = node->letter;
return;
}
void TELEGRAPH::Encode(char text[], char morse[]) {
int i;
char c, * t, * morsePtr;//goes through the morse code for that letter/symbol
cout << "encode called" << endl;
cout << "\sending >>> ";
for (t = text; *t; t++) {
c = toupper(*t);
if (c == ' ') {
*morse++ =' ';
continue;
}
for (i = 0; table[i].letter; i++) {
if (table[i].letter == c)break;
}
if (!table[i].letter) {
continue;
}
morsePtr = table[i].code;
while (*morsePtr) {
*morse++ = *morsePtr++;
}
*morse++ = ' ';
}
};
void TELEGRAPH::buildTree() {
TREENODE* node, * nextNode;
char* morsePtr;
root = new TREENODE;
if (!root) {
return;
}
root->letter = ' ';
cout << "alphabet in morse";
for (int i = 0; table[i].letter; i++) {
node = root;
for (morsePtr = table[i].code; *morsePtr; morsePtr++) {
if (*morsePtr == ' ') {
cout << *morsePtr;
nextNode = new TREENODE;
node->right = nextNode;
node = node->right;
}
else if (*morsePtr == '.') {
cout << *morsePtr;
nextNode = new TREENODE;
node->left = nextNode;
node = node->left;
}
}
}
};
int main() {
TELEGRAPH station;
char text[100], morse[600];
station.buildTree();
cout << "\enter sentence in english";
cin.getline(text, 80);
station.Encode(text, morse);
cout << morse;
cout << "message received";
station.Decode(morse, text);
cout << "message sent: " << text << endl;
}
|