判断回文的问题
程序代码:
#include<iostream> #include<string> using namespace std; class stack { private: int top; char s[100]; public: stack( ) { top=0;} bool push(char x) { if(top<99) { s[top]=x; top++; return true; } else cout<<"over flow"; return false; } char pop() { if(top>0) { top--; return s[top]; } cout<<"stack is empty"<<endl; return -1; } }; void main() { stack s; int n,ll_strlen; int i; char *p,*a; cout<<"input the string's max length."<<endl; cin>>n; p=new char[n]; a=new char[n]; cout<<"input the string."<<endl; cin>>p; ll_strlen=strlen(p); for(i=0;i<ll_strlen;i++) if(s.push(p[i])==false) {cout<<"fail.";exit(-1);} for(i=0;i<ll_strlen;i++) { a[i]=s.pop(); cout<<a[i]; } cout<<endl; for(i=ll_strlen;i>0;i--) { if(a[i]!=p[i]) {cout<<"not"<<endl;exit(0);} else {cout<<"yes";} } }
[ 本帖最后由 ou1111 于 2010-11-11 11:23 编辑 ]