mirror of
https://bitbucket.org/Mattrixwv/cipherstream.git
synced 2025-12-06 18:33:58 -05:00
Created Morse Code encoder/decoder
This commit is contained in:
34
Headers/Morse.hpp
Normal file
34
Headers/Morse.hpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
//Ciphers/Headers/Morse.hpp
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 5-1-18
|
||||||
|
//Modified: 5-1-18
|
||||||
|
//This file contains the declaration of the Morse class
|
||||||
|
//This class is designed to translate Morse Code into regular letters and numbers
|
||||||
|
|
||||||
|
#ifndef MORSE_HPP
|
||||||
|
#define MORSE_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
|
class Morse{
|
||||||
|
private:
|
||||||
|
static std::string code[];
|
||||||
|
std::stringstream inputString;
|
||||||
|
std::string outputString;
|
||||||
|
std::string encode();
|
||||||
|
std::string decode();
|
||||||
|
public:
|
||||||
|
Morse();
|
||||||
|
~Morse();
|
||||||
|
void setEncodeInputString(std::string input);
|
||||||
|
void setDecodeInputString(std::string input);
|
||||||
|
std::string getInputString() const;
|
||||||
|
std::string getOutputString() const;
|
||||||
|
std::string encode(std::string input);
|
||||||
|
std::string decode(std::string input);
|
||||||
|
void reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //MORSE_HPP
|
||||||
139
SourceFiles/Morse.cpp
Normal file
139
SourceFiles/Morse.cpp
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
//Ciphers/SourceFiles/Morse.cpp
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 5-1-18
|
||||||
|
//Modified: 5-1-18
|
||||||
|
//This file contains the implementation of the Morse class
|
||||||
|
|
||||||
|
|
||||||
|
#include "../Headers/Morse.hpp"
|
||||||
|
#include <string>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
|
|
||||||
|
std::string Morse::code[] {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L
|
||||||
|
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", //M-Z
|
||||||
|
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."}; //0-9
|
||||||
|
|
||||||
|
Morse::Morse(){
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
Morse::~Morse(){
|
||||||
|
}
|
||||||
|
|
||||||
|
void Morse::setEncodeInputString(std::string input){
|
||||||
|
//Make sure the input is empty to begin
|
||||||
|
std::string temp;
|
||||||
|
//Step through every element in input, removing anything that is not a letter and making all letters uppercase
|
||||||
|
for(int cnt = 0;cnt < input.size();++cnt){
|
||||||
|
char letter = input[cnt];
|
||||||
|
//If it is a letter or number add it to the inputString, turning all letters capital
|
||||||
|
if(isalnum(letter)){
|
||||||
|
temp += toupper(letter);
|
||||||
|
}
|
||||||
|
//If letter is not a letter or number ignore it (This removes punctuation and whitespace)
|
||||||
|
}
|
||||||
|
inputString.clear();
|
||||||
|
inputString.str(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Morse::setDecodeInputString(std::string input){
|
||||||
|
std::string temp;
|
||||||
|
//Step through every element in input, removing everything except . or -
|
||||||
|
for(int cnt = 0;cnt < input.size();++cnt){
|
||||||
|
char letter = input[cnt];
|
||||||
|
//If it is a letter or number add it to the inputString, turning all letters capital
|
||||||
|
if(letter == '.' || letter == '-' || letter == ' '){
|
||||||
|
letter = toupper(letter);
|
||||||
|
temp += letter;
|
||||||
|
}
|
||||||
|
//If letter is not a letter or number ignore it (This removes punctuation and whitespace)
|
||||||
|
}
|
||||||
|
inputString.clear();
|
||||||
|
inputString.str(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Morse::getInputString() const{
|
||||||
|
return inputString.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Morse::getOutputString() const{
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Morse::encode(){
|
||||||
|
outputString = "";
|
||||||
|
std::string temp = inputString.str();
|
||||||
|
//Loop through every element in the input string and see what type it is
|
||||||
|
//while(!inputString.eof()){
|
||||||
|
for(int cnt = 0;cnt < temp.size();++cnt){
|
||||||
|
char letter;
|
||||||
|
//Get the next character in the input
|
||||||
|
letter = temp[cnt];
|
||||||
|
//If it is a letter get the correct combination from code and add it to the outputString
|
||||||
|
if(isupper(letter)){
|
||||||
|
outputString += code[letter - 65] + " ";
|
||||||
|
}
|
||||||
|
//If it is a number get the correct combination from code and add it to the outputString
|
||||||
|
else if(isdigit(letter)){
|
||||||
|
std::string temp = "";
|
||||||
|
temp += letter;
|
||||||
|
outputString += code[std::stoi(temp) + 26] + " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Remove the final space from the output
|
||||||
|
if(outputString.size() > 0){
|
||||||
|
outputString.erase(outputString.end() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Morse::encode(std::string input){
|
||||||
|
setEncodeInputString(input);
|
||||||
|
return encode();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Morse::decode(){
|
||||||
|
outputString = "";
|
||||||
|
//Loop until you reach the end of the input
|
||||||
|
while(!inputString.eof()){
|
||||||
|
std::string current;
|
||||||
|
bool found = false;
|
||||||
|
//Get the next string of characters from the code
|
||||||
|
inputString >> current;
|
||||||
|
//Loop through code and see if current is a letter
|
||||||
|
for(int cnt = 0;(cnt < 26) && (!found);++cnt){
|
||||||
|
//See if current is the same as an element in code
|
||||||
|
if(current == code[cnt]){
|
||||||
|
//Add 65 to cnt to get the correct capital letter
|
||||||
|
outputString += (char)(65 + cnt);
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Loop through code and see if current is a number
|
||||||
|
for(int cnt = 26;(cnt < 36) && (!found);++cnt){
|
||||||
|
if(current == code[cnt]){
|
||||||
|
//Remove 26 from cnt to get the correct number
|
||||||
|
outputString += std::to_string((cnt - 26));
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//If it is neither print an error in the output
|
||||||
|
if(!found){
|
||||||
|
outputString += "<Unknown symbol: " + current + ">";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Morse::decode(std::string input){
|
||||||
|
setDecodeInputString(input);
|
||||||
|
return decode();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Morse::reset(){
|
||||||
|
inputString.str("");
|
||||||
|
outputString = "";
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user