Brainstorming for Stacks Project

This is a discussion on Brainstorming for Stacks Project within the C++ Programming forums, part of the General Programming Boards category; I have a project that is this Project: For this project you will write a program that will read in ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    Brainstorming for Stacks Project

    I have a project that is this
    Project:

    For this project you will write a program that will read in strings from a file named "reverse.in". Each word may be marked with an even number of "at" characters ('@'). The first '@' character signals that the following characters up to the next '@' are in reverse order. Your program will print the result to an output file called "reverse.out". You must use an STL stack to reverse the order of the characters.

    For example, if the input file looks like this:

    @egelloC@
    CS174
    re@srev@e
    sup@ilacre@fragilist@ilaipxeci@docious
    id@@en@@ti@@ty

    then the output would be:

    College
    CS174
    reverse
    supercalifragilisticexpialidocious
    identity
    i can't think of how to independently access the chars to reverse them. My idea as of so far is to have an if statement that will check to see if the char is equal to the @ sign, then it will copy all the chars into a temporary array or something, then when it reaches the second @ sign it will stop copying the chars, then what i can't think of how to do is to put the chars back into the sting, i would just do a reverse loop to make the chars print out backwards but those are my ideas. I have a feeling it would be completely wrong and idk how to deal with the problem where there are @@. any help would be greatful. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Why not make a temporary string and append the chars to the temporary string. When you have to do reversal, add the characters to the stack and then put them from the stack to the temporary string when the reversing is done.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    i can't think of how to independently access the chars to reverse them.
    You could read each string into a std::string then access each character either by index or via an iterator. Alternatively, you could read in character by character.

    what i can't think of how to do is to put the chars back into the sting
    "Your program will print the result to an output file called "reverse.out"."

    i would just do a reverse loop to make the chars print out backwards
    "You must use an STL stack to reverse the order of the characters."
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    ok i forgot about accessing by index, but how do i deal with the @ signs, i can't think of a solid way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you use a temporary string, just ignore them (other than to indicate when to start and stop the stack usage). Don't put them into the target string or the stack.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    only the chars between the @ signs r reversed

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Right. What's your question?

    When you hit an @ sign, you change what you do with the letters. You need to remember whether you are between @ signs or not.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    Quote Originally Posted by DarkDot View Post
    I have a project that is this

    i can't think of how to independently access the chars to reverse them. My idea as of so far is to have an if statement that will check to see if the char is equal to the @ sign, then it will copy all the chars into a temporary array or something, then when it reaches the second @ sign it will stop copying the chars, then what i can't think of how to do is to put the chars back into the sting, i would just do a reverse loop to make the chars print out backwards but those are my ideas. I have a feeling it would be completely wrong and idk how to deal with the problem where there are @@. any help would be greatful. Thanks in advance.

    Quote Originally Posted by Project
    You must use an STL stack to reverse the order of the characters.
    That should be enough to solve your problem. (assuming you learned what a stack is and how it works. The '@' problem should be fundamental if you know what a stack is.)
    Last edited by I BLcK I; 03-28-2007 at 11:19 PM.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    i know what a stack is, i mean i'll be storing the strings one on top of eachother, just what i don't know how to implement is using the stack to reverse the order and how to make sure when its reading in between the @ sign it will start to reverse the letters then when it hits the next one stop.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    What will happen if you do this:
    push 1
    push 2
    push 3
    print top
    pop
    print top
    pop
    print top
    pop

    Think about what the output will be.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by DarkDot View Post
    i know what a stack is, i mean i'll be storing the strings one on top of eachother, just what i don't know how to implement is using the stack to reverse the order and how to make sure when its reading in between the @ sign it will start to reverse the letters then when it hits the next one stop.
    it's not that hard if you thin about. How does a stack work?
    if I do
    Code:
    std::stack<char> st;
    st.push('k')
    st.pop('o')
    the stack looks like this
    Code:
    top:| |
        |o|
        |k|
        |-|
    now what happens if I pop the chars off the stack?
    Last edited by ChaosEngine; 03-29-2007 at 05:28 PM. Reason: damnit mir, you beat me to it!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> i mean i'll be storing the strings one on top of eachother
    You mean the characters, right? Is that the source of your confusion?

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    i think so...my thinking was that i would start off by making a stack, then as i read from the input file it would store the chars and would look for the @ sign and when i found it begin to reverse the letters until i hit another @ sign, then stop at the end of the string, but the whole string would be stored. So it would look something like this.

    top:|string |
    |string|
    |string|
    |-|

    based on what is being said, i would have to create a new stack for each string and store the chars individually in there including the @, then somehow reverse the letters in between them in the stack, which i still don't know how to do. I think the way to have it reverse in the first place would be to have something like
    Code:
    if(char == @)
      {
             while(char != @)
              {
                      //have it reverse the chars here
               }
       }

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I was thinking of only putting the chars on the stack that need to be reversed. That is what the stack is for, to help you reverse the characters. The ones that don't need to be reversed can just be added to the output string instead of being put on the stack.

  15. #15
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    i think i just figured out the way to do it based on your last post. Just put the chars that need to be reversed into the stack because then when you copy them to the outfile it will be in the correct order. example is: tsal when i put them in the stack the t will go in first followed but the other letters. leaving L on top..all i do it copy the top to the out file then pop.a and i can do a if string[i] = @
    {
    //loop through and copy the values into the stack until the string[i] is equall to @.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 08:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21