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个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...
随机推荐
- 《Oracle Applications DBA 基础》- 9 - Concurrent Processing[Z]
<Oracle Applications DBA 基础>- 9 - Concurrent Processing================================== 参考资料 ...
- 在什么情况下使用exist和in
http://www.itpub.net/thread-406784-4-1.htmlYou Asked (Jump to Tom's latest followup) Tom: can you gi ...
- api(一) 创建窗口 (转)
所有的Windows SDK编程都有一个类似的框架,本文就说说这个框架,Windows程序设计的框架分为“三部曲”: 一.注册窗口类 注册窗口类的API函数是RegisterClass或者Regist ...
- 记录一个php强制下载文件函数
整理个经常用的强制下载文件的函数,之前就说把一些常用的东西整理出来结果老是没时间,最近陆续把这些东西整理下. public function download() { $id = $this-> ...
- 转 git操作小结
UNDER MIT LICENSE. 公司几乎所有的项目都是使用 git 仓库来管理代码,以前对 git 只有些肤浅的了解,每次提交代码或者上线的时候总是会提心吊胆,生怕出现一些未知的问题.经过三个月 ...
- ubuntu 安装ruby rails
步骤0 - 安装系统需要的包 Mac 请安装 Xcode 开发工具,它将帮你安装好 Unix 环境需要的开发包 Ubuntu 请安装 $ sudo apt-get install -y build-e ...
- ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句
ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句 存储过程 1.找到正在执行的存储过程的 sid ,serial# select b.sid,b.SERIAL#,a.OBJEC ...
- Oracle EBS-SQL (SYS-20):职责使用菜单2.sql
select frt.responsibility_name, fr.menu_id, fm.menu_name, fr.request_group_i ...
- POJ1961Period
POJ1961 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ...
- C++_bool
1 0 #include <iostream> using namespace std; void main() { && || || - && ; std ...