📄 express.cpp
字号:
d2=atof(s2);
d3=d1/d2;
s1.Format("%f",d3);
str=ReplaceBetweenPos(str,s1,start,end);
GetSingleValue(str,result);
}
}
else
if (IsTimesExist(str))
{
pos=GetJJCCPos(str,"*");
GetJJCCTwoParm(str,pos,&s1,&s2,&start,&end);
d1=atof(s1);
d2=atof(s2);
d3=d1*d2;
s1.Format("%f",d3);
str=ReplaceBetweenPos(str,s1,start,end);
GetSingleValue(str,result);
}
else
if (IsDivideExist(str))
{
pos=GetJJCCPos(str,"/");
GetJJCCTwoParm(str,pos,&s1,&s2,&start,&end);
d1=atof(s1);
d2=atof(s2);
d3=d1/d2;
s1.Format("%f",d3);
str=ReplaceBetweenPos(str,s1,start,end);
GetSingleValue(str,result);
}
else
if (IsAddExist(str)&&IsPulsExist(str))
{
int pos1=GetJJCCPos(str,"*");
pos=GetJJCCPos(str,"/");
if (pos>pos1)
{
pos=GetJJCCPos(str,"+");
GetJJCCTwoParm(str,pos,&s1,&s2,&start,&end);
d1=atof(s1);
d2=atof(s2);
d3=d1+d2;
s1.Format("%f",d3);
str=ReplaceBetweenPos(str,s1,start,end);
GetSingleValue(str,result);
}
else
{
pos=GetJJCCPos(str,"-");
GetJJCCTwoParm(str,pos,&s1,&s2,&start,&end);
d1=atof(s1);
d2=atof(s2);
d3=d1-d2;
s1.Format("%f",d3);
str=ReplaceBetweenPos(str,s1,start,end);
GetSingleValue(str,result);
}
}
else
if (IsAddExist(str))
{
pos=GetJJCCPos(str,"+");
GetJJCCTwoParm(str,pos,&s1,&s2,&start,&end);
d1=atof(s1);
d2=atof(s2);
d3=d1+d2;
s1.Format("%f",d3);
str=ReplaceBetweenPos(str,s1,start,end);
GetSingleValue(str,result);
}
else
if (IsPulsExist(str))
{
pos=GetJJCCPos(str,"-");
GetJJCCTwoParm(str,pos,&s1,&s2,&start,&end);
d1=atof(s1);
d2=atof(s2);
d3=d1-d2;
s1.Format("%f",d3);
str=ReplaceBetweenPos(str,s1,start,end);
GetSingleValue(str,result);
}
else
{
d1=atof(str);
result=d1;
return ;
}
}
int CExpress::GetJJCCNum(CString str,CString name)
{
int n=0;
int falg=0;
while(str.Find(name)!=-1)
{
n=str.Find(name);
str.SetAt(n,'@');
falg++;
}
return falg;
}
void CExpress::GetSignFuncVal(CString str,double &result)
{
int s_s,s_e,p_s,p_e,style;
CString p1,p2;
double result1,result2;
if (IsContainSign(str))
{
style=GetSignStyle(str,&s_s,&s_e,&p_s,&p_e);
if (style==SIN)
{
p1=GetParmString(str,p_s,p_e);
GetSingleValue(p1,result1);
p2.Format("%f",sin(result1*PI/180.0));
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (style==COS)
{
p1=GetParmString(str,p_s,p_e);
GetSingleValue(p1,result1);
p2.Format("%f",cos(result1*PI/180.0));
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (style==TAN)
{
p1=GetParmString(str,p_s,p_e);
GetSingleValue(p1,result1);
p2.Format("%f",tan(result1*PI/180.0));
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (style==EXP)
{
p1=GetParmString(str,p_s,p_e);
GetSingleValue(p1,result1);
p2.Format("%f",exp(result1));
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (style==LOG)
{
GetParmTwo(str,p_s,p_e,&p1,&p2);
GetSingleValue(p1,result1);
GetSingleValue(p2,result2);
p2.Format("%f",log(result2)/log(result1));
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (style==ASIN)
{
// AfxMessageBox("asin");
p1=GetParmString(str,p_s,p_e);
GetSingleValue(p1,result1);
p2.Format("%f",asin(result1)*180.0/PI);
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (style==ACOS)
{
// AfxMessageBox("acos");
p1=GetParmString(str,p_s,p_e);
GetSingleValue(p1,result1);
p2.Format("%f",acos(result1)*180.0/PI);
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (style==ATAN)
{
// AfxMessageBox("atan");
p1=GetParmString(str,p_s,p_e);
GetSingleValue(p1,result1);
p2.Format("%f",atan(result1)*180.0/PI);
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (style==SIGN)
{
p1=GetParmString(str,p_s,p_e);
GetSingleValue(p1,result1);
int i=0;
if (result1<0)
{
i=-1;
}
else
if (result1>0)
{
i=1;
}
else
if (abs(result1)<0.000000001)
{
i=0;
}
p2.Format("%d",i);
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (style==POW)
{
// AfxMessageBox("pow");
GetParmTwo(str,p_s,p_e,&p1,&p2);
GetSingleValue(p1,result1);
GetSingleValue(p2,result2);
p2.Format("%f",pow(result1,result2));
str=ReplaceBetweenPos(str,p2,s_s,p_e);
}
if (IsContainSign(str))
GetSignFuncVal(str,result);
else
{
// AfxMessageBox("GetSingleValue");
GetSingleValue(str,result);
}
}
else
{
GetSingleValue(str,result);
}
}
bool CExpress::IsContainBracket(CString str)
{
if(str.Find("(",0)==-1) return false;
else return true;
}
void CExpress::GetBracketResult(CString str,double &result)
{
int start,end;
CString s1,s2;
double r;
fprintf(resultF,"step%d=%s\n",stepNum+1,str);
step[stepNum++]=str+"/n";
if(IsContainBracket(str))
{
GetBracketPos(str,&start,&end);
s1=GetStrFromStartAndEnd(str,start,end);
GetSignFuncVal(s1,r);
s2.Format("%f",r);
str=ReplaceBetweenPos(str,s2,start,end);
if (IsContainBracket(str))
{
fprintf(resultF,"step%d=%s\n",stepNum+1,str);
step[stepNum++]=str+"/n";
GetBracketResult(str,result);
}
else
{
step[stepNum++]=str+"/n";
GetSignFuncVal(str,result);
fprintf(resultF,"result=%.4f\n",result);
}
}
else
{
step[stepNum++]=str+"/n";
GetSignFuncVal(str,result);
fprintf(resultF,"result=%.4f\n",result);
}
}
double CExpress::GetResultFrString(CString str)
{
if (!Cheak(str))
{
AfxMessageBox("表达式非法!");
return 0;
}
resultF=fopen("result.txt","w");
fprintf(resultF,"****************计算步骤*******************\n");
stepNum=0;
double result;
GetBracketResult(str,result);
fclose(resultF);
return result;
}
double CExpress::GetResultFrString1(CString str,CString parm,double val)
{
//if (!Cheak(str))
{
// AfxMessageBox("表达式非法!");
// return 0;
}
CString st;
st.Format("%f",val);
str.Replace(parm,st);
return GetResultFrString(str);
}
double CExpress::GetResultFrString2(CString str,CString parm1,double val1,CString parm2,double val2)
{
if (!Cheak(str))
{
AfxMessageBox("表达式非法!");
return 0;
}
CString st1;
st1.Format("%f",val1);
str.Replace(parm1,st1);
st1.Format("%f",val2);
str.Replace(parm2,st1);
return GetResultFrString(str);
}double CExpress::GetResultFrStringN(CString str,CString parm[],double val[],int N)
{
if (!Cheak(str))
{
AfxMessageBox("表达式非法!");
return 0;
}
CString st;
for (int i=0;i<N;i++)
{
st.Format("%f",val[i]);
str.Replace(parm[i],st);
}
return GetResultFrString(str);
}
BOOL CExpress::Cheak(CString &str)
{
CString s=str;
int i,j;
int pos;
i=0;j=0;
//////////////////////////////
while (s.Find("(")!=-1)
{
pos=s.Find("(");
if (s.GetAt(pos+1)==')')
{
return false;
}
s.SetAt(pos,'!');
i++;
}
while (s.Find(")")!=-1)
{
pos=s.Find(")");
s.SetAt(pos,'!');
if (s.GetAt(pos-1)=='(')
{
return false;
}
j++;
}
if (i!=j) return false;
i=0;j=0;
while (s.Find("[")!=-1)
{
pos=s.Find("[");
if (s.GetAt(pos+1)==']')
{
return false;
}
s.SetAt(pos,'!');
i++;
}
while (s.Find("]")!=-1)
{
pos=s.Find("]");
if (s.GetAt(pos-1)=='[')
{
return false;
}
s.SetAt(pos,'!');
j++;
}
if (i!=j) return false;
//////////////////////////////
s=str;
while(IsContainSign(s))
{
int type,a,b,c,d;
type=GetSignStyle(s,&a,&b,&c,&d);
if (type==POW||type==LOG)
{
CString st=GetStrFromStartAndEnd(s,c,d);
if (st.Find(",")==-1)
{
return false;
}
}
else
{
CString st=GetStrFromStartAndEnd(s,c,d);
if (st.Find(",")!=-1)
{
return false;
}
}
s=ReplaceBetweenPos(s,"@",a,d);
}
////////////////////////
/*
str.Replace("X","@");
str.Replace("Y","#");
str.Replace("Z","$");
str.MakeLower();
str.Replace("@","X");
str.Replace("#","Y");
str.Replace("$","Z");*/
str.Replace("E","2.71828");
str.Replace("PI","3.1415926");
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -