面向对象c++——回文数

作者在 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;
}

数据结构 | 阅读 3839 次
文章评论,共3条
彦宏
2008-03-30 22:18
1
#include&lt;stdio.h&gt;
#define MAXSIZE 100
typedef struct string
{
    char data[MAXSIZE];
    int length;
};
int compare(struct string  *s1,struct string  *s2,int n)
{
    int i,k;
    k=0;
    for(i=0;i&lt;=n-1;i++)
        if(s1-&gt;data[i]==s2-&gt;data[i])
            k++;
    if(k==i)
        return 0;
    else
    return 1;
}

int change(struct string  *s1,int n,struct string  *s2)
{
    int i;
    for(i=0;i&lt;=n-1;i++)
        s2-&gt;data[n-1-i]=s1-&gt;data[i];
    return 1;
}
void main()
{
    int n,result;
    struct string  *s1,*s2;
    s2=NULL;
    printf(&quot;please input :(must less than 100)\n&quot;);
    gets(s1-&gt;data);
    n=strlen(s1-&gt;data);
    s1-&gt;length=n;
    change(s1,n,s2);
    s2-&gt;length=n;
    result=compare(s1,s2,n);
    if(result==0)
        printf(&quot;shi hui wen \n&quot;);
    else
        printf(&quot;bu shi hui wen\n&quot;);
    getch();
}
彦宏
2008-03-30 22:19
2
面向过程的字符串判断是否回文
keloy(作者)
2008-04-02 23:02
3
很好很强的哦
游客请输入验证码
浏览255988次