2012-09-23 42 views
0

我發現自己在這個家庭作業中很奇怪。這個想法是使用教授希望我們使用的特定標題創建一個迴文項目程序,但由於某種原因,當我運行它時,在我輸入該短語後,程序崩潰了。C++ 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 

回答

2

你是不是叫MakeStack,將設置堆棧的初始大小(0 )。

+3

哦,我的上帝,我不敢相信我忘了那...... – user1675108

+3

這就是構造函數的用途,所以不要認爲它是你的錯 - 它是庫設計造成這個問題:) – KCH