0

The objective of my problem is to extract all of the vowels, digits, and symbols from a string and put each char/int in their respective stacks (a vowel stack, a digit stack, and a symbol stack). After iterating through the string and extracting what's needed, I have to do arithmetic with the first two digits in the stack (the last two in the string) depending on what the last symbol in the string was.

One of the constraints is that I cannot have duplicates in the stack; the way that I dealt with this constraint is by having a vector that contained vowels, digits, and symbols that have already been used and each time I go through the logic of checking if the current character of the string is a vowel, digit, or symbol, I check to see if the character is already 'used' (aka present in the used vector). However, I was just informed that we aren't allowed to use vectors in this assignment so I am genuinely at a loss. I thought about creating a 'find' method to search through the stack, but that would require popping the whole stack and I don't think that's an option for me as I have to display and do things w the items in the digit and symbol stacks. Also, in the rubric it is specified that I can only use push(), pop(), isEmpty(), peek(), isFull().

Here is the code responsible for checking and adding to the stack:

// traverse through the string
    for (int i = 0; i < toParse.length(); i++)
    {

        // if the char is a vowel, add it to the vowel stack (if it's not already in the stack)
        if(isVowel(toParse[i]) == true)
        {

            if(find(used.begin(), used.end(), tolower(toParse[i])) == used.end())
            {
                vowels.push(tolower(toParse[i]));
                used.push_back(tolower(toParse[i]));
            }

        }
        
        // if the char is a digit, add it to the digit stack (if it's not already there)
        if(isdigit(toParse[i]))
        {

            if(find(used.begin(), used.end(), toParse[i]) == used.end() )
            {

                digits.push(atoi(&toParse[i]));
                used.push_back(toParse[i]);
            }

        }

        // if the char is a symbol, add it to the symbol stack regardless of whether it's   already there or not
        if(isSymbol(toParse[i]))
        {

            if(find(used.begin(), used.end(), toParse[i]) == used.end() )
            {

                symbols.push(toParse[i]);
                used.push_back(toParse[i]);

            }

        }

    }

Thank you for reading; also I'm not expecting anyone to do my homework I would just like a little guidance in the right direction!

Edit 1: As requested, here is the exact text of my task: [1]: https://i.stack.imgur.com/crM1P.png

  • 1
    Using vectors is not allowed... then, how about `map`, `set`, `unordered_map`, or `unordered_set`? – MikeCAT Mar 07 '23 at 14:08
  • No tags in the title please. – Evg Mar 07 '23 at 14:10
  • 2
    is using a `stack` a hard requirement? Note that a `stack` has a reduced interface, it can be extended without compromising the stacks operations but then its not a stack anymore. In other words, you can only access the top element of a stack, because thats what makes the stack a stack. – 463035818_is_not_an_ai Mar 07 '23 at 14:10
  • on the other hand, some exercises about stacks are just made to make you realize the limitations of a stack and to implement some operations in the hard longwinded way. So if you need to pop from the stack to see if it has duplicates then maybe thats what you are expected to do. What is the exact text of your task? – 463035818_is_not_an_ai Mar 07 '23 at 14:13
  • Thank you for all of the suggestions, I have thought about using a map or set but I just asked and it is also not allowed :( Also, I've included an exact description of the task – butterfly549 Mar 07 '23 at 14:19
  • 2
    text should be included as text. The assignment asks you to use an `ArrayStack`. It is unclear what that is or what operations it supports. We cannot help you with a class we do not know. – 463035818_is_not_an_ai Mar 07 '23 at 14:24
  • 1
    Paste the assignment as text, not image please. Anyway: "How do you know if you have pushed an 'a' already?" - there is no way to know it other than pop all elements from your stack and push to the other collection (well, also stack, considering it is the only tool you are allowed to use), and then pop/push them back to your original stack. Extremely inefficient, but maybe that's what your assignment is about. – pptaszni Mar 07 '23 at 14:25
  • The given hint suggests that you're allowed to use another structure to keep track of which elements are in the stack. (When an exercise includes a hint, it is always significant.) – molbdnilo Mar 07 '23 at 14:40

1 Answers1

0

You could pop all the elements from a stack into another and check each value as you go. This would reverse the order of the stack so you would need to then repeat this process again into the original stack. It is not the most elegant solution but perhaps will do what you desire.

Example to demonstrate what I meant:

while (!stack1.isEmpty())
{
     var value = stack1.pop();
     // perform some logic or check on the value
     stack2.push(value);
}

// To restore the original stack in the original order
while (!stack2.isEmpty())
{
     stack1.push(stack2.pop());
}

Another approach could be to process the input string and remove duplicates before you split it up and push them onto their respective stacks. This way you don't have to check what is already in the stack as you've removed any duplicates and guaranteed each character will show up once and only once. I'm not sure what the limitations of your assignment are but this link can get you started with this approach: Remove duplicates in string algorithm

DCaruso
  • 48
  • 6