正整数N作为使用堆栈的正整数之和

时间:2015-10-30 07:38:28

标签: c++ pointers integer sum stack

所以,我正在研究这个项目,我们必须写一个正整数n作为正整数。并且应该订购所有解决方案,例如:

n = 3
1: 1 1 1 
2: 1 2
3: 3

n = 5 
1: 1 1 1 1 1 
2: 1 1 1 2 
3: 1 1 3 
4: 1 2 2  
5: 2 3 
7: 5

我(我认为)在纸上有很好的算法;然而,我在将它放入代码时遇到了麻烦。它第一次进入内部do-while循环时运行平稳,但是'top'没有从getTop函数中获得正确的值。 这是我的Stack.h文件

    #include <iostream>
    using namespace std;

    template <class T>
    class Stack
    {
    private:
        // Structure for the stach nodes
        struct StackNode
        {
            T value;          // Value in the node
            StackNode *next;  // Pointer to next node
        };

        StackNode *top;     // Pointer to the stack top
        int count;

    public:
        //Constructor
        Stack(){top = NULL; count = 0;}

        // Destructor
        ~Stack();

        // Stack operations
        bool push(T);
        bool pop(T &);
        bool isEmpty();
        int getCount();
        int getTop();
        void print();

    };


template <class T>
Stack<T>::~Stack()
{
    StackNode *currNode, *nextNode;

    // Position nodePtr at the top of the stack.
    currNode = top;

    // Traverse the list deleting each node.
    while (currNode) //while (currNode != NULL)
    {
        nextNode = currNode->next;
        delete currNode;
        currNode = nextNode;
    }
}


template <class T>
bool Stack<T>::push(T item)
{
    StackNode *newNode; // Pointer to a new node

    // Allocate a new node and store num there.
    newNode = new StackNode;
    if (!newNode)
        return false;
    newNode->value = item;

    // Update links and counter
    newNode->next = top;
    top = newNode;
    count++;

    return true;
}


template <class T>
bool Stack<T>::pop(T &item)
{
    StackNode *temp; // Temporary pointer

    // empty stack
    if (count == 0)
        return false;

    // pop value off top of stack
    item = top->value;
    temp = top->next;
    delete top;
    top = temp;
    count--;

    return true;
}

template <class T>
bool Stack<T>::isEmpty()
{
    return count == 0;
}


template <class T>
int Stack<T>::getCount()
{
    return count;
}


template <class T>
int Stack<T>::getTop() 
{
    if (this->top->next == NULL)
    {
        cout << "EXCEPTION" << endl;
    }
    else
    {
        return this->top->next->value;
    }
    //return top->value;
}

template <class T>
void Stack<T>::print()
{
    StackNode *newNode;
    newNode = top;
    for (int i = count; i > 0; i--)
    {
    cout << newNode -> value;
    cout << " ";
    newNode = newNode -> next;      
    }

}

#endif

这是我的main.cpp

#include <iostream>
#include "Stack.h"

using namespace std;

int main()
{
    int input = 0;
    int counter = 0;
    int pop1 = 0;
    int pop2 = 0;
    int top;
    int CurrSum = 0;
    Stack<int> *stack = new Stack<int>;
    Stack<int> *tmpStack = new Stack<int>;

    cout << "Please enter a integer: " << endl;
    cin  >> input;
    while(input < 0)
    {
        cout << "Please enter a POSITIVE integer: " << endl;
        cin  >> input;
    }

    if ( input != 0)
    {
        counter ++;
        for (int n = 1; n <= input; n ++)
        {
            stack -> push(1);
        }

     do
     {


        do
        {
            top = stack->getTop();
            pop1 = stack->pop(top);
            top = stack->getTop();
            pop2 = stack->pop(top);

            //increment the pop2 and push it back
            stack->push(pop2 + 1);

            top = stack->getTop();

            CurrSum = CurrSum - pop1 - pop2 + top;

            if (CurrSum < input)
            {
                stack->push(top);
                CurrSum = CurrSum + top;
            }
            else if (CurrSum > input)
            {
                pop1 = stack->pop(top);
                top = stack->getTop();
                pop2 = stack->pop(top);

                CurrSum = CurrSum - pop1 - pop2 + top;
            }

        }while (CurrSum == input);

        cout << counter << ": ";
        stack->print();
        top = stack->getTop();

     }while (top != input);

        system("pause");
    }
    else
        cout << "Thank you for using this program. Bye!" << endl;
    return 0;
}

1 个答案:

答案 0 :(得分:-1)

首先,您不需要为自己编写Stack类,它已经为您完成了:它被称为std::stack,并且完全符合您的需要。其次,你真的不需要动态分配它,无论你使用自己的Stack还是std::stack,虽然功能相同,但动态分配的效率较低。

如果仍然无效,您现在可以确定您的算法错误(我没有检查)。因为它是一个典型的递归问题,所以我会尝试递归地编写它(现在你根本不需要Stack类),因为这样的递归算法在递归编写它们时通常更容易理解。之后,如果你理解了什么问题,你可以迭代地重写它。