Task {4} - Word

Здравейте,

 

Не мгоа да напаравя, така че този код : 
 

#include <iostream>
#include <string>
#include <sstream>


#ifndef initialization
#define initialization

#ifndef COMMAND_INTERFACE_H
#define COMMAND_INTERFACE_H

#include <map>
#include <memory>
#include <string>
#include <vector>
#include <sstream>

#include "TextTransform.h"


#define print(n) std::cout << n << std::endl;

using TextTransformPtr = std::shared_ptr<TextTransform>;
using Command = std::pair<std::string, TextTransformPtr>;

class CommandInterface {
private:
    class ToUpperTransform : public TextTransform {
    public:
        virtual void invokeOn(std::string& text, int startIndex, int endIndex) override {
            for (int i = startIndex; i < endIndex; i++) {
                text[i] = toupper(text[i]);
            }
        }
    };

    std::map<std::string, std::shared_ptr<TextTransform> > commandTransforms;
    std::string& text;
public:
    CommandInterface(std::string& text) : text{ text } {}

    void init() {
        this->commandTransforms.clear();
        for (std::pair<std::string, std::shared_ptr<TextTransform> > p : this->initCommands()) {
            commandTransforms[p.first] = p.second;
        }
    }

    void handleInput(std::string input) {
        std::istringstream parseStream(input);

        std::string commandName;
        int startInd, endInd;

        parseStream >> commandName >> startInd >> endInd;

        this->commandTransforms[commandName]->invokeOn(this->text, startInd, endInd);
    }

    virtual ~CommandInterface() = default;

protected:
    virtual std::vector<Command> initCommands() {
        std::vector<Command> commands;

        commands.push_back(Command("uppercase", std::make_shared<ToUpperTransform>()));

        return commands;
    }
};

#endif // COMMAND_INTERFACE_H


std::string unique, cut;

class Initializaiton : public CommandInterface
{
protected:
    class Paste : public TextTransform
    {
    private:

    public:

        virtual void invokeOn(std::string& text, int startIndex, int endIndex) override
        {
            text.replace(startIndex, 1, cut);
        }

    };

    class Cut : public TextTransform
    {
    public:

        virtual void invokeOn(std::string& text, int startIndex, int endIndex) override
        {    
            cut = text.substr(startIndex, endIndex);
            text.erase(startIndex, endIndex); // >= < 
        }
    };
public:
    Initializaiton(std::string& str) : CommandInterface(str)
    {
        unique = str;

    }

    virtual std::vector<Command> initCommands() override
    {

        std::vector<Command> x(CommandInterface::initCommands());

        x.push_back(Command("cut", std::make_shared<Cut>()));
        x.push_back(Command("paste", std::make_shared<Paste>()));

        return x;
    }
};

std::shared_ptr<CommandInterface> buildCommandInterface(std::string& str)
{
    std::shared_ptr<CommandInterface> a(new Initializaiton(str));

    a->init();

    return a;
}

#endif

int main()
{
    std::string text;
    std::getline(std::cin, text);

    std::shared_ptr<CommandInterface> interface = buildCommandInterface(text);

    std::string inputLine;
    std::getline(std::cin, inputLine);
    while (inputLine != "exit") {
        interface->handleInput(inputLine);
        std::getline(std::cin, inputLine);
    }

    std::cout << text << std::endl;

    return 0;
}

Да работи с тези входове :

som3. text
cut 1 7
paste 3 4
exit

 Expected output: sexom3. t

 

abc d e
cut 0 4
uppercase 1 3
paste 1 2
exit

Expected output: dabc E

 

Интересното е, че при втория това място ' ' трябва да го прилепя с abc, понеже "d e" в " ToUpperTransform " класа трябва да превърне само 'Е' в голямо, което ме наведе на мисълта, че този ред код трябва да работи, аз не виждам друг начин по който да го направя да работи, но първия вход противоречи на втория за кдоа ми. Просто не извежда правилният резултат и нямам никакви идеи.

 

Бихте ли ми помогнали ?