#include <iostream>
using namespace std;
#define MAXSIZE 100
class Stack
{
public:
Stack();
bool pop(char &e);
bool push();
bool IsEmpty();
bool huiwen();
char *GetStr();
~Stack();
private:
int top;
char *data;
char str[MAXSIZE];
};
<pre name="code" class="cpp">Stack::Stack()
{
top = -1;
data = new char[MAXSIZE];
cin >> str;
}
Stack::~Stack()
{
delete[]data;
}
bool Stack::IsEmpty()
{
return (top == -1);
}
bool Stack::push()
{
int i = 0;
while (str[i] != '\0')
{
if (top == MAXSIZE - 1)
{
return false;
}
data[top] = str[i];
i++;
top++;
}
return true;
}
bool Stack::pop(char &e)
{
if (IsEmpty())
{
return false;
}
top--;
e = data[top];
return true;
}
bool Stack::huiwen()
{
char temp;
for (int i = 0; str[i] != '\0'; i++)
{
pop(temp);
if (str[i] != temp)
{
return false;
}
}
return true;
}
char *Stack::GetStr()
{
return str;
}
int main()
{
Stack S;
<span style="white-space:pre"> </span>if(!S.push())
<span style="white-space:pre"> </span>cout << "error!" << endl;
if (S.huiwen())
{
cout << S.GetStr() << "是迴文數" << endl;
}
else
{
cout << S.GetStr() << "不是迴文數" << endl;
}
system("pause");
return 0;
}