【www.arisingsemi.com--语言培训】

回文结构

1、设计题目  回文检测
设计一个程序,,对输入的字符串进行检测,以判断该字符串是否是回文数据,并输出判断结果。如:“abcxcba”或“abccba”都是回文数据。


2、设计目的


(1)利用栈和队列的原理设计上述题目;


(2)键盘输入字符串;


(3)键盘输出判断结果(如abcxcba和abccba是回文数据)。
三、设计内容:
利用栈的性质。
先进先出,先将字符串全部入队,然后将一半的字符进行依次比较,
四、程序流程图

五、源程序
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define EMPTY 0
#define FULL 10000
#define MAX 10000
typedef char data;
typedef struct elem
{
data d;
struct elem *next;
}elem;
typedef struct stack
{
int cnt;
elem *top;
}stack;
void initialize(stack *stk);
void push(data d, stack *stk);
data pop(stack *stk);
bool empty(const stack *stk);
bool full(const stack *stk);
void initialize(stack *stk)
{
stk->cnt = 0;
stk->top = NULL;
}
bool empty(const stack *stk)
{
return stk->cnt == EMPTY;
}
bool full(const stack *stk)
{
return stk->cnt == FULL;
}
void push(data d, stack *stk)
{
elem *p;

if (。
full(stk))
{
p = (elem *)malloc(sizeof(elem));
p->d = d;
p->next = stk->top;
stk->top = p;
stk->cnt++;
}
}
data pop(stack *stk)
{
data d;
elem *p;
if(。empty(stk))
{
d = stk->top->d;
p = stk->top;
stk->top = stk->top->next;
stk->cnt--;
free(p);
}

return d;
}
int main(void)
{
printf("输入字符串(必须以@结尾来确定是否输入结束):");
data input[MAX];
stack temp;
int i = 0;
int flag = 0;
initialize(&temp);
scanf("%s", &input);
while (input[i] 。= "@")
{
push(input[i], &temp);
i++;
}
while (。empty(&temp))
{
if (->d == input[flag])
{
pop(&temp);
flag++;
}
else
{
printf("此字符序列不是回文数。\n");
break;
}
}
if (empty(&temp))
printf("此字符序列是回文数。\n");
return 1;
}


(6)软件测试与实验
输入54345
判断是回文

输入54321
判断不是回文

本文来源:http://www.arisingsemi.com/yuyanliuxue/141840/