简体   繁体   English

C ++ Palindrome程序

[英]C++ Palindrome program

I find myself oddly perplexed on this homework assignment. 我发现自己对这项家庭作业感到奇怪的困惑。 The idea is to create a Palindrome program, using a specific header the professor wants us to use, but for some reason, when I run it, right after I enter the phrase the program crashes on me. 我的想法是创建一个Palindrome程序,使用教授希望我们使用的特定标题,但出于某种原因,当我运行它时,在我输入短语之后,程序就崩溃了。

Here is the program 这是程序

#include   <iostream>
#include   <ctime>
#include   "STACK.h"
using namespace std;

int main(void)
{
// Declare variables
time_t          a;
STACK<char, 80>     s;
STACK<char, 80>     LR;
STACK<char, 80>     RL;
char            c;
char            c1;
char            c2;

// Displays the current time and date
time(&a);
cout << "Today is " << ctime(&a) << endl;

// Prompts the user to enter the string
cout << "Enter a phrase: ";
while(cin.get(c) && (c != '\n'))
{

    if(isalpha(c)) 
    {
        c = toupper(c); 
        LR.PushStack(c);
        s.PushStack(c);
    }

}

// Copies s into RL in reverse order
while(!(s.EmptyStack() ) )
{
    c = s.PopStack();
    RL.PushStack(c);
}

// Checks for Palindrome
while(!(LR.EmptyStack() ) )
{
    c1 = LR.PopStack();
    c2 = RL.PopStack();

    if( c1 != c2) 
    {
    break;
    }
}


// Displays the result
if(LR.EmptyStack() )
{
    cout << "Palindrome";
}
else
{
    cout << "Not a Palindrome";
}

return 0;
}

And here is the header (I am not allowed to change this) 这是标题(我不允许改变这个)

#ifndef STACK_H
#define STACK_H
template <class T, int n>
class STACK
{ private: T a[n];
    int counter;
  public:
     void  MakeStack() { counter = 0; }
 bool  FullStack()
    { return (counter == n) ? true : false ; }
 bool EmptyStack()
     { return (counter == 0) ? true : false ; }
 void  PushStack(T x)
     { a[counter] = x; counter++; }
 T PopStack()
     { counter--; return a[counter]; }
};
    #endif

您没有调用MakeStack ,它将设置STACK初始大小(0)。

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

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