Encoding/decoding morse code
I'm trying to write a program that encodes alphabetical characters from a file into morse code, and vise versa. I have it partially written, but I've confused myself while trying to write the encode and decode functions:
Code:
#include <iostream>
#include <vector>
using namespace std;
class Code
{
public:
Code(); // Default constructor - loads and uses morse code
Code(vector<int> codewords); // constructor loading customized code
string encode(vector<char> message); // encodes a message consisting of A-Z
string decode(vector<string> message); // decodes a message
private:
vector<string> codewords; // this is a codeword vector parallel to A-Z
vector<char> alpha; // this is the vector A-Z
vector<char> alphacode(); // function returns vector - A B C etc.
vector<string> morsecode(); // function returns vector containing morse code
string encode(char x); //returns the codeword for the character x
string decode(string c); //returns the character for the codeword c.
};
Code::Code(){
this->codewords=morsecode();
this->alpha=alphacode();
}
Code::Code(vector<int> codewords)
{
}
string Code::decode(vector<string> message)
{
int w=0;
vector<char> alph=alphacode();
string out;
while(w<message.size()){
string temp = ?
for(int i=0;i<alph.size();i++)
{
if(temp == codewords[i])
{
out += alph[i];
}
w+=temp.size();
}
}
return out;
}
string Code::encode(vector<char> message)
{
//message = morsecode();
//string morse;
}
vector<char> alphacode()
{// This returns a vector containing the alphabet a-z and " "
vector<char> temp;
for (char c='A'; c<='Z'; c++)
temp.push_back(c);
temp.push_back(' ');
temp.push_back('.');
return temp;
}
vector<string> morsecode()
{ // This function returns a vector containing the morse code vector<string> temp(28);
temp[0] =".-";
temp[1] ="-...";
temp[2] ="-.-.";
temp[3] ="-..";
temp[4] =".";
temp[5] ="..-.";
temp[6] ="--.";
temp[7] ="....";
temp[8] ="..";
temp[9] =".---";
temp[10] ="-.-";
temp[11] =".-..";
temp[12] ="--";
temp[13] ="-.";
temp[14] ="---";
temp[15] =".--.";
temp[16] ="--.--";
temp[17] =".-.";
temp[18] ="...";
temp[19] ="-";
temp[20] ="..-";
temp[21] ="...-";
temp[22] =".--";
temp[23] ="-..-";
temp[24] ="-.--";
temp[25] ="--..";
temp[26] =".......";
temp[26] ="x";
return temp;
}
int main()
{
Code t;
vector<string> mess;
char x;
cin >> x;
while(cin.good())
{
mess.push_back(x);
cin>>x;
}
cout << t.decode(mess) << endl;
Code m;
string HW = "Hello";
cout << m.encode(HW) << endl;
}
Any help is much appreciated!