MFC简单计算器(我的实验报告1.2版本

网友投稿 734 2022-11-17

MFC简单计算器(我的实验报告1.2版本)

MFC简单计算器(我的实验报告1.2版本)

和​​上一篇​​​思想一样,详解看上一篇,我就直接给全部代码了。这一篇继承了上一篇所有的功能,还增加了连续加或连续除、减、乘,但是做不到a+b-c或a-b+c,也就是不能换符号,这个就是困惑了我第一天一整天的地方,最后查了一天也解决不了,我还想到用正则表达式和我​​1.3版本​​思想的结合版本1.4,可是,我不会用正则表达式来分割字符串,所以做出了这1.2为1.1改良版,1.3思想转变的铺垫版。现在也是有个bug,等待大佬解救。不过,还是可以应付交作业的。

效果如下:

代码区(全部代码,后面有提示有用部分,请耐心看,也可以选择另外2个版本):

第一部分代码是不需我们打的,这里展示只是方便知道是哪个文件里的

// MFCApplication11Dlg.cpp: 实现文件//#include "stdafx.h"#include "MFCApplication11.h"#include "MFCApplication11Dlg.h"#include "afxdialogex.h"#ifdef#define#endif// 用于应用程序“关于”菜单项的 CAboutDlg 对话框class CAboutDlg : public CDialogEx{public: CAboutDlg(); // 对话框数据#ifdef enum { IDD = IDD_ABOUTBOX };#endifprotected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现protected: DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX){}void CAboutDlg::DoDataExchange(CDataExchange* pDX){ CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()// CMFCApplication11Dlg 对话框CMFCApplication11Dlg::CMFCApplication11Dlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_MFCAPPLICATION11_DIALOG, pParent){ m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CMFCApplication11Dlg::DoDataExchange(CDataExchange* pDX){ CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CMFCApplication11Dlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication11Dlg::OnBnClickedButton1) ON_BN_CLICKED(IDC_BUTTON2, &CMFCApplication11Dlg::OnBnClickedButton2) ON_BN_CLICKED(IDC_BUTTON3, &CMFCApplication11Dlg::OnBnClickedButton3) ON_BN_CLICKED(IDC_BUTTON4, &CMFCApplication11Dlg::OnBnClickedButton4) ON_BN_CLICKED(IDC_BUTTON5, &CMFCApplication11Dlg::OnBnClickedButton5) ON_BN_CLICKED(IDC_BUTTON6, &CMFCApplication11Dlg::OnBnClickedButton6) ON_BN_CLICKED(IDC_BUTTON7, &CMFCApplication11Dlg::OnBnClickedButton7) ON_BN_CLICKED(IDC_BUTTON8, &CMFCApplication11Dlg::OnBnClickedButton8) ON_BN_CLICKED(IDC_BUTTON9, &CMFCApplication11Dlg::OnBnClickedButton9) ON_BN_CLICKED(IDC_BUTTON10, &CMFCApplication11Dlg::OnBnClickedButton10) ON_BN_CLICKED(IDC_BUTTONPOINT, &CMFCApplication11Dlg::OnBnClickedButtonpoint) ON_BN_CLICKED(IDC_BUTTON12, &CMFCApplication11Dlg::OnBnClickedButton12) ON_BN_CLICKED(IDC_BUTTONADD, &CMFCApplication11Dlg::OnBnClickedButtonadd) ON_BN_CLICKED(IDC_BUTTONSUB, &CMFCApplication11Dlg::OnBnClickedButtonsub) ON_BN_CLICKED(IDC_BUTTONMUL, &CMFCApplication11Dlg::OnBnClickedButtonmul) ON_BN_CLICKED(IDC_BUTTONDIV, &CMFCApplication11Dlg::OnBnClickedButtondiv) ON_BN_CLICKED(IDC_BUTTONBACK, &CMFCApplication11Dlg::OnBnClickedButtonback) ON_BN_CLICKED(IDC_BUTTONCLEAR, &CMFCApplication11Dlg::OnBnClickedButtonclear) ON_BN_CLICKED(IDC_BUTTONJIECHENG, &CMFCApplication11Dlg::OnBnClickedButtonjiecheng) ON_BN_CLICKED(IDC_BUTTONSIN, &CMFCApplication11Dlg::OnBnClickedButtonsin) ON_BN_CLICKED(IDC_BUTTONCOS, &CMFCApplication11Dlg::OnBnClickedButtoncos) ON_BN_CLICKED(IDC_BUTTON30, &CMFCApplication11Dlg::OnBnClickedButton30) ON_BN_CLICKED(IDC_BUTTONABOUT, &CMFCApplication11Dlg::OnBnClickedButtonabout) ON_BN_CLICKED(IDC_BUTTONPINFANG, &CMFCApplication11Dlg::OnBnClickedButtonpinfang) ON_BN_CLICKED(IDC_BUTTONKAIFANG, &CMFCApplication11Dlg::OnBnClickedButtonkaifang) ON_BN_CLICKED(IDC_BUTTON26, &CMFCApplication11Dlg::OnBnClickedButton26) ON_BN_CLICKED(IDC_BUTTONLOG, &CMFCApplication11Dlg::OnBnClickedButtonlog) ON_BN_CLICKED(IDC_BUTTONLN, &CMFCApplication11Dlg::OnBnClickedButtonln) ON_BN_CLICKED(IDC_BUTTON29, &CMFCApplication11Dlg::OnBnClickedButton29) ON_BN_CLICKED(IDC_BUTTON31, &CMFCApplication11Dlg::OnBnClickedButton31) ON_BN_CLICKED(IDC_BUTTONEQUAL, &CMFCApplication11Dlg::OnBnClickedButtonequal)END_MESSAGE_MAP()// CMFCApplication11Dlg 消息处理程序BOOL CMFCApplication11Dlg::OnInitDialog(){ CDialogEx::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != nullptr) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 return TRUE; // 除非将焦点设置到控件,否则返回 TRUE}void CMFCApplication11Dlg::OnSysCommand(UINT nID, LPARAM lParam){ if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::OnSysCommand(nID, lParam); }}// 如果向对话框添加最小化按钮,则需要下面的代码// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,// 这将由框架自动完成。void CMFCApplication11Dlg::OnPaint(){ if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 int 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); }}//当用户拖动最小化窗口时系统调用此函数取得光标//显示。HCURSOR CMFCApplication11Dlg::OnQueryDragIcon(){ return static_cast(m_hIcon);}

前面都是没用的,后面是自己打的,有用的

double num1 = 0.0, num2 = 0.0; static double result = 0.0;const double EPSINON = 0.00001;CString temp;int opt; int Ctime = 0;

void CMFCApplication11Dlg::OnBnClickedButton1(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("1"));}void CMFCApplication11Dlg::OnBnClickedButton2(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("2"));}void CMFCApplication11Dlg::OnBnClickedButton3(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("3"));}void CMFCApplication11Dlg::OnBnClickedButton4(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("4"));}void CMFCApplication11Dlg::OnBnClickedButton5(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("5"));}void CMFCApplication11Dlg::OnBnClickedButton6(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("6"));}void CMFCApplication11Dlg::OnBnClickedButton7(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("7"));}void CMFCApplication11Dlg::OnBnClickedButton8(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("8"));}void CMFCApplication11Dlg::OnBnClickedButton9(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("9"));}void CMFCApplication11Dlg::OnBnClickedButton10(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); SetDlgItemText(IDC_EDIT1, cs + _T("0"));}

void CMFCApplication11Dlg::OnBnClickedButtonpoint(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs); if (cs.Find(_T(".")) == -1) { SetDlgItemText(IDC_EDIT1, cs + _T(".")); } opt = 8; cs.Format(_T("%d"), num1);}

void CMFCApplication11Dlg::OnBnClickedButton12(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs); opt = 6; SetDlgItemText(IDC_EDIT1, _T(" "));}

拿+ - * /为连续计算的运算符 原来设置Ctime是想看运用了计算符几次,后来,又改变了想法,把它看作计算符的编号

void CMFCApplication11Dlg::OnBnClickedButtonadd(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs);//_tstoi(cs); //"+"; if (Ctime == 0) result = num1; //if (Ctime !=1&&Ctime!=2&&Ctime!=3&&Ctime!=4) result = num1; else result += num1; num1 = result; result = num1; Ctime = 1; opt = 1; temp = temp + cs; temp += _T("+"); SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButtonsub(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs); //"-"; if (num1 == 0) { num1 = 0.0; SetDlgItemText(IDC_EDIT1, _T(" ")); } /*if (Ctime != 2) { result = num1; }*/ else result -= num1; num1 = result; Ctime = 2; opt = 2; result = num1; temp = temp + cs; temp += _T("-"); SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButtonmul(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs);//_tstoi(cs); //"*"; if (Ctime != 3) result = num1; else result *= num1; num1 = result; Ctime = 3; opt = 3; temp = temp + cs; temp += _T("*"); result = num1; SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButtondiv(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs); //"/"; if (Ctime == 0) result = num1; else { if (num1 == 0) { result = num1; MessageBox(_T("输入无效!")); } else result /= num1; } num1 = result; Ctime = 4; opt = 4; temp = temp + cs; temp += _T("/"); result = num1; SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButtonback(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); cs.Delete(cs.GetLength() - 1); SetDlgItemText(IDC_EDIT1, cs);}

void CMFCApplication11Dlg::OnBnClickedButtonclear(){ // TODO: 在此添加控件通知处理程序代码 CString cs; num1 = 0.0; num2 = 0.0; result = 0.0; SetDlgItemText(IDC_EDIT1, _T(" ")); temp = _T(" "); UpdateData(false);}

void CMFCApplication11Dlg::OnBnClickedButtonjiecheng(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs); double num = 1; for (int i = 1; i <= num1; i++) num *= i; num1 = num; opt = 5; SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButtonsin(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); double num; num1 = _ttof(cs); opt = 5; num = (num1*3.1415926) / 180; num1 = sin(num); SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButtoncos(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); double num; num1 = _ttof(cs); opt = 5; num = (num1*3.1415926) / 180; num1 = cos(num); SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButton30()// %{ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs); result = num1 / 100; opt = 5;}

void CMFCApplication11Dlg::OnBnClickedButtonabout(){ // TODO: 在此添加控件通知处理程序代码 CAboutDlg dlg; dlg.DoModal();}

void CMFCApplication11Dlg::OnBnClickedButtonpinfang(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs); opt = 5; double f = pow(num1, 2); //如果浮点数其实是个整数,就显示为整数 if (f - int(f) <= 1e-5) { cs.Format(L"%d", (int)f); } else { cs.Format(L"%f", f); } //SetDlgItemText(IDC_EDIT1, _T(" ")); num1 = f; num2 = 0.0f;}

v

oid CMFCApplication11Dlg::OnBnClickedButtonkaifang(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs); opt = 5; double f = sqrt(num1); //如果浮点数其实是个整数,就显示为整数 if (f - int(f) <= 1e-5) { cs.Format(L"%d", (int)f); } else { cs.Format(L"%f", f); } //SetDlgItemText(IDC_EDIT1, _T(" ")); num1 = f; num2 = 0.0f;}

void CMFCApplication11Dlg::OnBnClickedButton26(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num1 = _ttof(cs); opt = 10; SetDlgItemText(IDC_EDIT1, _T(""));}

void CMFCApplication11Dlg::OnBnClickedButtonlog(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); double num; num1 = _ttof(cs); opt = 5; num = log10(num1); num1 = num; SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButtonln(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); double num; num1 = _ttof(cs); opt = 5; num = log(num1); num1 = num; if (num1 < EPSINON) { SetDlgItemText(IDC_EDIT1, _T(" ")); MessageBox(_T("输入无效!")); num1 = 0.0; } SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButton29()//e^n{ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); double num; num1 = _ttof(cs); opt = 5; num = exp(num1); num1 = num; SetDlgItemText(IDC_EDIT1, _T(" "));}

void CMFCApplication11Dlg::OnBnClickedButtonequal(){ // TODO: 在此添加控件通知处理程序代码 CString cs; GetDlgItemText(IDC_EDIT1, cs); num2 = _ttof(cs); switch (opt) { case 1:result = result + num2; temp += cs; break; case 2:result = result - num2; temp += cs; break; case 3:result = result * num2; temp += cs; break; case 4:if (num2 == 0) { result = result; MessageBox(_T("输入无效!")); } else { result = result / num2; } temp += cs; break; case 5:result = num1; if (result < EPSINON) { MessageBox(_T("输入无效!")); } break; case 6: { double f = pow(num1, num2); //如果浮点数其实是个整数,就显示为整数 if (f - int(f) <= 1e-5) { cs.Format(L"%d", (int)f); } else { cs.Format(L"%f", f); } //SetDlgItemText(IDC_EDIT1, _T(" ")); num1 = f; num2 = 0.0f; result = num1; } break; case 7: break; case 8: result = num2; case 9: result = num1; break; case 10: result = pow(num1, 1 / num2); default:break; } num1 = result; num2 = 0; if (result - int(result) <= 1e-5) { cs.Format(L"=%d", (int)result); } else { cs.Format(_T(" = %f"), result); } temp += cs; SetDlgItemText(IDC_EDIT1, temp); num1 = 0.0; num2 = 0.0; result = 0.0; temp = _T(" ");}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot&nbsp;+&nbsp;JPA&nbsp;@ManyToMany的操作要点说明
下一篇:测试开发进阶——常用中间件概念——web容器
相关文章

 发表评论

暂时没有评论,来抢沙发吧~