基本计算器
来源:互联网 发布:董承非 知乎 编辑:程序博客网 时间:2024/05/29 09:40
输入表达式字符串,以“=”表示结束, 计算并输出表达式值。 操作数可以是正负整数或实数,操作符有“+”、“-”、“*”、“/”、“^”(乘方)和“sin( )”(正弦)、“cos( )”(余弦)、“log( )”(对数)、“ln( )”(自然对数)等函数。
int transfer(char c)
{
if(c=='+'||c=='-')
return1;
if(c=='*'||c=='/')
return2;
if(c=='^')
return3;
if(c=='s'||c=='c'||c=='g'||c=='n')
return4;
if(c=='(')
return5;
return0;
}
void push_in(stack<char> &op_s,vector<string> &res,char c)
{
if(c=='('||op_s.empty())
{
op_s.push(c);
return;
}
if(c==')')
{
while(op_s.top()!='(')
{
string k;
k+=op_s.top();
res.push_back(k);
op_s.pop();
}
op_s.pop();
return;
}
if(transfer(c)>transfer(op_s.top())||op_s.top()=='(')
{
op_s.push(c);
return;
}
if(transfer(c)<=transfer(op_s.top()))
{
while(transfer(c)<=transfer(op_s.top())&&transfer(op_s.top())!=5)
{
string k;
k+=op_s.top();
res.push_back(k);
op_s.pop();
if(op_s.empty())
break;
}
op_s.push(c);
return ;
}
}
void show(stack<char> s)
{
cout<<"now stack is: ";
while(!s.empty())
{
cout<<s.top()<<' ';
s.pop();
}
cout<<endl;
}
string SetString(string str)
{
string s;
if(str[0]=='-')
s+='0';
for(int i=0;i<=str.size()-1;i++)
{
if(i<str.size()-1&&str[i]=='('&&str[i+1]=='-')
{
s+='(';
s+='0';
}
else
s+=str[i];
}
return s;
}
vector<string> calculate(string str)
{
str=SetString(str);
stack<char> operator_s;
vector<string> res;
int i=-1;
while(str[++i]!='=')
{
if(str[i]>='0'&&str[i]<='9')
{
string num; num+=str[i];
i++;
while(str[i]=='.'||(str[i]>='0'&&str[i]<='9'))
{
num+=str[i];
i++;
}
res.push_back(num);
i--;
continue;
}
else
{
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='^'||str[i]=='('||str[i]==')')
{
//show(operator_s);
push_in(operator_s, res, str[i]);
continue;
}
if(str[i]=='s'||str[i]=='c')
{
push_in(operator_s, res, str[i]);
i=i+2;
continue;
}
if(str[i]=='n'||str[i]=='g')
{
push_in(operator_s, res, str[i]);
continue;
}
}
}
while(!operator_s.empty())
{
string k;
k+=operator_s.top();
res.push_back(k);
operator_s.pop();
}
return res;
}
double result(vector<string> &a)
{
stack<string> s;
for(auto n:a)
{
if(n[0]>='0'&&n[0]<='9')
s.push(n);
else
{
if(n[0]=='+')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 =atof(s1.c_str());
double c2 =atof(s2.c_str());
double l=c1+c2;
s.push(to_string(l));
}
elseif(n[0]=='-')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 =atof(s1.c_str());
double c2 =atof(s2.c_str());
double l=c2-c1;
s.push(to_string(l));
}
elseif(n[0]=='*')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 =atof(s1.c_str());
double c2 =atof(s2.c_str());
double l=c1*c2;
s.push(to_string(l));
}
elseif(n[0]=='/')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 =atof(s1.c_str());
double c2 =atof(s2.c_str());
double l=c2/c1;
s.push(to_string(l));
}
elseif(n[0]=='^')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 =atof(s1.c_str());
double c2 =atof(s2.c_str());
double l=pow(c2,c1);
//cout<<"pow"<<l<<endl;
s.push(to_string(l));
}
elseif(n[0]=='s')
{
string s1=s.top(); s.pop();
double c1=atof(s1.c_str());
double l=sin(c1);
s.push(to_string(l));
}
elseif(n[0]=='c')
{
string s1=s.top(); s.pop();
double c1=atof(s1.c_str());
double l=cos(c1);
s.push(to_string(l));
}
elseif(n[0]=='g')
{
string s1=s.top(); s.pop();
double c1=atof(s1.c_str());
double l=log2(c1);
s.push(to_string(l));
}
elseif(n[0]=='n')
{
string s1=s.top(); s.pop();
double c1=atof(s1.c_str());
double l=log(c1);
s.push(to_string(l));
}
}
}
returnatof(s.top().c_str());
}
int main()
{
cout<<"请输入表达式"<<endl;
string s;
cin>>s;
vector<string> a=calculate(s);
for(int i=0;i<=a.size()-1;i++)
cout<<a[i]<<' ';
cout<<endl;
cout<<result(a);
}
- 基本计算器
- 基本计算器(C#)
- 7.31 计算器基本完工
- 7.28 网页计算器基本界面设计
- 简单的逆波兰基本计算器
- C语言实现基本计算器功能
- Leetcode 224 Basic Calculator 基本计算器
- JS函数实现基本计算器计算
- LeetCode 224. Basic Calculator(基本计算器)
- java学习之计算器基本实现
- 计算器
- 计算器
- /* 计算器 */
- 计算器
- 计算器
- 计算器
- 计算器
- 计算器
- 【Scikit-Learn 中文文档】无监督降维
- 读取.tfrecords格式数据集,进行geture的cnn构建、训练、模型保存
- EM算法的两种理解角度
- 数据库设计
- 20171204_ConcurrentLinkedList源码的一些问题
- 基本计算器
- C++高级编程一些笔记
- linux操作命令
- pandas教程---------数据分组(12/4)
- NameNode元数据持久化
- SpringBoot文件上传下载和多文件上传(图文详解)
- XCTool介绍
- 物理standby database的日常维护
- latex for mac(Texworks)的使用入门