简体   繁体   English

基于堆栈的回文检查器

[英]Stack-based palindrome checker

i 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: 这是我的全部代码,问题出在情况d和e:

#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');
}

You've explicitly called your stack's destructor. 您已明确调用堆栈的析构函数。 There is almost never a good reason to do this. 几乎没有一个很好的理由要这样做。 If the stack is a local ("on the stack", hee hee), the compile will do it for you. 如果堆栈是本地堆栈(“在堆栈上”,嘻嘻),则编译将为您完成此操作。 If it's on the heap, created with new , call delete on it, which will cause the compiler to call the destructor. 如果它在使用new创建的堆上,则调用delete ,这将导致编译器调用析构函数。

    case'd':
            {
                    original.~stack();

您已评论回文读物:)

//cin.getline(palindrome,MAXSTACK);

There are a few things I would like to respond with. 我想回应几件事。 First, I think GMan, tpdi, and Vinay all have good points. 首先,我认为GMan,tpdi和Vinay都有优点。 This FAQ explains why calling the destructor on a local variable is a bad idea. 常见问题解答说明了为什么在局部变量上调用析构函数不是一个好主意。

I realize this is just a simple homework problem and you are probably just trying to keep your stack class lightweight, but you might consider using a container class instead of an array of characters in your stack class. 我意识到这只是一个简单的家庭作业问题,您可能只是想使堆栈类轻巧,但是您可以考虑在堆栈类中使用容器类而不是字符数组

Next, I'm not sure your emptystack and create functions are doing what you think they are doing. 接下来,我不确定您的堆栈和创建函数是否在执行您认为正在执行的操作。 When you declare your original and reversed stack classes in the main program the memory is allocated for your internal character array. 在主程序中声明原始堆栈类和反向堆栈类时,将为内部字符数组分配内存。 It's not really necessary in this case to have a create function. 在这种情况下,确实没有必要具有创建功能。 Perhaps if you were allocating memory on the heap for your character array, you would put that code into the create function (if you chose to leave it out of the constructor for some reason), but that's not the case here. 也许如果您正在为字符数组在堆上分配内存,则可以将该代码放入create函数中(如果您出于某种原因选择将其保留在构造函数之外),但事实并非如此。

Similarly, emptystack isn't really doing anything. 同样,emptystack并没有真正做任何事情。 It would be better to have empty stack set the stop member variable to 0. At least that way the stack would appear to be empty the next time someone tried to use it. 最好将空栈将stop成员变量设置为0。至少那样,下一次有人尝试使用它时,栈似乎就空了。

There's a lot more that could be said about this class, but it might be better if you tried some of the suggestions here like using the std::stack and debugging. 关于这个类,还有很多要说的,但是如果您尝试了一些建议,例如使用std :: stack和调试,可能会更好。 This is, after all, your homework assignment: it will help you a lot more in the future if you find the solution yourself! 毕竟,这是您的家庭作业:如果您自己找到解决方案,它将在将来为您提供更多帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM