2012-09-23 2 views
0

나는이 숙제에 이상하게 괴롭다. 아이디어는 교수가 우리에게 사용하기를 원하는 특정 헤더를 사용하여 Palindrome 프로그램을 만드는 것이지만, 어떤 이유로 프로그램을 실행할 때 프로그램이 나에게 충돌한다는 구절을 입력 한 직후입니다. 여기 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; 
} 

입니다 그리고 여기 헤더 0 (STACK 초기 크기를 설정합니다 당신은 MakeStack를 호출하지 않는

#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

을, (나는 이것을 변경할 수 없습니다입니다)).

+3

오, 세상에 내가 잊어 버린 것을 믿을 수 없다 ... – user1675108

+3

그게 무슨 생성자에 대한, 그래서 당신의 잘못 생각하지 마 -이 문제를 만든 라이브러리 디자인입니다 :) – KCH