同步操作将从 rewine/MFC实现浮点计算器 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
VS2019编写的计算器,支持括号和小数运算和进制转换 当进制发生转换时,如果表达式框内有表达式,会计算后转化,支持小数转换,如果表达式有误,将直接清空。
表达式框添加控制变量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,注意这个函数不可以直接处理负数
double GetNum(CString& Eq, int& i, int radix)
{
double x = 0;
while (i<Eq.GetLength() && IsNum(Eq[i]))
{
x = x * radix + Eq[i] - '0';
i++;
}
double rad = 1.0 / radix;
if (i < Eq.GetLength() && Eq[i] == '.')
{
i++;
while (i < Eq.GetLength() && IsNum(Eq[i]))
{
x = x + (Eq[i] - '0') * rad;
rad = rad / radix;
i++;
}
}
return x;
}
计算表达式的整体过程是通过表达式栈实现 num数字表达式 opt操作符表达式 因为有负数,已经要判断"2++3"之类错误,不方便直接判断某一位到底是数还是操作符,但是所有表达式都有一个特点:除了括号数字后面一定是操作符,操作符后面一定是数字,左括号左是操作符右是数字,右括号左是数字又是操作符,所以可以写以下循环
$while$ 表达式未处理完:
最后所有操作符出栈
这里出栈是指取出操作符和两个操作数,运算后结果数入栈这一整个过程
bool MyCalculator::Calc(CString Equation, CString &ans, int radix)
{
double num[MAXNUM];
int ntop = 0;
char opt[MAXOPT];
int otop = 0;
int Len = Equation.GetLength();
for (int i = 0 ; i < Len; )
{
if (Equation[i] == '(')
{
opt[++otop] = Equation[i++];
if (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
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<char>('0'+(int)dtmp);
dtmp = dtmp - (int)dtmp;
}
}
return TRUE;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。