【Zhejiang University PATest】02-3. 求前缀表达式的值
算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。前缀表达式指二元运算符位于两个运算数之前,例如2+3*(7-4)+8/4的前缀表达式是:+ + 2 * 3 - 7 4 / 8 4。请设计程序计算前缀表达式的结果值。
输入格式说明:
输入在一行内给出不超过30个字符的前缀表达式,只包含+、-、*、\以及运算数,不同对象(运算数、运算符号)之间以空格分隔。
输出格式说明:
输出前缀表达式的运算结果,精确到小数点后1位,或错误信息“ERROR”。
样例输入与输出:
| 序号 | 输入 | 输出 |
| 1 |
+ + 2 * 3 - 7 4 / 8 4 |
13.0 |
| 2 |
/ -25 + * - 2 3 4 / 8 4 |
12.5 |
| 3 |
/ 5 + * - 2 3 4 / 8 2 |
ERROR |
| 4 |
+10.23 |
10.2 |
【solution】
前缀表达式的求值,算法并不难,我觉得更难的地方是字符串的处理以及一些细节的地方,比较花时间。
算法如下:
“对于一个前缀表达式的求值而言,首先要从右至左扫描表达式,从右边第一个字符开始判断,如果当前字符是数字则一直到数字串的末尾再记录下来,如果是运算符, 则将右边离得最近的两个“数字串”作相应的运算,以此作为一个新的“数字串”并记录下来。一直扫描到表达式的最左端时,最后运算的值也就是表达式的值。例 如,前缀表达式“- 1 + 2 3“的求值,扫描到3时,记录下这个数字串,扫描到2时,记录下这个数字串,当扫描到+时,将+右移做相邻两数字串的运算符,记为2+3,结果为5,记录下这个新数字串,并继续向左扫描,扫描到1时,记录下这个数字串,扫描到-时,将-右移做相邻两数字串的运算符,记为1-5,结果为-4,所以表达式的值为-4。”——来自百度百科词条“前缀表达式”
按照这个算法用栈写出来就可以了,时间复杂度 O(n)。
AC的代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h> #define exception();\
printf("ERROR");\
exit(); int contain(char ch)
{
if (ch=='+' || ch=='-' || ch=='*' || ch=='/') return ;
return ;
} float calc(float a, float b, char ch)
{
float ans;
switch (ch)
{
case '+':
ans = a+b;
break;
case '-':
ans = a-b;
break;
case '*':
ans = a*b;
break;
case '/':
if (b == ) { exception(); }
ans = a/b;
break;
}
return ans;
} float addbit(float ans, int boo, char ch, int power)
{
if (!boo) return (ans*+ch-'');
else return (ans+(float)(ch-'')/pow(,power));
} float str2flo(char *ch)
{
float ans;
int power = ;
int boo = , positive = ; if (*ch == '-')
{
ch++;
positive = ;
}
else if (*ch == '+') ch++; ans = *ch-'';
ch++; while (*ch != ' ' && *ch != '\0' && *ch != '\n')
{
if (*ch>='' && *ch<='')
{
if (boo) power++;
ans = addbit(ans, boo, *ch, power);
}
else if (*ch=='.')
{
boo = ;
}
ch++;
} if (positive) return ans; else return -ans;
} int main()
{
char str[];
int n, i, tail = ;
float ans[];
int boo = ; gets(str); n = strlen(str) - ; while (n >= )
{
if ( contain(str[n]) )
{
if (tail < ) { exception(); }
else
{
ans[tail-] = calc(ans[tail-], ans[tail-], str[n]);
tail--;
n = n - ;
}
}
else if (str[n] != ' ')
{
while (str[n]!=' ' && n>=) n--;
ans[tail++] = str2flo(str+n+);
n--;
}
else n--;
} if (tail == )
{
printf("%.1f", ans[]);
}
else { exception(); } return ;
}
【Zhejiang University PATest】02-3. 求前缀表达式的值的更多相关文章
- pat02-线性结构3. 求前缀表达式的值(25)
02-线性结构3. 求前缀表达式的值(25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 算术表达式有前缀表示法.中缀表示法和后缀表示法 ...
- PTA笔记 堆栈模拟队列+求前缀表达式的值
基础实验 3-2.5 堆栈模拟队列 (25 分) 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操作函数: int IsFull(Sta ...
- 3-07. 求前缀表达式的值(25) (ZJU_PAT数学)
题目链接:http://pat.zju.edu.cn/contests/ds/3-07 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式.前缀表达式指二元运算符位于两个运算数之前,比如2+3*(7 ...
- 信息竞赛进阶指南--递归法求中缀表达式的值,O(n^2)(模板)
// 递归法求中缀表达式的值,O(n^2) int calc(int l, int r) { // 寻找未被任何括号包含的最后一个加减号 for (int i = r, j = 0; i >= ...
- openjduge 求简单表达式的值
表达式求值 总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 131072kB 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. 输入 输入仅有一行 ...
- K:双栈法求算术表达式的值
相关介绍: 该算法用于求得一个字符串形式的表达式的结果.例如,计算1+1+(3-1)*3-(21-20)/2所得的表达式的值,该算法利用了两个栈来计算表达式的值,为此,称为双栈法,其实现简单且易于理 ...
- 【Zhejiang University PATest】02-1. Reversing Linked List
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
随机推荐
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- hdu----(4308)Saving Princess claire_(搜索)
Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- 读书笔记3 Socket
Socket被称为网络插座.用于两个网络应用程序之间的通信. 通信地址:URI 通过协议,地址,端口号可以确定网络上的一个程序.地址和端口号组合称之为端点. 通常会有发信人通信地址,收信人通信地址这两 ...
- javascript 把字符串转换为对象
function strToJson(str) { var json = (new Function("return " + str))(); return json;}
- Windows server2003 + sql server2005 集群配置安装
http://blog.itpub.net/29500582/viewspace-1249319/
- 当进入log文件后就卡机
问题:一个目录打开,终端就卡死不动了 Ctrl+c也没用,cat一样没用? 解决办法:用的时间或用的数量删除(时间已经否决掉) ls -t |tail -1000 |xargs rm 原因: log ...
- sp转dp dp转px
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mTextSize, context.getResources().getDis ...
- UVa 11426 - GCD - Extreme (II)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- mac 连接mysql提示 Warning: mysqli::real_connect(): (HY000/2002): No such file or directory
mac 连接mysql的时候提示 Warning: mysqli::real_connect(): (HY000/2002): No such file or directory [说明1]MAC下M ...
- 工具&符号
持续更新中...... 1.RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RP ...