//迴文串的判斷
#include <iostream>
#include <string>
using namespace std;
class MyStack
{
private:
int top;
public:
char Key[20];
MyStack();
void Push(char c);
void Pop(char &c);
};
MyStack::MyStack()
{
top=0;
}
void MyStack::Push(char c)
{
top+=1;
Key[top]=c;
}
void MyStack::Pop(char &c)
{
c=Key[top];
top-=1;
}
int main()
{
MyStack by;
string str;
char c;
int i;
cout<<"請輸入一串字符:";
cin>>str;
i=str.length();
for (int j=0;j<i;j++)
{
by.Push(str.at(j));
}
for (int j=0;j<str.length();j++)
{
by.Pop(c);
if (c!=str.at(j))
{
cout<<"不是迴文串!"<<endl;
return 0;
}
}
cout<<str<<"是迴文串!"<<endl;
return 1;
}
嚴蔚敏3.31
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.