题目描述

输入一个字符串形式的表达式,该表达式中包括整数,四则运算符(+、-、*、/),括号,三角函数(sin(x)、cos(x)、tan(x)),底数函数(lg(x)、ln(x)),计算该表达式的值

输入

输入一个字符串形式的表达式,保证中间及最终结果不超出double的范围

 

输出

表达式的值,保留6位小数

样例输入

3
3+5
((2-1)*5-1)*6
1+cos(0)

样例输出

3.000000
8.000000
24.000000
2.000000

来源

2015机考D题

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<stack>
using namespace std;
int main()
{
string str;
while(cin>>str)
{
stack<double>num;
stack<char>chr;
for(int i=;i<str.size();i++)
{
//cout<<str[i]<<" ";
double number=;
if(str[i]>=&&str[i]<=)
{
number=str[i]-;
while(str[i+]>=&&str[i+]<=)
{
number=number*+(str[i+]-);
i++;
}
num.push(number);
}
else if(str[i]=='(')
chr.push(str[i]);
else if(str[i]=='+'||str[i]=='-')
chr.push(str[i]);
else if(str[i]=='*'||str[i]=='/')
{
char t=str[i];
i++;
number=str[i]-;
while(str[i+]>=&&str[i+]<=)
{
number=number*+(str[i+]-);
i++;
}
num.push(number);
double x=num.top();num.pop();
double y=num.top();num.pop();
double n=;
if(t=='*')
n=x*y;
else n=x/y;
num.push(n);
}
else if(str[i]==')')
{
char temp=chr.top();chr.pop();
double x=num.top();num.pop();
double y=num.top();num.pop();
double n=;
if(temp=='+')
n=x+y;
else if(temp=='-')
n=y-x;
num.push(n);
chr.pop();
chr.pop();
}
else if(str[i]=='s'||str[i]=='c'||str[i]=='t')
{
char t=str[i];
i=i+;
number=str[i]-;
while(str[i+]>=&&str[i+]<=)
{
number=number*+(str[i+]-);
i++;
}
if(t=='s')
{
number=sin(number);
num.push(number);
}
else if(t=='t')
{
number=tan(number);
num.push(number);
}
else if(t=='c')
{
number=cos(number);
num.push(number);
}
i++;
}
else if(str[i]=='l')
{
char t=str[i+];
i=i+;
number=str[i]-;
while(str[i+]>=&&str[i+]<=)
{
number=number*+(str[i+]-);
i++;
}
if(t=='g')
{
number=log10(number);
num.push(number);
}
else if(t=='n')
{
number=log(number);
num.push(number);
}
i++;
}
}
while(chr.size()>&&chr.size()<&&num.size()!=)
{
char temp=chr.top();chr.pop();
double x=num.top();num.pop();
double y=num.top();num.pop();
double n=;
if(temp=='+')
n=x+y;
else if(temp=='-')
n=y-x;
else if(temp=='(')
continue;
num.push(n);
}
printf("%lf",num.top());cout<<endl;
}
return ;
}

BUPT复试专题—解析表达式(2015)的更多相关文章

  1. BUPT复试专题—List(2015)

    题目描述 在该LIST上实现3种操作 1.append x在该LIST末尾添加x,x是32位整数 2.pop删除该LIST末尾的数 3.find i寻找第i个数,若i为负数表示寻找倒数第i个数,例如i ...

  2. BUPT复试专题—图像压缩存储(2015)

    题目描述 以二维数组表示图像,其值只有0.1两种,寻找两幅图像中最大的相同方阵   输入 第一行输入一个n,接下来的2n行输入两个n*n数组,寻找一个最大的m*m子区域,使得两个数组在该子区域完全相同 ...

  3. BUPT复试专题—求导数(2015)

    题目描述 描述:求函数f(x) = a*x^3 + b*x^2 + c*x + d在x = x0处的一阶导数.   输入 数据第一行是数据的组数m 接下来m行的每一行分别是 a b c d x0 输出 ...

  4. BUPT复试专题—字符串转换(2013计院)

    题目描述 我们将仅由若干个同一小写字母构成的字符串称之为简单串,例如"aaaa"是一个简单串,而"abcd"则不是简单串.现在给你一个仅由小写字母组成的字符串, ...

  5. BUPT复试专题—统计时间间隔(2013计院)

    题目描述 给出两个时间(24小时制),求第一个时间至少要经过多久才能到达第二个时间.给出的时间一定满足的形式,其中x和y分别代表小时和分钟.0≤x<24,0≤y<60. 输入格式 第一行为 ...

  6. BUPT复试专题—最值问题(2013计院)

    题目描述 给出N个数,求出这N个数中最大值和次大值.注意这里的次大值必须严格小于最大值.输入保证N个数中至少存在两个不同的数. 输入格式 第一行为测试数据的组数T(T≤20).请注意,任意两组测试数据 ...

  7. BUPT复试专题—数据库检索(2014软院)

    题目描述 在数据库的操作过程中,我们进场会遇到检索操作.这个题目的任务是完成一些特定格式的检索,并输出符合条件的数据库中的所有结果. 我们现在有一个数据库,维护了学生的姓名(Name),性别(Sex) ...

  8. BUPT复试专题—最近公共祖先(2014软院)

    题目描述 给出一棵有N个节点的有根树TREE(根的编号为1),对于每组查询,请输出树上节点u和v的最近公共祖先. 最近公共祖先:对于有向树TREE的两个结点u,v.最近公共祖先LCA(TREE u,v ...

  9. BUPT复试专题—最长连续等差子数列(2014软院)

    题目描述   给定-个长度为N的整数数列,你需要在其中找到最长的连续子数列的长度, 并满足这个子数列是等差的.注意公差小于或等于0的情况也是允许的. 输入 第一行为数据组数T(1~100),表示测试数 ...

随机推荐

  1. 《JAVA8实战》读书笔记之传递方法和传递lambda

    传递方法: 假设 你有一个Apple类,它 有一个getColor方法,还有一个变量inventory保存着一个Apples的列表.你可能想要选出所 有的绿苹果,并返回一个列表.通常我们用筛选(fil ...

  2. java连接Fastdfs图片服务器上传失败的解决方法

    照着视频上做,但是却连接不了虚拟机linux上的图片服务器,估计是linux防火墙的问题(这个实在是神烦,前面有好几次连接不了都是因为linux防火墙),果不其然,关闭即可. Linux关闭防火墙的命 ...

  3. hdu 1068(最大独立集)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. 通过Eclipse生成可运行的jar包

    本来转自http://www.cnblogs.com/lanxuezaipiao/p/3291641.html 我是个追新潮的人,早早用上了MyEclipse10.最近需要使用Fat jar来帮我对一 ...

  5. docker rancher 安装

    1.rancher 中文文档 https://rancher.com/docs/rancher/v1.6/zh/ 2.从阿里云拉取镜像 docker pull registry.cn-hangzhou ...

  6. AC日记——魔方 洛谷 P2007

    魔方 思路: 模拟: 代码: #include <cstdio> #include <cstring> #include <iostream> #include & ...

  7. AC日记——[JSOI2008]火星人prefix bzoj 1014

    1014 思路: 平衡树+二分答案+hash: 好了懂了吧. 代码: #include <cstdio> #include <cstring> #include <ios ...

  8. 部署web应用到虚拟主机的三种方式

    方式一:            在 [tomcat]/conf/server.xml 文件中的<Engine>标签下的<Host>标签内部, 添加一个 <Context ...

  9. EasyUI----DataGrid行明细增删改操作

    http://blog.csdn.net/huchiwei/article/details/7787947   本文实现的是EasyUI-DataGrid行明细的增删改操作.具体参考来自以下文章: 官 ...

  10. [thinkphp]验证码不显示: 图像因存在错误无法显示

    我只想说,该死的BOM FUKKKKK!!!!!!!!