课下选作Main dc
一.中后缀定义:
中缀表达式:我们平时写的数学表达式一般为中缀表达式,如“5+2(3(3-12+1))”,直接拿中缀表达式直接让计算机计算表达式的结果并不能做到。
后缀表达式:把中缀表达表达式“5+2(3(3-12+1))”转化“523312-1++”这样的形式,就是后缀表达式。
二.原理
堆栈的使用
三.代码以及运行截图
import java.util.Stack;
/j计算后缀表达式
20175131王泽龙
/
public class Main {
static Stack op = new Stack<>();
public static Float getv(char op, Float f1, Float f2){
if(op == '+') return f2 + f1;
else if(op == '-') return f2 - f1;
else if(op == '') return f2 f1;
else if(op == '/') return f2 / f1;
else return Float.valueOf(-0); }
/
calculate the value of the reverse Polish expression
* @param rp - reverse Polish expression
* @return - result of the expression
*/
public static float calrp(String rp){
Stack v = new Stack<>();
char[] arr = rp.toCharArray();
int len = arr.length;
for(int i = 0; i < len; i++){
Character ch = arr[i];
// if is operand, push to the stack
if(ch >= '0' && ch <= '9') v.push(Float.valueOf(ch - '0'));
// if is operator, calculate the result
// with top 2 operands in the stack,
// push the result into the stack
else v.push(getv(ch, v.pop(), v.pop()));
}
return v.pop();
}
/**
* from infix to postfix
* @param s - String in the form of infix
* @return String in the form of postfix
/
public static String getrp(String s){
char[] arr = s.toCharArray();
int len = arr.length;
String out = "";
for(int i = 0; i < len; i++){
char ch = arr[i];
if(ch == ' ') continue;
// if is operand, add to
// the output stream directly
if(ch >= '0' && ch <= '9') {
out+=ch;
continue;
}
//if is '(', push to the stack directly
if(ch == '(') op.push(ch);
//if is '+' or '-', pop the operator
// from the stack until '(' and add to
// the output stream
//push the operator to the stack
if(ch == '+' || ch == '-'){
while(!op.empty() && (op.peek() != '('))
out+=op.pop();
op.push(ch);
continue;
}
//if is '' or '/', pop the operator stack and
// add to the output stream
// until lower priority or '('
//push the operator to the stack
if(ch == '' || ch == '/'){
while(!op.empty() && (op.peek() == '' || op.peek() == '/'))
out+=op.pop();
op.push(ch);
continue;
}
//if is ')' pop the operator stack and
// add to the output stream until '(',
// pop '('
if(ch == ')'){
while(!op.empty() && op.peek() != '(')
out += op.pop();
op.pop();
continue;
}
}
while(!op.empty()) out += op.pop();
return out;
}
public static void main(String[] args){
//constraint: the operand should be
// equal or greater than 0
// but equal or less than 9
String exp = "5+5(3(2-1))";
System.out.println(calrp(getrp(exp)));
}
}
我做的比较简单,计算的表达式固定:5+5(3(2-1))

四.码云链接
https://gitee.com/WZL-DM/BESTI.java.is.20175131
课下选作Main dc的更多相关文章
- 20175312 2018-2019-2 《Java程序设计》第6周课下选做——类定义
20175312 2018-2019-2 <Java程序设计>第6周课下选做--类定义 设计思路 1.我觉得Book其实就是一个中转的作用,由测试类Bookself通过Book输入数据,然 ...
- 2017-2018-2 20165312 课下选做 MySort
2017-2018-2 20165312 课下选做 MySort 题目描述 模拟实现Linux下Sort -t : -k 2的功能,参考 Sort的实现. import java.util.*; pu ...
- 课下选做作业实现mypwd
2019-2020-1 20175227 <信息安全系统设计基础> 课下选做作业实现mypwd 要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 ...
- 课下选做作业MyOD
2019-2020-1 20175227 <信息安全系统设计基础> 课下选做作业MyOD 要求 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc ...
- 课下选做作业MySort
20175227张雪莹 2018-2019-2 <Java程序设计> 课下选做作业MySort 要求 注意:研究sort的其他功能,要能改的动代码,需要答辩 模拟实现Linux下Sort ...
- 20175221 《Java程序设计》迭代和JDB(课下作业,选做):
20175221 <Java程序设计> 迭代和JDB(课下作业,选做): 任务详情 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功 ...
- 迭代和JDB(课下作业,选做)
迭代和JDB(课下作业,选做) 题目要求 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 2 m,n 要通过命令行传入 3 提交测试运行截图 ...
- 20175314薛勐 MyOD(课下作业,选做)
MyOD(课下作业,选做) 要求 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 思路 伪代码: 读取命令行输入的参数(文件名) 以16为每个字 ...
- MyOD(课下作业,选做)
MyOD(课下作业,选做) 代码要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.b ...
随机推荐
- 题解1235. 洪水 (Standard IO)
Description 一天, 一个画家在森林里写生,突然爆发了山洪,他需要尽快返回住所中,那里是安全的.森林的地图由R行C列组成,空白区域用点“.”表示,洪水的区域用“*”表示,而岩石用“X”表示, ...
- Windows系统命令整理-Win10
硬件相关 显卡 显卡升级 - 我的电脑->属性->设备管理器->显示适配器->更新驱动程序 服务 telnet 安装:启用或关闭Windows 功能,勾选上“Telnet客户端 ...
- git报错-Initial commit Untracked files nothing added to commit but untracked ……
文章转自 https://www.jianshu.com/p/61c3db30d488 在目标执行命令 git stratus 报错 根据上面的文章,可以解决问题.不行的话,请留言. 感谢你的阅读
- [BZOJ 4771]七彩树(可持久化线段树+树上差分)
[BZOJ 4771]七彩树(可持久化线段树+树上差分) 题面 给定一棵n个点的有根树,编号依次为1到n,其中1号点是根节点.每个节点都被染上了某一种颜色,其中第i个节点的颜色为c[i].如果c[i] ...
- [BZOJ1901][luogu2617]Dynamic Rankings(树状数组+主席树)
题面 单点修改,区间求第k大 分析 首先,这道题卡权值线段树套treap的做法,所以只能用主席树做 对于静态的查询,root[i]对应的主席树的区间[l,r]保存的是a[1]~a[i]有多少个值落在区 ...
- installing-sql-server-2012-error-prior-visual-studio-2010-instances-requiring 转摘
there are two way: First : Inside your CD of SQL Server 2012 you can go to this path \redist\VisualS ...
- ASE Alpha Sprint - backend scrum 6
本次scrum于2019.11.11在sky garden进行,持续30分钟. 参与人: Zhikai Chen, Jia Ning, Hao Wang 请假: Xin Kang, Lihao Ran ...
- 快速的统计千万级别uv
菜菜,咱们网站现在有多少PV和UV了? Y总,咱们没有统计pv和uv的系统,预估大约有一千万uv吧 写一个统计uv和pv的系统吧 网上有现成的,直接接入一个不行吗? 别人的不太放心,毕竟自己写的,自己 ...
- wait()和sleep()、sleep()和yield()的区别
wait()和sleep()的区别主要表现在一下几个方面: 原理不同.sleep()方法是Thread类的静态方法,是线程用来控制自身流程的.它会使线程暂停执行一段时间,把执行机会让给其他线程,等到时 ...
- Spring 事物机制(总结)
Spring两种事物处理机制,一是声明式事物,二是编程式事物 声明式事物 1)Spring的声明式事务管理在底层是建立在AOP的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加 ...