I don't understand what my assignment is asking me to do?

Apr 13, 2020 at 5:33am
Here is my assignment below:

Design and write a C++ program that accepts input from a file and uses an STL stack of characters to process a line of text in the manner of a text editor. Use the following outline to design the pseudocode for your algorithm:

In a loop, read a single character at a time from a stream of characters until the file’s end is reached. If the current character is #, simply pop the stack; if the current character is $, read the next character and push that onto the stack; if the current character is @, then pop characters off the stack until a space is reached; otherwise, push the current character Use the standard operations of an STL stack for this purpose. Read each character and make a decision to push or pop or clear the stack. At the end, print out the residual string with the original ordering of letters (you must first reverse the contents of the stack by copying it to another stack).

(Above the assignment, in summary, it says "text editors have an erase character, for example '#', some text editors have a kill character, we will use '@' as the kill character, and our editor will have a replace character, which is '$'. )

The assignment comes with a file that is basically a long story with said special characters sprinkled in between it. I am NOT asking for my homework to be done for me - however, I don't really understand what my teacher wants. I would appreciate an explanation. Thank you very much.
Apr 13, 2020 at 6:06am
In a loop, read a single character at a time from a stream of characters until the file’s end is reached. If the current character is #, simply pop the stack; if the current character is $, read the next character and push that onto the stack; if the current character is @, then pop characters off the stack until a space is reached; otherwise, push the current character
This sounds fairly straightforward to me. What don't you understand?

Let's start with something basic. Can you write a function that implements what the excerpt I copied says?
1
2
3
void process_character(char c, std::stack<char> &stack){
    //...
}
Apr 13, 2020 at 6:34am
Can you write a function that implements what the excerpt I copied says?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
void process_characer(char c, stack<char> &stack){
if(c == '#'){
  stack.pop()
}
else if (c == '$'){
  stack.push(c + 1); //Not a hundred percent sure about that one. 
}
else if(c == '@'){
  if(c != ' '){
    stack.pop();
  }
  else {
    stack.push(c);
  }
}


I got this, though I'm not one hundred percent sure that it's correct because apparently there's no way to print out a stack? (I read that somewhere but I'm not sure if it's true or not)

What don't you understand?


I guess I just don't really understand how the stacks and the text file really relate. I understand that I take these special characters and put it in the stacks, but then I'm not sure what to do with it.
Last edited on Apr 13, 2020 at 6:35am
Apr 13, 2020 at 6:45am
Not a hundred percent sure about that one.

Adding 1 to a dollar sign is pretty strange.
Apr 13, 2020 at 6:49am
Yeah lol, it said to read the next character and push it into the stack, I wasn't really sure how to get the next character
Apr 13, 2020 at 6:58am
I wonder if you can just do nothing.
The $ action doesn't make much sense to me.
Apr 13, 2020 at 11:54am
if the current character is $, read the next character and push that onto the stack;
[...]
our editor will have a replace character, which is ‘$’

Not that clear, indeed. Perhaps you could ask your teacher.
Let me venture an hypothesis: perhaps, when we meet a ‘$’, the next character is to be pushed into the stack regardless if it is a ‘@’, a ‘#’, another ‘$’ or a ‘normal’ character.

I wasn't really sure how to get the next character

That’s different kettle of fish. If you’re processing the read characters in a function which is not the one which extracts them from the stream (which is a good idea), than these two processes must ‘talk’ with each other.
Either process_characer() returns instructions for the caller, or it ‘remembers’ its state between different calls, or the caller send an additional parameter which provides further information.
Apr 13, 2020 at 1:58pm
Your teacher wants a program that can take some text with certain special characters sprinkled within and produce an output that has applied some operations based on those characters.

For example, if the original text looks like this:
Here is some text that I'm going to do some edits with.
I'll take out some letttters, remove some some words.
I will also have some $peci@l c#@r@cter$.


A person may mark up the text with suggested edits. In this case, the word "letters" needs to have a couple of extra 't's removed from it, and the word "some" seems to have been repeated accidentally.

To remove individual characters, the editor will put a '#' AFTER the character they want to remove.
To remove a word, the editor will put an '@' AFTER the word.
The '$' is used to tell the computer program not to process the next character as a control character, so it needs to come BEFORE the character being escaped.

Here's the text after it has been marked up by the person editing. This is the text that your computer program will see as input.
Here is some text that I'm going to do some edits with.
I'll take out some lettt#t#ers, remove some some@ words.
I will also have some $$peci$@l c$#$@r$@cter$$.

After your program runs, here is the expected output:
Here is some text that I'm going to do some edits with.
I'll take out some letters, remove some  words.
I will also have some $peci@l c#@r@cter$.

The editor might notice that
remove some  words
has an extra space left over. They would just need to drop in another '#' in the right place to remove it.
Apr 14, 2020 at 4:53pm
Okay, I think I understand that.

I don't understand what that has to do with the stack. What am I doing with it? I can't print out a stack, so why is it needed?
Apr 14, 2020 at 5:19pm
Enoizat wrote:
Let me venture an hypothesis: perhaps, when we meet a ‘$’, the next character is to be pushed into the stack regardless

Excellent hypothesis. In that case it would be an "escape" character, which would defintely be needed. (But why do the instructions call it a "replace" character?)

As an escape char you would process it by setting a bool that would tell you to push the next char regardless of it's value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool escape_mode = false;

for (...)
{
    if (escape_mode)
    {
        st.push(c);
        escape_mode = false;
    }
    else if (c == '$')
        escape_mode = true;
    else if (...)
        ...
}

OP needs to clarify the meaning of '$' with his prof.
Apr 14, 2020 at 5:22pm
I don't understand what that has to do with the stack

You don't understand what pushing and popping has to do with a stack?
And of course you can print out a stack.
You can print it backwards like this:

1
2
3
4
5
while (!st.empty())
{
    std::cout << st.top();
    st.pop();
}

To print it forwards your instructions tell you to first push the entire stack to another stack (which will reverse it) and then print that stack as above.
Last edited on Apr 14, 2020 at 5:23pm
Apr 14, 2020 at 5:34pm
OP needs to clarify the meaning of '$'


Here is the full description of '$' in the assignment:

Our editor will have a replace character, whose effect is to replace the previous character with the character immediately following it. Our replace character is ‘$’. For example, if the string is wr$ac then the result is the string wac.

You don't understand what pushing and popping has to do with a stack?


So, what I understand from the assignment so far is that when I reach a certain key character ( #, $, or @) , that I'm supposed to do something with it(like # erases the previous character, etc.) I don't understand what I'm doing with the stacks - like it's purpose in this code. Am I pushing everything into a stack and popping/pushing?

I'm really sorry for all the questions. I don't know why I'm not fully understanding it.
Apr 14, 2020 at 5:40pm
For example, if the string is wr$ac then the result is the string wac.

How is it different from # then?
Surely wr#ac is also wac.
Or is it something different?

Am I pushing everything into a stack and popping/pushing?

I don't understand what you mean.
Apr 14, 2020 at 5:45pm
How is it different from # then?

I was confused about that as well, since they sounded like they did the same thing. Here's what '#' says in the assignment:

Text editors allow some character (e.g. ‘backspace’) to serve as an erase character, which has the effect of canceling the previous uncanceled character. For example, if ‘#’ is the erase character, then the string wr#et##az#c is really the string wac. The first ‘#’ cancels out r, the second e, the third t, and the fourth z.

I don't understand what you mean.

What I mean is, when I'm reading in the characters from the file, am I pushing all the characters into the stack, and then, when I encounter a symbol, I pop/push the stack?

Apologies for the confusion.
Apr 14, 2020 at 5:56pm
What I mean is, when I'm reading in the characters from the file, am I pushing all the characters into the stack, and then, when I encounter a symbol, I pop/push the stack?

Yes, if there were no special chars at all in the input, then you would just end up pushing all the chars to the stack.
But when you encounter a special char, you do some pops. E.g., @ means to keep popping until either the next char to pop is a space or the stack is empty (i.e., it deletes the last word). It's unclear whether or not you are supposed to also pop the space, although I think that would be unusual (for a real editor, at least).

BTW, I still see no difference between # and $.
They are badly defined.
The only possible difference is that ab$$c might be a$c, whereas ab##c would be c.
Still, that's kind of a pathetic useless difference.
One of them should just be an escape char, which you would obviously need. Otherwise how do you put an actual # in your document?
This is a bad assignment.
Last edited on Apr 14, 2020 at 5:57pm
Apr 14, 2020 at 5:58pm
My previous post was just some context as to 'what the user sees'. Your instructions deal heavily with 'how the code is written'. The code uses a stack to create the behavior that the user sees.

Suppose your user starts with some text like this.
Happpy

She sees that she needs to remove one of the 'p' characters. To do this, she will inject a '#' sign after the letter she wants to remove. Let's say she decides that the second 'p' is getting cut.
Happ#py

Now this text is ready to be fed to your program. Your program will go character by character through the input string, enforcing one of four rules:
1. Pop the stack if this character is '#'.
2. Read the next character and put it on the stack if this character is '$'
3. Pop the stack over and over until a ' ' <space> is found if this character is '@'.
4. Push this character onto the stack if it isn't one of the special characters.

Here's what your stack looks like as you iterate over the input string.

                  p
             p    p
        a    a    a
   H    H    H    H
---------------------------------------------------------
  (0)  (1)  (2)  (3)                  : iteration
   H    a    p    p                   : current character
   4    4    4    4                   : rule applied

Nothing too surprising for the first four characters. None of them are special, so Rule 4 applies: Push the character onto the stack.

The next character, however, is special. It is the '#' sign, which means Rule 1 applies: Pop the stack.

                  p
             p    p    p
        a    a    a    a
   H    H    H    H    H
---------------------------------------------------------
  (0)  (1)  (2)  (3)  (4)             : iteration
   H    a    p    p    #              : current character
   4    4    4    4    1              : rule applied


Since this is a simple example, and that's the only special character in the string, I can quickly show you how the rest of the iteration goes.

                                 y
                  p         p    p
             p    p    p    p    p
        a    a    a    a    a    a
   H    H    H    H    H    H    H
---------------------------------------------------------
  (0)  (1)  (2)  (3)  (4)  (5)  (6)   : iteration
   H    a    p    p    #    p    y    : current character
   4    4    4    4    1    4    4    : rule applied

If you were to pop and print everything on your stack, you would see "yppaH". That's where the reversal will come into play.
Topic archived. No new replies allowed.