作者在 2008-03-27 20:02:00 发布以下内容
用面向对象写的回文数:
请大家指教:
#include <iostream>
#include <vector>
using namespace std;
class Panlindrome
{
private:
vector < char > t;
int len;
public:
void Init_panlindrome()
{
char temp;
this->len=0;
while(cin >>temp&&temp!='@')
{
this->t.push_back(temp);
this->len++;
}
}
bool Is_panlindrome()
{
int half_len;
if(len%2==0) half_len=len/2;
else half_len=(len-1)/2;
for(int i=0;i<half_len;i++)
if(this->t[i]!=this->t[len-i-1])
return 0;
else return 1;
}
};
int main()
{
Panlindrome r;
cout<<"please input a string and end with @:"<<endl;
r.Init_panlindrome();
if(r.Is_panlindrome())
cout<<"this string is a panlindrome"<<endl;
else
cout<<"this string not a panlindrome"<<endl;
return 0;
}