#include "calculator.h"
/*
程序思路:
(1)从标准输入(控制台,键盘)获取原始表达式
(2)将原始表达式中包含sin(x),cos(x)等的部分计算出,并准换成:(结果)的形式
------其中括号()是避免函数的计算结果为-的情况,从而使表达式满足只包含运算符和4则运算
(3)‘-’符号前添加0处理,使表达式便于逆波兰法计算
(4)对于(2)(3)处理后的表达式进行中缀表达式合理性验证
(5)中缀表达式转化成后缀表达式
(6)计算后缀表达式
*/
/////代码中数据的定义和内存分配可能还不是最优,有待优化
/////////////////构造函数+析构函数///////////////////////////////
calculator::calculator()
{
}
calculator::~calculator()
{
}
///////////////将字符串转换成浮点型数/////////////////////////////
double calculator:: str2num(std::string str )
{
double num ;
std::stringstream ss;
ss << str;
ss >> num;
return num;
}
////////////////将浮点型转换成字符串//////////////////////////////
std::string calculator::num2str(double num)
{
std::string str;
std::stringstream ss;
ss << num;
ss >> str;
return str ;
}
//////////////////需要对字符串进行判断///////////////////////
//////////////////判断某字符串是不是数字///////////////////
bool calculator::isDouble(std::string s1)
{
///////////////////字符串合理性判断,保证it有效
if (s1.empty())
{
return false;
}
else if (*s1.begin() == '-' && s1.size() < 2)
{
return false;
}
auto it = s1.begin();
if (*it == '-') ++it;
size_t doutNum = 0;
for (; it != s1.end(); it++)
{
char temp = *it;
if (temp != '.' && (!(temp >= '0' && temp <= '9')))
{
return false;
}
else if (temp == '.')
{
++doutNum;
}
}
if (doutNum > 1)
{
return false;
}
return true;
}
///////////////将sin(x)等的数字字符转化成数字字符//////////////
std::string calculator::function2str(std::string in)
{
std::string out;
out.push_back('('); //加上括号方便处理
auto it = in.begin();
switch (*in.begin())
{
case 'p': //pow(x,y)函数
for (it = in.begin(); it != in.end(); it++)
if (*it == ',') break;
double n1, n2;
n1 = str2num(std::string(in.begin() + 4, it));
n2 = str2num(std::string(it + 1, in.end()));
out = out + num2str(pow(n1, n2));
break;
case 'a':
if (*(in.begin() + 1) == 's') //asin(x)函数
{
out = out + num2str(asin(str2num(std::string(in.begin() + 5, in.end()))));
}
else if (*(in.begin() + 1) == 'c') //acos(x)函数
{
out = out + num2str(acos(str2num(std::string(in.begin() + 5, in.end()))));
}
else //atan(x)函数
{
out = out + num2str(atan(str2num(std::string(in.begin() + 5, in.end()))));
}
break;
case 's': //sin(x)
out = out + num2str(sin(str2num(std::string(in.begin() + 4, in.end()))));
break;
case 'c': //cos(x)
out = out + num2str(cos(str2num(std::string(in.begin() + 4, in.end()))));
break;
case 't': //tan(x)
out = out + num2str(tan(str2num(std::string(in.begin() + 4, in.end()))));
break;
}
out.push_back(')'); //加上括号方便处理
return out;
}
/////////////////将原始表达式中的函数函数替换成数字字符串//////////////////
int calculator::FunctionPro(std::string s1)
{
if (!s1.empty())
{
std::vector<int> spot;
for (size_t i = 0; i < s1.size(); i++)
{
std::string temp(s1.begin() + i, s1.end());
if (!temp.find("sin(") || !temp.find("cos(") || !temp.find("tan("))
{
for (size_t j = i + 4; j < s1.size(); j++)
{
if (s1[j] == ')')
{
std::string check(s1.begin() + i + 4, s1.begin() + j);
if (isDouble(check))
{
///////////记录位置///////////////
spot.push_back(i);
spot.push_back(j + 1);
///////////更新 i ////////////////
i = j + 1;
break;
}
}
}
}
else if (!temp.find("asin(") || !temp.find("acos(") || !temp.find("atan("))
{
for (size_t j = i + 5; j < s1.size(); j++)
{
if (s1[j] == ')')
{
std::string check(s1.begin() + i + 5, s1.begin() + j);
if (isDouble(check))
{
///////////记录位置///////////////
spot.push_back(i);
spot.push_back(j + 1);
///////////更新 i ////////////////
i = j + 1;
break;
}
}
}
}
else if (!temp.find("pow("))
{
for (size_t j = i + 4; j < s1.size(); j++)
{
if (s1[j] == ')')
{
bool flag = 0;
for (size_t k = i + 4; k < j - 1; k++)
{
if (s1[k] == ',')
{
std::string check1(s1.begin() + i + 4, s1.begin() + k);
std::string check2(s1.begin() + k + 1, s1.begin() + j);
if (isDouble(check1) && isDouble(check2))
{
flag = 1;
break;
}
}
}
if (flag)
{
///////////记录位置///////////////
spot.push_back(i);
spot.push_back(j + 1);
///////////更新 i ////////////////
i = j + 1;
break;
}
}
}
}
}
///////////////记录新的位置后处理数据(替换函数)///////////////////////////
spot.insert(spot.begin(), 0);
spot.push_back(s1.size());
//////////////////////替换数据///////////////////////////
std::string stemp;
for (int i = 0; i < spot.size() / 2 - 1; i++)
{
std::string check;
stemp = stemp + s1.substr(spot[2 * i], spot[2 * i + 1] - spot[2 * i]);
check = s1.substr(spot[2 * i + 1], spot[2 * i + 2] - spot[2 * i + 1]);
stemp = stemp + function2str(check);
}
stemp = stemp + s1.substr(spot[spot.size() - 2], spot[spot.size() - 1] - spot[spot.size() - 2]);
s1 = stemp;
}
infix = s1 ;
return 0;
}
//获取原始的输入数据,先进行数据处理,包括:
//sin,cos等函数数值化处理,'-'(负号)前补0处理,从而使表达式符合四则运�
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本资源是在QT5下开发的多功能计算器界面程序,包含普通的四则运算(并加入的三角函数/反三角函数/开发乘方),并且都配有相关说明书。因此资源中包含有QT5.61下,在qtcreator下的源代码,并且已经将动态发布程序以及已经打包好的程序同时分享出来。此外,程序的底层算法部分都是作者现在vs2017的控制台程序下先测试然后移植到qt中的,所以同时将vs2017下的源代码分享给大家。 本程序中包含给中数据结构的调用,计算器算法主要使用逆波兰算法,因此也是一个非常不错的数据结构的练习程序,欢迎大家下载。此外qt程序中还留有一个键盘小界面尚未实现功能,大家也可以不断改进。
资源推荐
资源详情
资源评论
收起资源包目录
计算器.rar (89个子文件)
计算器
发布的动态库程序
libGLESV2.dll 2.19MB
libEGL.dll 21KB
计算器.exe 318KB
platforms
qwindows.dll 1.42MB
libwinpthread-1.dll 48KB
libstdc++-6.dll 1002KB
Qt5Core.dll 5.12MB
libgcc_s_dw2-1.dll 117KB
Qt5Widgets.dll 6.22MB
Qt5Gui.dll 5.39MB
Qt_Calculation
Caculatior
keyboard.cpp 278B
radixconvert.cpp 4KB
images
write2file.png 8KB
open.png 9KB
header.bmp 294B
clearbytes.png 12KB
logo129-66.png 5KB
cleanport.png 16KB
logo42-22.png 4KB
edit-clear.png 1KB
loadfile.png 17KB
lcd.bmp 226B
stop.png 13KB
mesage.png 16KB
send.png 828B
exit.png 928B
save.png 11KB
add.png 9KB
qcom.ICO 766B
direction_for_calculator.cpp 327B
crc16.cpp 2KB
Caculatior.pro.user 23KB
calculator.cpp 17KB
Caculatior.pro 873B
radixconvert.h 657B
images.qrc 747B
direction_for_calculator.h 417B
calculator.h 1KB
myico.ico 66KB
main.cpp 386B
mainwindow.ui 17KB
mainwindow.cpp 10KB
mainwindow.h 1KB
direction_for_calculator.ui 4KB
direction_for_crc16_and_radix.cpp 362B
direction_for_crc16_and_radix.h 457B
direction_for_crc16_and_radix.ui 4KB
crc16.h 3KB
keyboard.ui 8KB
keyboard.h 347B
宝妹佳计算器_已打包_直接可用.exe 9.58MB
calculator
.vs
calculator
v15
Browse.VC.db 6.48MB
.suo 58KB
ipch
AutoPCH
61ee61daeb4a18b0
CALCULATOR.ipch 37.38MB
8488de0db37ba6ef
MAIN.ipch 47.81MB
71fd94b15d94c8d0
CALCULATOR.ipch 37.38MB
6ac758c3a3d4bbcf
MAIN.ipch 48.25MB
e7f1e09324e3207
MAIN.ipch 47.81MB
e07e6daa9560a048
CALCULATOR.ipch 37.38MB
4527323d866947fd
RADIXCONVERT.ipch 29.88MB
2369398c3446d611
CRC16.ipch 32.25MB
calculator.sln 1KB
Debug
calculator.ilk 1.59MB
calculator.exe 252KB
calculator.pdb 1.22MB
calculator
crc16.cpp 2KB
calculator.cpp 13KB
calculator.h 1KB
calculator.vcxproj.user 165B
main.cpp 2KB
radixConvert.cpp 3KB
calculator.vcxproj 6KB
Debug
calculator.log 554B
crc16.obj 140KB
radixConvert.obj 280KB
calculator.tlog
CL.write.1.tlog 6KB
CL.read.1.tlog 120KB
CL.command.1.tlog 5KB
link.write.1.tlog 2KB
link.command.1.tlog 6KB
link.read.1.tlog 8KB
calculator.lastbuildstate 214B
calculator.obj 801KB
main.obj 177KB
vc141.idb 299KB
vc141.pdb 980KB
crc16.h 3KB
radixConvert.h 607B
calculator.vcxproj.filters 1KB
共 89 条
- 1
资源评论
- 凉风枯叶2020-09-15不能进行进制运算啊
ojylijia
- 粉丝: 7
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功