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

📄 semantic.cpp

📁 用VC做的一个简单编译器.实现简单的画图
💻 CPP
字号:

// ----------------------------- semantic.cpp ---------------------------------

#include "semantic.h"

extern double
	Parameter,					// 参数T的存储空间
	Origin_x, Origin_y,			// 横、纵平移距离
	Scale_x,  Scale_y, 			// 横、纵比例因子
	Rot_angle;					// 旋转角度
extern int color_line;			// 线条颜色

double GetExprValue(struct ExprNode * root);		// 获得表达式的值
void DrawPixel(unsigned long x, unsigned long y);	// 绘制一个点
void DrawLoop(double Start,							// 绘制图形
			  double End,
			  double Step,
			  struct ExprNode * HorPtr,
			  struct ExprNode * VerPtr);
void DelExprTree(struct ExprNode * root);			// 删除一棵语法树

static void Errmsg (char *string);					// 出错处理
static void CalcCoord(struct ExprNode * Hor_Exp,	// 计算点的坐标
					  struct ExprNode * Ver_Exp,
					  double &Hor_x,
					  double &Ver_y);

// ------------------------ 出错处理
void Errmsg (char *string) { exit (1); }

// ------------------------ 计算被绘制点的坐标
static void CalcCoord(struct ExprNode * Hor_Exp,
					  struct ExprNode * Ver_Exp,
					  double &Hor_x,
					  double &Ver_y)
{
// ------------------------ 完善此部分代码
	double HorCord,VerCord,Hor_tmp;
	//计算表达式的值,得到点的原始坐标
	HorCord=GetExprValue(Hor_Exp);
	VerCord=GetExprValue(Ver_Exp);
	//进行比例变换
	HorCord*=Scale_x;
	VerCord*=Scale_y;
	//进行旋转变换
	Hor_tmp=HorCord*cos(Rot_angle)+VerCord*sin(Rot_angle);
	VerCord=VerCord*cos(Rot_angle)-HorCord*sin(Rot_angle);
	HorCord=Hor_tmp;
	//进行平移变换
	HorCord+=Origin_x;
	VerCord+=Origin_y;
	//返回变换后点的坐标
	Hor_x=HorCord;
	Ver_y=VerCord;


//MessageBox(NULL,"完善计算被绘制点的坐标 !","提示",MB_OK);

}

// ------------------------ 循环绘制点坐标
void DrawLoop(double Start,
			  double End,
			  double Step,
			  struct ExprNode * HorPtr,
			  struct ExprNode * VerPtr)
{	extern double Parameter;
	
	// ------------------------ 完善此部分代码
	extern double Parameter;
	double x,y;
	for(Parameter=Start;Parameter<=End;Parameter+=Step)
	{
		CalcCoord(HorPtr,VerPtr,x,y);
		DrawPixel((unsigned long)x,(unsigned long)y);
	}

}

// ------------------------ 计算表达式的值
double GetExprValue(struct ExprNode * root)
{	
	// ------------------------ 完善此部分代码
	if(root==NULL)return 0.0;
	switch(root->OpCode)
	{
	case PLUS:
		return GetExprValue(root->Content.CaseOperator.Left)+GetExprValue(root->Content.CaseOperator.Right);
	case MINUS:
		return GetExprValue(root->Content.CaseOperator.Left)-GetExprValue(root->Content.CaseOperator.Right);
	case MUL:
		return GetExprValue(root->Content.CaseOperator.Left)*GetExprValue(root->Content.CaseOperator.Right);
	case DIV:
		return GetExprValue(root->Content.CaseOperator.Left)/GetExprValue(root->Content.CaseOperator.Right);
	case POWER:
		return pow(GetExprValue(root->Content.CaseOperator.Left),GetExprValue(root->Content.CaseOperator.Right));
	case FUNC:
		return (*root->Content.CaseFunc.MathFuncPtr)(GetExprValue(root->Content.CaseFunc.Child));
	case CONST_ID:
		return root->Content.CaseConst;
	case T:
		return *(root->Content.CaseParmPtr);
	default:
		return 0.0;
	}
}

// ------------------------ 删除一棵语法树
void DelExprTree(struct ExprNode * root)
{	if (root == NULL) return;
	switch (root -> OpCode)
	{   case PLUS  :			// 两个孩子的内部节点
	    case MINUS :
	    case MUL   :
	    case DIV   :
	    case POWER :
			DelExprTree (root->Content.CaseOperator.Left ) ;
			DelExprTree (root->Content.CaseOperator.Right) ;
			break ;
	    case FUNC  : 			// 一个孩子的内部节点
			DelExprTree (root->Content.CaseFunc.Child) ;
			break ;
	    default    : 			// 叶子节点
			break ;
	}
	delete(root) ;				// 删除叶子节点
}



// ------------------------ 绘制一个点
void DrawPixel(unsigned long x, unsigned long y)
{
	extern color_line;
	SetPixel(hDC, x, y, color_line);
	SetPixel(hDC, x+1, y, color_line);
	SetPixel(hDC, x, y+1, color_line);
	SetPixel(hDC, x+1, y+1, color_line);
}
//#ifdef _BC_COMPILER
//int InGraphMode=0;
//int InitGraph(void)
//{
//	int gd=DETECT,gm;
//	if(InGraphMode) return 0;
//	registerbgidriver(EGAVGA_drive);
//	initgraph(&gd,&gm,"");
//	setcolor(-1);
//	InGraphMode=1;
//	return 1;
//}

⌨️ 快捷键说明

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