C++程序设计实践指导1.15找出回文数改写要求实现

时间:2023-03-08 19:40:13
C++程序设计实践指导1.15找出回文数改写要求实现

改写要求1:用单链表实现

#include <cstdlib>
#include <iostream> using namespace std;
struct LinkNode
{
int data;
LinkNode *next;
};
class PALINDROME
{
int low,up;
int a[];
int count;
public:
PALINDROME(int t1,int t2);
int IsPalin(int x);
LinkNode* IsPalinAndStore();
void OutputResults(LinkNode* Head);
}; PALINDROME::PALINDROME(int t1,int t2)
{
count=;
low=t1;
up=t2;
}
LinkNode* PALINDROME::IsPalinAndStore()
{
LinkNode* Head=new LinkNode;
Head->next=NULL;
LinkNode* p=Head;
for(int i=low;i<=up;i++)
{
int x=i*i;
if(IsPalin(x))
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->data=i;
p->next=newLinkNode;
p=newLinkNode;
}
}
return Head;
}
void PALINDROME::OutputResults(LinkNode* Head)
{
LinkNode* p=Head->next;
cout<<"count="<<count<<endl;
cout<<"x"<<'\t'<<"x*x"<<endl;
while(p)
{
cout<<p->data<<'\t'<<p->data*p->data<<endl;
p=p->next;
}
}
int PALINDROME::IsPalin(int x)
{
int i=,j,n;
int a[];
while(x)
{
a[i]=x%;
x=x/;
i++;
}
n=i;
for(i=,j=n-;i<=j;i++,j--)
if(a[i]!=a[j])
return ;
return ;
}
int main(int argc, char *argv[])
{
LinkNode* Head=new LinkNode;
PALINDROME p(,);
Head=p.IsPalinAndStore();
p.OutputResults(Head);
system("PAUSE");
return EXIT_SUCCESS;
}