?? semantic.cpp
字號:
#include "semantic.h"
extern double
Parameter, //參數T的存儲空間
Origin_x, Origin_y, //橫、縱平移距離
Scale_x, Scale_y, //橫、縱比例因子
Rot_angle; //旋轉角度
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 &Very);
//------出錯處理
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;
}
//-------循環繪制點坐標
void DrawLoop(double Start,
double End,
double Step,
struct ExprNode * HorPtr,
struct ExprNode * VerPtr ){
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){
SetPixel(hDC,x,y,black);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -