(整理)基于vcmfc的科学计算器程序实验报告
- 格式:doc
- 大小:332.50 KB
- 文档页数:23
VC课程设计实验报告课题名称:计算器实现姓名:陈锋学号:2008221105110010提交报告时间: 2010年 11 月 22日课程设计目标实验设计一个计算器,要求可通过按钮输入数字、运算符,能通过按钮实现退格、清除功能,实现整数的加、减、乘、除、取余、开方、平方等运算功能,必要的错误处理,如除零;可以通过键盘输入数字、退格、运算符(+、-、*、/、%、=),实现括号运算;实现不同进制(二进制、十进制、八进制、十六进制)下的加、减、乘、除、取余、开方、平方等运算功能。
1. 分析与设计(1)实现方法:编程语言为C++语言。
编程方法:通过一个文本框接收所输入的运算表达式,然后将其转换成后缀表达式并将各个数字先转换成十进制数值进行计算,最后再转换成相应进制的字符串。
(2)代码设计说明:文件和类的设计说明:创建一个头文件:Calculate.h内容如下:#include"stdio.h"const int MaxSize=30;const int MaxPri=8;BOOL decimal_2; //为真代表选择相应的进制BOOL decimal_8;BOOL decimal_10;BOOL decimal_16;struct{char ch;int pri;}lpri[]={{'=',0},{'(',1},{'*',5},{'/',5},{'%',5},{'+',3},{'-',3},{')',8} ,{'^',7}},rpri[]={{'=',0},{'(',8},{'*',4},{'/',4},{'%',4},{'+',2},{'-',2},{')',1} ,{'^',6}};int leftpri(char op) //求左运算符的优先级{for(int i=0;i<MaxPri;i++)if(lpri[i].ch==op)return lpri[i].pri;}int rightpri(char op) //求右运算符op的优先级{for(int i=0;i<MaxPri;i++)if(rpri[i].ch==op)return rpri[i].pri;}int InOp(char ch) //判断ch是否为运算符{if(ch=='('||ch==')'||ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch =='^'||ch=='('||ch==')')return 1;else return 0;}int Precede(char op1,char op2){if(leftpri(op1)==rightpri(op2))return 0;else if(leftpri(op1)<rightpri(op2))return -1;else return 1;}void trans(char *exp,char postexp[]) //将算术表达式转换成后缀表达式{struct{char data[MaxSize];int top;}op;int i=0;op.top=0;op.data[op.top]='=';while(*exp!='\0'){if(!InOp(*exp)){while(*exp>='0'&&*exp<='9'||*exp=='.'||*exp>='a'&&*exp<='f'){postexp[i++]=*exp;exp++;}postexp[i++]='#';}else{switch(Precede(op.data[op.top],*exp)){case -1:op.top++;op.data[op.top]=*exp;exp++;break;case 0:op.top--;exp++;break;case 1:postexp[i++]=op.data[op.top];op.top--;break;}}}while(op.data[op.top]!='='){postexp[i++]=op.data[op.top];op.top--;}postexp[i]='\0';}float compvalue(char *postexp) //计算后缀表达式的值{struct{float data[MaxSize];int top;}st;float d,a,b,c;st.top=-1;while(*postexp!='\0'){switch(*postexp){case '+':a=st.data[st.top];st.top--;b=st.data[st.top];st.top--;c=a+b;st.top++;st.data[st.top]=c;break;case '-':a=st.data[st.top];st.top--;b=st.data[st.top];st.top--;c=b-a;st.top++;st.data[st.top]=c;break;case '*':a=st.data[st.top];st.top--;b=st.data[st.top];st.top--;c=a*b;st.top++;st.data[st.top]=c;break;case '/':a=st.data[st.top];st.top--;b=st.data[st.top];st.top--;if(a!=0){c=b/a;st.top++;st.data[st.top]=c;}else{MessageBox(NULL,"\t除零错误!","Error",MB_OK);st.data[st.top]=0;break;}break;case '%':a=st.data[st.top];st.top--;b=st.data[st.top];st.top--;if(a!=0){c=(int)b%(int)a;st.top++;st.data[st.top]=c;}else{MessageBox(NULL,"\t余零错误!","Error",MB_OK);st.data[st.top]=0;break;}break;case '^':{a=st.data[st.top];st.top--;b=st.data[st.top];st.top--;float d=1;for(int i=0;i<a;i++)d*=b;st.top++;st.data[st.top]=d;break;}default: //将数值字符转换成十进制字符串保存d=0;/************整数部分************/while(*postexp>='0'&&*postexp<='9'||*postexp>='a'&&*postexp<='f'){if(decimal_2)d=d*2+*postexp-'0';else if(decimal_8)d=d*8+*postexp-'0';else if(decimal_10)d=d*10+*postexp-'0';else if(decimal_16){if(*postexp>='0'&&*postexp<='9')d=d*16+*postexp-'0';elsed=d*16+*postexp-'a'+10;}postexp++;}/************可能的小数部分************/if(*postexp=='.'){postexp++;int n=0;if(decimal_2){while(*postexp>='0'&&*postexp<='1') {n++;d=d*2+*postexp-'0';postexp++;}while(n){d=d/2;n--;}}else if(decimal_8){while(*postexp>='0'&&*postexp<='9') {n++;d=d*8+*postexp-'0';postexp++;}while(n){d=d/8;n--;}}else if(decimal_10){while(*postexp>='0'&&*postexp<='9') {n++;d=d*10+*postexp-'0';postexp++;}while(n){d=d/10;n--;}}else if(decimal_16){while(*postexp>='0'&&*postexp<='9'||*postexp>='a'&&*postexp<='f'){n++;if(*postexp>='a'&&*postexp<='f')d=d*16+*postexp-'a'+10;elsed=d*16+*postexp-'0';postexp++;}while(n){d=d/16;n--;}}}st.top++;st.data[st.top]=d;break;}postexp++;}return(st.data[st.top]);}(3)各控件变量:(如图)(4)界面图示:2.程序代码实现BOOL CMyDlg::OnInitDialog(){CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, F ALSE); // Set small icon// TODO: Add extra initialization here/******************初始化******************/CheckRadioButton(IDC_RADIO_TEN,IDC_RADIO_SIXTEEN,IDC_RADIO_TEN);//设置单选按钮m_show='0'; //文本框内容初始化为'0'UpdateData(F ALSE); //显示文本框内容GetDlgItem(IDC_BTN_10)->EnableWindow(false);GetDlgItem(IDC_BTN_11)->EnableWindow(false); //控件灰显,默认十进制标准GetDlgItem(IDC_BTN_12)->EnableWindow(false);GetDlgItem(IDC_BTN_13)->EnableWindow(false);GetDlgItem(IDC_BTN_14)->EnableWindow(false);GetDlgItem(IDC_BTN_15)->EnableWindow(false);decimal_2=false;decimal_8=false; //进制初始化,选择十进制decimal_10=true;decimal_16=false;return TRUE; // return TRUE unless you set the focus to a control}/******************************************************************************/ /*******************************系列控件点击输入*******************************/ /******************************************************************************/void CMyDlg::OnBtn0() //输入'0'{// TODO: Add your control notification handler code hereif(m_show=='0')m_show='0';else m_show+='0';UpdateData(F ALSE);}void CMyDlg::OnBtn1() //输入'1'{// TODO: Add your control notification handler code hereif(m_show=='0')m_show='1';else m_show+='1';UpdateData(F ALSE);}void CMyDlg::OnBtn2() //输入'2'{// TODO: Add your control notification handler code here if(m_show=='0')m_show='2';else m_show+='2';UpdateData(F ALSE);}void CMyDlg::OnBtn3() //输入'3' {// TODO: Add your control notification handler code here if(m_show=='0')m_show='3';else m_show+='3';UpdateData(F ALSE);}void CMyDlg::OnBtn4() //输入'4' {// TODO: Add your control notification handler code here if(m_show=='0')m_show='4';else m_show+='4';UpdateData(F ALSE);}void CMyDlg::OnBtn5() //输入'5' {// TODO: Add your control notification handler code here if(m_show=='0')m_show='5';else m_show+='5';UpdateData(F ALSE);}void CMyDlg::OnBtn6() //输入'6' {// TODO: Add your control notification handler code here if(m_show=='0')m_show='6';else m_show+='6';UpdateData(F ALSE);}void CMyDlg::OnBtn7() //输入'7'{// TODO: Add your control notification handler code hereif(m_show=='0')m_show='7';else m_show+='7';UpdateData(F ALSE);}void CMyDlg::OnBtn8() //输入'8'{// TODO: Add your control notification handler code hereif(m_show=='0')m_show='8';else m_show+='8';UpdateData(F ALSE);}void CMyDlg::OnBtn9() //输入'9'{// TODO: Add your control notification handler code hereif(m_show=='0')m_show='9';else m_show+='9';UpdateData(F ALSE);}void CMyDlg::OnBtn10() //输入'a' (16进制) {// TODO: Add your control notification handler code hereif(m_show=='0')m_show='a';else m_show+='a';UpdateData(F ALSE);}void CMyDlg::OnBtn11() //输入'b' (16进制) {// TODO: Add your control notification handler code hereif(m_show=='0')m_show='b';else m_show+='b';UpdateData(F ALSE);}void CMyDlg::OnBtn12() //输入'c' (16进制) {// TODO: Add your control notification handler code hereif(m_show=='0')m_show='c';else m_show+='c';UpdateData(F ALSE);}void CMyDlg::OnBtn13() //输入'd' (16进制) {// TODO: Add your control notification handler code hereif(m_show=='0')m_show='d';else m_show+='d';UpdateData(F ALSE);}void CMyDlg::OnBtn14() //输入'e' (16进制) {// TODO: Add your control notification handler code hereif(m_show=='0')m_show='e';else m_show+='e';UpdateData(F ALSE);}void CMyDlg::OnBtn15() //输入'f' (16进制) {// TODO: Add your control notification handler code hereif(m_show=='0')m_show='f';else m_show+='f';UpdateData(F ALSE);}void CMyDlg::OnBtnDot() //输入'.'{// TODO: Add your control notification handler code herem_show+='.';UpdateData(F ALSE);}void CMyDlg::OnBtnEqual() //点击"="按钮,计算表达式的结果并显示{// TODO: Add your control notification handler code hereif(m_show=='0')m_show='0';elseif(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_show[m _show.GetLength()-1]==')'){LPTSTR exp=(LPTSTR)(LPCTSTR)m_show;char postexp[20];trans(exp,postexp); //转换成后缀表达式float result=compvalue(postexp); //计算该后缀表达式的值(10进制值)if(decimal_2) //转换成2进制结果显示{Trans_T en_to_Two(result);}else if(decimal_8) //转换成8进制结果显示{Trans_T en_to_Eight(result);}else if(decimal_16) //转换成16进制结果显示{Trans_T en_to_Sixteen(result);}else //直接作为结果显示m_show.Format("%g",result);}UpdateData(F ALSE);}void CMyDlg::OnBtnAdd() //输入'+'{// TODO: Add your control notification handler code hereif(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')'||m_show[m_show.GetLength()-1]>='a'||m_show[m_show.GetLength()-1]<='f')m_show+='+';UpdateData(F ALSE);}void CMyDlg::OnBtnSub() //输入'-'{// TODO: Add your control notification handler code hereif(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')'||m_show[m_show.GetLength()-1]>='a'||m_show[m_show.GetLength()-1]<='f')m_show+='-';UpdateData(F ALSE);}void CMyDlg::OnBtnMul() //输入'*'{// TODO: Add your control notification handler code hereif(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')'||m_show[m_show.GetLength()-1]>='a'||m_show[m_show.GetLength()-1]<='f')m_show+='*';UpdateData(F ALSE);}void CMyDlg::OnBtnDiv() //输入'/'{// TODO: Add your control notification handler code hereif(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')'||m_show[m_show.GetLength()-1]>='a'||m_show[m_show.GetLength()-1]<='f')m_show+='/';UpdateData(F ALSE);}void CMyDlg::OnBtnMol() //输入'%' (求余){// TODO: Add your control notification handler code hereif(m_show=='0')m_show='%';elseif(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_show[m _show.GetLength()-1]==')'||m_show[m_show.GetLength()-1]>='a'||m_show[m_show.GetLength()-1]<='f')if(m_show)m_show+='%';UpdateData(F ALSE);}void CMyDlg::OnBtnLeft() //输入'('{// TODO: Add your control notification handler code hereif(m_show=='0')m_show='(';if(m_show[m_show.GetLength()-1]=='+'||m_show[m_show.GetLength()-1]=='-'||m_show[ m_show.GetLength()-1]=='*'||m_show[m_show.GetLength()-1]=='/'||m_show[m_show.GetLength()-1]=='%')m_show+='(';UpdateData(F ALSE);}void CMyDlg::OnBtnRight() //输入')'{// TODO: Add your control notification handler code hereif(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')')m_show+=')';UpdateData(F ALSE);}void CMyDlg::OnBtnPower(){// TODO: Add your control notification handler code hereif(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')'||m_show[m_show.GetLength()-1]>='a'||m_show[m_show.GetLength()-1]<='f')m_show+='^';UpdateData(F ALSE);}void CMyDlg::OnBtnSqrt() //计算表达式的值的平方根{// TODO: Add your control notification handler code hereOnBtnEqual(); //计算表达式的值Trans_to_T en(); //转换成十进制字符串形式int length=m_show.GetLength();float d=0; //保存该十进制的大小/**********整数部分**********/while(length&&(m_show.GetAt(m_show.GetLength()-length)>='0'&&m_show.GetAt(m_ show.GetLength()-length)<='9'||m_show.GetAt(m_show.GetLength()-length)>='a'&&m_show.GetAt(m_show.GetLengt h()-length)<='f')){if(m_show.GetAt(m_show.GetLength()-length)>='0'&&m_show.GetAt(m_show.GetLengt h()-length)<='9')d=d*10+m_show.GetAt(m_show.GetLength()-length)-'0';elsed=d*10+m_show.GetAt(m_show.GetLength()-length)-'a'+10;length--;}/**********可能的小数部分**********/if(length&&m_show.GetAt(m_show.GetLength()-length)=='.'){length--;int n=0;while(length&&(m_show.GetAt(m_show.GetLength()-length)>='0'&&m_show.GetAt(m_ show.GetLength()-length)<='9'||m_show.GetAt(m_show.GetLength()-length)>='a'&&m_show.GetAt(m_show.GetLengt h()-length)<='f')){n++;if(m_show.GetAt(m_show.GetLength()-length)>='0'&&m_show.GetAt(m_show.GetLengt h()-length)<='9')d=d*10+m_show.GetAt(m_show.GetLength()-length)-'0';elsed=d*10+m_show.GetAt(m_show.GetLength()-length)-'a'+10;length--;}while(n){d=d/10;n--;}}float a=sqrt(d); //求该数的平方根if(decimal_2) //转换成原来的进制表示的字符串{Trans_T en_to_Two(a);}else if(decimal_8){Trans_T en_to_Eight(a);}else if(decimal_16){Trans_T en_to_Sixteen(a);}elsem_show.Format("%g",a);UpdateData(F ALSE);}void CMyDlg::OnBtnReciprocal() //计算表达式值的倒数{// TODO: Add your control notification handler code hereOnBtnEqual(); //计算表达式的值Trans_to_T en(); //转换成十进制字符串形式int length=m_show.GetLength();float d=0; //保存该十进制的大小/**********整数部分**********/while(length&&(m_show.GetAt(m_show.GetLength()-length)>='0'&&m_show.GetAt(m_ show.GetLength()-length)<='9'||m_show.GetAt(m_show.GetLength()-length)>='a'&&m_show.GetAt(m_show.GetLengt h()-length)<='f')){if(m_show.GetAt(m_show.GetLength()-length)>='0'&&m_show.GetAt(m_show.GetLengt h()-length)<='9')d=d*10+m_show.GetAt(m_show.GetLength()-length)-'0';elsed=d*10+m_show.GetAt(m_show.GetLength()-length)-'a'+10;length--;}/**********可能的小数部分**********/if(length&&m_show.GetAt(m_show.GetLength()-length)=='.'){length--;int n=0;while(length&&(m_show.GetAt(m_show.GetLength()-length)>='0'&&m_show.GetAt(m_ show.GetLength()-length)<='9'||m_show.GetAt(m_show.GetLength()-length)>='a'&&m_show.GetAt(m_show.GetLengt h()-length)<='f')){n++;if(m_show.GetAt(m_show.GetLength()-length)>='0'&&m_show.GetAt(m_show.GetLengt h()-length)<='9')d=d*10+m_show.GetAt(m_show.GetLength()-length)-'0';elsed=d*10+m_show.GetAt(m_show.GetLength()-length)-'a'+10;length--;}while(n){d=d/10;n--;}}float a=1/d; //计算该数的倒数if(decimal_2) //转换成原来的进制表示的字符串{Trans_T en_to_Two(a);}else if(decimal_8){Trans_T en_to_Eight(a);}else if(decimal_16){Trans_T en_to_Sixteen(a);}elsem_show.Format("%g",a);OnBtnEqual();}void CMyDlg::OnBtnBack() //退格操作,删除最后的字符{// TODO: Add your control notification handler code here//m_show.Remove(m_show.GetAt(m_show.GetLength()-1)); //这一句是移除字符串中与最后一个字符相同的所有字符m_show=m_show.Left(m_show.GetLength()-1); //这一句是移除最后一个字符,不管前面是否有相同字符if(m_show.GetLength()==0)m_show='0';UpdateData(F ALSE);}void CMyDlg::OnBtnZero() //清零操作,删除所有字符,并赋值为'0' {// TODO: Add your control notification handler code herem_show='0';UpdateData(F ALSE);}void CMyDlg::OnRadioT en() //选择10进制{// TODO: Add your control notification handler code here//MessageBox("您选择的是十进制计算!");/********************系列控件灰显调度********************/GetDlgItem(IDC_BTN_2)->EnableWindow(true);GetDlgItem(IDC_BTN_3)->EnableWindow(true);GetDlgItem(IDC_BTN_4)->EnableWindow(true);GetDlgItem(IDC_BTN_5)->EnableWindow(true);GetDlgItem(IDC_BTN_6)->EnableWindow(true);GetDlgItem(IDC_BTN_7)->EnableWindow(true);GetDlgItem(IDC_BTN_8)->EnableWindow(true);GetDlgItem(IDC_BTN_9)->EnableWindow(true);GetDlgItem(IDC_BTN_10)->EnableWindow(false);GetDlgItem(IDC_BTN_11)->EnableWindow(false);GetDlgItem(IDC_BTN_12)->EnableWindow(false);GetDlgItem(IDC_BTN_13)->EnableWindow(false);GetDlgItem(IDC_BTN_14)->EnableWindow(false);GetDlgItem(IDC_BTN_15)->EnableWindow(false);/********************进制转换************************/OnBtnEqual();Trans_to_T en();UpdateData(F ALSE);/*************************进制选择***********************/ decimal_2=false;decimal_8=false;decimal_10=true;decimal_16=false;}void CMyDlg::OnRadioTwo() //选择2进制{// TODO: Add your control notification handler code here/********************系列控件灰显调度********************/ GetDlgItem(IDC_BTN_2)->EnableWindow(false);GetDlgItem(IDC_BTN_3)->EnableWindow(false);GetDlgItem(IDC_BTN_4)->EnableWindow(false);GetDlgItem(IDC_BTN_5)->EnableWindow(false);GetDlgItem(IDC_BTN_6)->EnableWindow(false);GetDlgItem(IDC_BTN_7)->EnableWindow(false);GetDlgItem(IDC_BTN_8)->EnableWindow(false);GetDlgItem(IDC_BTN_9)->EnableWindow(false);GetDlgItem(IDC_BTN_10)->EnableWindow(false);GetDlgItem(IDC_BTN_11)->EnableWindow(false);GetDlgItem(IDC_BTN_12)->EnableWindow(false);GetDlgItem(IDC_BTN_13)->EnableWindow(false);GetDlgItem(IDC_BTN_14)->EnableWindow(false);GetDlgItem(IDC_BTN_15)->EnableWindow(false);/********************进制转换************************/OnBtnEqual();Trans_to_T en();Trans_T en_to_Two(Trans_char_to_IntT en());UpdateData(F ALSE);/*************************进制选择***********************/ decimal_2=true;decimal_8=false;decimal_10=false;decimal_16=false;}void CMyDlg::OnRadioEight() //选择8进制{// TODO: Add your control notification handler code here/********************系列控件灰显调度********************/ GetDlgItem(IDC_BTN_2)->EnableWindow(true);GetDlgItem(IDC_BTN_3)->EnableWindow(true);GetDlgItem(IDC_BTN_4)->EnableWindow(true);GetDlgItem(IDC_BTN_5)->EnableWindow(true);GetDlgItem(IDC_BTN_6)->EnableWindow(true);GetDlgItem(IDC_BTN_7)->EnableWindow(true);GetDlgItem(IDC_BTN_8)->EnableWindow(false);GetDlgItem(IDC_BTN_9)->EnableWindow(false);GetDlgItem(IDC_BTN_10)->EnableWindow(false);GetDlgItem(IDC_BTN_11)->EnableWindow(false);GetDlgItem(IDC_BTN_12)->EnableWindow(false);GetDlgItem(IDC_BTN_13)->EnableWindow(false);GetDlgItem(IDC_BTN_14)->EnableWindow(false);GetDlgItem(IDC_BTN_15)->EnableWindow(false);/********************进制转换************************/OnBtnEqual();Trans_to_T en();Trans_T en_to_Eight(Trans_char_to_IntT en());UpdateData(F ALSE);/*************************进制选择***********************/ decimal_2=false;decimal_8=true;decimal_10=false;decimal_16=false;void CMyDlg::OnRadioSixteen() //选择16进制{// TODO: Add your control notification handler code here/********************系列控件灰显调度********************/GetDlgItem(IDC_BTN_2)->EnableWindow(true);GetDlgItem(IDC_BTN_3)->EnableWindow(true);GetDlgItem(IDC_BTN_4)->EnableWindow(true);GetDlgItem(IDC_BTN_5)->EnableWindow(true);GetDlgItem(IDC_BTN_6)->EnableWindow(true);GetDlgItem(IDC_BTN_7)->EnableWindow(true);GetDlgItem(IDC_BTN_8)->EnableWindow(true);GetDlgItem(IDC_BTN_9)->EnableWindow(true);GetDlgItem(IDC_BTN_10)->EnableWindow(true);GetDlgItem(IDC_BTN_11)->EnableWindow(true);GetDlgItem(IDC_BTN_12)->EnableWindow(true);GetDlgItem(IDC_BTN_13)->EnableWindow(true);GetDlgItem(IDC_BTN_14)->EnableWindow(true);GetDlgItem(IDC_BTN_15)->EnableWindow(true);/********************进制转换************************/OnBtnEqual();Trans_to_T en();Trans_T en_to_Sixteen(Trans_char_to_IntT en());UpdateData(F ALSE);/*************************进制选择***********************/decimal_2=false;decimal_8=false;decimal_10=false;decimal_16=true;}/******************************************************************/ /*******************######## 键盘输入########*******************/ /******************************************************************/ BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)// TODO: Add your specialized code here and/or call the base classif(pMsg->message==WM_KEYDOWN){if(pMsg->wParam>=VK_NUMP AD0&&pMsg->wParam<=VK_NUMPAD9) //小键盘输入1->9{if(m_show=='0')m_show.Format("%c",pMsg->wParam-48);else{CString str=m_show;m_show.Format("%s%c",str,pMsg->wParam-48);}}else if(pMsg->wParam>=48&&pMsg->wParam<=57) //大键盘输入1->9{if(m_show=='0')m_show.Format("%c",pMsg->wParam);else{CString str=m_show;m_show.Format("%s%c",str,pMsg->wParam);}}else if(pMsg->wParam==VK_ADD) //键盘输入'+'{if(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')')m_show+='+';}else if(pMsg->wParam==VK_SUBTRACT||pMsg->wParam==189) //键盘输入'-',前者是小键盘输入的,后者是大键盘输入的{if(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')')m_show+='-';else if(pMsg->wParam==VK_MUL TIPL Y) //键盘输入'*'{if(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')')m_show+='*';}else if(pMsg->wParam==VK_DIVIDE) //键盘输入'/'{if(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_sho w[m_show.GetLength()-1]==')')m_show+='/';}else if(pMsg->wParam==VK_DECIMAL) //键盘输入'.'{if(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9')m_show+='.';}else if(pMsg->wParam==187) //键盘输入'=' {if(m_show=='0')m_show='0';elseif(m_show[m_show.GetLength()-1]>='0'&&m_show[m_show.GetLength()-1]<='9'||m_show[m _show.GetLength()-1]==')'){LPTSTR exp=(LPTSTR)(LPCTSTR)m_show;char postexp[20];trans(exp,postexp);float result=compvalue(postexp);m_show.Format("%g",result);}}else if(pMsg->wParam==VK_BACK) //键盘输入退格m_show.Remove(m_show.GetAt(m_show.GetLength()-1));if(m_show.GetLength()==0)m_show='0';}UpdateData(F ALSE);}return CDialog::PreTranslateMessage(pMsg);}void CMyDlg::Trans_to_T en() //将文本框字符串内容转换成十进制字符串{if(decimal_2){int length=m_show.GetLength();int i=0;float d=0;while(i<length&&m_show.GetAt(i)>='0'&&m_show.GetAt(i)<='1'){d=d*2+m_show.GetAt(i)-'0';i++;}i++;int decimal=1;while(i<length&&m_show.GetAt(i)>='0'&&m_show.GetAt(i)<='1'){decimal*=2;d=d+(float)(m_show.GetAt(i)-'0')/decimal;i++;}m_show.Format("%g",d);}else if(decimal_8){int length=m_show.GetLength();int i=0;float d=0;while(i<length&&m_show.GetAt(i)>='0'&&m_show.GetAt(i)<='9'){d=d*8+m_show.GetAt(i)-'0';i++;}i++;int decimal=1;while(i<length&&m_show.GetAt(i)>='0'&&m_show.GetAt(i)<='9'){decimal*=8;d=d+(float)(m_show.GetAt(i)-'0')/decimal;i++;}m_show.Format("%g",d);}else if(decimal_16){int length=m_show.GetLength();int i=0;float d=0;while(i<length&&(m_show.GetAt(i)>='0'&&m_show.GetAt(i)<='9'||m_show.GetAt(i)>=' a'&&m_show.GetAt(i)<='f')){if(m_show.GetAt(i)>='0'&&m_show.GetAt(i)<='9')d=d*16+m_show.GetAt(i)-'0';elsed=d*16+m_show.GetAt(i)-'a'+10;i++;}i++;int decimal=1;while(i<length&&(m_show.GetAt(i)>='0'&&m_show.GetAt(i)<='9'||m_show.GetAt(i)>=' a'&&m_show.GetAt(i)<='f')){decimal*=16;if(m_show.GetAt(i)>='0'&&m_show.GetAt(i)<='9')d=d+(float)(m_show.GetAt(i)-'0')/decimal;elsed=d+(float)(m_show.GetAt(i)-'a'+10)/decimal;i++;}m_show.Format("%g",d);}}void CMyDlg::Trans_T en_to_Two(float a) //将10进制浮点数a转换成2进制字符串{int a1[16]={0};int i=0,k1;float k2;k1=(int)a;。
计算机MFC课程设计报告——模拟计算器姓名:学号:02011227联系电话:指导教师:东南大学机械工程学院2012年10月14日模拟计算器摘要摘要内容:以课本简单计算器为基础,首先实现了加、减、乘、除、求倒数和平方根的混合运算,并能进行清屏及倒退操作,然后自行完善了书中未能实现的乘、除运算的连续操作,最后自行设计并完成了lg、ln 、sin、cos、^、.、pi、e八个按钮控件的消息映射及程序代码的添加和修改。
从中学会了制作简单的基于对话框的小工具、掌握了常用控件的使用、明白了消息映射及消息处理、提高了VC++编程水平。
关键词:四则运算小数点计算器Analog calculatorAbstractContent of abstract: Simple calculator in textbooks as the foundation, first realized the addition, subtraction, multiplication, division, and reciprocal and square root of the mixed operation, and can clear screen and reverse operation, and then to improve the book failed to realize the multiplication, division operation of continuous operation, and finally to be designed and completed the eight button control news mapping and program code to add and modify. Learnt how to make simple dialog-based small tools, mastered the use of commonly used controls, see news mapping and information processing, improve the level of programming.Key word: arithmetic Decimal point Calculator本计算器是基于VC++的MFC编程,可完成大多数简单操作,其运行后界面如下图:以课本简单计算器为基础,首先实现了加、减、乘、除、求倒数和平方根的混合运算,并能进行清屏及倒退操作,然后自行完善了书中未能实现的乘、除运算的连续操作,最后自行设计并完成了lg、ln 、sin、cos、^、.、pi、e 八个按钮控件的消息映射及程序代码的添加和修改。
软件基础课程设计报告一、需求分析系统目标:设计的计算器至少能够进行简单的四则运算和求倒数求反以及开方运算。
主体功能:程序能实现:加,减,乘,除,开方,倒数等运算功能;还要实现数据的输入,输出,计算,显示及程序退出等功能。
另外还可以实现多种科学计算的功能,如:三角函数的计算,弧度与角度间的转换,对数指数的计算等。
开发环境:操作系统:Windows XP编程环境:Microsoft Visual C++ 6.0二、功能说明(1)包含的功能有:加、减、乘、除运算,开方、求倒数、三角函数、弧度与角度间的转换、对数指数的计算等功能。
(2)计算器上数字0—9为一个控件数组,加、减、乘、除为一个控件数组,其余为单一的控件。
(3)给对话框添加菜单。
(4)计算功能基本上是用系统内部函数。
(5)程序可以能自动判断输入数据的正确性,保证不出现多于一个小数点、以0开头等不正常现象。
(6)“CE”按钮可以清除所有已输入的数据从头计算,“Back”按钮可以实现退位功能。
(7)能够显示时间、日期。
三、详细设计(2)编辑资源(3)效果图四、程序附录计算器Dlg.cpp :#include "stdafx.h"#include "计算器.h"#include "计算器Dlg.h"#include "math.h"#include"FirstDlg.h"#include <windows.h>#define PI 3.14;#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATAprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMyDlg dialogCMyDlg::CMyDlg(CWnd* pParent /*=NULL*/): CDialog(CMyDlg::IDD, pParent){//{{AFX_DATA_INIT(CMyDlg)m_result = 0.0;//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CMyDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CMyDlg)DDX_Control(pDX, IDC_EDIT1, m_EDIT);DDX_Text(pDX, IDC_EDIT1, m_result);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CMyDlg, CDialog)//{{AFX_MSG_MAP(CMyDlg)ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_NUM0, OnNum0)ON_BN_CLICKED(IDC_NUM1, OnNum1)ON_BN_CLICKED(IDC_NUM2, OnNum2)ON_BN_CLICKED(IDC_NUM3, OnNum3)ON_BN_CLICKED(IDC_NUM4, OnNum4)ON_BN_CLICKED(IDC_NUM5, OnNum5)ON_BN_CLICKED(IDC_NUM6, OnNum6)ON_BN_CLICKED(IDC_NUM7, OnNum7)ON_BN_CLICKED(IDC_NUM8, OnNum8)ON_BN_CLICKED(IDC_NUM9, OnNum9)ON_BN_CLICKED(IDC_DOT, OnDot)ON_BN_CLICKED(IDC_CE, OnCe)ON_BN_CLICKED(IDC_jia, Onjia)ON_BN_CLICKED(IDC_jian, Onjian)ON_BN_CLICKED(IDC_cheng, Oncheng)ON_BN_CLICKED(IDC_chu, Onchu)ON_BN_CLICKED(IDC_dengyu, Ondengyu)ON_BN_CLICKED(IDC_daoshu, Ondaoshu)ON_BN_CLICKED(IDC_zhengfu, Onzhengfu)ON_BN_CLICKED(IDC_BACKSPACE, OnBackspace) ON_BN_CLICKED(IDC_Ln, OnLn)ON_BN_CLICKED(IDC_log, Onlog)ON_BN_CLICKED(IDC_RADIO2, OnRadio2)ON_BN_CLICKED(IDC_RADIO1, OnRadio1)ON_BN_CLICKED(IDC_sqrt, Onsqrt)ON_BN_CLICKED(IDC_sin, Onsin)ON_BN_CLICKED(IDC_cos, Oncos)ON_BN_CLICKED(IDC_tan, Ontan)ON_BN_CLICKED(IDC_pingfang, Onpingfang)ON_BN_CLICKED(IDC_lifang, Onlifang)ON_BN_CLICKED(IDC_mi, Onmi)ON_BN_CLICKED(IDC_Exp, OnExp)ON_BN_CLICKED(IDC_10demi, On10demi)ON_BN_CLICKED(IDC_jiecheng, Onjiecheng)ON_COMMAND(ID_TIME, OnTime)ON_COMMAND(ID_DATE, OnDate)ON_COMMAND(ID_EXIT, OnExit)ON_COMMAND(ID_HELP, OnHelp)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMyDlg message handlersBOOL CMyDlg::OnInitDialog(){CDialog::OnInitDialog();ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization hereCheckRadioButton(IDC_RADIO1,IDC_RADIO2,IDC_RADIO1);UpdateData(FALSE);quan2=1;dotflag=0;numflag=0;return TRUE; // return TRUE unless you set the focus to a control}void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.void CMyDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags // the minimized window.HCURSOR CMyDlg::OnQueryDragIcon(){return (HCURSOR) m_hIcon;}void CMyDlg::OnNum0(){UpdateData();if(dotflag){quan2*=10;m_result+=0/(double)quan2;}else{if(numflag){m_result=m_result*10+0;}else m_result=0;}numflag=1;UpdateData(FALSE);}void CMyDlg::OnNum1(){UpdateData();if(dotflag){quan2*=10;m_result+=1/(double)quan2;}if(numflag){m_result=m_result*10+1;}else m_result=1;}numflag=1;UpdateData(FALSE);}void CMyDlg::OnNum2(){UpdateData();if(dotflag){quan2*=10;m_result+=2/(double)quan2;}else{if(numflag){m_result=m_result*10+2;}else m_result=2;}numflag=1;UpdateData(FALSE);}void CMyDlg::OnNum3(){UpdateData();if(dotflag){quan2*=10;m_result+=3/(double)quan2;}else{if(numflag){m_result=m_result*10+3;}else m_result=3;}numflag=1;UpdateData(FALSE);{UpdateData();if(dotflag){quan2*=10;m_result+=4/(double)quan2;}else{if(numflag){m_result=m_result*10+4;}else m_result=4;}numflag=1;UpdateData(FALSE);}void CMyDlg::OnNum5(){UpdateData();if(dotflag){quan2*=10;m_result+=5/(double)quan2;}else{if(numflag){m_result=m_result*10+5;}else m_result=5;}numflag=1;UpdateData(FALSE);}void CMyDlg::OnNum6(){UpdateData();if(dotflag){quan2*=10;m_result+=6/(double)quan2;}else{m_result=m_result*10+6;}else m_result=6;}numflag=1;UpdateData(FALSE);}void CMyDlg::OnNum7(){UpdateData();if(dotflag){quan2*=10;m_result+=7/(double)quan2;}else{if(numflag){m_result=m_result*10+7;}else m_result=7;}numflag=1;UpdateData(FALSE);}void CMyDlg::OnNum8(){UpdateData();if(dotflag){quan2*=10;m_result+=8/(double)quan2;}else{if(numflag){m_result=m_result*10+8;}else m_result=8;}numflag=1;UpdateData(FALSE);}UpdateData();if(dotflag){quan2*=10;m_result+=9/(double)quan2;}else{if(numflag){m_result=m_result*10+9;}else m_result=9;}numflag=1;UpdateData(FALSE);}void CMyDlg::OnDot(){UpdateData();dotflag=1;m_result+=0.0;UpdateData(FALSE);}void CMyDlg::OnCe(){UpdateData();m_EDIT.SetSel(0,-1);m_EDIT.ReplaceSel("");m_result=0;num1=0;num2=0;numflag=0;dotflag=0;quan2=1;UpdateData(FALSE);}void CMyDlg::Onjia(){UpdateData();num1=m_result;cal='+';numflag=0;dotflag=0;quan2=1;}UpdateData();num1=m_result;cal='-';numflag=0;dotflag=0;quan2=1;}void CMyDlg::Oncheng(){UpdateData();num1=m_result;cal='*';numflag=0;dotflag=0;quan2=1;}void CMyDlg::Onchu(){UpdateData();num1=m_result;cal='/';numflag=0;dotflag=0;quan2=1;}void CMyDlg::Ondengyu(){UpdateData();num2=m_result;switch(cal){case'+':m_result=num1+num2;break; case'-':m_result=num1-num2;break; case'*':m_result=num1*num2;break; case'/':m_result=num1/num2;break;case'x':m_result=pow(num1,num2);break; }numflag=0;dotflag=0;quan2=1;UpdateData(FALSE);}void CMyDlg::Ondaoshu(){UpdateData();m_result=1/m_result;numflag=0;UpdateData(FALSE);}void CMyDlg::Onzhengfu(){UpdateData();m_result=0-m_result;UpdateData(FALSE);}void CMyDlg::OnBackspace(){UpdateData();static int dotquan=quan2;long temp;if(dotflag&&numflag){if(dotquan>=10){temp=(long)(m_result*dotquan);m_result=(double)(temp/10);dotquan/=10;m_result=m_result/dotquan;}}else{//UpdateData();if(numflag&&m_result){m_result=(long)m_result/10;}}UpdateData(FALSE);}void CMyDlg::OnLn(){UpdateData();m_result=log(m_result);numflag=0;dotflag=0;quan2=1;UpdateData(FALSE);}void CMyDlg::Onlog(){UpdateData();m_result=log(m_result)/log(10);quan2=1;UpdateData(FALSE);}void CMyDlg::OnRadio2(){m_IsDegree=1;i=1;UpdateData(TRUE);}void CMyDlg::OnRadio1(){m_IsDegree=0;i=(2*3.1415926)/360;UpdateData(FALSE);}void CMyDlg::Onsqrt(){UpdateData();if(m_result<0){MessageBox("输入无效!");return;}m_result = sqrt(m_result); UpdateData(FALSE);}void CMyDlg::Onsin(){m_result=sin(m_result*i); UpdateData(FALSE);}void CMyDlg::Oncos(){m_result=cos(m_result*i); UpdateData(FALSE);}void CMyDlg::Ontan(){m_result=tan(m_result*i); UpdateData(FALSE);}void CMyDlg::Onpingfang() {UpdateData();m_result=m_result*m_result; UpdateData(FALSE);void CMyDlg::Onlifang(){UpdateData();m_result=m_result*m_result*m_result; UpdateData(FALSE);}void CMyDlg::Onmi(){UpdateData();num1=m_result;cal='x';numflag=0;dotflag=0;quan2=1;}void CMyDlg::OnExp(){m_result=exp(m_result); UpdateData(FALSE);}void CMyDlg::On10demi(){UpdateData();m_result=pow(10,m_result); UpdateData(FALSE);}void CMyDlg::Onjiecheng(){if(m_result<0){MessageBox("输入数据无效!");return;}int q;for(q=(int)m_result-1;q>=1;q--)m_result*=q;UpdateData(FALSE);}void CMyDlg::OnDate(){CFirstDlg dlg;dlg.DoModal();}void CMyDlg::OnTime(){UpdateData();TIME=1;tNow=CTime::GetCurrentTime();if(TIME){m_EDIT.SetSel(0,-1);m_EDIT.ReplaceSel("");CString sNow=tNow.Format("%I:%M:%S");m_EDIT.SetSel(0,-1);m_EDIT.ReplaceSel(sNow);}else{CString sNow=tNow.Format("%I:%M:%S");m_EDIT.SetSel(0,-1);m_EDIT.ReplaceSel(sNow);}}void CMyDlg::OnExit(){OnOK();}void CMyDlg::OnHelp(){MessageBox("班级:09通信一班组员:陈熙竹叶文晖周方"); }。
课程名称:Visual C++面向对象与可视化程序设计实验项目:计算器设计姓名:专业:计算机科学与技术班级:学号:计算机科学与技术学院实验教学中心2016 年11月19日哈尔滨理工大学计算机科学与技术学院实验报告实验项目名称:计算器设计( 2课时)一、实验目的1.灵活使用MFC应用程序设计2.设计计算器二、实验内容设计一个简单的计算器,可以实现加法,减法,除法,乘法,取模,开平方,输入数字要求可以输入小数,可以连加。
具有清空,回退功能。
三、实验步骤1.启动VS打2012开新建项目2.出现的New对话框的Projects标签内选择MFC应用程序3.Projects Name指定项目的名字,在Location中指定路径后按OK按钮4.选择创建类型,(例如基于对话框),确定项目类型后按Finish完成。
5.编写实验代码四、实验结果五、程序代码// project_1Dlg.h : 头文件//#pragma once#include "afxwin.h"// Cproject_1Dlg 对话框class Cproject_1Dlg : public CDialogEx{// 构造public:Cproject_1Dlg(CWnd* pParent = NULL); // 标准构造函数void Equal2();void Cproject_1Dlg::AddDigit(char numKey);// 对话框数据enum { IDD = IDD_PROJECT_1_DIALOG };protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现protected:HICON m_hIcon;long double m_op1,m_op2,m_result;int m_operation;int i,s;long double r;long double m_m;long double data[50];bool st1,st2;bool pflag;long double p;// 生成的消息映射函数virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();DECLARE_MESSAGE_MAP()public:double m_num;CEdit m_control_e;BOOL m_inv;BOOL m_hp;// CString m_sd;CString m_sd;afx_msg void OnClicked1x();afx_msg void OnClickedAdd();afx_msg void OnClickedB1();afx_msg void OnClickedB2();afx_msg void OnClickedB3();afx_msg void OnClickedB4();afx_msg void OnClickedB5();afx_msg void OnClickedB6();afx_msg void OnClickedB7();afx_msg void OnClickedB8();afx_msg void OnClickedB9();afx_msg void OnClickedB10();afx_msg void OnClickedClaer();afx_msg void OnClickedClaer2();afx_msg void OnClickedBp();afx_msg void OnClickedBp2();afx_msg void OnClickedBsp();afx_msg void OnClickedM();afx_msg void OnClickedMc();afx_msg void OnClickedMm();afx_msg void OnClickedMod();afx_msg void OnClickedSqrt();afx_msg void OnClickedMr();afx_msg void OnClickedSubract();afx_msg void OnClickedMul();afx_msg void OnClickedDevide();afx_msg void OnChangeEdit1();afx_msg void OnClickedEqual();};// project_1Dlg.cpp : 实现文件//#include "stdafx.h"#include "project_1.h"#include "project_1Dlg.h"#include "afxdialogex.h"#include<math.h>#ifdef _DEBUG#define new DEBUG_NEW#endif// 用于应用程序“关于”菜单项的 CAboutDlg 对话框class CAboutDlg : public CDialogEx{public:CAboutDlg();// 对话框数据enum { IDD = IDD_ABOUTBOX };protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现protected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD){}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()// Cproject_1Dlg 对话框Cproject_1Dlg::Cproject_1Dlg(CWnd* pParent /*=NULL*/) : CDialogEx(Cproject_1Dlg::IDD, pParent), m_num(0), m_sd(_T("")){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void Cproject_1Dlg::DoDataExchange(CDataExchange* pDX) {CDialogEx::DoDataExchange(pDX);DDX_Text(pDX, IDC_EDIT1, m_num);DDX_Control(pDX, IDC_EDIT1, m_control_e);DDX_Text(pDX, IDC_sd, m_sd);}BEGIN_MESSAGE_MAP(Cproject_1Dlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_1x, &Cproject_1Dlg::OnClicked1x)ON_BN_CLICKED(IDC_ADD, &Cproject_1Dlg::OnClickedAdd) ON_BN_CLICKED(IDC_b1, &Cproject_1Dlg::OnClickedB1)ON_BN_CLICKED(IDC_b2, &Cproject_1Dlg::OnClickedB2)ON_BN_CLICKED(IDC_b3, &Cproject_1Dlg::OnClickedB3)ON_BN_CLICKED(IDC_b4, &Cproject_1Dlg::OnClickedB4)ON_BN_CLICKED(IDC_b5, &Cproject_1Dlg::OnClickedB5)ON_BN_CLICKED(IDC_b6, &Cproject_1Dlg::OnClickedB6)ON_BN_CLICKED(IDC_b7, &Cproject_1Dlg::OnClickedB7)ON_BN_CLICKED(IDC_b8, &Cproject_1Dlg::OnClickedB8)ON_BN_CLICKED(IDC_b9, &Cproject_1Dlg::OnClickedB9)ON_BN_CLICKED(IDC_b10, &Cproject_1Dlg::OnClickedB10)ON_BN_CLICKED(IDC_claer, &Cproject_1Dlg::OnClickedClaer)ON_BN_CLICKED(IDC_claer2, &Cproject_1Dlg::OnClickedClaer2) ON_BN_CLICKED(IDC_bp, &Cproject_1Dlg::OnClickedBp)ON_BN_CLICKED(IDC_bp2, &Cproject_1Dlg::OnClickedBp2)ON_BN_CLICKED(IDC_bsp, &Cproject_1Dlg::OnClickedBsp)ON_BN_CLICKED(IDC_M, &Cproject_1Dlg::OnClickedM)ON_BN_CLICKED(IDC_MC, &Cproject_1Dlg::OnClickedMc)ON_BN_CLICKED(IDC_MM, &Cproject_1Dlg::OnClickedMm)ON_BN_CLICKED(IDC_MOD, &Cproject_1Dlg::OnClickedMod)ON_BN_CLICKED(IDC_sqrt, &Cproject_1Dlg::OnClickedSqrt)ON_BN_CLICKED(IDC_MR, &Cproject_1Dlg::OnClickedMr)ON_BN_CLICKED(IDC_SUBRACT, &Cproject_1Dlg::OnClickedSubract) ON_BN_CLICKED(IDC_mul, &Cproject_1Dlg::OnClickedMul)ON_BN_CLICKED(IDC_devide, &Cproject_1Dlg::OnClickedDevide) ON_EN_CHANGE(IDC_EDIT1, &Cproject_1Dlg::OnChangeEdit1)ON_BN_CLICKED(IDC_EQUAL, &Cproject_1Dlg::OnClickedEqual) END_MESSAGE_MAP()// Cproject_1Dlg 消息处理程序BOOL Cproject_1Dlg::OnInitDialog(){CDialogEx::OnInitDialog();// 将“关于...”菜单项添加到系统菜单中。
使用VC6编写一个计算器MFC回答如下:要使用VC6编写一个计算器MFC应用程序,首先需要创建一个新的MFC项目。
在VC6中,选择“创建一个新的工程”,然后选择“MFC应用程序”,并命名该项目为“Calculator”。
接下来,在“CalculatorDlg.h”文件中,可以编写计算器对话框类的代码。
在该类中,需要声明所有添加到对话框中的控件的成员变量。
例如,可以声明一个`CEdit`类型的指针变量来引用屏幕上的文本框控件。
此外,还可以声明其他成员变量来存储计算器的状态和当前操作数的值。
然后,在“CalculatorDlg.cpp”文件中,可以实现计算器对话框类的代码。
在该文件中,需要实现对话框的初始化、按钮单击事件的处理等功能。
例如,可以在对话框的`OnInitDialog`函数中初始化计算器的状态,并将其他控件的消息映射到对应的成员函数上。
然后,在对应的成员函数中,可以编写相应的代码来处理按钮单击事件,进行计算,并将结果显示在屏幕上。
在编写计算器的核心功能时,可以使用`+`、`-`、`*`和`/`等运算符来实现加法、减法、乘法和除法。
此外,还可以添加其他功能,如取余、开方等。
为了实现这些功能,可以在对话框类中添加成员函数,然后在相应的按钮单击事件中调用这些函数。
最后,在项目的构建选项中,将“字符集”设置为“使用Unicode字符集”,以支持使用中文。
然后,编译并运行该项目,就可以看到一个MFC计算器应用程序在屏幕上显示出来。
总结起来,在VC6中编写一个计算器MFC应用程序需要完成以下步骤:1.创建新的MFC项目,并添加所需的控件。
2.在对话框类中声明和实现计算器界面的成员变量和函数。
3.在对应的按钮单击事件处理函数中编写计算器的核心功能。
4. 在项目的构建选项中设置字符集为Unicode字符集。
5.编译并运行项目,测试计算器应用程序。
以上是一个简单的使用VC6编写一个计算器MFC的步骤。
当然,具体的实现还有很多细节需要注意和完成,如错误处理、界面美化等。
VC课程设计实验报告课题名称:计算器实现姓名:陈锋学号:05110010…提交报告时间: 2010年 11 月 22日课程设计目标实验设计一个计算器,要求可通过按钮输入数字、运算符,能通过按钮实现退格、清除功能,实现整数的加、减、乘、除、取余、开方、平方等运算功能,必要的错误处理,如除零;可以通过键盘输入数字、退格、运算符(+、-、*、/、%、=),实现括号运算;实现不同进制(二进制、十进制、八进制、十六进制)下的加、减、乘、除、取余、开方、平方等运算功能。
1. 分析与设计(1)实现方法:#编程语言为C++语言。
编程方法:通过一个文本框接收所输入的运算表达式,然后将其转换成后缀表达式并将各个数字先转换成十进制数值进行计算,最后再转换成相应进制的字符串。
(2)代码设计说明:文件和类的设计说明:创建一个头文件:内容如下:#include""const int MaxSize=30;const int MaxPri=8;BOOL decimal_2; h==op):return lpri[i].pri;}int rightpri(char op) h==op)return rpri[i].pri;}int InOp(char ch) ||*exp>='a'&&*exp<='f'){postexp[i++]=*exp;exp++;}postexp[i++]='#';,}else{switch(Precede[],*exp)){case -1:++;[]=*exp;exp++;break;case 0:…;exp++;break;case 1:postexp[i++]=[];;break;}}}while[]!='=')。
{postexp[i++]=[];;}postexp[i]='\0';}float compvalue(char *postexp){postexp++;int n=0;{if(decimal_2){while(*postexp>='0'&&*postexp<='1'){n++;d=d*2+*postexp-'0';postexp++;}while(n){d=d/2;$n--;}}else if(decimal_8){while(*postexp>='0'&&*postexp<='9'){n++;d=d*8+*postexp-'0';postexp++;}?while(n){d=d/8;}}else if(decimal_10){while(*postexp>='0'&&*postexp<='9'){n++;`d=d*10+*postexp-'0';postexp++;}while(n){d=d/10;n--;}}else if(decimal_16){|while(*postexp>='0'&&*postexp<='9'||*postexp>='a'&&*postexp<='f'){n++;if(*postexp>='a'&&*postexp<='f')d=d*16+*postexp-'a'+10;elsed=d*16+*postexp-'0';postexp++;}while(n){{n--;}}}++;[]=d;break;}postexp++;}/return[]);}(3)各控件变量:(如图);(4)界面图示:\《—、?2.程序代码实现BOOL CMyDlg::OnInitDialog(){CDialog::OnInitDialog();." menu item to system menu.`ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;(IDS_ABOUTBOX);if (!()){@pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}The framework does this automatically{;UpdateData(FALSE);}^void CMyDlg::OnBtnEqual(){length--;int n=0;while(length&&()-length)>='0'&&()-length)<='9'||()-length)>='a'&&()-length)<='f')){n++;if()-length)>='0'&&()-length)<='9')d=d*10+()-length)-'0';]elsed=d*10+()-length)-'a'+10;length--;}while(n){d=d/10;n--;}}float a=sqrt(d);】{length--;int n=0;while(length&&()-length)>='0'&&()-length)<='9' ||()-length)>='a'&&()-length)<='f')){n++;if()-length)>='0'&&()-length)<='9')d=d*10+()-length)-'0';elsed=d*10+()-length)-'a'+10;:length--;}while(n){d=d/10;n--;}}float a=1/d;{if(m_show[()-1]>='0'&&m_show[()-1]<='9');m_show+='.';}else if(pMsg->wParam==187) ;for(i=0;i<10&&k2>;i++){decimal*=2;int k=(int)(k2*2);str=m_show;("%s%c",str,k+'0');k2=k2-(float)k/2;k2*=2;&}}void CMyDlg::Trans_Ten_to_Eight(float a) ;for(i=0;i<10&&k2>0;i++){decimal*=16;int k=(int)(k2*16);if(0<=k&&k<=9){str=m_show;·("%s%c",str,k+'0');}else{CString str=m_show;("%s%c",str,k+'a');}k2=k2-(float)k/16;k2*=16;}})float CMyDlg::Trans_char_to_IntTen() //将10进制字符串转换成10进制数值并返回{int length=();int i=0;float d=0;while(i<length&&(i)>='0'&&(i)<='9'){d=d*10+(i)-'0';i++;}i++;int decimal=1;while(i<length&&(i)>='0'&&(i)<='9'){decimal*=10;d=d+(float)(i)-'0')/decimal;i++;}return d;}实验结果(如界面图示)实验总结:本实验对算法的考查主要就是对表达式通过后缀表达式求值,以及各进制之间的转换,不能说难,只是有点冗长,特别是两者统一在一起时变来变去容易混淆,特别是浮点数的消暑部分的处理,稍微不小心就会出现错误,因此做这个程序主要考查大家对控件使用的熟悉与细心了。
题目:界面计算器学生姓名:专业:学号:指导老师:1.实验目的:设计一个简单的计算器程序,实现简单的计算功能。
2.实验内容:(1)体系设计:程序是一个简单的计算器,能正确输入数据,能实现加、减、乘、除等算术运算,运算结果能正确显示。
(2)设计思路:1)先在Visual C++ 6.0中建立一个MFC工程文件,名为calculator;2)在对话框中添加适当的编辑框、按钮、静态文件、复选框和单选框;3)设计按钮,并修改其相应的ID与Caption;4)选择和设置各控件的单击鼠标事件;5)为编辑框添加double类型的关联变量m_edit1;6)在calculatorDlg.h中添加math.h头文件,然后添加public成员;7)打开calculatorDlg.cpp文件,在构造函数中,进行成员初始化和完善各控件的响应函数代码.3.程序调试4.附录添加的public成员:double tempvalue; //存储中间变量double result; //存储显示结果的值int sort; //判断后面是何种运算:1.加法2.减法 3.乘法 4.除法int append; //判断后面是否添加数字成员初始化:CCalculatorDlg::CCalculatorDlg(CWnd* pParent/*=NULL*/): CDialog(CCalculatorDlg::IDD, pParent){//{{AFX_DATA_INIT(CCalculatorDlg)m_edit1 = 0.0;//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);tempvalue=0;result=0;sort=0;append=0;}各控件响应函数代码:void CCalculatorDlg::OnButton1() //按钮“1”{// TODO: Add your control notification handler code hereif(append==1)result=0;result=result*10+1;m_edit1=result;append=0;UpdateData(FALSE);void CCalculatorDlg::OnButton2() //按钮“2”{// TODO: Add your control notification handler code hereif(append==1)result=0;result=result*10+2;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculatorDlg::OnButton3() //按钮“3”{// TODO: Add your control notification handler code hereif(append==1)result=0;result=result*10+3;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculatorDlg::OnButton4() //按钮“4”// TODO: Add your control notification handler code hereif(append==1)result=0;result=result*10+4;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculatorDlg::OnButton5() //按钮“5”{// TODO: Add your control notification handler code hereif(append==1)result=0;result=result*10+5;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculatorDlg::OnButton6() //按钮“6”{// TODO: Add your control notification handler codehereif(append==1)result=0;result=result*10+6;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculatorDlg::OnButton7() //按钮“7”{// TODO: Add your control notification handler code hereif(append==1)result=0;result=result*10+7;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculatorDlg::OnButton8() //按钮“8”{// TODO: Add your control notification handler code hereif(append==1)result=0;result=result*10+8;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculatorDlg::OnButton9() //按钮“9”{// TODO: Add your control notification handler code hereif(append==1)result=0;result=result*10+9;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculatorDlg::OnBUTTONzero() //按钮“0”{// TODO: Add your control notification handler code hereif(append==1)result=0;result=result*10+0;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculatorDlg::OnBUTTONequal() //按钮“=”{// TODO: Add your control notification handler code hereswitch(sort){case 1:result=result+tempvalue;break;case 2:result=tempvalue-result;break;case 3:result=result*tempvalue;break;case 4:result=tempvalue/result;break;}m_edit1=result;sort=0;append=1;UpdateData(FALSE);}void CCalculatorDlg::OnBUTTONclean() //按钮“C”{// TODO: Add your control notification handler code heretempvalue=0;result=0;m_edit1=0.0;UpdateData(FALSE);}void CCalculatorDlg::OnBUTTONplus() //按钮“+”{// TODO: Add your control notification handler code heresort=1;tempvalue=result;m_edit1=0;append=1;}void CCalculatorDlg::OnBUTTONminus() //按钮“-”{// TODO: Add your control notification handler code heresort=2;tempvalue=result;m_edit1=0;append=1;}void CCalculatorDlg::OnBUTTONmulti() //按钮“*”{// TODO: Add your control notification handler code heresort=3;tempvalue=result;m_edit1=0;append=1;}void CCalculatorDlg::OnBUTTONdiv() //按钮“/”{// TODO: Add your control notification handler code heresort=4;tempvalue=result;m_edit1=0;append=1;}10。
MFC表达式计算器课程设计报告题目:利用MFC框架编写简易表达式计算器【分析】一.设计过程1.Windows消息处理机制的理解首先编写程序需要对Windows程序的消息处理机制(Message Handle)有个比较清晰的了解。
Windows的程序都是通过消息来传送数据,有不需要用户参与的系统消息,比如异常处理等。
还有用户消息,比如鼠标的单击,双击,键盘的键入等。
2.界面的设计1)界面的初步设计仿照Windows附件里面的计算器,在资源视图中画好界面,如图:2)修改每个static的属性ID CAPTIONIDD_STA TIC4 简易表达式计算器IDC_STATIC1 待求表达式IDC_STATIC2 运算结果IDC_STATIC3 系统当前时间3)修改每个button的属性IDC_BUTTON1 等于(=)IDC_BUTTON2 全清(C)IDC_BUTTON3 清除(A) 结果如下图:4)修改每个button的处理机制在类向导Classwizard窗口中进行,如下图:其他button按钮的修改类似5)修改每个edit的类型和名称在类向导Classwizard窗口中进行:单击Add Variable按钮,在如下窗口中进行修改其他edit的修改类似最终结果如下:注:主要使用到Layout菜单中的Align功能对各个按钮进行对齐,使界面更加整洁。
拖出的控件有上面的一个Edit控件用于显示数字,Button控件用于处理鼠标的消息。
6)系统菜单的添加在Menu的IDR_MENU1中添加系统菜单:同理在“帮助”菜单中添加“关于”。
二.设计步骤1. 添加头文件将Calculate.cpp(见附录)改为Calculate.h将其添加到计算器Dlg.cpp : implementation file中,如下:#include "Calculate.h"。
2.成员函数及其释义:void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMyDlg dialogCMyDlg::CMyDlg(CWnd* pParent /*=NULL*/): CDialog(CMyDlg::IDD, pParent){//{{AFX_DATA_INIT(CMyDlg)// NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CMyDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CMyDlg)DDX_Control(pDX, IDC_EDIT3, m_time);DDX_Control(pDX, IDC_EDIT2, m_result);DDX_Control(pDX, IDC_EDIT1, m_input);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CMyDlg, CDialog)//{{AFX_MSG_MAP(CMyDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_BUTTON2, OnButton2)ON_WM_TIMER()ON_BN_CLICKED(IDC_BUTTON3, OnButton3)ON_COMMAND(ID_ABOUT, OnAbout)ON_COMMAND(ID_QUIT, OnQuit)//}}AFX_MSG_MAPEND_MESSAGE_MAP()////////////////////////////////////////////////////////////////////////////// CMyDlg message handlers3.OnButton1()按钮的处理函数双击“等于(=)”按钮,添加如下代码:void CMyDlg::OnButton1(){// TODO: Add your control notification handler code hereCString str;char *ch;m_input.GetWindowText(str);ch = (LPSTR)(LPCTSTR)str;char ch2[50];strcpy(ch2,ch);Cal a(ch2);if(!a.OK){m_result.SetWindowText("表达式不合法!");// 表达式不合法,判别出来并给出相应的错误提示}else{if(a.Sign){m_result.SetWindowText("除数为零!");// 表达式不合法,可以判别出来并给出相应的错误提示}else{str.Format("%lf",a.GetV());m_result.SetWindowText(str);}}}以OnButton1()作为求值处理函数,函数的功能是单击等于(=)按钮,运算结果显示在IDC_EDIT2中4.OnButton2()按钮的处理函数双击“清除(C)”按钮,添加如下代码:void CMyDlg::OnButton2(){// TODO: Add your control notification handler code herem_result.SetWindowText("0");m_input.SetWindowText("");m_input.SetFocus();}//函数的功能是把上次输入的表达式清空5.OnButton3()按钮的处理函数双击“全清(A)”按钮,添加如下代码:void CMyDlg::OnButton3(){// TODO: Add your control notification handler code herem_input.SetWindowText("");m_input.SetFocus();}//函数的功能是把上次输入的表达式和运算结果都清除6.OnTimer(UINT nIDEvent)处理函数CWnd::SetTimer(1,500,NULL);//设置时间每隔500ms更新一次。
院系:计算机学院实验课程:计算机基础实验实验项目:程序设计入门基础指导老师:杨志强开课时间:2010 ~ 2011年度第 2学期专业:计算机类班级: 10本4学生:杨晓添学号:20102100114华南师范大学教务处1.题目:简单计算器2.实验目的:模仿日常生活中所用的计算器,自行设计一个简单的计算器程序,实现简单的计算功能。
3.实验主要硬件软件环境:Window 7Visual C++ 6.04.实验内容:(1)体系设计:程序是一个简单的计算器,能正确输入数据,能实现加、减、乘、除等算术运算,能进行简单三角运算,运算结果能正确显示,可以清楚数据等。
(2)设计思路:1)先在Visual C++ 6.0中建立一个MFC工程文件,名为calculator.2)在对话框中添加适当的编辑框、按钮、静态文件、复选框和单选框3)设计按钮,并修改其相应的ID与Caption.4)选择和设置各控件的单击鼠标事件。
5)为编辑框添加double类型的关联变量m_edit1.6)在calculatorDlg.h中添加math.h头文件,然后添加public成员。
7)打开calculatorDlg.cpp文件,在构造函数中,进行成员初始化和完善各控件的响应函数代码。
(3)程序清单:程序代码如下:// calculator_1Dlg.cpp : implementation file//#include "stdafx.h"#include "calculator_1.h"#include "calculator_1Dlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CCalculator_1Dlg dialogCCalculator_1Dlg::CCalculator_1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CCalculator_1Dlg::IDD, pParent){//{{AFX_DATA_INIT(CCalculator_1Dlg)m_edit1 = 0.0;//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);tempvalue=0;result=0;sort=0;append=0;}void CCalculator_1Dlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CCalculator_1Dlg)DDX_Text(pDX, IDC_EDIT1, m_edit1);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CCalculator_1Dlg, CDialog)//{{AFX_MSG_MAP(CCalculator_1Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_BUTTON2, OnButton2)ON_BN_CLICKED(IDC_BUTTON3, OnButton3)ON_BN_CLICKED(IDC_BUTTON4_plus, OnBUTTON4plus)ON_BN_CLICKED(IDC_BUTTON5_4, OnButton54)ON_BN_CLICKED(IDC_BUTTON6_5, OnButton65)ON_BN_CLICKED(IDC_BUTTON7_6, OnButton76)ON_BN_CLICKED(IDC_BUTTON8_minus, OnBUTTON8minus) ON_BN_CLICKED(IDC_BUTTON12_multi, OnBUTTON12multi) ON_BN_CLICKED(IDC_BUTTON16_div, OnBUTTON16div)ON_BN_CLICKED(IDC_BUTTON9_7, OnButton97)ON_BN_CLICKED(IDC_BUTTON10_8, OnButton108)ON_BN_CLICKED(IDC_BUTTON11_9, OnButton119)ON_BN_CLICKED(IDC_BUTTON13_zero, OnBUTTON13zero) ON_BN_CLICKED(IDC_BUTTON14_equal, OnBUTTON14equal) ON_BN_CLICKED(IDC_RADIO1_sin, OnRADIO1sin)ON_BN_CLICKED(IDC_RADIO2_cos, OnRADIO2cos)ON_BN_CLICKED(IDC_RADIO3_tan, OnRADIO3tan)ON_BN_CLICKED(IDC_RADIO5_log10, OnRADIO5log10)ON_BN_CLICKED(IDC_BUTTON15_clean, OnBUTTON15clean) //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CCalculator_1Dlg message handlersBOOL CCalculator_1Dlg::OnInitDialog(){CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control}void CCalculator_1Dlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.void CCalculator_1Dlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags // the minimized window.HCURSOR CCalculator_1Dlg::OnQueryDragIcon(){return (HCURSOR) m_hIcon;}void CCalculator_1Dlg::OnButton1(){// TODO: Add your control notification handler code hereif(append==1) result=0;result=result*10+1;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnButton2(){// TODO: Add your control notification handler code hereif(append==1) result=0;result=result*10+2;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnButton3(){// TODO: Add your control notification handler code here if(append==1) result=0;result=result*10+3;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnBUTTON4plus(){// TODO: Add your control notification handler code here sort=1;tempvalue=result;m_edit1=0;append=1;}void CCalculator_1Dlg::OnButton54(){// TODO: Add your control notification handler code here if(append==1) result=0;result=result*10+4;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnButton65(){// TODO: Add your control notification handler code here if(append==1) result=0;result=result*10+5;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnButton76(){// TODO: Add your control notification handler code here if(append==1) result=0;result=result*10+6;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnBUTTON8minus(){// TODO: Add your control notification handler code here sort=2;tempvalue=result;m_edit1=0;append=1;}void CCalculator_1Dlg::OnBUTTON12multi(){// TODO: Add your control notification handler code here sort=3;tempvalue=result;m_edit1=0;append=1;}void CCalculator_1Dlg::OnBUTTON16div(){// TODO: Add your control notification handler code here sort=4;tempvalue=result;m_edit1=0;append=1;}void CCalculator_1Dlg::OnButton97(){// TODO: Add your control notification handler code here if(append==1) result=0;result=result*10+7;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnButton108(){// TODO: Add your control notification handler code here if(append==1) result=0;result=result*10+8;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnButton119(){// TODO: Add your control notification handler code here if(append==1) result=0;result=result*10+9;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnBUTTON13zero(){// TODO: Add your control notification handler code here if(append==1) result=0;result=result*10+0;m_edit1=result;append=0;UpdateData(FALSE);}void CCalculator_1Dlg::OnBUTTON14equal(){// TODO: Add your control notification handler code here switch(sort){case 1:result=result+tempvalue;break;case 2:result=tempvalue-result;break;case 3:result=tempvalue*result;break;case 4:result=tempvalue/result;break;}m_edit1=result;UpdateData(FALSE);}void CCalculator_1Dlg::OnRADIO1sin(){// TODO: Add your control notification handler code here m_edit1=sin(result);UpdateData(FALSE);}void CCalculator_1Dlg::OnRADIO2cos(){// TODO: Add your control notification handler code here m_edit1=cos(result);UpdateData(FALSE);}void CCalculator_1Dlg::OnRADIO3tan(){// TODO: Add your control notification handler code here m_edit1=tan(result);UpdateData(FALSE);}void CCalculator_1Dlg::OnRADIO5log10(){// TODO: Add your control notification handler code here m_edit1=log10(result);UpdateData(FALSE);}void CCalculator_1Dlg::OnBUTTON15clean(){// TODO: Add your control notification handler code here tempvalue=0;result=0;m_edit1=0.0;UpdateData(FALSE);}(4)运行结果5实验小结:该次实验,是首次使用面向对象窗口进行的实验。