数据结构第三章基本作业(表达式匹配)

#include<stdio.h>
#include<stdlib.h>
#include<string>
#define OK 1;
#define ERROR 0;
using namespace std;
typedef struct StackNode
{
	char data;
	struct StackNode* pNext;
}stackNode, *pStackNode;
int flag = 0;
int init(pStackNode& pHead)
{
	flag = 0;
	pHead = NULL;
	return OK;
}
int push(pStackNode& pHead, char c)
{
	flag--;
	stackNode* p;
	p = (stackNode*)malloc(sizeof(stackNode));
	p->data = c;
	p->pNext = pHead;
	pHead = p;
	return OK;
}
char pop(pStackNode& pHead)
{
	if (pHead == NULL)
	{
		return ERROR;
	}
	flag++;
	pStackNode p;
	p = pHead;
	pHead = pHead->pNext;
	return p->data;
}
bool isEmpty(pStackNode pHead)
{
	if (pHead == NULL)
	{
		return  true;
	}
	else
	{
		return false;
	}
}
bool isPair(char a, char b)
{
	if (a == '('&&b == ')')
	{
		return true;
	}
	else if (a == '['&&b == ']')
	{
		return true;
	}
	else if (a== '{'&&b == '}')
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool judge(string s)
{
	pStackNode pHead;
	init(pHead);
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '(' || s[i] == '[' || s[i] == '{')
		{
			push(pHead, s[i]);
		}
		if (s[i] == ')' || s[i] == ']' || s[i] == '}')
		{
			if (!isPair(pop(pHead), s[i]))
			{
				return false;
			}
		}
	}
	
	if (isEmpty(pHead)&& flag==0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	//string s="3*{2*[2*(x+y)/(1-x)]+3*(3+4)})";
	string s = "2*(2+1)";
	if (judge(s))
	{
		printf("匹配!");
	}
	else
	{
		printf("不匹配!");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值