MFC计算器(支持表达式识别及绘图)

一.概要

东南大学吴健雄学院22级大一暑校大作业

玉米肠,王同学完成了主要的软件框架,五花肉给予了我一些指导,向他们表示感谢,写于文前.

特别地,玉米肠完成了计算部分的主要内容,她对于该软件的编写展现了深厚的热情,也付出了大量的时间.

           文件获取可以评论区留下邮箱,或是加该qq号:2969639923,备注mfc计算器.

                         

二. 软件介绍

软件界面如图,函数绘图部分时间比较赶,稍稍有点粗糙.

三.主要算法思想

表达式的识别用到了中缀表达式转后缀表达式

网站上有很多资源,简要介绍一些:如下图:

计算流程:

1.创建一个Calculator对象;
2.获取用户输入的表达式;
3.将表达式转为string类型;
4.调用Calculator类里的calculate(string)函数计算结果;
5.获取错误信息和结果;
6.过滤结果:删除多余的零和小数点并将大数用科学计数法表示;
7.显示结果:错误信息为空则显示结果,否则显示错误信息;
8.更新历史记录:历史记录依次顺移,顶部更新为最新历史记录,底部舍弃。

四.部分代码   

 文件挺多,包括见面美化,计算,绘图,故不可能全部贴出,只留下部分主要核心代码.计算,绘图部分

#include "pch.h"
#include "Calculator.h"
#include "pch.h"
#include "framework.h"
#include "MFC_Calculator.h"
#include "MFC_CalculatorDlg.h"
#include "afxdialogex.h"
#include "Calculator.h"	

#include <stack>
#include <vector>
#include <string>
#include <cmath>

//const int MAX_EXP_LEN = 100;			//最大表达式长度

using namespace std;


//算术符号优先权等级
enum PRIO_LV {
	PRIO_LV0 = 0,
	PRIO_LV1 = 1,
	PRIO_LV2 = 2,
	PRIO_LV3 = 3,
	PRIO_LV4 = 4,
};


Calculator::Calculator() {				//构造函数,初始化成员变量

	result = 0.0;
	//cal_ErrorImfo = "";
}


//表达式自定义标准格式化
void Calculator::getFormat(string infix) {

	stdInfix = infix;
	size_t l = 0, ll = 0;                                      //检查左右括号个数是否相等
	size_t r = 0, rr = 0;
	for (size_t i = 0; i < stdInfix.size(); i++) {
		if (stdInfix[i] == '(') {
			l++;
		}
		else if (stdInfix[i] == ')') {
			r++;
		}
	}

	if (l != r) {
		cal_ErrorImfo = "括号输入有误(1)";
	}
	//实现正负数、sin、cos、tan、log、sqrt、PI
	for (size_t i = 0; i < stdInfix.size(); i++) {					//string.size()返回size_type类型,避免下标运算时的类型溢出
		if (cal_ErrorImfo != "")
			break;
		if (stdInfix[i] == '.') {
			if (i == 0) {                                           //检查小数点前后是否有数字
				cal_ErrorImfo = "表达式输入有误";
				break;
			}
			else if (!(stdInfix[i - 1] >= '0' && stdInfix[i - 1] <= '9' && stdInfix[i + 1] >= '0' && stdInfix[i + 1] <= '9')) {
				cal_ErrorImfo = "表达式输入有误";
				break;
			}
		}
		if (stdInfix[i] == '-' || stdInfix[i] == '+') {				//-x转换为0-x,+x转化为0+x
			if (i == 0) {                                           //sin转为s、cos转为c、tan转为t、log转为l、sqrt转为q
				stdInfix.insert(0, 1, '0');                         //PI转为P
			}
			else if (stdInfix[i - 1] == '(') {
				stdInfix.insert(i, 1, '0');
			}
		}
		else if (stdInfix[i] == 'i' || stdInfix[i] == 'o' || stdInfix[i] == 'a') {
			string t;
			if (stdInfix[i] == 'i')
				t = "s";
			else if (stdInfix[i] == 'o')
				t = "c";
			else
				t = "t";
			size_t tmp = 0;
			for (size_t j = i + 2; j < stdInfix.size(); j++) {
				if (stdInfix[j] == '(') {
					ll++;
				}
				else if (stdInfix[j] == ')') {
					rr++;
				}
				if (ll == rr) {
					tmp = j;
					break;
				}				
			}
			if (ll != rr) {
				cal_ErrorImfo = "括号输入有误(2)";
				break;
			}
			else {
				stdInfix.insert(tmp + 1, t);
				stdInfix.replace(i - 1, 1, "k");
			}
			    
		}
		else if (stdInfix[i] == 'q') {
			stdInfix.erase(i+1, 2);
			stdInfix.replace(i - 1, 1, "2");
		}
		else if (stdInfix[i] == 'P') {
			stdInfix.erase(i + 1 , 1);
		}
		else if (stdInfix[i] == 'l') {
			stdInfix.erase(i + 1, 2);
		}
	}

	for (size_t i = 0; i < stdInfix.size(); i++) {
		if (stdInfix[i] == 'k') {
			stdInfix.erase(i , 3);
		}
	}
}

//获取算术符号优先级
int Calculator::getPrior(char c) {

	if (c == '+' || c == '-') {
		return PRIO_LV1;
	}
	else if (c == '*' || c == '/') {
		return PRIO_LV2;
	}
	else if (c == '%' || c == '^' || c == 'l') {
		return PRIO_LV3;
	}
	else if (c == '!' || c == 's' || c == 'c' || c == 't' || c=='q') {
		return PRIO_LV4;
	}
	else {
		return PRIO_LV0;
	}
	//else { cout << c << 非法符号! << endl; }
}

//后缀表达式转换
void Calculator::getPostfix() {

	string tmp;

	if (cal_ErrorImfo != "")
		goto EndLoop;

	//for (int i = 0; i < stdInfix.length(); i++) {
	for (size_t i = 0; i < stdInfix.size(); i++) {					//string.size()返回size_type类型,避免下标运算时的类型溢出
		tmp = "";
		switch (stdInfix[i]) {
		case '+':
		case '-':
		case '*':
		case '/':
		case '%':
		case '^':
		case 'l':
		case 'q':
			if (i == 0 ) {
				cal_ErrorImfo = "表达式输入有误";
				goto EndLoop;
			}
			if (!(((stdInfix[i - 1] <= '9' && stdInfix[i - 1] >= '0') || stdInfix[i - 1] == ')' || stdInfix[i - 1] == 's' || stdInfix[i - 1] == 'c' || stdInfix[i - 1] == 't' || stdInfix[i - 1] == '!' || stdInfix[i - 1] == 'e' || stdInfix[i - 1] == 'P') && ((stdInfix[i + 1] <= '9' && stdInfix[i + 1] >= '0') || stdInfix[i + 1] == '(' || stdInfix[i + 1] == 'e' || stdInfix[i + 1] == 'P'))) {
				cal_ErrorImfo = "表达式输入有误";
				goto EndLoop;
			}
			if (symStack.empty() || symStack.top() == '(') {
				symStack.push(stdInfix[i]);
			}
			else {
				while (!symStack.empty() && (getPrior(symStack.top()) >= getPrior(stdInfix[i]))) {
					tmp += symStack.top();
					postfix.push_back(tmp);
					symStack.pop();
					tmp = "";
				}
				symStack.push(stdInfix[i]);
			}
			break;
		case '!':
		case 's':
		case 'c':
		case 't':
			if (i == 0) {
				cal_ErrorImfo = "表达式输入有误";
				goto EndLoop;
			}
			if (!(((stdInfix[i - 1] <= '9' && stdInfix[i - 1] >= '0') || stdInfix[i - 1] == ')' || stdInfix[i - 1] == 'e' || stdInfix[i - 1] == 'P') && !((stdInfix[i + 1] <= '9' && stdInfix[i + 1] >= '0') || stdInfix[i + 1] == '('))) {
				cal_ErrorImfo = "表达式输入有误";
				goto EndLoop;
			}
			if (symStack.empty() || symStack.top() == '(') {
				symStack.push(stdInfix[i]);
			}
			else {
				while (!symStack.empty() && (getPrior(symStack.top()) >= getPrior(stdInfix[i]))) {
					tmp += symStack.top();
					postfix.push_back(tmp);
					symStack.pop();
					tmp = "";
				}
				symStack.push(stdInfix[i]);
			}
			break;
		case '(':
			if (stdInfix[i + 1] == ')') {
				cal_ErrorImfo = "括号输入有误(3)";
				goto EndLoop;
			}
			else
			    symStack.push(stdInfix[i]);
			break;
		case ')':
			if (symStack.empty()) {
				cal_ErrorImfo = "括号输入有误(4)";
				goto EndLoop;
			}
			while (!symStack.empty() && symStack.top() != '(') {
				tmp += symStack.top();
				postfix.push_back(tmp);
				symStack.pop();
				tmp = "";
			}
			if (!symStack.empty() && symStack.top() == '(') {
				symStack.pop();							//将左括号出栈丢弃
			}
			else {
				cal_ErrorImfo = "括号输入有误(5)";
				goto EndLoop;
			}
			break;
		default:
			if ((stdInfix[i] >= '0' && stdInfix[i] <= '9')) {
				tmp += stdInfix[i];
				while (i + 1 < stdInfix.length() && (stdInfix[i + 1] >= '0' && stdInfix[i + 1] <= '9' || stdInfix[i + 1] == '.')) {		//小数处理

					tmp += stdInfix[i + 1];			//是连续的数字,则追加
					i++;
				}
				if (tmp[tmp.length() - 1] == '.') {
					tmp += '0';						//将x.做x.0处理
				}
				postfix.push_back(tmp);
			}
			else if (stdInfix[i] == 'P') {
				tmp += 'P';                       
				postfix.push_back(tmp);
			}
			else if (stdInfix[i] == 'e') {
				tmp += 'e';
				postfix.push_back(tmp);
			}
			break;
		}//end switch
	}//end for
EndLoop:
	//if(!symStack.empty()) {
	while (!symStack.empty() && cal_ErrorImfo == "") {						//将栈中剩余符号加入后缀表达式
		tmp = "";
		tmp += symStack.top();
		if (tmp == "(") {
			cal_ErrorImfo = "括号输入有误(6)";
			break;
		}
		postfix.push_back(tmp);
		symStack.pop();
	}

}

//获取运算结果
void Calculator::calResult() {

	string tmp;
	double number = 0;
	double op1 = 0, op2 = 0;

	for (size_t i = 0; i < postfix.size(); i++) {
		if (cal_ErrorImfo != "")
			break;
		tmp = postfix[i];
		if (tmp[0] >= '0' && tmp[0] <= '9') {
			number = atof(tmp.c_str());
			figStack.push(number);
		}
		else if (tmp[0] == 'P') {
			number = 3.14159265358979323846;          //将P数字化
			figStack.push(number);
		}
		else if (tmp[0] == 'e') {
			number = 2.71828182845904523536;          //将e数字化
			figStack.push(number);
		}
		else if (postfix[i] == "+") {
			if (!figStack.empty()) {
				op2 = figStack.top();
				figStack.pop();
			}
			if (!figStack.empty()) {
				op1 = figStack.top();
				figStack.pop();
			}
			figStack.push(op1 + op2);
		}
		else if (postfix[i] == "-") {
			if (!figStack.empty()) {
				op2 = figStack.top();
				figStack.pop();
			}
			if (!figStack.empty()) {
				op1 = figStack.top();
				figStack.pop();
			}
			figStack.push(op1 - op2);
		}
		else if (postfix[i] == "*") {
			if (!figStack.empty()) {
				op2 = figStack.top();
				figStack.pop();
			}
			if (!figStack.empty()) {
				op1 = figStack.top();
				figStack.pop();
			}
			figStack.push(op1 * op2);
		}
		else if (postfix[i] == "/") {
			if (!figStack.empty()) {
				op2 = figStack.top();
				figStack.pop();
			}
			if (!figStack.empty()) {
				op1 = figStack.top();
				figStack.pop();
			}
			if (op2 != 0) {
				figStack.push(op1 / op2);
			}
			else {
				cal_ErrorImfo = "除数不能为0";
				break;
			}		
		}
		else if (postfix[i] == "%") {
			if (!figStack.empty()) {
				op2 = figStack.top();
				figStack.pop();
			}
			if (!figStack.empty()) {
				op1 = figStack.top();
				figStack.pop();
			}
			if (op2 != 0) {
				figStack.push(fmod(op1, op2));			//可进行小数求余
			}
			else {
				cal_ErrorImfo = "除数不能为0";
				break;
			}
		}
		else if (postfix[i] == "^") {
			if (!figStack.empty()) {
				op2 = figStack.top();
				figStack.pop();
			}
			if (!figStack.empty()) {
				op1 = figStack.top();
				figStack.pop();
			}
			figStack.push(pow(op1, op2));
		}
		else if (postfix[i] == "q") {
			if (!figStack.empty()) {
				op2 = figStack.top();
				figStack.pop();
			}
			if (!figStack.empty()) {
				op1 = figStack.top();
				figStack.pop();
			}
			if (op2 >= 0) {
				figStack.push(pow(op2, 1 / op1));
			}
			else {
				cal_ErrorImfo = "底数不能小于0";
				break;
			}
		}
		else if (postfix[i] == "l") {
			if (!figStack.empty()) {
				op2 = figStack.top();
				figStack.pop();
			}
			if (!figStack.empty()) {
				op1 = figStack.top();
				figStack.pop();
			}
			if (op2 > 0 && op1 > 0 && op1 != 1) {
				figStack.push(log(op2) / log(op1));
			}
			else if (op2 <= 0){
				cal_ErrorImfo = "真数必须大于0";
				break;
			}
			else if (op1 <= 0 || op1==1) {
				cal_ErrorImfo = "底数必须大于0且不能为1";
				break;
			}		
		}
		else if (postfix[i] == "!") {
			if (!figStack.empty()) {
				op1 = figStack.top();
				figStack.pop();
			}
			if (op1 > 0) {
				//阶乘数应大于0;为小数时(转化为整数求阶)
				double factorial = 1;
				for (int i = 1; i <= op1; ++i)
				{
					factorial *= i;
				}
				op1 = factorial;
			}
			else if (op1 == 0)
				op1 = 1;
			figStack.push(op1);
		}
		else if (postfix[i] == "s") {
		    if (!figStack.empty()) {
			op1 = figStack.top();
			figStack.pop();
			op1 = sin(op1);
		    }
		    figStack.push(op1);
        }
		else if (postfix[i] == "c") {
		   if (!figStack.empty()) {
			op1 = figStack.top();
			figStack.pop();
			op1 = cos(op1);
		}
		   figStack.push(op1);
		}
		else if (postfix[i] == "t") {
		   if (!figStack.empty()) {
			   op1 = figStack.top();
			   figStack.pop();
			   if (fmod((abs(op1) / 3.14159265358979323846 * 2), 2) == 1) {     //tan(+-(pi/2的奇数倍))不可取
				   cal_ErrorImfo = "表达式输入有误";
				   break;
			   }
			   else
				   op1 = tan(op1);
		   }
		   figStack.push(op1);
		}
	}//end for
	if (!figStack.empty() && cal_ErrorImfo == "") {
		result = figStack.top();
	}
	if (std::isinf(result)) {
		if (std::signbit(result)) {
			cal_ErrorImfo = "超过计算下限";
		}
		else {
			cal_ErrorImfo = "超过计算上限";
		}
		result = 0;
	}
}


//计算方法
void Calculator::calculate(string infix) {
	getFormat(infix);			//表达式自定义标准格式化
	getPostfix();				//后缀表达式转换
	calResult();				//计算结果
}

//获取结果
double Calculator::getResult() {
	return result;
}

//获取异常信息
string Calculator::getErrorImfo() {
	return cal_ErrorImfo;
}
// GraphDlg.cpp: 实现文件
//

#include "pch.h"
#include "MFC_Calculator.h"
#include "afxdialogex.h"
#include "GraphDlg.h"
#include "PLOT.h"
#include "PlotCal.h"

// GraphDlg 对话框

IMPLEMENT_DYNAMIC(GraphDlg, CDialogEx)

GraphDlg::GraphDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG3, pParent)
{

}

GraphDlg::~GraphDlg()
{
}

void GraphDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(GraphDlg, CDialogEx)
	ON_WM_PAINT()
END_MESSAGE_MAP()


// GraphDlg 消息处理程序


void GraphDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(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
	{
		CPaintDC dc(this);
		CRect rect;//声明客户区矩形
		GetClientRect(&rect);//获得客户区坐标
		dc.SetMapMode(MM_ANISOTROPIC);//设置映射模式
		dc.SetWindowExt(rect.Width(), rect.Height());//设置窗口
		dc.SetViewportExt(rect.Width(), -rect.Height());//x轴水平向右,y轴垂直向上
		dc.SetViewportOrg(rect.Width() / 2, rect.Height() / 2);//客户区中心为坐标系原点
		rect.OffsetRect(-rect.Width() / 2, -rect.Height() / 2);//将rect移回到客户区内

		//画x轴
		dc.MoveTo(-rect.Width() / 2, 0);
		dc.LineTo(rect.Width() / 2, 0);

		//画x轴箭头
		dc.MoveTo(rect.Width() / 2, 0);
		dc.LineTo(rect.Width() / 2 - 20, 20);

		dc. MoveTo(rect.Width() / 2, 0);
		dc.LineTo(rect.Width() / 2 - 20, -20);

		//画y轴
		dc.MoveTo(0, -rect.Height() / 2);
		dc.LineTo(0, rect.Height() / 2);

		//画y轴箭头
		dc.MoveTo(0, rect.Height() / 2);
		dc.LineTo(-20, rect.Height() / 2 - 20);

		dc.MoveTo(0, rect.Height() / 2);
		dc.LineTo(20, rect.Height() / 2 - 20);

		double detX = (rect.Width()) / (Xmax - Xmin);//图像修正
		bool succ;

		CPen pen(PS_SOLID, 5, RGB(0, 0, 0)); // 实线,线宽5,颜色黑色
		
		// 选择画笔
		CPen* pOldPen = dc.SelectObject(&pen);

		for (int i = 0; i < Step - 1; i++)
		{
			double startX = (Xmin + i * (Xmax - Xmin) / (Step - 1)) * 10;
			double startY = CalcEquation(Equation, succ, 'x', startX);
			double endX = (Xmin + (i + 1) * (Xmax - Xmin) / (Step - 1)) * 10;
			double endY = CalcEquation(Equation, succ, 'x', endX);

			// 将double类型坐标转换为int类型坐标
			int startXInt = static_cast<int>(startX * detX);
			int startYInt = static_cast<int>(startY * detX);
			int endXInt = static_cast<int>(endX * detX);
			int endYInt = static_cast<int>(endY * detX);

			// 绘制线
			dc.MoveTo(startXInt, startYInt);
			dc.LineTo(endXInt, endYInt);

			// 恢复原始的画笔对象
			dc.SelectObject(pOldPen);
		}
	}
}

五.总结

1.存在问题:不能限制非法输入

计算器的耦合程度很高,例如输入'+'号后必须输入数值而不能是另一个运算符,在电脑自带的计算器程序中,它实现了这个功能,输入一个运算符后非法的输入按钮将会变灰无法按下,我们的程序只能做到检测错误的输入并报错.

2.改进方向:用正则表达式判断

修改方法:我们认为在目前的代码下应该无法修改,因为我们对表达式的处理是在按下’=‘号之后.无法限制用户之前的输入,如果要修改必须及时的检测用户的每一次输入并对各个按钮进行大量的更改,故在目前的能力下无法解决该问题.

最后,感谢小组里每一位同学的参与和付出.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
课程设计:简易计算器,逻辑运算 功能代码: // 计算器1Dlg.cpp : implementation file // #include "stdafx.h" #include "计算器1.h" #include "计算器1Dlg.h" #include<cmath> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About int focus=0,point1=0,point2=0,i=0,j=0; double temp1=1,temp2=1,equal; double temp3[100],temp4[100]; class 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 // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_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_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMy1Dlg dialog CMy1Dlg::CMy1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CMy1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CMy1Dlg) m_1 = 0.0; m_2 = 0.0; m_result = 0.0; m_string = _T(""); m_string2 = _T(""); m_string3 = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CMy1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMy1Dlg) DDX_Control(pDX, IDC_BUTTON_point, m_point); DDX_Text(pDX, IDC_EDIT1, m_1); DDX_Text(pDX, IDC_EDIT2, m_2); DDX_Text(pDX, IDC_EDIT3, m_result); DDX_Text(pDX, IDC_EDIT4, m_string); DDX_Text(pDX, IDC_EDIT5, m_string2); DDX_Text(pDX, IDC_EDIT7, m_string3); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMy1Dlg, CDialog) //{{AFX_MSG_MAP(CMy1Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_EN_SETFOCUS(IDC_EDIT1, OnSetfocusEdit1) ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1) ON_EN_SETFOCUS(IDC_EDIT2, OnSetfocusEdit2) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON_point, OnBUTTONpoint) ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) 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_BUTTON0, OnButton0) ON_BN_CLICKED(IDC_BUTTON_delete, OnBUTTONdelete) ON_BN_CLICKED(IDC_BUTTON_equal, OnBUTTONequal) ON_BN_CLICKED(IDC_BUTTON15, OnButton15) ON_BN_CLICKED(IDC_BUTTON16, OnButton16) ON_BN_CLICKED(IDC_BUTTON17, OnButton17) ON_BN_CLICKED(IDC_BUTTON18, OnButton18) ON_BN_CLICKED(IDC_BUTTON19, OnButton19) ON_BN_CLICKED(IDC_BUTTON20, OnButton20) ON_BN_CLICKED(IDC_BUTTON21, OnButton21) ON_BN_CLICKED(IDC_BUTTON22, OnButton22) ON_BN_CLICKED(IDC_BUTTON23, OnButton23) ON_BN_CLICKED(IDC_BUTTON24, OnButton24) ON_BN_CLICKED(IDC_BUTTON25, OnButton25) ON_BN_CLICKED(IDC_BUTTON27, OnButton27) ON_BN_CLICKED(IDC_BUTTON28, OnButton28) ON_BN_CLICKED(IDC_BUTTON29, OnButton29) ON_BN_CLICKED(IDC_BUTTON26, OnButton26) ON_BN_CLICKED(IDC_BUTTON30, OnButton30) ON_BN_CLICKED(IDC_BUTTON31, OnButton31) ON_BN_CLICKED(IDC_BUTTON32, OnButton32) ON_BN_CLICKED(IDC_BUTTON10, OnButton10) ON_BN_CLICKED(IDC_RADIO1, OnRadio1) ON_BN_CLICKED(IDC_RADIO2, OnRadio2) ON_COMMAND(ID_MENUITEM32792, OnMenuitem32792) ON_COMMAND(ID_MENUITEM32793, OnMenuitem32793) ON_COMMAND(ID_MENUITEM32794, OnMenuitem32794) ON_COMMAND(ID_MENUITEM32795, OnMenuitem32795) ON_COMMAND(ID_MENUITEM32796, OnMenuitem32796) ON_COMMAND(ID_MENUITEM32797, OnMenuitem32797) ON_COMMAND(ID_MENUITEM32798, OnMenuitem32798) ON_COMMAND(ID_MENUITEM32799, OnMenuitem32799) ON_COMMAND(ID_MENUITEM32800, OnMenuitem32800) ON_COMMAND(ID_MENUITEM32801, OnMenuitem32801) ON_COMMAND(ID_MENUITEM32802, OnMenuitem32802) ON_COMMAND(ID_MENUITEM32791, OnMenuitem32791) ON_COMMAND(ID_MENUITEM32790, OnMenuitem32790) ON_COMMAND(ID_MENUITEM32789, OnMenuitem32789) ON_COMMAND(ID_MENUITEM32788, OnMenuitem32788) ON_COMMAND(ID_MENUITEM32803, OnMenuitem32803) ON_COMMAND(ID_MENUITEM32804, OnMenuitem32804) ON_BN_CLICKED(IDC_BUTTON33, OnButton33) ON_BN_CLICKED(IDC_BUTTON14, OnButton14) ON_COMMAND(ID_MENUITEM32809, OnMenuitem32809) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMy1Dlg message handlers BOOL CMy1Dlg::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 dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CMy1Dlg::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 CMy1Dlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle 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; // Draw the icon dc.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 CMy1Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CMy1Dlg::OnSetfocusEdit1() { focus=1; } void CMy1Dlg::OnChangeEdit1() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here } void CMy1Dlg::OnSetfocusEdit2() { focus=2; } void CMy1Dlg::OnBUTTONpoint() //--------------------------------------------------小数点键 { if(focus==1) point1=1; if(focus==2) point2=1; } void CMy1Dlg::OnChangeEdit2() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here } void CMy1Dlg::OnButton1() //-----------------------------------------------------数字键1 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10+1; i++;temp3[i]=m_1;} if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1+m_1; i++;temp3[i]=m_1;point1++;} if(focus==2&&point2;==0) {m_2=m_2*10+1; j++;temp4[j]=m_2;} if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2+m_2; j++;temp4[j]=m_2;point2++;} UpdateData(false); } void CMy1Dlg::OnButton2() //-----------------------------------------------------数字键2 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10+2; i++;temp3[i]=m_1;} else if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1*2+m_1; i++;temp3[i]=m_1;point1++;} else if(focus==2&&point2;==0) {m_2=m_2*10+2; j++;temp4[j]=m_2;} else if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2*2+m_2; j++;temp4[j]=m_2;point2++;} UpdateData(false); } void CMy1Dlg::OnButton3() //-----------------------------------------------------数字键3 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10+3; i++;temp3[i]=m_1;} else if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1*3+m_1; i++;temp3[i]=m_1;point1++;} else if(focus==2&&point2;==0) {m_2=m_2*10+3; j++;temp4[j]=m_2;} else if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2*3+m_2; j++;temp4[j]=m_2;point2++;} UpdateData(false); } void CMy1Dlg::OnButton4() //-----------------------------------------------------数字键4 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10+4; i++;temp3[i]=m_1;} else if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1*4+m_1; i++;temp3[i]=m_1;point1++;} else if(focus==2&&point2;==0) {m_2=m_2*10+4; j++;temp4[j]=m_2;} else if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2*4+m_2; j++;temp4[j]=m_2;point2++;} UpdateData(false); } void CMy1Dlg::OnButton5() //-----------------------------------------------------数字键5 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10+5; i++;temp3[i]=m_1;} else if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1*5+m_1; i++;temp3[i]=m_1;point1++;} else if(focus==2&&point2;==0) {m_2=m_2*10+5; j++;temp4[j]=m_2;} else if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2*5+m_2; j++;temp4[j]=m_2;point2++;} UpdateData(false); } void CMy1Dlg::OnButton6() //-----------------------------------------------------数字键6 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10+6; i++;temp3[i]=m_1;} else if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1*6+m_1; i++;temp3[i]=m_1;point1++;} else if(focus==2&&point2;==0) {m_2=m_2*10+6; j++;temp4[j]=m_2;} else if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2*6+m_2; j++;temp4[j]=m_2;point2++;} UpdateData(false); } void CMy1Dlg::OnButton7() //-----------------------------------------------------数字键7 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10+7; i++;temp3[i]=m_1;} else if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1*7+m_1; i++;temp3[i]=m_1;point1++;} else if(focus==2&&point2;==0) {m_2=m_2*10+7; j++;temp4[j]=m_2;} else if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2*7+m_2; j++;temp4[j]=m_2;point2++;} UpdateData(false); } void CMy1Dlg::OnButton8() //-----------------------------------------------------数字键8 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10+8; i++;temp3[i]=m_1;} else if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1*8+m_1; i++;temp3[i]=m_1;point1++;} else if(focus==2&&point2;==0) {m_2=m_2*10+8; j++;temp4[j]=m_2;} else if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2*8+m_2; j++;temp4[j]=m_2;point2++;} UpdateData(false); } void CMy1Dlg::OnButton9() //-----------------------------------------------------数字键9 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10+9; i++;temp3[i]=m_1;} else if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1*9+m_1; i++;temp3[i]=m_1;point1++;} else if(focus==2&&point2;==0) {m_2=m_2*10+9; j++;temp4[j]=m_2;} else if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2*9+m_2; j++;temp4[j]=m_2;point2++;} UpdateData(false); } void CMy1Dlg::OnButton0() //-----------------------------------------------------数字键0 {UpdateData(true); if(focus==1&&point1;==0) {m_1=m_1*10; i++;temp3[i]=m_1;} else if(focus==1&&point1;>=1) {temp1=temp1/10;m_1=temp1*0+m_1; i++;temp3[i]=m_1;point1++;} else if(focus==2&&point2;==0) {m_2=m_2*10; j++;temp4[j]=m_2;} else if(focus==2&&point2;>=1) {temp2=temp2/10;m_2=temp2*0+m_2; j++;temp4[j]=m_1;point2++;} UpdateData(false); } void CMy1Dlg::OnBUTTONdelete() //--------------------------------退格 {UpdateData(true); if(focus==1) {i--;m_1=temp3[i]; if(point1>0) {point1=point1-1;} if(temp1<1) {temp1=temp1*10;} } if(focus==2) {j--;m_2=temp4[j]; if(point2>0) {point2=point2-1;} if(temp2<1) {temp2=temp2*10;} } UpdateData(false); } void CMy1Dlg::OnBUTTONequal() {//--------------------------------------------------------------------等号键 UpdateData(true); m_result=equal; UpdateData(false); } void CMy1Dlg::OnButton14() { // TODO: Add your control notification handler code here------------- 加法 UpdateData(true); equal=m_1+m_2; m_string="+"; UpdateData(false); } void CMy1Dlg::OnButton15() { // TODO: Add your control notification handler code here-----------减法 UpdateData(true); equal=m_1-m_2; m_string="-"; UpdateData(false); } void CMy1Dlg::OnButton16() {// TODO: Add your control notification handler code here--------------乘法 UpdateData(true); equal=m_1*m_2; m_string="*"; UpdateData(false); } void CMy1Dlg::OnButton17() { // TODO: Add your control notification handler code here--------除法 UpdateData(true); equal=m_1/m_2; m_string="/"; UpdateData(false); } void CMy1Dlg::OnButton18() { // TODO: Add your control notification handler code here------------------逻辑与 UpdateData(true); equal=m_1&&m_2; m_string="And";m_string2=" "; UpdateData(false); } void CMy1Dlg::OnButton19() { // TODO: Add your control notification handler code here--------------逻辑或 UpdateData(true); equal=m_1||m_2; m_string="or"; m_string2=" "; UpdateData(false); } void CMy1Dlg::OnButton20() { // TODO: Add your control notification handler code here------------------逻辑非 UpdateData(true); equal=!m_1; m_string="not"; m_string2=" "; UpdateData(false); } void CMy1Dlg::OnButton21() { // TODO: Add your control notification handler code here---------------------逻辑异或 UpdateData(true); equal=int(m_1)^int(m_2); m_string="Xor"; m_string2=" "; UpdateData(false); } void CMy1Dlg::OnButton22() { // TODO: Add your control notification handler code here---------------------正弦 UpdateData(true); m_2=sin(m_1); m_string2="Sin";m_string="="; UpdateData(false); } void CMy1Dlg::OnButton23() { // TODO: Add your control notification handler code here----------------------余弦 UpdateData(true); m_2=cos(m_1); m_string2="Cos";m_string="="; UpdateData(false); } void CMy1Dlg::OnButton24() { // TODO: Add your control notification handler code here------------正切 UpdateData(true); m_2=tan(m_1); m_string2="tan";m_string="="; UpdateData(false); } void CMy1Dlg::OnButton25() { // TODO: Add your control notification handler code here----------------求余 UpdateData(true); equal=int(m_1)%int(m_2); m_string="Mod"; UpdateData(false); } void CMy1Dlg::OnButton27() { // TODO: Add your control notification handler code here------------------log UpdateData(true); m_result=log(m_2)/log(m_1); m_string2="log"; UpdateData(false); } void CMy1Dlg::OnButton28() { // TODO: Add your control notification handler code here--------------------ln UpdateData(true); m_2=log(m_1); m_string2="ln"; m_string="="; UpdateData(false); } void CMy1Dlg::OnButton29() { // TODO: Add your control notification handler code here-------------------乘方 UpdateData(true); equal=pow(m_1,m_2); m_string="^"; UpdateData(false); } void CMy1Dlg::OnButton26() { // TODO: Add your control notification handler code here----------------阶乘 UpdateData(true); int j=1,k=int(m_1); for(;j<m_1;j++) {k=k*j;} equal=k; m_2=equal; m_string="!="; UpdateData(false); } void CMy1Dlg::OnButton30() { // TODO: Add your control notification handler code here---------------pi if(focus==1) m_1=3.1415926; else if(focus==2) m_2=3.1415926; UpdateData(false); } void CMy1Dlg::OnButton31() { // TODO: Add your control notification handler code here-------------00 m_string3="00"; UpdateData(false); } void CMy1Dlg::OnButton32() { // TODO: Add your control notification handler code here-----------------%(百分号) m_string3="%"; UpdateData(false); } void CMy1Dlg::OnButton10() { // TODO: Add your control notification handler code here-------------------复原MR focus=0,point1=0,point2=0,i=0,j=0,temp1=1,temp2=1; m_1=0;m_2=0;m_result=0; m_string=" ";m_string2=" ";m_string3=" "; UpdateData(false); } void CMy1Dlg::OnRadio1() { // TODO: Add your control notification handler code here----------------十进制转二进制 int change(int); m_2=change(int(m_1)); m_string="--->"; UpdateData(false); } int change(int c) {int h[100],k=0,x=0; for(;c!=0;) {h[k]=c%2; c=c/2; k++; } k=k-1; for(;k>=0;k--) {x=x*10+h[k]; } return x; } void CMy1Dlg::OnRadio2() { // TODO: Add your control notification handler code here------------二进制转十进制(Two to Ten) int q,num=int(m_1),shi=0; int TtT(int); q=TtT(num); for(;num>1;) {num=num-pow(10,q); shi=pow(2,q)+shi; q=TtT(num); } if(num==1) shi=shi+1; m_2=shi; m_string="--->"; UpdateData(false); } int TtT(int m) {int k,j=0; double n; n=m; for(k=0;n>2;k++) {n=n/10;} if(m==1) k=0; return k; } void CMy1Dlg::OnMenuitem32792() //------------------------菜单。加 { // TODO: Add your command handler code here CMy1Dlg::OnButton14() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32793() //----------------------菜单。减 { // TODO: Add your command handler code here CMy1Dlg::OnButton15() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32794() { // TODO: Add your command handler code here//---------------菜单。乘 CMy1Dlg::OnButton16() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32795() //-------------------------------菜单。除 { // TODO: Add your command handler code here CMy1Dlg::OnButton17() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32796() //------------------------------菜单。与 { // TODO: Add your command handler code here CMy1Dlg::OnButton18() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32797() //------------------------------菜单。或 { // TODO: Add your command handler code here CMy1Dlg::OnButton19() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32798() //------------------------------菜单。非 { // TODO: Add your command handler code here CMy1Dlg::OnButton20() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32799() //------------------------------菜单。异或 { // TODO: Add your command handler code here CMy1Dlg::OnButton21() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32800() //-------------------------------菜单。正弦 { // TODO: Add your command handler code here CMy1Dlg::OnButton22() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32801() //-------------------------------菜单。余弦 { // TODO: Add your command handler code here CMy1Dlg::OnButton23() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32802() //-------------------------------菜单。正切 { // TODO: Add your command handler code here CMy1Dlg::OnButton24() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32791() //-------------------------------菜单。求余 { // TODO: Add your command handler code here CMy1Dlg::OnButton25() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32790() //---------------------------------菜单。阶乘 { // TODO: Add your command handler code here CMy1Dlg::OnButton26() ; CMy1Dlg::OnBUTTONequal(); } void CMy1Dlg::OnMenuitem32789() //--------------------------------菜单。复位 { // TODO: Add your command handler code here CMy1Dlg::OnButton10() ; } void CMy1Dlg::OnMenuitem32788() //---------------------------------菜单。退格 { // TODO: Add your command handler code here CMy1Dlg::OnBUTTONdelete(); } void CMy1Dlg::OnMenuitem32803() //----------------------------------菜单。十进制转二进制 { // TODO: Add your command handler code here CMy1Dlg::OnRadio1() ; } void CMy1Dlg::OnMenuitem32804() //---------------------------------菜单。二进制转十进制 { // TODO: Add your command handler code here CMy1Dlg::OnRadio2() ; } void CMy1Dlg::OnButton33() //--------------------------------------求倒数 { // TODO: Add your control notification handler code here UpdateData(true); m_2=1/m_1; m_string="->"; UpdateData(false); } void CMy1Dlg::OnMenuitem32809() //----------------------------------菜单求倒数 { // TODO: Add your command handler code here CMy1Dlg::OnButton33(); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

踏破楼兰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值