NYOJ - 35 表达式求值 分类: NYOJ 2015-03-18 10:33 31人阅读 评论(0) 收藏
#include<iostream>
#include<string>
#include<stack>
#include<cstdio>
using namespace std;
stack<double>sn;
stack<char>sc;
double num, des, dou;
bool prt, flag;
int t, len;
string str;
bool isNum(char c)
{
if(c >= '0' && c <= '9')
return true;
if(c== '.')
{
flag = true;
return true;
}
return false;
} bool isOp(char c)
{
if(c == '+' || c == '-' || c == '*' || c == '/')
return true;
return false;
} void init()
{
while(sn.size()) sn.pop();
while(sc.size()) sc.pop();
str.clear();
len = num = des = 0;
dou = 1;
prt = false;
flag = false;
} void calm()
{
if(sc.size() && isOp(sc.top()))
{
double ans;
double y = sn.top();
sn.pop();
double x = sn.top();
sn.pop();
char op = sc.top();
sc.pop();
switch(op)
{
case '+': ans = x + y; break;
case '-': ans = x - y; break;
case '*': ans = x * y; break;
case '/': ans = x / y; break;
}
// cout<<op<<" "<<ans<<endl;
sn.push(ans);
} } int main()
{
// freopen("in.in","r",stdin);
cin>>t;
while(t--)
{
init();
cin>>str;
int len = str.length(); for(int i = 0; i < len; i++)
{
if(isNum(str[i]))
{
if(str[i] == '.') continue;
if(!flag) num = num*10 + str[i] - '0';
else{
des = des*10 + str[i] - '0';
dou *= 10;
}
}
else
{
num = num + des/dou;
if(str[i] == '+' || str[i] == '-')
{
if(!prt) sn.push(num);
while(sc.size() && sc.top() != '(')calm();
sc.push(str[i]);
prt = false;
}
else if(str[i] == '*' || str[i] == '/')
{
if(!prt) sn.push(num);
if(sc.size())
if(sc.top()=='*' || sc.top()=='/')
calm();
sc.push(str[i]);
prt = false;
}
else if(str[i] == '(')
{
sc.push(str[i]);
}
else if(str[i] == ')')
{
if(!prt) sn.push(num);
while(sc.top() != '(') calm(); sc.pop();
prt = true;
}
else
{
if(!prt) sn.push(num);
while(sn.size() > 1) calm();
printf("%0.2lf\n",sn.top());
sn.pop();
break;
}
num = 0;
flag = false;
dou = 1;
des = 0;
}
}
} return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
NYOJ - 35 表达式求值 分类: NYOJ 2015-03-18 10:33 31人阅读 评论(0) 收藏的更多相关文章
- 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏
也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...
- 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏
文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...
- 积分图像 分类: 图像处理 Matlab 2015-06-06 10:30 149人阅读 评论(0) 收藏
积分图像(integral image)是一种快速计算矩形区域之和的数据结构,常利用它对算法进行加速.积分图像中处的值是原始灰度图像的左上角与当前点所围成的矩形区域内所有像素点的灰度值之和,即: 其中 ...
- makefile基础实例讲解 分类: C/C++ 2015-03-16 10:11 66人阅读 评论(0) 收藏
一.makefile简介 定义:makefile定义了软件开发过程中,项目工程编译链.接接的方法和规则. 产生:由IDE自动生成或者开发者手动书写. 作用:Unix(MAC OS.Solars)和Li ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏
1. 概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...
- C#多线程(下) 分类: C# 线程 2015-03-09 10:41 153人阅读 评论(0) 收藏
四.多线程的自动管理(线程池) 在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应 这一般使用ThreadPool(线 ...
- iOS8 UISearchViewController搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏
在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...
- iOS开发:创建真机调试证书 分类: ios相关 2015-04-10 10:22 149人阅读 评论(0) 收藏
关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架发布问题.今天就着重说一下关于针对于苹果 ...
随机推荐
- Codeforces Round #568 (Div. 2) G1. Playlist for Polycarp (easy version) (状压dp)
题目:http://codeforces.com/contest/1185/problem/G1 题意:给你n给选项,每个选项有个类型和价值,让你选择一个序列,价值和为m,要求连续的不能有两个相同的类 ...
- 使用Nodejs 的http-proxy 模块做代理服务器的尝试
参考 : https://blog.csdn.net/zhihuoqian9683/article/details/78944482 (亲测可行) http://www.mizuiren.com/4 ...
- (转)OpenFire源码学习之七:组(用户群)与花名册(用户好友)
转:http://blog.csdn.net/huwenfeng_2011/article/details/43413651 Group 在openfire中的gorop——组,也可以理解为共享组.什 ...
- java求两个数中的大数
java求两个数中的大数 java中的max函数在Math中 应用如下: int a=34: int b=45: int ans=Math.max(34,45); 那么ans的值就是45.
- OpenLayers绘制图形
OpenLayers绘制图形 OpenLayers的显示构成由外向内为: ol.Map:地图对象. ol.layer.Vector:图层对象layer.Map含有多个layer,最终的显示效果是由 ...
- HDU 6651 Final Exam (思维)
2019 杭电多校 7 1006 题目链接:HDU 6651 比赛链接:2019 Multi-University Training Contest 7 Problem Description Fin ...
- Spark:三种任务提交流程standalone、yarn-cluster、yarn-client
spark的runtime参考:Spark:Yarn-cluster和Yarn-client区别与联系浪尖分享资料 standalone Spark可以通过部署与Yarn的架构类似的框架来提供自己的集 ...
- PAT_A1041#Be Unique
Source: PAT A1041 Be Unique (20 分) Description: Being unique is so important to people on Mars that ...
- leetcode 596 BUG笔记
There is a table courses with columns: student and class Please list out all classes which have more ...
- JAVA求回文数
Manacher算法(马拉车算法)时间复杂度O(n) 用过中心检测法(就是上面说的O(n2) O(n^2)O(n )的算法)的都知道对于奇数回文串和偶数回文串的处理是不同的,奇数回文串有2n+1 2n ...