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 133 134 135 136 137 138
|
#pragma warning(disable : 4996)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <string>
#include <cctype>
#include<stdlib.h>
using namespace std;
struct TREENODE {
char letter;
TREENODE* left;
TREENODE* right;
TREENODE() {
letter = '*';
left = 0;
right = 0;
}
};
struct MORSECODE {
char letter;
char code[27];
};
class TELEGRAPH {
private:
static MORSECODE table[40];
static TREENODE* root;
static void destroyTree(TREENODE* NODE);
public:
TELEGRAPH() {
root = NULL;
}
static void buildTree();
static void destroyTree();
void Encode(char text[], char morse[]);
void Decode(char morse[], char text[]);
};
MORSECODE TELEGRAPH::table[40] = {
{"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 = 0;
void TELEGRAPH::Decode(char morse[], char text[]) {
char* morsePtr;
TREENODE* node;
node = root;
cout << "Decode called" << endl;
for (morsePtr = morse; *morsePtr; morsePtr++) {
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[]) {
int i;
char c, *t, *morsePtr, *morse;
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[80], morse[600];
station.buildTree();
cout << "\enter telegram in english";
cin.getline(text, 80);
station.Encode(text, morse);
cout << morse;
cout << "message received";
station.Decode(morse, text);
cout << "message sent: " << text << endl;
station.destroyTree();
}
|