关键是不明确你是要判断字符串的回文还是 数字的回文
构造函数写错了,构造函数都没实体
#include
#include
#include
void main()
{
int data(0);
std::cin >> data;
std::cout << "您输入的是:" << data << std::endl;
int count(0), tmp(data);
while (tmp){
count++;
tmp /= 10;
}
std::cout << "这个数有" << count << "位" << std::endl;
count--;
bool flag = true;
while (1)
{
if (data < 10)
{
break;
}
else if (static_cast(data / (pow(10, count))) != data % 10)
{
flag = false;
break;
}
data %= static_cast(pow(10, count));
data /= 10;
count -= 2;
}
if (flag)
{
std::cout << "yes" << std::endl;
}
else
{
std::cout << "no" << std::endl;
}
system("pause");
}
转为字符串在判断:
#include
#include
#include
bool fun(char *str)
{
char *ib = str, *ie = str + strlen(str) - 1;
while (*ib++ == *ie--)
{
if (ib == ie || ib > ie)
{
return true;
}
}
return false;
}
void main()
{
char arr[20];
int data(0);
std::cin >> data;
_itoa(data, arr, 10);
std::cout << arr << std::endl;
bool res = fun(arr);
if (res)
{
std::cout << "yes" << std::endl;
}
else
{
std::cout << "no" << std::endl;
}
system("pause");
}
还有递归的,
像你这种类模板,需要显式构造函数,否则用隐式构造函数会出问题