The task is to define the class ‘RleCodec’ for run-length encoding.
About implementing the virtual function:
We have the base class ‘Codec’ as an interface. The member functions in ‘Codec’ are pure virtual functions. Therefore we need to implement those virtual functions in the derived class ‘RleCodec’. The functions ‘decode’, ‘show’, ‘is_encoded’ are already done. The only function you need to complete is ‘RleCodec::encode’ in ‘function.cpp’.
In ‘main.cpp’, we see two functions having an argument of type ‘Codec&’:
std::ostream& operator<<(std::ostream& os, Codec& data);
void encode_decode(Codec& data);
Since ‘RleCodec’ is a derived class of ‘Codec’, we may pass an object of ‘RleCodec’ to the above two functions by reference as if it is an object of ‘Codec’. Calling ‘data.show(os);’ will invoke the virtual function of the corresponding derived class.
About run-length encoding:
The rule of run-length encoding is simple: Count the number of consecutive repeated characters in a string, and replace the repeated characters by the count and a single instance of the character. For example, if the input string is ‘AAADDDDDDDBBGGGGGCEEEE’, its run-length encoding will be ‘3A7DBB5GC4E’, because there are three A’s, seven D’s, … etc. Note that we do not need to encode runs of length one or two, since ‘2B’ and ‘1C’ are not shorter than ‘BB’ and ‘C’.
In ‘function.h’, we add the class ‘DummyCodec’ as a sample of implementing a derived class of the base class ‘Codec’. You do not need to change anything in ‘function.h’. The only function you need to write for this problem is the function ‘RleCodec::encode’ in ‘function.cpp’.
Hint: std::stringstream could be useful in solving this problem. Please refer to ‘RleCodec::decode’ for how to use std::stringstream.
You only need to submit ‘function.cpp’. OJ will compile it with ‘main.cpp’ and ‘function.h’.
We have already provided partial function.cpp belowed.
Note that if you can't use "auto".
For codeblock, go to the codeblock menu Settings --> Compiler ... --> Compiler flags and check Have g++ follow the C++11 ISO C++ language standard [-std=c++11]
For command line compiler, use g++ myprog.cpp -std=c++11 -o myprog
main.cpp
#include <iostream>
#include "function.h"
std::ostream& operator<<(std::ostream& os, Codec& data)
{
data.show(os);
return os;
}
void encode_decode(Codec & data)
{
if (!data.is_encoded())
data.encode();
else
data.decode();
}
int main()
{
std::string input_string;
std::cin >> input_string;
DummyCodec dummy{input_string};
encode_decode(dummy);
std::cout << "Dummy encoding: ";
std::cout << dummy << std::endl;
encode_decode(dummy);
std::cout << "Dummy decoding: ";
std::cout << dummy << std::endl;
RleCodec rle{input_string};
encode_decode(rle);
std::cout << "RLE encoding: ";
std::cout << rle << std::endl;
encode_decode(rle);
std::cout << "RLE decoding: ";
std::cout << rle << std::endl;
}
function.h
#ifndef _FUNC_H
#define _FUNC_H
#include <iostream>
#include <string>
class Codec {
public:
virtual void encode() = 0;
virtual void decode() = 0;
virtual ~Codec() { } // Do nothing
virtual void show(std::ostream& os) const = 0;
virtual bool is_encoded() const = 0;
};
class DummyCodec: public Codec {
public:
DummyCodec(std::string s): encoded{false}, code_str{s} { }
void encode() {
encoded = true;
}
void decode() {
encoded = false;
}
void show(std::ostream& os) const {
os << code_str;
}
bool is_encoded() const { return encoded; }
private:
bool encoded;
std::string code_str;
};
class RleCodec: public Codec
{
public:
RleCodec(std::string s): encoded{false}, code_str{s} { }
void encode();
void decode();
void show(std::ostream& os) const {
os << code_str;
}
bool is_encoded() const { return encoded; }
private:
bool encoded;
std::string code_str;
};
#endif
function.cpp
void RleCodec::encode()
{
// Your code here
encoded = true;
}
void RleCodec::decode()
{
std::stringstream os;
std::string int_str;
for (auto c : code_str) {
if (std::isdigit(c)) {
int_str.push_back(c);
} else {
int cnt = 0;
std::stringstream is{int_str};
is >> cnt;
if (cnt==0) {
os << c;
} else {
for (int i=0; i<cnt; ++i)
os << c;
}
int_str.clear();
}
}
code_str = os.str();
encoded = false;
}
A line contains of several characters .
There are four lines.
The first and second lines are dummy encoding and decoding. You don't need to implement it.
The third and forth lines are RLE encoding and decoding.
Each line is followed by a new line character.