// StringResolution.cpp: implementation of the CStringResolution class.
/***********************************************************************************************
* Copyright (c) Zhou Yuncai(周云才 江汉石油学院计算机系),2002
* All rights reserved EXCEPT as allow by the following statements:
* You can freely use this file for your own work (personal or commercial).
* But,you cannot remove this copyright and notice.
* You cannot distribute modified versions of the source code in this package.
* You cannot use this file in printed media without the express permission of the author.
* Zhou Yuncai makes no representation about the suitability of this software for any purpose.
* It is provided "as is" without express or implied warranty of any kind, including any implied
* warranty of merchantability, fitness for a particular purpose, or non-infringement.
* The entire risk as to the quality and performance of the software is with you.
* Zhou Yuncai shall not be liable for any damages suffered by you or any third party as a result of
* using or distributing this software.
* In no event will Zhou Yuncai be liable for any lost revenue, profit, or data, or for direct,
* indirect, special, consequential, incidental, or punitive damages, however caused and regardless of
* the theory of liability, arising out of the use of or inability to use software, even if Zhou Yuncai
* has been advised of the possibility of such damages.
* Should the software prove defective, you assume the cost of all necessary servicing, repair,
* or correction. If you think you've found an error, please e-mail the correction using the
* form you will find to zyc262@163.net
* address:湖北省荆州市江汉石油学院计算机系
**********************************************************************************************/
#include "stdafx.h"
#include "StringResolution.h"
#include <math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#ifdef _ERROR_INFO_H_
BEGIN_CATCH_MATH_ERROR
_BEGIN_GET_TYPE
GET_ERROR_TYPE(_DOMAIN,BIGNUMBER,Argument domain error.)
GET_ERROR_TYPE(_SING,BIGNUMBER,Argument singularity.)
GET_ERROR_TYPE(_OVERFLOW,BIGNUMBER,Overflow range error.)
GET_ERROR_TYPE(_UNDERFLOW,0,Underflow range error.)
GET_ERROR_TYPE(_TLOSS,0,Total loss of precision.)
GET_ERROR_TYPE(_PLOSS,0,Partial loss of precision.)
_END_GET_TYPE
GET_ERROR_FUNCTION(pow,power function exception. )
GET_ERROR_FUNCTION(tan,tangent function exception. )
GET_ERROR_FUNCTION(asin,arcsine function exception. )
GET_ERROR_FUNCTION(acos,arccosine function exception. )
END_CATCH_MATH_ERROR
IMPLEMENT_CATCH_ERROR(CStringResolution)
#endif
CStringResolution::CStringResolution(char*formula)
:m_oprandNum(0),m_oprandANum(0),m_operatorNum(0)
{
m_operator=0;
m_oprand=0;
m_oprandA=0;
#ifdef _ERROR_INFO_H_
_SAVE_ERROR_INFO(0,FALSE,"Good");
#endif
if(formula)
{
m_formula=formula;
Initialize();
}
}
CStringResolution::~CStringResolution()
{
if(m_operator)
delete[]m_operator;
if(m_oprand)
delete[]m_oprand;
if(m_oprandA)
delete[]m_oprandA;
}
void CStringResolution::SetFormula(char *formula)
{
if(!formula)
return;
m_formula=formula;
if(m_operator)
{
delete[]m_operator;
m_operator=0;
}
if(m_oprand)
{
delete[]m_oprand;
m_oprand=0;
}
if(m_oprandA)
{
delete[]m_oprandA;
m_oprandA=0;
}
m_oprandNum=0,m_oprandANum=0,m_operatorNum=0;
#ifdef _ERROR_INFO_H_
_SAVE_ERROR_INFO(0,FALSE,"Good!");
#endif
Initialize();
}
int CStringResolution::IsFormula()
{
int bRet=0;
stack<char> charStack;
int i,num=m_formula.GetLength();
char notChar[]={';','\'',':',' ','[',']','{','}','\\','|',',','\"','=','&','%','$','@','#','!','`','~','?'};
int notNum=sizeof notChar;
for(i=0;i<num;i++)
{
for(int j=0;j<notNum;j++)
{
if(m_formula[i]==notChar[j])
{
CString s;
s.Format("The %dth character,\"%c\" is invalidated!",i+1,m_formula[i]);
#ifdef _ERROR_INFO_H_
_SAVE_ERROR_INFO(-i-1,TRUE,s);
#endif
return -i-1;//the ith symbol is invalidating.
}
}
}
for(i=0;i<num;i++)
{
if(m_formula[i]=='(')
charStack.push('(');
if(m_formula[i]==')')
{
if(charStack.empty())
{
#ifdef _ERROR_INFO_H_
_SAVE_ERROR_INFO(1,TRUE,"There are some more right brackets!");
#endif
return 1;//right bracket if more.
}
else
charStack.pop();
}
}
if(!charStack.empty())
{
#ifdef _ERROR_INFO_H_
_SAVE_ERROR_INFO(2,TRUE,"There are some more left brackets!");
#endif
return 2;//left bracket is more.
}
return bRet;
}
BOOL CStringResolution::GetStack(OperatorStack &Operator, OperandStack &Oprand)
{
if(IsFormula())
return FALSE;//illogicality return
BOOL bRet=TRUE;
EmptyStack(Operator);
EmptyStack(Oprand);
CString string=m_formula;
bRet=bRet&&GetOperatorStack(Operator,string);
bRet=bRet&&GetOperandStack(Oprand,string);
return bRet;
}
BOOL CStringResolution::Initialize()
{
BOOL bRet=FALSE;
OperatorStack Op; OperandStack Od;
bRet=GetStack(Op,Od);
if(!bRet)
{
EmptyStack(Op);
EmptyStack(Od);
return FALSE;
}
m_oprandNum=Od.size(),m_operatorNum=Op.size();
if(m_operatorNum)
m_operator=new COperator[m_operatorNum];
if(m_oprandNum)
m_oprand=new COperand[m_oprandNum];
int i;
for(i=0;i<m_operatorNum;i++)
{
COperator*op=Op.top();
m_operator[m_operatorNum-i-1]=*op;
Op.pop();
delete op;
}
for(i=0;i<m_oprandNum;i++)
{
COperand*od=Od.top();
m_oprand[m_oprandNum-i-1]=*od;
Od.pop();
delete od;
}
m_oprandA=new COperand[m_oprandNum];
BOOL IsIn=FALSE;
for(i=0;i<m_oprandNum;i++)
{
for(int j=0;j<m_oprandANum;j++)
{
if(m_oprand[i]==m_oprandA[j])
{
IsIn=TRUE;
break;
}
}
if(!IsIn&&!m_oprand[i].m_IsConst)
{
m_oprandA[m_oprandANum]=m_oprand[i];
m_oprandANum++;
}
IsIn=FALSE;
}
return TRUE;
}
double CStringResolution::Computer(OperatorStack &Operator, OperandStack &Oprand)
{
double value =0.0;
if(Operator.empty())
{
if(Oprand.size()==1)
{
COperand* od=Oprand.top();
value=atof(od->m_name);
delete od;
return value;
}
else
{
while(!Oprand.empty())
{
COperand*od=Oprand.top();
Oprand.pop();
delete od;
}
#ifdef _ERROR_INFO_H_
_SAVE_ERROR_INFO(12,TRUE,"Oprand is less or more!");
#endif
return BIGNUMBER;
}
}
OperatorStack tmpOO;
OperandStack tmpOD;
COperator* op=0,*op1=0;
COperand *oprand=0,*oprand1=0;
op=Operator.top();
Operator.pop();
if(!Operator.empty())
op1=Operator.top();
while(op1&&(op1->m_level>op->m_level))
{
tmpOO.push(op);
if(op->m_type==BINARY)
{
if(!Oprand.empty())
{
oprand=Oprand.top();
Oprand.pop();
tmpOD.push(oprand);
}
}
op=op1;
Operator.pop();
if(!Operator.empty())
op1=Operator.top();
else
op1=0;
}
if(op->m_type==UNARY)
{
if(Oprand.empty())
{
#ifdef _ERROR_INFO_H_
_SAVE_ERROR_INFO(12,TRUE,"Oprand is less!");
#endif
return BIGNUMBER;
}
oprand=Oprand.top();
double x=computing(op,oprand);
oprand->m_name.Format("%g",x);
}
else
{
if(Oprand.empty())
{
#ifdef _ERROR_INFO_H_
_SAVE_ERROR_INFO(12,TRUE,"Oprand less!");
#endif
return BIGNUMBER;
}
oprand1=Oprand.top();
Oprand.pop();
if(Oprand.empty())
{
#ifdef _ERROR_INFO_H_
_SAVE_ERROR_INFO(12,TRUE,"Oprand less!");
#endif
return BIGNUMBER;
}
oprand=Oprand.top();
double x=computing(op,oprand1,oprand);
oprand->m_name.Format("%g",x);
delete oprand1;
}
delete op;
while(!tmpOO.empty())
{
op=tmpOO.top();
tmpOO.pop();
Operator.push(op);
}
while(!tmpOD.empty())
{
oprand=
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
表达式中只能有圆括号、函数名、运算符、常数与变量。 一、变量名的命名规则:1、可以是字符、或字符串;2、不能以数字开头;3、不能夹杂有空格、运算符或标点符号,以及以下字符:\、|、=、&、%、$、@、#、`、~. 二、函数有sin、cos、tan、cot、sqrt、asin、acos、atan、log,均需要小写。函数后的变量名可以加括号,也可以不要括号,如:log(C1)与logC1是相同的。 三、运算符有+(加)、-(减)、*(乘)、/(除)与^(幂次)。
资源推荐
资源详情
资源评论
收起资源包目录
VC++表达式解析(计算)工具源代码.rar (31个子文件)
Computer
Computer.h 1KB
Computer.dsw 541B
StdAfx.cpp 210B
ComputerDlg.cpp 9KB
EditArray.h 1003B
Computer.clw 2KB
StaticArray.cpp 2KB
StaticArray.h 861B
my.h 3KB
EditArray.cpp 3KB
ComputerDlg.h 2KB
StdAfx.h 1KB
StringResolution.cpp 18KB
StringResolution.h 5KB
Computer.rc 8KB
Resource.h 1KB
Computer.cpp 2KB
res
downx.bmp 502B
upd.bmp 502B
downd.bmp 502B
downu.bmp 502B
upu.bmp 502B
upx.bmp 502B
Computer.rc2 400B
bitmap1.bmp 630B
Computer.ico 1KB
ico00001.ico 766B
upd1.bmp 502B
ok.bmp 630B
icon1.ico 766B
Computer.dsp 5KB
共 31 条
- 1
资源评论
- 鲁夺2019-05-03代码还好,拿来参考一下。
- LostInParadises2016-02-06解决了我的燃眉之急
- sxzly2017-12-11很好的资源
拼搏
- 粉丝: 36
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功