用一个算法小芝士怎样判断输入回文内容

用一个算法小芝士怎样判断输入回文内容

什么是回文?

在小学就学过,这个就是例如蜜蜂采蜂蜜,奶牛喝牛奶,跑步是我们大家喊的121这样样式的就是回文,所以这样运用算法的判断这个输入的内容是回文呢?

让我们来实现它:

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#include "string"
#define NULL 0
#define OK 1
#define ERROR 0
#define false 0
#define ture 1
#define MAXSIZE  1024   
typedef  int datatype;
typedef  struct
{
	datatype  data[MAXSIZE];
	int  top;
}SeqStack;



//顺序栈

//顺序栈置空栈:首先建立栈空间,然后初始化栈顶指针。
SeqStack* Init_SeqStack()
{
	SeqStack* s;
	s = new SeqStack;
	s->top = -1;
	return s;
}
//顺序栈判空栈
int Empty_SeqStack(SeqStack* s)
{
	if (s->top == -1)  return 1;
	else  return 0;
}
//顺序栈入栈
int Push_SeqStack(SeqStack* s, datatype  x)
{
	if (s->top == MAXSIZE - 1)  return 0; //栈满不能入栈
	else {
		s->top++;
		s->data[s->top] = x;
		return 1;
	}
}
//顺序栈出栈
int  Pop_SeqStack(SeqStack* s, datatype* x)
{
	if (Empty_SeqStack(s))  return 0; //栈空不能出栈 
	else {
		*x = s->data[s->top];
		s->top--;  return 1;        //栈顶元素存入*x,返回
	}
}

bool Expression(SeqStack* S) {
	int x;
	char ch;
	printf("请输入待匹配的表达式字符串:(回车键为结束符)\n");
	ch = getchar();
	while (ch != '\n') 
	{
		if (ch == '(')
			Push_SeqStack(S, ch);
		else if (ch == ')')
		{
			if (S->top == -1)
				return false;
			else
			
				Pop_SeqStack(S,&x);
			
		
		}
		ch = getchar();
	}
	if (S->top == -1)
		return ture;
	else
		return false;
}

int main()
{
	SeqStack* S;
	bool flag;
	S = Init_SeqStack();
	if (S == 0)
		printf("初始化顺序表失败!\n");
	else
	{
		flag = Expression(S);
		if (flag)
			printf("匹配成功");
		else
			printf("匹配失败!");

	}
	return 0;
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值