⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 signrule.cpp

📁 这是一个用c++写的预测分析法程序,是编译原理课程设计的内容,可以预测分析语言
💻 CPP
字号:

#include <stdio.h>
#include "SignRule.h"

/*------------------- RULE 规则类的实现 ----------------*/
RULE::RULE()
{
	Left = 0;
	ZeroMemory(Right,sizeof(SIGN) * (MAX_RULE_LEN + 1));
}

RULE::RULE(const RULE& other)
{
	Left = other.Left;
	strcpy(Right,other.Right);
}

void RULE::operator=(const RULE& other)
{
	Left = other.Left;
	strcpy(Right,other.Right);
}

bool RULE::operator==(const RULE& other)
{
	if (Left == other.Left && strcmp(Right,other.Right) == 0 )
		return true;
	else
		return false;
}

/*------------------- SIGN_SET 符号集合类的实现 ----------------*/
SIGN_SET::SIGN_SET()
{
	SignNum = 0;
	pSigns  = NULL;
}

SIGN_SET::SIGN_SET(const SIGN_SET& other)
{
	SignNum = 0;
	pSigns  = NULL;
	*this = other;
}

SIGN_SET::~SIGN_SET()
{
	if (pSigns != NULL) delete []pSigns;
}

int SIGN_SET::GetSignNum()
{
	return SignNum;
}

bool SIGN_SET::IsSignInSet(SIGN sign)
{
	for(int i = 0; i < SignNum; i++)
		if (pSigns[i] == sign) return true;
	return false;
}

bool SIGN_SET::AppendSign(SIGN sign,bool CanRepeat)
{	// 不允许重复,但是需要加入的符号已经存在
	if (!CanRepeat && IsSignInSet(sign) ) return false;	

	SIGN* pTemp;
	pTemp = pSigns;
	pSigns = new SIGN[SignNum + 1];
	ZeroMemory(pSigns,sizeof(SIGN) * (SignNum + 1));
	CopyMemory(pSigns,pTemp,sizeof(SIGN) * SignNum);
	*(pSigns + SignNum) = sign;

	SignNum = SignNum + 1;
	delete []pTemp;

	return true;
}

bool SIGN_SET::DeleteSign(SIGN sign,bool CheckRepeat)
{
	if (!IsSignInSet(sign)) return false;

	int   i,n;
	SIGN* pTemp;

	pTemp = pSigns;
	pSigns = new SIGN[SignNum - 1];
	n = 0;
	for(i = 0; i < SignNum; i++)
		if (pTemp[i] != sign)
			pSigns[n++] = pTemp[i];
	SignNum = SignNum - 1;
	delete []pTemp;
	// 删除多个重复的符号
	if (CheckRepeat) 
		while (DeleteSign(sign));

	return true;
}

bool SIGN_SET::DeleteSign(int no)
{
	if (no >= 0 && no < SignNum)
	{
		int i;
		for(i = no; i < SignNum - 1; i++)		
			pSigns[i] = pSigns[i + 1];
		SignNum = SignNum - 1;
		return true;
	}
	else return false;
}

SIGN SIGN_SET::operator[](int no)
{
	if (no >= 0 && no < SignNum) return pSigns[no];
	else return 0;
}

void SIGN_SET::operator=(const SIGN_SET& other)
{
	if (pSigns != NULL) delete []pSigns;
	SignNum = other.SignNum;
	pSigns = new SIGN[SignNum];
	ZeroMemory(pSigns,sizeof(SIGN) * SignNum);
	CopyMemory(pSigns,other.pSigns,sizeof(SIGN) * SignNum);
}

void SIGN_SET::operator=(const SIGN* pString)
{
	int i,len;
	Reset();
	len = strlen(pString);
	for(i = 0; i < len; i++)
		AppendSign(pString[i],true);
}

bool SIGN_SET::operator+=(const SIGN_SET& other)
{
	int i;	
	for(i = 0; i < other.SignNum; i++)
		AppendSign(other.pSigns[i]);
	return true;
}

void SIGN_SET::Reset()
{
	if (pSigns != NULL) delete []pSigns;
	pSigns = NULL;
	SignNum = 0;
}	

void SIGN_SET::Output(LPTSTR pStr,int startpos)
{	
	TCHAR szSign[2];

	pStr[0] = 0;
	for(int i = startpos; i < SignNum; i++) 
	{
		wsprintf(szSign,TEXT("%c"),pSigns[i]);	
		lstrcat(pStr,szSign);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -