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

📄 calc.h

📁 科思ERP部分源码及控件
💻 H
字号:
//---------------------------------------------------------------------------
#include "vcl.h"
#ifndef CalcH
#define CalcH

#define DIVISION_BY_0 1
#define ILEGAL_OPERATION 2
#define UNDEFINED_VARIABLE 3
#define INVALID_DOMAIN 4

typedef bool __fastcall (__closure *TIsVariable)(AnsiString Variable);
typedef double __fastcall (__closure *TGetValue)(AnsiString Variable);
class TExpression;
//--------------------------------------------------------------------
//   TExpression
//----------------------------------------------------------------------
class TExpression : public TObject
{
public:
	TExpression &operator=(TExpression& expr);
	TExpression(TExpression & expresie);

	struct nod
	{
		struct nod *left,*right;
		unsigned char operatie;
		union {
                    double valoare;
		    AnsiString *valoarestr;
                };
	};

	typedef nod NOD;
	typedef nod *arbore;
        int code;

	arbore CloneTree(void);
	int UpdateArbore(void);		// Update the tree
	arbore GetArbore();
	int Value(double & valoare);	// gets the value of the expression
	int ChangeExpression(AnsiString expresie);	// Change expression

	__fastcall TExpression();
	__fastcall ~TExpression();

        __property TIsVariable OnIsVariable = {read=FIsVariable, write=FIsVariable};
        __property TGetValue OnGetValue = {read=FGetValue, write=FGetValue};
        __property AnsiString Expression = {read=m_definitie}; 
private:
        TIsVariable FIsVariable;
        TGetValue FGetValue;
	arbore sgOp();
	arbore logicalOp();
	void SkipSpaces();
	AnsiString m_definitie;			// the expression in string
	int pozitie;					// string parsing variable
	arbore m_Arbore;				// the expresion as a binary tree

	double vexp ( arbore a );
	arbore factor (void);			// the partial expresion parsing functions
	arbore identificator(void);
	arbore putere(void);
	arbore termen(void);
	arbore expresie(void);
	void elibmem ( arbore a);
	arbore clone(arbore arb);
};
#endif
//------------------------------------------------------------------------
//   说明:
//-------------------------------------------------------------------------
/*
1。基本使用方法
    Expression = new TExpression();//生成实例
    double r; //用于存储结果
    Expression->ChangeExpression(Text); //Text为字符串表达式
    int i = Expression->Value(r); //进行运算,结果及保存在 r 中,i为操作结果,如下
                                    0:成功
                                    1:DIVISION_BY_0 除零
                                    2:ILEGAL_OPERATION
                                    3:UNDEFINED_VARIABLE 包含未定义的变量
                                    4:INVALID_DOMAIN  包含未定义的操作符
    delete Expression;  //删除实例
2。使用自定义变量
    Expression->OnIsVariable = IsVariable;
                //是否是一个定义的变量的事件,ChangeExpression时调用
    Expression->OnGetValue = GetValue;
                //取变量的值

bool __fastcall TForm1::IsVariable(AnsiString Variable)//定义事件处理函数
{
    if(Variable.UpperCase() == "VARIABLE1")
        return true;
    else
        return false;
}
double __fastcall TForm1::GetValue(AnsiString Variable)//定义事件处理函数
{
    if(Variable.UpperCase() == "VARIABLE1")
        return StrToInt(a->Text);
    else
        return 0;
}
3。求值能力
    "(3+5)*2"      16
   支持的运算符:
   '+'           加       3+5
   '-'           减
   '*'           乘
   '/'           除
   sin           sin
   cos           cos
   exp           e的指数
   sqrt          平方
   log           自然对数
   tg            tan
   ctg           1 / tan
   asin          asin
   acos          acos
   atg           atan
   '|'           绝对值
   '^'           幂
   '@'           return (a->valoare);
   '<'           小于
   '>'           大于
   '!'           非
   '(' ')'       扩号
   变量:
   例如:  5+VARIABLE1*3*VARIABLE2
   当    VARIABLE1   为   3
         VARIABLE2   为   2   结果    23
   Note: OnIsVariable 事件 返回 true (当Variable 为 "VARIABLE1" "VARIABLE2")
          OnGetValue   事件 返回 3 (当Variable 为 "VARIABLE1")
                                 2 (当Variable 为 "VARIABLE2")
*/





⌨️ 快捷键说明

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