nyist0j 35 表达式求值
题目链接:表达式求值
该题以前做过但是WA了,今天终于把他解决了,各种悲剧啊,又是考虑不周到啊。。。。。。。。。。。。。。。。。。。
所以贴出来纪念一下,并作为一个警示
/****
ps:注意当遇到 )时处理到遇到(之前
当一个符号进栈,判断之前的符号是否可以出栈,知道没有可出栈的
*/ #include <cstdio>
#include <cstring>
#include <cstdlib> using namespace std; bool judge_fuhao(char ch,char ch1)//ch1为将进栈
{
//printf("fuhao---->%c %c\n",ch,ch1);
if(ch == '(' )
return true;
else if(ch1 == '+' ||ch1 == '-')
{
return false;
}
else if(ch1 == '*' || ch1 == '/')
{
if(ch == '+' || ch == '-' )
return true;
else
return false;
}
} double judge_mtah(char ch,double y,double x)//运算
{
//printf("%c %lf %lf\n",ch,x,y);
switch(ch)
{
case '+':
return x + y; break;
case '-':
return x - y; break;
case '/':
return 1.0*x / y; break;
case '*':
return x * y; break;
}
} int main()
{
int T;
scanf("%d",&T);
char ch[1003];
char stack_ch[1003];
double stack_a[1003];
int stack_a_t = 0;
int stack_ch_t = 0; while(T--)
{
//freopen("2.txt","w",stdout);
scanf("%s",ch);
stack_a_t = 0;
stack_ch_t = 0;
int len = strlen(ch);
char ans[1005];
int ans_t = 0;
double tmp = 0;
int i;
double x,y,z;
for(i = 0; i < len; i++)
{
if(ch[i] == '+'|| ch[i]=='-'||ch[i]=='*'||ch[i]=='/'||ch[i]=='('||ch[i]=='=')
{
if(ans_t != 0)
{
tmp = atof(ans);
ans_t = 0;
ans[ans_t] = '\0';
stack_a[stack_a_t++] = tmp;
}
if(ch[i] != '=')
{
if(stack_ch_t == 0 || ch[i] == '(')
{
stack_ch[stack_ch_t++] = ch[i];
}
else
while(1)//+-*/进来
{
if(stack_ch_t-1 < 0 || judge_fuhao(stack_ch[stack_ch_t-1],ch[i])== true)
{
stack_ch[stack_ch_t++] = ch[i];
break;
}
else
{
x = stack_a[stack_a_t-1];
stack_a_t--;
y = stack_a[stack_a_t-1];
stack_a_t--;
z = judge_mtah(stack_ch[stack_ch_t-1],x,y);
stack_ch_t--;
stack_a[stack_a_t++] = z;
}
}
}
else if(ch[i] == '='){
{
if(stack_ch_t > 1 && judge_fuhao(stack_ch[stack_ch_t-2],stack_ch[stack_ch_t-1]) == true )
{
x = stack_a[stack_a_t-1];
stack_a_t--;
y = stack_a[stack_a_t-1];
stack_a_t--;
z = judge_mtah(stack_ch[stack_ch_t-1],x,y);
stack_ch_t--;
stack_a[stack_a_t++] = z; }
}
}
}
else if(ch[i] == ')')
{
if(ans_t != 0)
{
tmp = atof(ans);
ans_t = 0;
ans[ans_t] = '\0';
stack_a[stack_a_t++] = tmp;
} while(1)
{
if(stack_ch[stack_ch_t-1] == '(')
{
stack_ch_t--;
break;
}
else
{
x = stack_a[stack_a_t-1];
stack_a_t--;
y = stack_a[stack_a_t-1];
stack_a_t--;
z = judge_mtah(stack_ch[stack_ch_t-1],x,y);
stack_a[stack_a_t++] = z;
stack_ch_t--;
}
}
}
else
{
ans[ans_t++] = ch[i];
ans[ans_t] = '\0';
}
}
for(i = stack_ch_t-1; i >= 0; i--)
{
x = stack_a[stack_a_t-1];
stack_a_t--;
y = stack_a[stack_a_t-1];
stack_a_t--;
z = judge_mtah(stack_ch[i],x,y);
stack_a[stack_a_t++] = z;
}
printf("%0.2lf\n",stack_a[0]);
}
return 0;
}
nyist0j 35 表达式求值的更多相关文章
- NYOJ 35 表达式求值(逆波兰式求值)
http://acm.nyist.net/JudgeOnline/problemset.php?typeid=4 NYOJ 35 表达式求值(逆波兰式求值) 逆波兰式式也称后缀表达式. 一般的表达式求 ...
- NYOJ 35 表达式求值 (字符串处理)
题目链接 描述 ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧. 比如输入:&quo ...
- NYOJ 35 表达式求值
一个模板了 哈哈. 这题由于已经包括了整形.浮点形了,以后也不须要特别处理了. /* 这里主要是逆波兰式的实现,使用两个stack 这里用字符串来模拟一个stack,第一步,将中缀表达式转变为后缀表达 ...
- NYOJ - 35 表达式求值 分类: NYOJ 2015-03-18 10:33 31人阅读 评论(0) 收藏
#include<iostream> #include<string> #include<stack> #include<cstdio> using n ...
- 数据结构--栈的应用(表达式求值 nyoj 35)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- leetcode算法学习----逆波兰表达式求值(后缀表达式)
下面题目是LeetCode算法:逆波兰表达式求值(java实现) 逆波兰表达式即后缀表达式. 题目: 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式.同 ...
- C语言中缀表达式求值(综合)
题前需要了解的:中缀.后缀表达式是什么?(不知道你们知不知道,反正我当时不知道,搜的百度) 基本思路:先把输入的中缀表达式→后缀表达式→进行计算得出结果 栈:"先进先出,先进后出" ...
- 表达式求值(noip2015等价表达式)
题目大意 给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...
随机推荐
- lightoj 1038 Race to 1 Again
题意:给一个数,用这个数的因数除以这个数,直到为1时,求除的次数的期望. 设一个数的约数有M个,E[n] = (E[a[1]]+1)/M+(E[a[2]]+1)/M+...+(E[a[M]]+1)/M ...
- leetcode Merge K sorted Lists python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- QF——关于iOS的强引用,弱引用及strong,retain,copy,weak,assignd的关系
强引用和弱引用: 我们已经知道OC中的内存管理是通过“引用计数器”来实现的.一个对象的生命周期取决于它是否还被其他对象引用(是否retainCount=0).但在有些情况下,我们并不希望对象的销毁时间 ...
- day9_python学习笔记_chapter12_模块
1. 名称空间加载顺序: 首先加载内建名称空间,他由__builtin模块中的名字构成.然后加载执行模块的全局名称空间,他会在模块开始执行后变为活动名称空间.如 果在执行期间调用了一个函数,那么将创建 ...
- Apache新版配置虚拟主机的注意事项
1.关于没有默认索引文件(index.php或者index.html)时,列出目录:需要开启模块 LoadModule autoindex_module modules/mod_autoindex.s ...
- android:configChanges 屏幕横竖屏切换
出处:http://blog.csdn.net/djy1992/article/details/9378195 ---> android:screenOrientation="por ...
- JAVA代码静态检测之PMD
今天再次想启动Java代码静态检测工具的利用问题,主要再次尝试用了PMD,发现不少代码编码规范问题和好的代码建议,并学到不少自己之前没有注意到的Java方便的基础知识,感觉很不错,把相关明白的好的规则 ...
- js的replace的用法;
obj.replace("需要替换的字符串","替换后的字符串")
- 剑指offer 22 栈的压入、弹出序列
class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { bool resu ...
- Ignatius and the Princess III(母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...