📄 calcdlgn.txt
字号:
CString chengstr,strup="9",strdown="8";
for(i=0;i<str.GetLength();i++)//实现乘法
{
if(str.GetAt(i)=='*')
{
for(chengup=i-1;chengup>=0;chengup--)
{
if(str.GetAt(chengup)<46||str.GetAt(chengup)>57||str.GetAt(chengup)==47)
{break; }
// strup.Format("%d\n%d",chengup,i);
}
for(chengdown=i+2;chengdown<str.GetLength();chengdown++)
{
if(str.GetAt(chengdown)<46||str.GetAt(chengdown)>57||str.GetAt(chengdown)==47)
break;
}
break;
}
}
if(chengdown)
{
strup=str.Mid(chengup+1,i-chengup-1);
strdown=str.Mid(i+1,chengdown-i-1);
// MessageBox(strdown);
cheng=GetDataFloat(strup)*GetDataFloat(strdown);
chengstr.Format("%g",cheng);
str.Delete(chengup+1,chengdown-chengup-1);
str.Insert(chengup+1,chengstr);
return GetFloat(str);
}
float chu=0;int chudown=0,chuup=0;
CString chustr,struup="9",strudown="8";
for(i=0;i<str.GetLength();i++)//实现除法
{
if(str.GetAt(i)=='/')
{
for(chuup=i-1;chuup>=0;chuup--)
{
if(str.GetAt(chuup)<46||str.GetAt(chuup)>57||str.GetAt(chuup)==47)
{break; }
}
for(chudown=i+2;chudown<str.GetLength();chudown++)
{
if(str.GetAt(chudown)<46||str.GetAt(chudown)>57||str.GetAt(chudown)==47)
break;
}
break;
}
}
if(chudown)
{
struup=str.Mid(chuup+1,i-chuup-1);
strudown=str.Mid(i+1,chudown-i-1);
chu=GetDataFloat(struup)/GetDataFloat(strudown);
chustr.Format("%g",chu);
str.Delete(chuup+1,chudown-chuup-1);
str.Insert(chuup+1,chustr);
return GetFloat(str);
}
for(i=0;i<str.GetLength();i++)//实现加法
{
if(str.GetAt(i)=='+')
{
CString temp1,temp2;float xx=0;
temp1=str.Left(i);
temp2=GetFloat(str.Mid(i+1));
xx=GetDataFloat(temp1)+GetDataFloat(temp2);
s.Format("%g",xx);
return s;
}
}
}
return s;
}
CString CCalcDlg::GetInt(CString str)
{
int count[10][2];for(int i=0;i<10;i++){count[i][0]=-1;count[i][1]=-1;}
/////以上初始化数组count,使之全为-1
int cou=0;//用于填充数组count的第一维
int sgn=0;//用于记录无用的“(”和“)”,遇“(”加1,遇“)”减1。
for(i=str.GetLength()-1;i>=0;i--)
{
if(str.GetAt(i)==41)
{
if(sgn==0)
count[cou][0]=i;
sgn++;
}
if(str.GetAt(i)==40)
{
if(sgn==1)
{
count[cou][1]=i;
cou++;
}
sgn--;
}
}
if(cou)
{
int kuo=0;CString kuostr,kuoanswer;
for(kuo=0;kuo<cou;kuo++)
{
kuostr=str.Mid(count[kuo][1]+1,count[kuo][0]-count[kuo][1]-1);
// MessageBox(kuostr);
kuoanswer=GetInt(kuostr);
str.Delete(count[kuo][1],count[kuo][0]-count[kuo][1]+1);
str.Insert(count[kuo][1],kuoanswer);
return GetInt(str);
}
}
//以上得到m_str是几个(...)组成的
if(str.GetAt(0)=='+')str.Delete(0);
if(str.Mid(1).SpanIncluding("0123456789").GetLength()==str.Mid(1).GetLength())
return str;
CString s;s=str;
for(i=0;i<str.GetLength();i++)//负负得正;“--”号变“+”。
{
if(str.GetAt(i)=='-'&&i!=0&&str.GetAt(i+1)=='-')
{
str.Delete(i,2);
str.Insert(i,'+');
}
}
s=str;
for(i=0;i<str.GetLength();i++)//减法变加法;“-”号变“+-”。
{
if(str.GetAt(i)=='-'&&i!=0&&str.GetAt(i-1)!='+'&&str.GetAt(i-1)!='*'&&str.GetAt(i-1)!='/')
str.Insert(i,'+');
}
s=str;
for(i=0;i<str.GetLength();i++)//去掉多余加号
{
if(str.GetAt(i)=='+'&&str.GetAt(i+1)=='+')
{s.Delete(i);}
}
str=s;
// MessageBox(s);
if(cou==0)//式中没有括号
{
int fang=0,fangdown=0,fangup=0;
CString fangstr,strfup="9",strfdown="8";
for(i=0;i<str.GetLength();i++)//实现乘方
{
if(str.GetAt(i)=='^')
{
for(fangup=i-1;fangup>=0;fangup--)
{
if(str.GetAt(fangup)<48||str.GetAt(fangup)>57)
{break; }
// strup.Format("%d\n%d",chengup,i);
}
for(fangdown=i+2;fangdown<str.GetLength();fangdown++)
{
if(str.GetAt(fangdown)<48||str.GetAt(fangdown)>57)
break;
}
break;
}
}
if(fangdown)
{
strfup=str.Mid(fangup+1,i-fangup-1);
strfdown=str.Mid(i+1,fangdown-i-1);
// MessageBox(strdown);
fang=int(pow(GetData(strfup),GetData(strfdown)));
fangstr.Format("%d",fang);
str.Delete(fangup+1,fangdown-fangup-1);
str.Insert(fangup+1,fangstr);
return GetInt(str);
}
int cheng=0,chengdown=0,chengup=0;
CString chengstr,strup="9",strdown="8";
for(i=0;i<str.GetLength();i++)//实现乘法
{
if(str.GetAt(i)=='*')
{
for(chengup=i-1;chengup>=0;chengup--)
{
if(str.GetAt(chengup)<48||str.GetAt(chengup)>57)
{break; }
// strup.Format("%d\n%d",chengup,i);
}
for(chengdown=i+2;chengdown<str.GetLength();chengdown++)
{
if(str.GetAt(chengdown)<48||str.GetAt(chengdown)>57)
break;
}
break;
}
}
if(chengdown)
{
strup=str.Mid(chengup+1,i-chengup-1);
strdown=str.Mid(i+1,chengdown-i-1);
// MessageBox(strdown);
cheng=GetData(strup)*GetData(strdown);
chengstr.Format("%d",cheng);
str.Delete(chengup+1,chengdown-chengup-1);
str.Insert(chengup+1,chengstr);
return GetInt(str);
}
int chu=0,chudown=0,chuup=0;
CString chustr,struup="9",strudown="8";
for(i=0;i<str.GetLength();i++)//实现除法
{
if(str.GetAt(i)=='/')
{
for(chuup=i-1;chuup>=0;chuup--)
{
if(str.GetAt(chuup)<48||str.GetAt(chuup)>57)
{break; }
}
for(chudown=i+2;chudown<str.GetLength();chudown++)
{
if(str.GetAt(chudown)<48||str.GetAt(chudown)>57)
break;
}
break;
}
}
if(chudown)
{
struup=str.Mid(chuup+1,i-chuup-1);
strudown=str.Mid(i+1,chudown-i-1);
chu=GetData(struup)/GetData(strudown);
chustr.Format("%d",chu);
str.Delete(chuup+1,chudown-chuup-1);
str.Insert(chuup+1,chustr);
return GetInt(str);
}
int mo=0,modown=0,moup=0;
CString mostr,strmup="9",strmdown="8";
for(i=0;i<str.GetLength();i++)//实现求模
{
if(str.GetAt(i)=='%')
{
for(moup=i-1;moup>=0;moup--)
{
if(str.GetAt(moup)<48||str.GetAt(moup)>57)
{break; }
// strup.Format("%d\n%d",chengup,i);
}
for(modown=i+2;modown<str.GetLength();modown++)
{
if(str.GetAt(modown)<48||str.GetAt(modown)>57)
break;
}
break;
}
}
if(modown)
{
strmup=str.Mid(moup+1,i-moup-1);
strmdown=str.Mid(i+1,modown-i-1);
// MessageBox(strdown);
mo=GetData(strmup)%GetData(strmdown);
mostr.Format("%d",mo);
str.Delete(moup+1,modown-moup-1);
str.Insert(moup+1,mostr);
return GetInt(str);
}
for(i=0;i<str.GetLength();i++)//实现加法
{
if(str.GetAt(i)=='+')
{
CString temp1,temp2;int xx=0;
temp1=str.Left(i);
temp2=GetInt(str.Mid(i+1));
xx=GetData(temp1)+GetData(temp2);
s.Format("%d",xx);
return s;
}
}
}
return s;
}
int CCalcDlg::GetData(CString str)
{
// int num=0;int sign=1;
// CString temp;
// temp=str.SpanIncluding("-0123456789");
// if(temp.GetAt(0)=='-'){sign=-1;temp.Delete(0);}
// for(int i=0;i<temp.GetLength();i++)
// num=num*10+temp.GetAt(i)-48;
// num=num*sign;
// return num;
return atoi(str);
}
float CCalcDlg::GetDataFloat(CString str)
{
float num=0;
// CString temp;
// temp=str.SpanIncluding("-0123456789.");
// if(temp.GetAt(0)=='-'){sign=-1;temp.Delete(0);}
// for(int i=0;i<temp.GetLength();i++)
num=float(atof(str));
return num;
}
void CCalcDlg::OnChangeEditAnswer()
{
UpdateData(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -