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个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...
随机推荐
- js 控制台的错误提示
错误:程序运行过程中发生的异常状态 导致程序停止运行——异常 错误处理:当程序发生错误时,保证程序不退出的机制 发生错误时,程序会自动创建一个Error对象: Error对象中仅封装了错误的信息 js ...
- MySQL函数笔记
MySQL函数笔记 日期函数 SELECT t1.xcjyrq, t1.* FROM view_sbxx t1 WHERE t1.syzt ; SELECT t1.xcjyrq, t1.* FROM ...
- hadoop笔记之Hive的数据存储(外部表)
Hive的数据存储(外部表) Hive的数据存储(外部表) 外部表 指向已经在HDFS中存在的数据,可以创建Partition 它和内部表在元数据的组织上是相同的,而实际数据的存储则有较大的差异 外部 ...
- scala函数进阶与lazy的作用
内容如下. lazy修饰的变量可以延迟初始化,如下面所示,文件未必存在,file变量未必有内容.
- 【转】IOS开发小技巧
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- 【Chromium中文文档】跨平台开发的约定与模式
跨平台开发的约定与模式 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/C ...
- MySQL----cluster安装
第一步.下载MySQL cluster: http://cdn.mysql.com/Downloads/MySQL-Cluster-7.4/mysql-cluster-gpl-7.4.7-linux- ...
- uva 10129 poj 1386 hdu 1116 zoj 2016 play on words
//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...
- thanks使用注意事项;
router.get('/api/users/search/:key/:page', function(req, res) { if(_.isEmpty(req.params.key)) { res. ...
- 关于hasOwnProperty()方法的应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...