堆栈递归程序问题

时间:2012-11-25 23:21:02

标签: recursion compiler-errors stack

我是一名新手C ++编码员,显然不是很擅长。我对这个程序有很大的麻烦。

  • 我的函数的开始和结束括号上出现语法错误,我的标题cpp文件中的“<”上的语法错误,以及我缺少括号的错误。
  • 我的第一个堆栈无法识别(主驱动程序文件),而在我的StackType.cpp文件中,original是“未声明的标识符”。
  • 最后,Push的左边必须有我的for循环中的class / struct / union,当用环填充第一个堆栈时。

我提前为所有这些问题道歉。任何你能给我的帮助将不胜感激! 谢谢。

====================== Stack Header ======================= =========

// File: StackType.h
// Stack template class definition.
// Dynamic array implementation

#ifndef StackType
#define StackType

template <class ItemType>
class StackType

{
private:
    int ItemType;
    ItemType *myStack;  // pointer to dynamic array
    int _top, _maxSize; // using underscores to remind that it's private

    public:

    StackType(int numRings = 50);       // Constructor
    StackType (const StackType<ItemType>&); // Copy Constructor


    // Member Functions
    void Push(ItemType);            // Push
    void Pop(ItemType &);           // Pop
    void stackTop(ItemType &) const;    // retrieve top
    bool stackIsEmpty() const;          // Test for Empty stack
    bool stackIsFull() const;       // Test for Full stack


    ~StackType();       // Destructor
};
#endif

===================== Stack cpp file ======================= ===========

#include "StackType.h"

#include "stdafx.h"
#include <iostream> 
#include <stdio.h>

// Constructor with argument, size is numRings, limit is 50 (set in .h header)
template <class ItemType>
StackType<ItemType>::StackType()
{  
    _maxSize = numRings; 
    _top = -1; 
}

// Copy Constructor
template <class ItemType>
StackType<ItemType>::StackType(const StackType<ItemType>& original :  
                                       _maxSize(original._maxSize), top(original._top)
{
    myStack = new ItemType[_maxSize];
    for (int i = 0; i <= top; i++) myStack[i] = original.myStack[i];
}


// Destructor
template <class ItemType>
StackType<ItemType>::~StackType()
{ 
    delete [] myStack;
}

// Push
template <class ItemType>
void StackType<ItemType>::Push(StackType<ItemType> ringVal)
{
    if(stackIsFull()) cout << "\t There is not enough available memory = the stack is 
                                  full!" << endl;
    else myStack[++_top] = ringVal;
}

// Pop
template <class ItemType>
void StackType<ItemType>::Pop(StackType<ItemType> &ringVal)
{
    if(stackIsEmpty()) cout << "\t The stack is empty!" << endl;
    else ringVal = myStack[_top--];
}

// Retrieve stack top without removing it
template <class ItemType>
void StackType<ItemType>::stackTop(StackType<ItemType> &ringVal) const
{
    if(stackIsEmpty()) cout << "The stack is empty!";
    else ringVal = myStack[_top];
}

// Test for Empty stack
template <class ItemType>
bool StackType<ItemType>::stackIsEmpty() const
{ 
    return (_top < 0); 
}

// Test for Full stack
template <class ItemType>
bool StackType<class ItemType>::stackIsFull() const
{ 
    return (_top >= (_maxSize - 1)); 
}
 // end StackType.cpp

=========================主要驱动程序文件=================== ====================

#include "StackType.h"
#ifdef _DEBUG
#include "StackType.cpp"
#endif // _DEBUG


#include <stack>
#include "StdAfx.h"
#include <iostream>
using namespace std;

//  Global Variable - Counter to display the number of moves.
int count = 0; 

class StackType;

//  Functions Prototypes
void MoveRings(StackType<ItemType>&, StackType<ItemType>&);
//  Function to move the rings
void Pegs(int D,StackType<ItemType>& b,StackType<ItemType>& e, StackType<ItemType>& h);
//  This is a recursive function.  
void Display (int, StackType <ItemType>& , StackType<ItemType>&, StackType<ItemType>&); 
//  Function to display the pegs  

//  Main - Driver File
int main()
{

    // create 3 empty stacks
    StackType<ItemType> FirstPeg;   // Receiving an error that this is not identified
    StackType<ItemType> EndPeg;
    StackType<ItemType> HelperPeg;

    // Number of rings.
    int numRings;

    cout << "\n\t *********** Rings to Pegs (Towers of Hanoi) ***********\n" << endl;
    cout << "\t Please Enter the number of rings you want to play with: ";
    //  Input number of rings
    cin >> numRings;    
    cout << endl;
    while(numRings < 0 || isalpha(numRings))  //  To make sure that the user did not 
                                              //  enter an invalid number
    {
        cout << "  Your entry is invalid. Please use only integers. Please re-
                                                                              enter: ";
        cin >> numRings;
        cout << endl;
    }

    for(int i = 1; i <= numRings; i++)
     // Fill the first peg with the number of rings.
    {
        FirstPeg.Push(i);
    }


    Pegs(int, StackType<ItemType>&, StackType<ItemType>&, StackType<ItemType>&); 
    //  To call the recursive function that will move the rings
    Display (int, StackType<ItemType>&, StackType<ItemType>&, StackType<ItemType>&); 
    //  To call the display function

    cin.clear();
    cin.ignore('\n');
    cin.get();
    return 0;

}

//  This function will move an ring from first peg to the second peg
void MoveRings(StackType<ItemType>& beg, StackType<ItemType>& theEnd) //End
{
    int r; // disk will be removed from one stack and added to the other

    beg.Pop(r);//pop from source

    theEnd.Push(r);//and move to target

}

//  This function displays the moves
void Display(int R, StackType<ItemType>& toBegin , StackType<ItemType>& toEnd,  
             StackType<ItemType>& toHelp) 

{
    StackType<int> B;// create temporarily first stack 
    StackType<int> E;// create temporarily End(End) stack 
    StackType<int> H;// create temporarily helper stack 
    for(int i = 1; i <= R; i++)
    {
        toBegin.Pop(i);//moves the ring from source
        B.Push(i);//to the temporarily stack to display it
        cout << "Beginning Peg:" << &B << endl;

        toEnd.Pop(i);//moves the ring from source
        E.Push(i);//to the temporarily stack to display it
        cout << "  End(Final) Peg: " << &E << endl;

        toHelp.Pop(i);//moves the ring from source
        H.Push(i);//to the temporarily stack to display it
        cout << "  Helper Peg:" << &H << endl;
    }
}



//-------------------------------------------------------------------

void Pegs(int D,StackType<ItemType>& b,StackType<ItemType>& e,StackType<ItemType>& h) 
//  This is a recursive function. 
{
    if (D == 0) // The base 
    {
        return 1;
    }

    else if(D == 1)              //  If there is only one ring, move this ring from the 
                                 //  first peg to the end(final) peg
    {
        MoveRings(b, e); // moves the ring from the first to the end(final) peg 
        cout<<"  Really? You have entered one ring..." << endl;
        cout<<"  It moves directly from the first peg to the End peg." << endl;

        count++; // increment the number of moves

        cout << "There has been " << count << " move. "<< endl;// display the          
                                                                   // number of moves
        Display (D, b, e, h);

    }
    else if (D > 1) // a recursive function in order to move the rings
    {

            Pegs(D - 1, b, e, h);    //  to move N-1 rings from the first peg to the
                                     //  end(final) peg by using the helper peg

        MoveRings(b, e);//  to move the last ring to the end(final) peg 
        count++; //  increment the number of steps before displaying
        cout << "There has been " << count << " moves. "<< endl;

Pegs(D - 1, b, e, h); 
    //  to move N-1 rings from the helper peg to the end(final) peg with the help of                    
    //  first peg

    //Display ( D(rings), First Peg, End(Final) Peg, Helper Peg );
    }
}

1 个答案:

答案 0 :(得分:2)

我可以立即看到的一个问题是,您的头文件定义了StackType以防止双重包含,它也被用作类名。在#define StackType之后,它最终成为一个扩展为空的宏,因此您的代码看起来像class { ... }

您应该使用符号来防止未包含任何其他内容的双重包含。对于名为StackType.h的文件,典型的用法是STACKTYPE_H。

一旦你解决了这个问题,你遇到的其他一些问题可能就会消失。如果您遇到更多问题,请返回更新,如果您有问题,请发布确切的编译错误。