题目链接:表达式求值

该题以前做过但是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 表达式求值的更多相关文章

  1. NYOJ 35 表达式求值(逆波兰式求值)

    http://acm.nyist.net/JudgeOnline/problemset.php?typeid=4 NYOJ 35 表达式求值(逆波兰式求值) 逆波兰式式也称后缀表达式. 一般的表达式求 ...

  2. NYOJ 35 表达式求值 (字符串处理)

    题目链接 描述 ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧. 比如输入:&quo ...

  3. NYOJ 35 表达式求值

    一个模板了 哈哈. 这题由于已经包括了整形.浮点形了,以后也不须要特别处理了. /* 这里主要是逆波兰式的实现,使用两个stack 这里用字符串来模拟一个stack,第一步,将中缀表达式转变为后缀表达 ...

  4. NYOJ - 35 表达式求值 分类: NYOJ 2015-03-18 10:33 31人阅读 评论(0) 收藏

    #include<iostream> #include<string> #include<stack> #include<cstdio> using n ...

  5. 数据结构--栈的应用(表达式求值 nyoj 35)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...

  6. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  7. leetcode算法学习----逆波兰表达式求值(后缀表达式)

    下面题目是LeetCode算法:逆波兰表达式求值(java实现) 逆波兰表达式即后缀表达式. 题目:  有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式.同 ...

  8. C语言中缀表达式求值(综合)

    题前需要了解的:中缀.后缀表达式是什么?(不知道你们知不知道,反正我当时不知道,搜的百度) 基本思路:先把输入的中缀表达式→后缀表达式→进行计算得出结果 栈:"先进先出,先进后出" ...

  9. 表达式求值(noip2015等价表达式)

    题目大意 给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...

随机推荐

  1. iOS百度推送的基本使用

    一.iOS证书指导 在 iOS App 中加入消息推送功能时,必须要在 Apple 的开发者中心网站上申请推送证书,每一个 App 需要申请两个证书,一个在开发测试环境下使用,另一个用于上线到 App ...

  2. 2016-09-06 J2EE基础知识之不知

    1.中间件.容器.Web服务器 1.1中间件 中间件是提供系统软件和应用软件之间连接的软件,以便于软件各部件之间的沟通.中间件处于操作系统和更高一级应用程序之间. J2EE提出的背景: 1)企业级应用 ...

  3. Spring事务异常回滚,捕获异常不抛出就不会回滚(转载) 解决了我一年前的问题

    最近遇到了事务不回滚的情况,我还考虑说JPA的事务有bug? 我想多了.......    为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常 ...

  4. HttpServletRequest 各种方法总结(转自百度经验)

    HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息. req ...

  5. opcache effect

    with open opcache, the monitor cpu idle log , there are so much curl_exec and gzip in our php logic ...

  6. 解决eclipse创建Maven项目后无法生成src/main/java资源文件夹的方法

    在项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE System Library,选择workspace default jre.

  7. 网站linux.linuxidc.com有很多好资料

    免费下载地址在 http://linux.linuxidc.com/ 用户名与密码都是www.linuxidc.com 有一些介绍:www.linuxidc.com/download

  8. ArcEngine栅格和矢量渲染(含可视化颜色带)

    使用ArcEngine9.3开发的栅格和矢量的渲染. 开发环境:ArcEngine9.3,VS2008. 功能:栅格(拉伸和分级)和矢量(简单.唯一值.分级.比例)渲染. 开发界面如图所示. 图1 主 ...

  9. 修改SlidingMenu,使其能够完美运行

    今天想给项目添加一个侧边栏的效果,使用到了https://github.com/jfeinstein10/SlidingMenu这个开源项目.项目本身可以通过github下载.此项目同时又依赖于一个名 ...

  10. ios jsbrige

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...