#define _CRT_SECURE_NO_WARNINGS//调用C语言的语法
#include<iostream>
#include<string>
#define OK 1
#define ERROR 0
using namespace std;
typedef char ElemType;
typedef int status;
typedef struct stack {
ElemType date;
struct stack* next;
}stack,*SqStack;
//初始化一个栈
status init(SqStack& L) {
L = new stack;
L->next = NULL;
return OK;
}
//进栈
status in(SqStack& L, ElemType e) {
SqStack p;
p = new stack;
p->date = e;
p->next = L;
L = p;
return OK;
}
ElemType Out(SqStack& L) {
ElemType r;
r = L->date;
L = L->next;
return r;
}
int main()
{
while (1) {
SqStack L;
init(L);
int length, result = 0;
ElemType ch1, ch2;
string str;
cout << "请输入待判断字符串" << endl;
cin >> str;
const char* ch = str.data();
length = strlen(ch);
for (int k = 1;k <= length / 2;k++)
{
in(L, ch[k - 1]);
}
if (length % 2 == 0) //判断字符串长度是奇数还是偶数
{
for (int i = 1;i <= length / 2;i++)
{
ch1 = Out(L);
ch2 = ch[length / 2 + i - 1];
if (ch1 == ch2) //进行判断
{
result = 1;
continue;
}
else
{
break;
}
}
}
else
{
for (int j = 1;j <= length / 2;j++)
{
ch1 = Out(L);
ch2 = ch[length / 2 + j];
if (ch1 == ch2) //进行判断
{
result = 1;
continue;
}
else
{
break;
}
}
}
//输出操作
if (result)
{
cout << "该字符串是回文字符串!" << endl;
}
else
{
cout << "该字符串不是回文字符串!" << endl;
}
}
return 0;
}
1157

被折叠的 条评论
为什么被折叠?



