我改过了,可是还是不行,数据不对。感觉是不管s1和s2相不相等,都会跳出while循环。不知错在哪,请帮忙看一下!谢谢大家!源代码如下:
#include<string.h>
#include<iostream>
#include<fstream>
using namespace std;
NoMem()
{cout<<"Error!"<<endl;
return 0;
}
class String
{
public:
//构造函数
String(char *s="");
String(const String&s);
//析构函数
~String();
//赋值运算
String&operator = (const String&s);
//关系运算
bool operator == (const String&s)const;
//串长运算
int Length()const;
//串接运算
String operator + (const String&s)const;
void operator += (const String&s);
//查找字符运算
int Find(char c,int start)const;
//子串运算
String SubStr(int index,int count)const;
//插入运算
void Insert(const String&s,int index);
//删除运算
void Delete(int index,int count);
//读入字符串
//int ReadString(istream&is=in,char delimiter="\n");
//其他串运算
void Prefix();
void ModifiedPrefix();
int Match(String& t);
char *str;//串数组
int *pre;//前缀函数数组
int size;//串长
};
String::String(char *s)
{
//串尾有一个空字符
size=strlen(s)+1;
str=new char [size];
if(str==0) throw NoMem();
strcpy(str,s);
pre=new int [size];
if(pre==0) throw NoMem();
}
String::String(const String&s)
{
//复制构造函数
size=s.size;
str=new char [size];
if(str==0) throw NoMem();
strcpy(str,s.str);
pre=new int [size];
if(pre==0) throw NoMem();
}
String::~String()//析构函数
{
delete []str;
delete []pre;
}
bool String::operator ==(const String&s)const
{
return strcmp(str,s.str)==0;
}
int main()
{
ifstream in("input.txt");
if(in.fail())
{cout<<"the input.txt is not exist!\n";
exit(1);
}
ofstream out("output.txt");
int num=0,max,k;
char x[50];
in>>k;
in>>max;
in.getline(x,50,'\n');
String s1,s2;
strcpy(s1.str,x);
//delete []s1.str;
//delete []s2.str;
char digist[7]={10,11,12,13,14,15};
while(num<=max)
{
for(int i=0;i<=s1.size-2;i++) //转换成10进制
if(s1.str[i]>='A'&&s1.str[i]<='F')
s1.str[i]=digist[s1.str[i]-65];
for(i=0;i<=s1.size-2;i++) //s1倒置到s2中
for(int j=s1.size-2;j>=0;j--)
s2.str[i]=s1.str[j];
if(s1.operator ==(s2)) break;
else
{
for(int j=s1.size-2;j>=0;j--)//按位做加法
{
s1.str[j]=s1.str[j]+s2.str[j];
if(s1.str[0]>=k)//最高位有进位时多开一位
{
String temp;
delete []temp.str;
strcpy(temp.str,s1.str);
s1.size++;
delete []s1.str;
for(int t=1;t<=s1.size-2;t++)
for(i=0;i<=s1.size-3;i++)
s1.str[t]=temp.str[i];
s1.str[0]=0;
if(s1.str[1]>=k)
{
s1.str[1]=s1.str[1]%k;
s1.str[0]++;
}
}
if(s1.str[j]>=k)
{
s1.str[j]=s1.str[j]%k;
s1.str[j-1]=s1.str[j-1]+1;
}
}
for(i=0;i<=s1.size-2;i++) //转回k进制数
if(s1.str[i]>'9')
s1.str[i]=char(s1.str[i]+55);
}
num++;
}
if(num>max)
out<<"No solution!"<<endl;
else
{
out<<num<<endl;
}
for(int i=0;i<=s1.size-2;i++)//输出回文
out<<s1.str[i];
return 0;
}