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个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...
随机推荐
- 小鱼提问3 static方法中可以访问某个类的私有变量吗(不通过反射的其他非正常手段)?什么情况下可以?
class Student { private string _name; public int Age = 0; public static void Test() { Student stu = ...
- C语言的声明和定义
在程序设计中,时时刻刻都用到变量的定义和变量的声明,可有些时候我们对这个概念不是很清楚,知道它是怎么用,但却不知是怎么一会事. 下面我就简单的把他们的区别介绍如下: 变量的声明有两种情况: (1)一种 ...
- Linux 学习之防火墙配置
1.安装iptables防火墙 yum install iptables 2. 清除已有的iptables规则 iptables -F iptables -X iptables -Z 3.显 ...
- linux杂记(二)主机硬盘规划
linux安装过程中,至少要两个partition,一个是[/],另一个是虚拟内存[swap].但比较不保险. 稍微麻烦点的方式: / /usr:linux操作系统 /home:使用者信息 /var: ...
- 说出x的结果,并解释为什么?
var x = 1; if(function f(){}){ x += typeof f; } x; //x的结果是? x=1undefined 首先是 if表达式的问题 if括号里,不一定非要用== ...
- 《离散数学之把妹要诀》的js实现
网上看到一篇有意思的文章<离散数学之把妹要诀> 就用JS写了上面所讲的配对方式: 首先设定变量 // 男生理想列表 var menPreference = { A: [1, 2, 3, 4 ...
- 从零开始PHP学习 - 第一天
写这个系列文章主要是为了督促自己 每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...
- 不同的strcmp
Android libc中的strcmp https://android.googlesource.com/platform/bootable/bootloader/legacy/+/donut-re ...
- Oracle_系统和对象权限管理
授予系统权限: GRANT { system_privilege | role } [,{ system_privilege | role }]... ... TO {user | role | PU ...
- C函数数组元素初始化
初始化时,可随意指定初始化的元素或者元素的范围. 附gnu c 手册. http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html 代码: t ...