簡體   English   中英

C ++ Palindrome程序

[英]C++ Palindrome program

我發現自己對這項家庭作業感到奇怪的困惑。 我的想法是創建一個Palindrome程序,使用教授希望我們使用的特定標題,但出於某種原因,當我運行它時,在我輸入短語之后,程序就崩潰了。

這是程序

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

這是標題(我不允許改變這個)

#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