Undo and Redo (Optional)

Time limit: 2000ms
Memory limit: 256mb

Description:
In this exercise, we use two stacks to implement a struct text, which support undo and redo. For text, you can undo the character that you have added. Or, you may want to redo the charcter that you have just undone.

For example, given a text 'a','b','c','d','e','f','g'. By performing following sequence of redo and undo, you will have:

	1. undo	on 'a','b','c','d','e','f','g' ----> 'a','b','c','d','e','f'
	2. undo	on 'a','b','c','d','e','f' ----> 'a','b','c','d','e'
	3. undo	on 'a','b','c','d','e' ----> 'a','b','c','d'
	4. redo	on 'a','b','c','d' ----> 'a','b','c','d','e'
	5. redo	on 'a','b','c','d','e' ----> 'a','b','c','d','e','f'


You are required to complete two functions to support undo and redo for struct text.

void undo(text* t); 
Performs undo operation and returns a new text sequence.

void redo(text* t);
Performs redo operation and returns a new text sequence.

Notice: we use 'u' and 'r' to denote "undo" and "redo" operation in the input of the program.


Sample Input 1:
4
abcd
2
uu

Sample Output 1:
Input the number of chars in buf:
Input these chars:
Input the number of undo and redo operations:
Input undo and redo sequence:
ab

Sample Input 2:
7
abcdefg
4
uuur

Sample Output 2:
Input the number of chars in buf:
Input these chars:
Input the number of undo and redo operations:
Input undo and redo sequence:
abcde


Hint:
Be careful of the efficiency issue. 


-------------------------Copy the following code, complete it and submit-------------------------

/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/

#include <stdio.h>
#include <stdlib.h>
#define MAX 100000

typedef enum{FALSE =0, TRUE = 1} Boolean;

typedef struct{
    int size;
    int top;
    char *stacklist;
}stack;

typedef struct{
    stack* undo_stack;
    stack* redo_stack;
}text;

stack *createS(int size){
    stack *s;
    s = (stack*)malloc(sizeof(stack));
    s->size =size;
    s->stacklist = (char*)malloc(size * sizeof(char));
    s->top = -1;
    return s;
}


Boolean IsFull(stack *s){
    if (s->top == s->size -1)
        return TRUE;
    else return FALSE;
}



Boolean IsEmpty(stack *s){
    if (s->top == -1)
        return TRUE;
    else return FALSE;
}


void push(stack *s, char e){
    if (!IsFull(s)){
        s->top++;
        s->stacklist[s->top] = e;
    }
}


char pop(stack *s){
    char i;
    if (!IsEmpty(s)){
        i = s-> stacklist[s->top];
        s->top--;
        return i;
    }else{printf("error\n"); return -1;}
}

char top(stack *s){ return s->stacklist[s->top];}

void undo(text* t)
{
    // perform undo for text
}

void redo(text* t)
{
    // perform redo for text
}

text* createText(char* raw_text, int size){
    int i = 0;
    text *t;
    t = (text*)malloc(sizeof(text));
    t->redo_stack = createS(20000);
    t->undo_stack = createS(20000);
    
    for(i =0; i < size; i++)
    {
        push(t->undo_stack, raw_text[i]);
    }
    
    return t;
}

void printText(text* t)
{
    for (int i = 0; i <= t->undo_stack->top; i++) {
        printf("%c", t->undo_stack->stacklist[i]);
    }
    printf("\n");
}


int main()
{
    char value;
    char buf[MAX];
    int size_n;
    printf("Input the number of chars in buf:\n");
    scanf("%d", &size_n);
    printf("Input these chars:\n");
    scanf("%s", buf);
    text* t = createText(buf, size_n);
    printf("Input the number of undo and redo operations:\n");
    scanf("%d", &size_n);
    printf("Input undo and redo sequence:\n");
    scanf("%s", buf);
    
    int i = 0;
    for (i = 0 ; i < size_n; i++) {
        if (buf[i]=='u') {
            undo(t);
        } else if (buf[i] == 'r'){
            redo(t);
        }
    }
    
    printText(t);

    return 0;
}

-------------------------------------------End of Code-------------------------------------------

Submit