VC计算器实现实验报告(总35页_第1页
VC计算器实现实验报告(总35页_第2页
VC计算器实现实验报告(总35页_第3页
VC计算器实现实验报告(总35页_第4页
VC计算器实现实验报告(总35页_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、VC课程设计实验报告课题名称:计算器实现姓名: 陈锋 学号:2008221105110010 提交报告时间: 2010年 11 月 22日课程设计目标实验设计一个计算器,要求可通过按钮输入数字、运算符,能通过按钮实现退格、清除功能,实现整数的加、减、乘、除、取余、开方、平方等运算功能,必要的错误处理,如除零;可以通过键盘输入数字、退格、运算符(+、-、*、/、%、=),实现括号运算;实现不同进制(二进制、十进制、八进制、十六进制)下的加、减、乘、除、取余、开方、平方等运算功能。1.分析与设计(1)实现方法:编程语言为C+语言。编程方法:通过一个文本框接收所输入的运算表达式,然后将其转换成后缀表

2、达式并将各个数字先转换成十进制数值进行计算,最后再转换成相应进制的字符串。(2)代码设计说明:文件和类的设计说明:创建一个头文件:Calculate.h内容如下:#includestdio.hconst int MaxSize=30;const int MaxPri=8;BOOL decimal_2; /为真代表选择相应的进制BOOL decimal_8;BOOL decimal_10;BOOL decimal_16;structchar ch;int pri;lpri=,0,(,1,*,5,/,5,%,5,+,3,-,3,),8,7,rpri=,0,(,8,*,4,/,4,%,4,+,2,-

3、,2,),1,6;int leftpri(char op)/求左运算符的优先级for(int i=0;iMaxPri;i+)if(lprii.ch=op)return lprii.pri;int rightpri(char op)/求右运算符op的优先级for(int i=0;iMaxPri;i+)if(rprii.ch=op)return rprii.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

4、 op1,char op2)if(leftpri(op1)=rightpri(op2)return 0;else if(leftpri(op1)=0&*exp=a&*exp=f)postexpi+=*exp;exp+;postexpi+=#;else switch(Precede(op.dataop.top,*exp)case -1:op.top+;op.dataop.top=*exp;exp+;break;case 0:op.top-;exp+;break;case 1:postexpi+=op.dataop.top;op.top-;break;while(op.dataop.top!=)p

5、ostexpi+=op.dataop.top;op.top-;postexpi=0;float compvalue(char *postexp)/计算后缀表达式的值structfloat dataMaxSize;int top;st;float d,a,b,c;st.top=-1;while(*postexp!=0)switch(*postexp)case +:a=st.datast.top;st.top-;b=st.datast.top;st.top-;c=a+b;st.top+;st.datast.top=c;break;case -:a=st.datast.top;st.top-;b=s

6、t.datast.top;st.top-;c=b-a;st.top+;st.datast.top=c;break;case *:a=st.datast.top;st.top-;b=st.datast.top;st.top-;c=a*b;st.top+;st.datast.top=c;break;case /:a=st.datast.top;st.top-;b=st.datast.top;st.top-;if(a!=0)c=b/a;st.top+;st.datast.top=c;else MessageBox(NULL,t除零错误!,Error,MB_OK);st.datast.top=0;br

7、eak;break;case %:a=st.datast.top;st.top-;b=st.datast.top;st.top-;if(a!=0)c=(int)b%(int)a;st.top+;st.datast.top=c;else MessageBox(NULL,t余零错误!,Error,MB_OK);st.datast.top=0;break;break;case :a=st.datast.top;st.top-;b=st.datast.top;st.top-;float d=1;for(int i=0;i=0&*postexp=a&*postexp=0&*postexp=0&*post

8、exp=0&*postexp=0&*postexp=0&*postexp=a&*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.datast.top=d;break;postexp+;return(st.datast.top);(3) 各控件变量:(如图)(4)界面图示: 2. 程序代码实现BOOL CMyDlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to s

9、ystem menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applicat

10、ions main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization here/*初始化*/CheckRadioButton(IDC_RADIO_TEN,IDC_RADIO_SIXTEEN,IDC_RADIO_TEN);/设置单选按钮m_show=0;/文本框内容初始化为0UpdateData(FALSE);/显示文本框内容GetDlgItem(IDC_BTN_10)-EnableW

11、indow(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

12、;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(FALSE);void CMyDlg:OnBtn1()/输入1/ TODO: Add your control notification handler

13、 code hereif(m_show=0)m_show=1;else m_show+=1;UpdateData(FALSE);void CMyDlg:OnBtn2()/输入2/ TODO: Add your control notification handler code hereif(m_show=0)m_show=2;else m_show+=2;UpdateData(FALSE);void CMyDlg:OnBtn3()/输入3/ TODO: Add your control notification handler code hereif(m_show=0)m_show=3;els

14、e m_show+=3;UpdateData(FALSE);void CMyDlg:OnBtn4()/输入4/ TODO: Add your control notification handler code hereif(m_show=0)m_show=4;else m_show+=4;UpdateData(FALSE);void CMyDlg:OnBtn5()/输入5/ TODO: Add your control notification handler code hereif(m_show=0)m_show=5;else m_show+=5;UpdateData(FALSE);void

15、 CMyDlg:OnBtn6()/输入6/ TODO: Add your control notification handler code hereif(m_show=0)m_show=6;else m_show+=6;UpdateData(FALSE);void CMyDlg:OnBtn7()/输入7/ TODO: Add your control notification handler code hereif(m_show=0)m_show=7;else m_show+=7;UpdateData(FALSE);void CMyDlg:OnBtn8()/输入8/ TODO: Add yo

16、ur control notification handler code hereif(m_show=0)m_show=8;else m_show+=8;UpdateData(FALSE);void CMyDlg:OnBtn9()/输入9/ TODO: Add your control notification handler code hereif(m_show=0)m_show=9;else m_show+=9;UpdateData(FALSE);void CMyDlg:OnBtn10()/输入a (16进制)/ TODO: Add your control notification ha

17、ndler code hereif(m_show=0)m_show=a;else m_show+=a;UpdateData(FALSE);void CMyDlg:OnBtn11()/输入b (16进制)/ TODO: Add your control notification handler code hereif(m_show=0)m_show=b;else m_show+=b;UpdateData(FALSE);void CMyDlg:OnBtn12()/输入c (16进制)/ TODO: Add your control notification handler code hereif(

18、m_show=0)m_show=c;else m_show+=c;UpdateData(FALSE);void CMyDlg:OnBtn13()/输入d (16进制)/ TODO: Add your control notification handler code hereif(m_show=0)m_show=d;else m_show+=d;UpdateData(FALSE);void CMyDlg:OnBtn14()/输入e (16进制)/ TODO: Add your control notification handler code hereif(m_show=0)m_show=e;

19、else m_show+=e;UpdateData(FALSE);void CMyDlg:OnBtn15()/输入f (16进制)/ TODO: Add your control notification handler code hereif(m_show=0)m_show=f;else m_show+=f;UpdateData(FALSE);void CMyDlg:OnBtnDot()/输入./ TODO: Add your control notification handler code herem_show+=.;UpdateData(FALSE);void CMyDlg:OnBtn

20、Equal()/点击=按钮,计算表达式的结果并显示/ TODO: Add your control notification handler code hereif(m_show=0)m_show=0;else if(m_showm_show.GetLength()-1=0&m_showm_show.GetLength()-1=0&m_showm_show.GetLength()-1=a|m_showm_show.GetLength()-1=0&m_showm_show.GetLength()-1=a|m_showm_show.GetLength()-1=0&m_showm_show.GetL

21、ength()-1=a|m_showm_show.GetLength()-1=0&m_showm_show.GetLength()-1=a|m_showm_show.GetLength()-1=0&m_showm_show.GetLength()-1=a|m_showm_show.GetLength()-1=0&m_showm_show.GetLength()-1=0&m_showm_show.GetLength()-1=a|m_showm_show.GetLength()-1=0&m_show.GetAt(m_show.GetLength()-length)=a&m_show.GetAt(m

22、_show.GetLength()-length)=0&m_show.GetAt(m_show.GetLength()-length)=0&m_show.GetAt(m_show.GetLength()-length)=a&m_show.GetAt(m_show.GetLength()-length)=0&m_show.GetAt(m_show.GetLength()-length)=0&m_show.GetAt(m_show.GetLength()-length)=a&m_show.GetAt(m_show.GetLength()-length)=0&m_show.GetAt(m_show.

23、GetLength()-length)=0&m_show.GetAt(m_show.GetLength()-length)=a&m_show.GetAt(m_show.GetLength()-length)=0&m_show.GetAt(m_show.GetLength()-length)EnableWindow(true);GetDlgItem(IDC_BTN_3)-EnableWindow(true);GetDlgItem(IDC_BTN_4)-EnableWindow(true);GetDlgItem(IDC_BTN_5)-EnableWindow(true);GetDlgItem(ID

24、C_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_1

25、3)-EnableWindow(false);GetDlgItem(IDC_BTN_14)-EnableWindow(false);GetDlgItem(IDC_BTN_15)-EnableWindow(false);/*进制转换*/OnBtnEqual();Trans_to_Ten();UpdateData(FALSE);/*进制选择*/decimal_2=false;decimal_8=false;decimal_10=true;decimal_16=false;void CMyDlg:OnRadioTwo()/选择2进制/ TODO: Add your control notificat

26、ion 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_B

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论