Calculator of a simple identification function

xiaoxiao2021-03-06  94

Simple function calculations can be supported: such as ( , -, *, /, ^, sin, cos), and more. Written by regular expression, the core is as follows:

// compute.h // #ifndef headfile_compute_h_h

#define headfile_compute_h_h

#pragma overce

Class CCompute

{

PUBLIC:

CCompute (file * fp);

~ Ccompute ();

Void compute ();

Private:

Void Match (Char ExpectedToken);

Void error ();

Void Delspace ();

Const Double Term ();

Const Double Exp ();

Const Double Factor ();

Const Double Power ();

Const Double KeywordOperator ();

Const Double KeyExp ();

Const cstring getoperatorname ();

Bool isaccepted (char);

Bool islegal (char) const;

PUBLIC:

BOOL M_BERROR;

CSTRING M_CRESULT;

CSTRING M_STREXPRESS;

Private:

File * m_fp;

Char m_ctoke;

}

#ENDIF

// compute.h // #include "stdafx.h"

#include "compute.h"

/ ** expression

* -> { }

* -> |

* -> { }

* -> * | /

* -> { }

* -> ^

* -> | Number

* -> (exp) | KeywordOper (EXP)

** /

CCompute :: ccompute (file * fp)

{

m_fp = fp;

m_ctoke = '';

m_cresult = ""

m_berror = false;

M_STREXPRESS = ""

}

CCompute :: ~ ccompute ()

{

m_fp = null;

}

void ccompute :: error ()

{

m_berror = true;

}

Void ccompute :: match (char expectedtoken)

{

IF (m_ctoken == expectedtoken)

{

m_ctoken = getc (m_fp);

M_STREXPRESS = EXPECTEDTOKEN;

}

Else

Error ();

}

Const Double CCompute :: EXP ()

{

Double Temp;

DELSPACE ();

Temp = term ();

DELSPACE ();

While ((m_ctoken == ' ') || (m_ctoken == '-')) {

Switch (m_ctoke)

{

Case ' ':

Match (' ');

DELSPACE ();

TEMP = TERM ();

DELSPACE ();

Break;

Case '-':

Match ('-');

DELSPACE ();

Temp - = term ();

DELSPACE ();

Break;

DEFAULT:

Break;

}

}

Return Temp;

}

Const Double CCompute :: Term ()

{

Double T;

DELSPACE ();

Double Temp = Factor ();

DELSPACE ();

While ((m_ctoken == '*') || (m_ctoken == '/'))))

{

Switch (m_ctoke)

{

Case '*':

Match ('*');

DELSPACE ();

Temp * = factor ();

DELSPACE ();

Break;

Case '/':

Match ('/');

DELSPACE ();

T = Factor ();

IF (t! = 0.0)

TEMP / = T;

Else

Error ();

DELSPACE ();

Break;

DEFAULT:

DELSPACE ();

Break;

}

}

Return Temp;

}

Const Double CCompute :: Factor ()

{

DELSPACE ();

Double Temp = Power ();

DELSPACE ();

While (m_ctoken == '^')

{

Match ('^');

DELSPACE ();

Temp = POW (Temp, Power ());

DELSPACE ();

}

Return Temp;

}

Void CCompute :: Delspace ()

{

IF ('' == m_ctoke)

{

m_ctoken = getc (m_fp);

DELSPACE ();

}

}

Const Double CCompute :: Power ()

{

Double Temp = 0.0;

DELSPACE ();

IF (isdigit (m_ctoken) || (m_ctoken == '.')

|| (m_ctoken == '-') || (m_ctoken == ' '))))

{

CString Strtemp;

IF ('-' == m_ctoke)

{

FSCANF (M_FP, "% LF", & TEMP);

Strtemp.Format ("% 0.10LF", TEMP);

Temp * = -1;

M_STREXPRESS = M_ctoken Cuttail (Strtemp);

m_ctoken = getc (m_fp);

IF (isaccepted (m_ctoke))

DELSPACE ();

Else

Error ();

}

ELSE IF (' ' == m_ctoke) {

FSCANF (M_FP, "% LF", & TEMP);

Strtemp.Format ("% 0.10LF", TEMP);

M_STREXPRESS = M_ctoken Cuttail (Strtemp);

m_ctoken = getc (m_fp);

IF (isaccepted (m_ctoke))

DELSPACE ();

Else

Error ();

}

Else

{

Ungetc (M_CToken, M_FP);

FSCANF (M_FP, "% LF", & TEMP);

Strtemp.Format ("% 0.10LF", TEMP);

M_STREXPRESS = Cuttail (strTemp);

m_ctoken = getc (m_fp);

IF (isaccepted (m_ctoke))

DELSPACE ();

Else

Error ();

}

}

Else

Temp = keyexp ();

Return Temp;

}

Void ccompute :: compute ()

{

m_ctoken = getc (m_fp);

Double result = exp ();

IF (false == m_berror)

{

IF (m_ctoke == '/ n' || m_ctoken == EOF)

M_CResult.Format ("% 0.10LF", result);

}

Else

m_cresult = "error";

M_CRESULT = (m_cResult == "" "" error ": m_cresult);

}

Const cstring ccompute :: getoperatorname ()

{

Cstring _strname;

While (islegal (m_ctoke))

{

_strname = m_ctoke;

m_ctoken = getc (m_fp);

}

_strname.maker ();

M_STREXPRESS = _STRNAME;

Return_strname;

}

Bool CCompute :: Islegal (Char C) Const

{

IF ((C> 0x60 && c <0x7b)

|| (C> 0x40 && c <0x5b)

|| (C> 0x2f && c <0x3a)

|| (c == 0x25)))

Return True;

Else

Return False;

}

Const Double CCompute :: KeywordOperator ()

{

DELSPACE ();

CString _stropName = getoperatorname ();

DELSPACE ();

Match (');

DELSPACE ();

//

Double Temp = 0.0;

Double _nexp = EXP ();

IF (_STropname)

{

IF (_STROPNAME == "SQRT")

Temp = SQRT (_NEXP);

Else IF ((_ stropName == "ABS")

|| (_StropName == "FABS")) TEMP = FABS (_NEXP);

Else IF (_STropName == "sin")

Temp = sin (_nexp);

Else IF (_STropName == "COS")

Temp = cos (_nexp);

Else IF ((_ stropName == "tan")

|| (_STROPNAME == "TG"))))

Temp = TAN (_nexp);

Else IF (_ stropName == "asin")

|| (_STROPNAME == "Arsin")

|| (_StropName == "Arcsin")))))

Temp = asin (_nexp);

Else IF ((_ stropName == "ACOS")

|| (_StropName == "Arcos")

|| (_STropName == "Arccos")))

Temp = acOS (_nexp);

Else IF ((_ stropName == "Atan")

|| (_StropName == "Artan")

|| (_STROPNAME == "Arctan"))))

Temp = Atan (_nexp);

Else IF ((_ stropName == "lg")

|| (_STropName == "LGE")))

Temp = log (_nexp);

Else IF ((_ stropName == "log")

|| (_STROPNAME == "log10"))))

Temp = log10 (_nexp);

Else IF ((_ stropName == "ctan")

|| (_STROPNAME == "CTG"))

Temp = 1.0 / tan (_nexp);

Else

Error ();

}

//

Match (')');

DELSPACE ();

Return Temp;

}

Const Double CCompute :: Keyexp ()

{

Double Temp = 0.0;

DELSPACE ();

IF (m_ctoken == '(')

{

Match (');

DELSPACE ();

TEMP = Exp ();

DELSPACE ();

Match (')');

}

Else IF (islegal (m_ctoke))

Temp = keywordoperator ();

Else

Error ();

Return Temp;

}

Bool CCompute :: isaccepted (char C)

{

Return

(

('' == c) || ​​(' ' == C) ||

('-' == c) || ​​('*' == c) ||

('/' == c) || ​​('^' == c) || ​​('(' == c) || ​​(')' == C) ||

(EOF == C) || ('/ n' == c)

);

}

转载请注明原文地址:https://www.9cbs.com/read-100024.html

New Post(0)