分类: C/C++
2008-10-14 12:13:42
public:现在,我们编写构造函数:
char m_sign; //记录运算符 、-、*、/等
int m; //控制编辑框中的字符
int n; //用于判断连续进行了几次运算
CString strItem; //用于记录当前编辑框中的数据
CString m_string; //用于记录前n-1次的运算结果
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);我们然后为数字键编写程序:(以0为例)
m=n=0;
m_string="";
strItem="";
if(!m) {m_Edit.SetWindowText("");m ;} //用于得到连续的输入其他字符同样处理 这些字符控件实际上就是数字发生器,只不过一字符形式保存。 下面,我们应该处理这些数据了 我们以加法为例:
m_Edit.GetWindowText(strItem); //将当前字符保存在strItem中
CString str="0";输入数字
strItem =str;//连续输入字符
m_Edit.SetWindowText(strItem); //显示连续的输入
//n用来判断是不是第一次按 号按扭像其他的-、*、/可以同样的处理。 最后,显示最终结果:(即等号运算)
if(!n)
{
m_string=strItem;
if(m_string==""){m_string="";return;}
}
else
{
double num1,num2;
num1=atof(m_string);
num2=atof(strItem);
switch(m_sign)
{
case '' '':num1 =num2;break;
case ''-'':num1-=num2;break;
case ''*'':num1*=num2;break;
case ''/'':if(!num2) AfxMessageBox("the divisor is 0!"); else num1/=num2;break;
case ''%'':if(!num2) AfxMessageBox("the divisor is 0!"); else num1=(int)num2%(int)num1;break;
default:break;
}
m_string.Format("%.6f",num1);
}//以上是进行判别与运算,这里用了CString对象转换成数据的函数
m_sign='' '';
strItem="";
n ;
if(m>0)m--;//是执行完加法后,编辑框输入新数据
m_Edit.SetWindowText(m_string);//显示上一次按运算件的结果
if(!n)当然你可以加一些辅助功能: 下面我们举两个例子:一个是退格功能;一个是清除功能。 退格功能:
{
m_string=strItem;
}
else
{
double num1,num2;
num1=atof(m_string);
num2=atof(strItem);
switch(m_sign)
{
case '' '':num1 =num2;break;
case ''-'':num1-=num2;break;
case ''*'':num1*=num2;break;
case ''/'':if(!num2) AfxMessageBox("the divisor is 0!"); else num1/=num2;break;
case ''%'':if(!num2) AfxMessageBox("the divisor is 0!"); else num1=(int)num1%(int)num2;break;
default:break;
}
m_string.Format("答案: %.6f",num1);
}
m_Edit.SetWindowText(m_string);
m_string="";
strItem="";
n=0;
m=0;
m_sign='' '';//等号运算完所有数据回归成默认
m_Edit.GetWindowText(strItem);读者可以自己体会,并相处更好的更多的其他功能。 清除功能:
if(!strItem.GetLength())::AfxMessageBox("the contents is empty!");
else
{
strItem.SetAt(strItem.GetLength()-1,NULL);
m_Edit.SetWindowText(strItem);
}
strItem="";好了,其他的功能,用户可以同样处理,只不过是换个样子而已。 当然了,读者可以随时想到随时增加进去。 希望,读者能够有所收获
m_string="";
n=0;m=0;
m_sign='' '';
m_Edit.SetWindowText(m_string);//即所有回归默认