刚学习MFC,我用MFC写一个计算器程序,考虑优先级的额问题,发现计算不出得数,求各位大神改正
HCURSOR CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::OnButton30() //1
{
// TODO: Add your control notification handler code here
m_strValue +="1";
UpdateData(FALSE);
}
void CMyDlg::OnButton22() //3
{
// TODO: Add your control notification handler code here
m_strValue +="3";
UpdateData(FALSE);
}
void CMyDlg::OnButton21() //2
{
// TODO: Add your control notification handler code here
m_strValue +="2";
UpdateData(FALSE);
}
void CMyDlg::OnButton29() //4
{
// TODO: Add your control notification handler code here
m_strValue +="4";
UpdateData(FALSE);
}
void CMyDlg::OnButton20() //5
{
// TODO: Add your control notification handler code here
m_strValue +="5";
UpdateData(FALSE);
}
void CMyDlg::OnButton19() //6
{
// TODO: Add your control notification handler code here
m_strValue +="6";
UpdateData(FALSE);
}
void CMyDlg::OnButton28() //7
{
// TODO: Add your control notification handler code here
m_strValue +="7";
UpdateData(FALSE);
}
void CMyDlg::OnButton23() //8
{
// TODO: Add your control notification handler code here
m_strValue +="8";
UpdateData(FALSE);
}
void CMyDlg::OnButton18() //9
{
// TODO: Add your control notification handler code here
m_strValue +="9";
UpdateData(FALSE);
}
void CMyDlg::OnButton3() //0
{
// TODO: Add your control notification handler code here
m_strValue +="0";
UpdateData(FALSE);
}
void CMyDlg::OnButton6() //后退
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(!m_strValue.IsEmpty())
{
m_strValue=m_strValue.Left(m_strValue.GetLength ()-1);//移除最右边一个字符
}
UpdateData(FALSE);
}
void CMyDlg::OnButton10() //清空
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_strValue="0";//将对话框初始值设为0
UpdateData(FALSE);
}
void CMyDlg::OnButton2() //加法运算
{
// TODO: Add your control notification handler code here
m_strValue+="+";
UpdateData(FALSE);
}
void CMyDlg::OnButton7() //减法运算
{
// TODO: Add your control notification handler code here
m_strValue +="-";
UpdateData(FALSE);
}
void CMyDlg::OnButton9() //乘法
{
// TODO: Add your control notification handler code here
m_strValue +="*";
UpdateData(FALSE);
}
void CMyDlg::OnButton8() //除法
{
// TODO: Add your control notification handler code here
m_strValue+="/";
UpdateData(FALSE);
}
void CMyDlg::OnButton4() //小数点
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(-1==m_strValue.Find ('.'))
{
m_strValue+=".";
}
UpdateData(FALSE);
}
void CMyDlg::OnButton13() //cos
{
// TODO: Add your control notification handler code here
m_strValue +="cos";
UpdateData(FALSE);
}
void CMyDlg::OnButton12() //tan
{
// TODO: Add your control notification handler code here
m_strValue+="tan";
UpdateData(FALSE);
}
void CMyDlg::OnButton14()//sin
{
// TODO: Add your control notification handler code here
m_strValue+="sin";
UpdateData(FALSE);
}
void CMyDlg::OnButton11() //ln
{
// TODO: Add your control notification handler code here
m_strValue+="ln(";
UpdateData(FALSE);
}
void CMyDlg::OnButton5() //log
{
// TODO: Add your control notification handler code here
m_strValue+="log(";
UpdateData(FALSE);
}
void CMyDlg::OnButton24() //√X
{
// TODO: Add your control notification handler code here
m_strValue+="√";
UpdateData(FALSE);
}
void CMyDlg::OnButton16() //PI
{
// TODO: Add your control notification handler code here
m_strValue+="π";
UpdateData(FALSE);
}
void CMyDlg::OnButton17() //e
{
// TODO: Add your control notification handler code here
m_strValue+="e";
UpdateData(FALSE);
}
void CMyDlg::OnButton1() //=
{
// TODO: Add your control notification handler code here
m_strValue+="=";
UpdateData(FALSE);
string m =(LPCTSTR)m_strValue;
sur=shorten(m);
calculate(sur);
}
bool isone(char c){
return (c=='+' || c=='-');
}
bool istwo(char c){
return (c=='*' || c=='/');
}
string CMyDlg::shorten(string m){
stack s;
string sur;
int i;
for(i=0;i<m.size();i++){
if(isdigit(m[i]) || m[i]=='.')
{
while(isdigit(m[i]) || m[i]=='.') sur += m[i++];
i--;
sur += '$';
}
else if(isone(m[i])){
while(s.size() && (isone(s.top()) || istwo(s.top()))){
sur+=s.top();
s.pop();
}
s.push(m[i]);
}
else if(m[i]==')'){
while(s.top()!='('){
sur+=s.top();
s.pop();
}
s.pop();
}
else if(istwo(m[i])){
while(s.size() && istwo(s.top())){
sur+=s.top();
s.pop();
}
s.push(m[i]);
}
else s.push(m[i]);
}
while(s.size()){
sur+=s.top();
s.pop();
}
return sur;
}//考虑优先级
double CMyDlg::tentimes(int n){
double res=1;
for(int i=0;i
res *= 10;
}
return res;
}
double CMyDlg::str2double(string s){
double res=0;
char c;
int dec=0;
for(int i=1;i
c=s[i-1];
if(c=='.') dec=i;
else if(!dec) res = res*10 + c-'0';
else res += (c-'0')/tentimes(i-dec);
}
return res;
}
double CMyDlg::calculate(string s){
double res, t;
stack num;
string temp;
int i;
for(i=0;i<s.size();i++){
temp="";
if(isdigit(s[i]) || s[i]=='.'){
while(isdigit(s[i]) || s[i]=='.') temp+=s[i++];
num.push(str2double(temp));
}
else{
switch (s[i]){
case '+': t=num.top(); num.pop(); t+=num.top();num.pop();num.push(t);break;
case '-': t=num.top(); num.pop(); t=num.top()-t;num.pop();num.push(t);break;
case '*': t=num.top(); num.pop(); t*=num.top();num.pop();num.push(t);break;
case '/': t=num.top(); num.pop(); t=num.top()/t;num.pop();num.push(t);break;
system("pause");break;
}
}
}
res=num.top();
return res;
}