# MFC实现计算器 **Repository Path**: afeng11/calc4mfc ## Basic Information - **Project Name**: MFC实现计算器 - **Description**: MFC编写的计算器,支持浮点运算和进制转换 - **Primary Language**: C++ - **License**: 0BSD - **Default Branch**: master - **Homepage**: https://gitee.com/rewine/mfc_implementation_calculator - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2021-01-11 - **Last Updated**: 2021-10-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 简介 VS2019编写的计算器(VC6分支),支持括号和小数运算和进制转换。 当进制发生转换时,如果表达式框内有表达式,会计算后转化,支持小数转换,如果表达式有误,将直接清空。 ## 界面 ![输入图片说明](https://images.gitee.com/uploads/images/2020/0511/203118_323a0c52_7548283.png "0.PNG") # VC6编译 打包下载后,请先自行将 IntCalculatorDlg.cpp IntCalculator.rc 文件编码转换为 ANSI;可以通过用记事本打开另存为 ANSI 编码实现. IntCalculator.ico 自行换成创建 VC6 MFC 项目时产生的系统图标. IntCalculator.aps 是无用文件(非源代码),可以自行删除. ## 实现方法 表达式框添加控制变量CString类型的m_sEquation,除了求值运算其他按键都可以认为是对字符串的操作 下面重点介绍值的运算,为此写了一个MyCalculator类,它有两个成员函数: >bool MyCalculator::Calc(CString Equation, CString &ans, int radix) >传入表达式Equation,和进制radix(默认为十进制),将计算结果存入ans中 当表达式有错误时,如"(1+1++1)","(1+6",函数返回值FALSE,否则返回TRUE 被除数为0时,ans赋值为"除0异常" 为了获取小数,需要下面的GetNum函数,传入表达式Eq,起始位置i,进制radix,注意这个函数不可以直接处理负数 - 对于整数部分,和普通整数变换一样$x=x*10+Eq[i]-'0'$,但是现在不一定是十进制,换成$*radi$x就可以; - 判断是否有小数点 - 如果有,小数部分第i位要乘上$1/radix$的i次方 ```cpp double GetNum(CString& Eq, int& i, int radix) { double x = 0; while (i= Len) return FALSE; } bool flag = false; if (Equation[i] == '-') { flag = true; i++; if (i >= Len) return FALSE; } if (!IsNum(Equation[i])) return FALSE; num[++ntop] = GetNum(Equation, i, radix) * (flag ? -1 : 1); if (i >= Len) break; if (Equation[i] == ')') { while (otop > 0 && opt[otop] != '(') { INT cas = GetOutSta(num, ntop, opt, otop); if (cas == -1) return FALSE; if (cas == 0) { ans = _T("除0异常"); return TRUE; } } if (otop == 0) return FALSE; otop--; i++; if (i >= Len) break; } if (!IsOpt(Equation[i])) return FALSE; while (otop > 0 && opt[otop]!='(' && !Greater(Equation[i],opt[otop])) { INT cas = GetOutSta(num, ntop, opt, otop); if (cas == -1) return FALSE; if (cas == 0) { ans = _T("除0异常"); return TRUE; } } opt[++otop] = Equation[i++]; if (i >= Len) return FALSE; } //return 0; while (otop) { INT cas = GetOutSta(num,ntop,opt,otop); if (cas == -1) return FALSE; if (cas == 0) { ans = _T("除0异常"); return TRUE; } } if (ntop != 1) return FALSE; ans.Format(_T("%g"), num[ntop]); if (radix != 10) { ConvertRad(ans, 10, radix); } return TRUE; } ``` >bool MyCalculator::ConvertRad(CString& Num, int radixFrom, int radixTo) >传入原数字Num,转换前的进制radixFrom,转换后的进制radixTo,转换失败时(Num不是数字)返回FALSE 首先用GeNum转换为double型dtmp - 如果是负数dtmp=-dtmp,ans加‘-’ - 整数部分用自带的_itoa_s转换 - 小数部分不断乘radixTo,顺序取整数位,默认最多转成8位小数 ```cpp bool MyCalculator::ConvertRad(CString& Num, int radixFrom, int radixTo) { int Len = Num.GetLength(); int j = 0; bool flag = false; if (Num[j] == '-') { j++; flag = true; } double dtmp = GetNum(Num,j,radixFrom); if (j != Len) return FALSE; Num = ""; if (flag) { Num = "-"; } int itmp = (int)dtmp; char str[100]; _itoa_s(itmp, str, 100, radixTo); Num += (CString)str; dtmp -= itmp; int dotLen = 0; if (dtmp > 1e-7) { Num += "."; while (dtmp > 1e-7 && dotLen <= 8) { dtmp *= radixTo; Num += static_cast('0'+(int)dtmp); dtmp = dtmp - (int)dtmp; } } return TRUE; } ```