Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write this program using the C language please comment which each statement does

ID: 3856369 • Letter: W

Question

write this program using the C language

please comment which each statement does

use the following structure: typedef struct word_

{

char word[10];

struct word_* nextWord;

struct word_* lastWord;

} Word;

you will be impementing the undo and redo functionality, although it will be clunky. you will input a word at a time to form a sentence, then have the option to undo and redo the last word put it. you will only have to redo the last word that is undone. this functionality is in the form of a stack.

Function Prototypes

Word* addWord(Word* lastWord, Word* newWord);

this function will adds the newest word to the stack of words the last word passed into this function is the top of the stack. return the newest top of the stack after

void printSentence(Word* lastWord);

prints out the sentence as one would read it. be careful the last word (the top of the stack) is the parameter to this function

void freeSentence(Word* lastWord);

frees every word in the sentence

Word* undoWord(Word** lastWordPtr);

takes out the last word entered from the stack and returns it. update the new stack after

sample output

1: add word

2: undo

3: redo

4: quit

option: 2

nothing to undo

nothing to print

1: add word

2: undo

3: redo

4: quit

option: 1

enter your word: This

This

1: add word

2: undo

3: redo

4: quit

option:1

enter your word: is

This is

1: add word

2: undo

3: redo

4: quit

option:2

This

1: add word

2: undo

3: redo

4: quit

option:3

This is

1: add word

2: undo

3: redo

4: quit

option:4

Exiting...

Explanation / Answer

typedef struct Word_t {

int dirty;

int len, size, offset, eoff;

char *Word;

char *c;

} Word_t;

typedef struct file_t {

char *name;

line_t **lines;

int line_cnt;

int cur_x, cur_y;

int tab_width;

int text_selected, select_start_row, select_end_row, select_start_col, select_end_col;

int line_offset, cur_row, cur_col;

int last_indent;

} Word_t;

#include <stack>

#include <iostream>

#include <memory>

// ----- the Command Interface -----

class ICommand {

public:

virtual void execute() = 0;

virtual void undo() = 0;

virtual void redo() = 0;

};

class Tv {

bool mOn;

int mChannel;

public:

Tv() {}

void switchOn() { mOn = true; }

void switchOff() { mOn = false; }

  

void switchChannel(int channel) {

mChannel = channel;

}

  

bool isOn() { return mOn; }

int getChannel() { return mChannel; }

};

class TvOnCommand : public ICommand {

Tv *mTv;

public:

TvOnCommand(Tv &tv):mTv(&tv) {}

void execute() { mTv->switchOn(); }

void undo() { mTv->switchOff();}

void redo() { mTv->switchOn(); }

};

class TvOffCommand : public ICommand {

TvOnCommand mTvOnCommand;

public:

TvOffCommand(Tv &tv):mTvOnCommand(tv) {}

void execute() { mTvOnCommand.undo(); }

void undo() { mTvOnCommand.execute(); }

void redo() { mTvOnCommand.undo(); }

};

class TvSwitchChannelCommand : public ICommand {

Tv *mTv;

int mOldChannel;

int mNewChannel;

public:

TvSwitchChannelCommand(Tv *tv, int channel) {

mTv = tv;

mNewChannel = channel;

}

void execute() {

mOldChannel = mTv->getChannel();

mTv->switchChannel(mNewChannel);

}

void undo() {

mTv->switchChannel(mOldChannel);

}

void redo() {

mTv->switchChannel(mNewChannel);

}

};

typedef std::stack<std::shared_ptr<ICommand> > commandStack_t;

class CommandManager {

commandStack_t mUndoStack;

commandStack_t mRedoStack;

public:

CommandManager() {}

void executeCmd(std::shared_ptr<ICommand> command) {

mRedoStack = commandStack_t();

command->execute();

mUndoStack.push(command);

}

void undo() {

if (mUndoStack.size() <= 0) {

return;

}

mUndoStack.top()->undo();

mRedoStack.push(mUndoStack.top());

mUndoStack.pop();

}

  

void redo() {

if (mRedoStack.size() <= 0) {

return;

}

mRedoStack.top()->redo();

mUndoStack.push(mRedoStack.top());

mRedoStack.pop();   

}

};

int main() {

using namespace std;

Tv tv;

CommandManager commandManager;

std::shared_ptr<ICommand> c1(new TvSwitchChannelCommand(&tv, 1));

commandManager.executeCmd(c1);

std::cout << "switched to channel " << tv.getChannel() << std::endl;

  

std::shared_ptr<ICommand> c2(new TvSwitchChannelCommand(&tv, 2));

commandManager.executeCmd(c2);

std::cout << "switched to channel: " << tv.getChannel() << std::endl;

  

std::shared_ptr<ICommand> c3(new TvSwitchChannelCommand(&tv, 3));

commandManager.executeCmd(c3);

std::cout << "switched to channel: " << tv.getChannel() << std::endl;

  

std::cout << "undoing..." << std::endl;

commandManager.undo();

std::cout << "current channel: " << tv.getChannel() << std::endl;

  

std::cout << "undoing..." << std::endl;

commandManager.undo();

std::cout << "current channel: " << tv.getChannel() << std::endl;

  

std::cout << "redoing..." << std::endl;

commandManager.redo();

std::cout << "current channel: " << tv.getChannel() << std::endl;

  

std::cout << "redoing..." << std::endl;

commandManager.redo();

std::cout << "current channel: " << tv.getChannel() << std::endl;

return 0;

}