+ Reply to Thread
Results 1 to 2 of 2

Thread: stack problem: print after pop function

  1. #1
    Newbie george_ya is an unknown quantity at this point
    Join Date
    Oct 2008
    Posts
    2

    stack problem: print after pop function

    have a problem with my program. It should be program that recognize palindome through the stack. Everything works great, only thing that don't work is printing stacks(original and reversed) after the funcion is done. Here is my entire code, and the problem is at case d and e:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    const int MAXSTACK = 21;
    class stack {
    private:
        int  stop;  
        char stk[MAXSTACK];
    public:
        stack();
        ~stack();
        stack(const stack& s);
        void push(const char c);
        char pop();
        char top(void);
        int  emptystack(void);
        int  fullstack(void);
        void stack_print(void);
        int stack::create(void);
    };
    stack::stack()
    {
        stop = 0;
    }
    stack::~stack() { }  
    stack::stack(const stack& s)
    {
        stop = s.stop;
        strcpy(stk,s.stk);
    }
    void stack::push(const char c)
    {
        stk[stop++] = c;
    }
    char stack::pop()
    {
        return stop--;
    }
    char stack::top(void)
    {
        return stk[stop - 1];
    }
    int  stack::emptystack(void)
    {
        return !stop; 
    }
    int  stack::fullstack(void)
    {
        return stop == MAXSTACK;
    }
    void stack::stack_print(void)
    {
        for (int i=0; i<stop; i++)
            cout<<stk[i];
        cout<<endl;
    }
    int  stack::create(void)
    {
        return !stop; 
    }
    char menu()
    {
    
        char volba;
    
        cout<<"\n";
        cout<<" **********.\n";
        cout<<"\n";
        cout<<" a ... make new containers\n";
        cout<<" b ... delete content\n";
        cout<<" c ... enter string\n";
        cout<<" d ... print on screen first stack\n";
        cout<<" e ...  print on screen first stack\n";
        cout<<" f ... is it palindrom\n";
        cout<<" x ... exit\n";
        cout<<"\n your choice : ";
    
        cin >>  volba;
        return volba;
    }
    int main() {
        char  palindrome[MAXSTACK]; 
        char volba;
        stack original,reversed;
        int   stackitems = 0,i;
        //cin.getline(palindrome,MAXSTACK);
        do{
            volba = menu();
            switch (volba)
            {
            case'a':
                {
                    original.create();
                    reversed.create();
                    cout<<"done'";
                    break;
                }
            case'b':
                {
                original.emptystack();
                reversed.emptystack();
                cout<<"empty";
                break;
                }
            case'c':
                {
                    cout<<"enter your string"<<endl;
                cin.get();
                //cin.get();
                cin.getline(palindrome,MAXSTACK);
        for(int o = 0; o < strlen(palindrome); o++)
    
            if (isalpha(palindrome[o]))
            {
                original.push(tolower(palindrome[o]));
                stackitems++;                           
            }
                original.stack_print();
    
            break;
                }
            case'd':
                {
                    original.~stack();
                    for(int g = 0; g < strlen(palindrome); g++)
                    original.push(tolower(palindrome[g]));
                    original.stack_print();
                }
                /*//cin.getline(palindrome,MAXSTACK);
        for(int g = 0; g < strlen(palindrome); g++)
    
            if (isalpha(palindrome[g]))
            {
                original.push(tolower(palindrome[g]));
                stackitems++;                           
            }
    
                }
                original.stack_print();*/
                break;
    
    
            /*{
                    cout<<"original: ";
            original.stack_print();
                    break;
                }*/
                break;
            case'e':
                {
                cout<<"reversed:"<<endl;
                for( i = 0; i < stackitems; i++) {
                reversed.push(original.top());
                original.pop();
            }
            reversed.stack_print();
                }
                break;
    
            case'f':
                {
                for( i = 0; i < stackitems / 2; i++) {
                reversed.push(original.top());
                original.pop();
            }
    
    
            if (stackitems % 2)
                original.pop();
    
            while (!original.emptystack()) {
                if (original.top() != reversed.top()) break;
                original.pop(); reversed.pop();
            }
            if (original.emptystack())
                cout << "it is palindrom\n";
            else
                cout << "not palindrom\n";
    
            break;
    
                }
            default:cout<<"!??!";
    
    
            }
        } while(volba!='x');
    }
    this destutctor thing was just something i tried, but didn't work

  2. #2
    Newbie Korsicall is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    15

    Re: stack problem: print after pop function

    Whats the error you get? If everything else works fine, from a brief glance it should work.

    One change of code that you can do:

    from
    Code:
    stack2.push(stack1.top())
    stack1.pop()

    to this
    Code:
    stack2.push(stack1.pop())
    It doesn't do anything different, but I always find it easier to look at.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Stacks
    By chili5 in forum Java Tutorials
    Replies: 5
    Last Post: 03-07-2009, 08:16 PM
  2. Please Help
    By Prestidigitator in forum Python
    Replies: 15
    Last Post: 02-28-2009, 08:22 PM
  3. Debugging a C++/C File with GDB Debugger
    By awesome001 in forum C and C++
    Replies: 2
    Last Post: 01-01-2009, 07:07 PM
  4. C++ - Problem with a binary tree program; need help
    By Ratchet2246 in forum C and C++
    Replies: 4
    Last Post: 07-15-2008, 02:32 PM
  5. Replies: 5
    Last Post: 07-07-2008, 09:54 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts