1、计算器系统实现目 录0摘要11引言1 1.1课题背景1 1.2课设要求12分析1 2.1可行性分析2 2.2需求分析23正文23.1.利用MFC框架编写简易计算器23.1.1建立的变量,控件的命名23.1.2界面的设计23.1.3变量的定义及初始化33.1.4成员函数33.1.5源代码43.1.6调试过程193.2. VC+6.0下windows API编写的计算器193.2.1建立的变量193.2.2界面的设计203.2.3成员函数203.2.4源代码253.2.5调试过程294小结295参考文献2920 摘 要随着时代的发展,简洁、方便已经成为当今时代人们所需求的特点。这次课程设计的任务就
2、是用API与MFC编程来设计一个简洁、方便的计算器,就是类似于window自带的计算器,要求计算器能够运算简单的四则运算。在人们使用的计算器的时候,能够便捷的进行操作,从而节约了时间,提高了工作的效率。本次课程设计的要求就是使用MFC框架在VC6.0的环境下编写一个简单的计算器。关键词 :计算器、MFC、API、简洁、方便1 引 言本课程设计主要是设计一个简易的计算器,能够实现简单的四则运算。1.1 课题背景计算器是大家经常用的辅助工具,在日常生活中有了计算器能大大地提高人们运算的速度和准确率,能够实现加减乘除的功能,要实现计算功能,可以用VC+的知识编写程序来解决此问题,对于我们使用VC也是
3、很好的一次锻炼。1.2 课设要求通过本课程设计,目标是使学生了解可视化程序设计语言的基础知识,掌握面向对象编程的分析设计方法。实现计算器的基本功能:连续数据的无优先级混合运算(加减乘除)实现其它附加功能:优先级运算,加入括号,加入其它函数运算功能等。要求界面良好,功能完整。 2 分 析2.1 可行性分析本设计是简单计算器。简单计算器有着简单易操作的优点,在日常生活中很使用,它的界面简洁,使人们能容易地操作计算器,它占用的空间资源少,对电脑的影响也很小,运行速度快,能使人们快速的得到计算的结果,充分提高了计算效率,对人们的日常生活有一定的帮助。2.2 需求分析 本软件主要用于计算机windows
4、 2000/XP以上版本的用户的使用,这次课程设计一般是实现简单计算器的功能。要程序能实现:加,减,乘,除等运算功能;二进制,十进制,还要实现数据的输入,输出,计算,显示及清零等功能。 3 正 文3.1.利用MFC框架编写简易计算器要求使用MFC框架在Visual Studio 6.0环境下编写一个简易的计算器,支持任意位数的加减乘数。3.1.1建立的变量,控件的命名5IDC_BUTTON0 0IDC_BUTTON1 1IDC_BUTTON2 2IDC_BUTTON3 3IDC_BUTTON4 4IDC_BUTTON5 5IDC_BUTTON6 6IDC_BUTTON7 7IDC_BUTTON
5、8 8IDC_BUTTON9 9IDC_BUTTON10 .IDC_BUTTON11 =IDC_BUTTON12 +IDC_BUTTON13 -IDC_BUTTON14 *IDC_BUTTON15 /IDC_BUTTON16 DELIDC_BUTTON17 clearIDC_BUTTON18 (IDC_BUTTON19 )IDC_BUTTON20 sinIDC_BUTTON21 cosIDC_BUTTON22 sqrtIDC_BUTTON23 1/xIDC_BUTTON24 logIDC_BUTTON25 tanIDC_BUTTON26 二进制IDC_BUTTON27 十进制IDC_BUTTO
6、N28 OFFIDC_EDIT1 编辑IDC_EDIT2 编辑3.1.2界面的设计 3.1.3变量的定义及初始化int k=-1;float p;int j2=0;int j8=0;int j16=0;float stor=0;int m_type=10;CString m_str;CString m_edit2;bool optr=false;bool Sin=false;bool Cos=false;bool Tan=false;bool Ln=false;bool dot=false;bool qiudao=false;bool Sqrt=false;bool equals=false;
7、float ExpEvaluation();3.1.4成员函数void CMFCDlg:OnButton0() 按键0void CMFCDlg:OnButton1() 按键1void CMFCDlg:OnButton2() 按键2void CMFCDlg:OnButton3() 按键3void CMFCDlg:OnButton4() 按键4void CMFCDlg:OnButton5() 按键5void CMFCDlg:OnButton6() 按键6void CMFCDlg:OnButton7() 按键7void CMFCDlg:OnButton8() 按键8void CMFCDlg:OnBu
8、tton9() 按键9void CMFCDlg:OnButton10() 按键.void CMFCDlg:OnButton11() 按键=void CMFCDlg:OnButton12() 按键+void CMFCDlg:OnButton13() 按键-void CMFCDlg:OnButton14() 按键*void CMFCDlg:OnButton15() 按键/void CMFCDlg:OnButton16() 按键DELvoid CMFCDlg:OnButton17() 按键clearvoid CMFCDlg:OnButton18() 按键(void CMFCDlg:OnButton1
9、9() 按键)void CMFCDlg:OnButton20() 按键sinvoid CMFCDlg:OnButton21() 按键cosvoid CMFCDlg:OnButton22() 按键sqrtvoid CMFCDlg:OnButton23() 按键1/xvoid CMFCDlg:OnButton24() 按键logvoid CMFCDlg:OnButton25() 按键tanvoid CMFCDlg:OnButton26()按键二进制void CMFCDlg:OnButton27() 按键十进制void CMFCDlg:OnButton28() 按键OFF3.1.5源代码#inclu
10、de stdafx.h#include 我的计算器MFC.h#include 我的计算器MFCDlg.h#include#include#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutint k=-1;float p;int j2=0;int j8=0;int j16=0;float stor=0;int m_type=10;CString m_str;CString m_edit2;bool o
11、ptr=false;bool Sin=false;bool Cos=false;bool Tan=false;bool Ln=false;bool dot=false;bool qiudao=false;bool Sqrt=false;bool equals=false;float ExpEvaluation();class CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated
12、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_INITvoi
13、d CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/栈的操作函数#define TRUE 1#define FALSE 0#define Stack_Size 20#define Stack_Float 30
14、/*建立字符栈*/ #include#include#define STACK_INIT_SIZE 20/存储空间初始分配量#define STACKINCREMENT 10/存储空间分配增量typedef float SElemType;typedef struct sqstack SElemType *base; SElemType *top; int stacksize;*SqStack;void InitStack(SqStack s)/构造栈 s-base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType); if(!s-bas
15、e) printf(OVERFLOW!); exit(1); s-top=s-base; s-stacksize=STACK_INIT_SIZE;void GetTop(SqStack s,SElemType *e)/读取栈顶元素 if(s-top=s-base) printf(The Stack Is Empty!n); exit(1); *e=*(s-top-1);void Push(SqStack s,SElemType e)/插入新元素 if(s-top-s-base=s-stacksize) s-base=(SElemType *)realloc(s-base,(s-stacksiz
16、e+STACKINCREMENT)*sizeof(SElemType); /增大栈空间 if(!s-base) printf(The Stack Is Empty!n); exit(1); s-top=s-base+s-stacksize; s-stacksize+=STACKINCREMENT; *(s-top)=e; s-top+;void Pop(SqStack s,SElemType *e)/删除栈顶元素 if(s-top=s-base) printf(The Stack Is Empty!n); exit(1); *e=*(-s-top);int In(char e)/判断是否为7种
17、运算符之一 switch(e) case +: case -: case *: case /: case (: case ): case #:return(1);break;/是运算符返回 1,否则,返回 0 default:return(0); char Precede(char p,char c)/比较OPTR栈顶元素与取得元素的优先级 /栈顶元素优先级高,返回 ,反之返回 switch(p) case +: case -: switch(c) case *: case /: case (:return ;break; break; case *: case /: switch(c) ca
18、se (:return ;break; break; case (: switch(c) case ):return =;break; case #:printf(ERROR!n);exit(1); default:return ;break; break; case #: switch(c) case ):printf(ERROR!n);exit(1); case #:return =;break; default:return LoadIcon(IDR_MAINFRAME);void CMFCDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoD
19、ataExchange(pDX);/AFX_DATA_MAP(CMFCDlg)DDX_Control(pDX, IDC_EDIT2, m_edit);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CMFCDlg, CDialog)/AFX_MSG_MAP(CMFCDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON0, OnButton0)ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_BUTTON2, OnB
20、utton2)ON_BN_CLICKED(IDC_BUTTON3, OnButton3)ON_BN_CLICKED(IDC_BUTTON4, OnButton4)ON_BN_CLICKED(IDC_BUTTON5, OnButton5)ON_BN_CLICKED(IDC_BUTTON6, OnButton6)ON_BN_CLICKED(IDC_BUTTON7, OnButton7)ON_BN_CLICKED(IDC_BUTTON8, OnButton8)ON_BN_CLICKED(IDC_BUTTON9, OnButton9)ON_BN_CLICKED(IDC_BUTTON10, OnButt
21、on10)ON_BN_CLICKED(IDC_BUTTON11, OnButton11)ON_BN_CLICKED(IDC_BUTTON13, OnButton13)ON_BN_CLICKED(IDC_BUTTON14, OnButton14)ON_BN_CLICKED(IDC_BUTTON15, OnButton15)ON_BN_CLICKED(IDC_BUTTON12, OnButton12)ON_BN_CLICKED(IDC_BUTTON16, OnButton16)ON_BN_CLICKED(IDC_BUTTON17, OnButton17)ON_BN_CLICKED(IDC_BUTT
22、ON18, OnButton18)ON_BN_CLICKED(IDC_BUTTON19, OnButton19)ON_BN_CLICKED(IDC_BUTTON20, OnButton20)ON_BN_CLICKED(IDC_BUTTON21, OnButton21)ON_BN_CLICKED(IDC_BUTTON23, OnButton23)ON_BN_CLICKED(IDC_BUTTON22, OnButton22)ON_BN_CLICKED(IDC_BUTTON26, OnButton26)ON_BN_CLICKED(IDC_BUTTON27, OnButton27)ON_BN_CLIC
23、KED(IDC_BUTTON28, OnButton28)ON_BN_CLICKED(IDC_TIME_CHECK, OnTimeCheck)ON_WM_TIMER()ON_BN_CLICKED(IDC_BUTTON24, OnButton24)ON_BN_CLICKED(IDC_BUTTON25, OnButton25)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMFCDlg message handlersBOOL CMFCDlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to system
24、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 applications m
25、ain 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 controlvoid CMFCDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlg
26、About;dlgAbout.DoModal();elseCDialog: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 CMFCDlg:OnPaint() if (IsIconic()
27、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
28、 = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint(); PAINTSTRUCT ps; CDC *dc = BeginPaint(&ps); CDC memdc; CRect rect; CBitmap bitmap; BITMAP szbitmap; bitmap.LoadBitmap(IDB_BITMAP2); bitmap.GetObject( sizeof(BITMAP),&szbitmap ); CSize size( szbitmap.b
29、mWidth,szbitmap.bmHeight ); memdc.CreateCompatibleDC(dc); CBitmap *oldbitmap=memdc.SelectObject(&bitmap); GetClientRect(&rect); dc-StretchBlt(0,0,rect.Width(),rect.Height(), &memdc,0,0,size.cx,size.cy,SRCCOPY); memdc.SelectObject(oldbitmap); memdc.DeleteDC(); EndPaint(&ps);/ The system calls this to
30、 obtain the cursor to display while the user drags/ the minimized window.HCURSOR CMFCDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CMFCDlg:OnButton0() / TODO: Add your control notification handler code herem_str+=0;m_edit2+=0;/optr=false;CEdit *pEdit;pEdit=(CEdit*)GetDlgItem(IDC_EDIT1);pEdit-Se
31、tWindowText(m_str);void CMFCDlg:OnButton1() / TODO: Add your control notification handler code herem_str+=1;m_edit2+=1;/optr=false;dot=false;CEdit *pEdit;pEdit=(CEdit*)GetDlgItem(IDC_EDIT1);pEdit-SetWindowText(m_str);void CMFCDlg:OnButton2() / TODO: Add your control notification handler code herem_s
32、tr+=2;m_edit2+=2;/optr=false;dot=false;CEdit *pEdit;pEdit=(CEdit*)GetDlgItem(IDC_EDIT1);pEdit-SetWindowText(m_str);void CMFCDlg:OnButton3() / TODO: Add your control notification handler code herem_str+=3;m_edit2+=3;/optr=false;dot=false;CEdit *pEdit;pEdit=(CEdit*)GetDlgItem(IDC_EDIT1);pEdit-SetWindo
33、wText(m_str);void CMFCDlg:OnButton4() / TODO: Add your control notification handler code herem_str+=4;m_edit2+=4;/optr=false;dot=false;CEdit *pEdit;pEdit=(CEdit*)GetDlgItem(IDC_EDIT1);pEdit-SetWindowText(m_str);void CMFCDlg:OnButton5() / TODO: Add your control notification handler code herem_str+=5;
34、m_edit2+=5;/optr=false;dot=false;CEdit *pEdit;pEdit=(CEdit*)GetDlgItem(IDC_EDIT1);pEdit-SetWindowText(m_str);void CMFCDlg:OnButton6() / TODO: Add your control notification handler code herem_str+=6;m_edit2+=6;/optr=false;dot=false;CEdit *pEdit;pEdit=(CEdit*)GetDlgItem(IDC_EDIT1);pEdit-SetWindowText(m_str);void CMFCDlg:OnButton7() / TODO: Add your control notification handler code herem_str+=7;m_edit2+=7;/optr=false;dot=false;CEdit *pEdit;pEdit=(CEdit*)GetDlg