一.中后缀定义:
中缀表达式:我们平时写的数学表达式一般为中缀表达式,如“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的更多相关文章

  1. 20175312 2018-2019-2 《Java程序设计》第6周课下选做——类定义

    20175312 2018-2019-2 <Java程序设计>第6周课下选做--类定义 设计思路 1.我觉得Book其实就是一个中转的作用,由测试类Bookself通过Book输入数据,然 ...

  2. 2017-2018-2 20165312 课下选做 MySort

    2017-2018-2 20165312 课下选做 MySort 题目描述 模拟实现Linux下Sort -t : -k 2的功能,参考 Sort的实现. import java.util.*; pu ...

  3. 课下选做作业实现mypwd

    2019-2020-1 20175227 <信息安全系统设计基础> 课下选做作业实现mypwd 要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 ...

  4. 课下选做作业MyOD

    2019-2020-1 20175227 <信息安全系统设计基础> 课下选做作业MyOD 要求 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc ...

  5. 课下选做作业MySort

    20175227张雪莹 2018-2019-2 <Java程序设计> 课下选做作业MySort 要求 注意:研究sort的其他功能,要能改的动代码,需要答辩 模拟实现Linux下Sort ...

  6. 20175221 《Java程序设计》迭代和JDB(课下作业,选做):

    20175221 <Java程序设计> 迭代和JDB(课下作业,选做): 任务详情 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功 ...

  7. 迭代和JDB(课下作业,选做)

    迭代和JDB(课下作业,选做) 题目要求 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 2 m,n 要通过命令行传入 3 提交测试运行截图 ...

  8. 20175314薛勐 MyOD(课下作业,选做)

    MyOD(课下作业,选做) 要求 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 思路 伪代码: 读取命令行输入的参数(文件名) 以16为每个字 ...

  9. MyOD(课下作业,选做)

    MyOD(课下作业,选做) 代码要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.b ...

随机推荐

  1. Vue作用域插槽:用作循环结构的模版

    一 项目结构 二 App组件 <template> <div id="app"> <!-- 子组件 --> <todos :list=&q ...

  2. Buy Tickets 【POJ - 2828】【线段树】

    题目链接 有N次操作,每次都是将第i个数放置在第pos个数的后面,并且这个数的值是val. 这个线段树的思维确实很好,我们可以发现,后面放进去的数,一定是强制位置的,而前面放的数,会随着后面的数进入而 ...

  3. SEC1- 数据库的相关概念

    一.数据库的好处1. 可以持久化数据到本地2. 结构化查询   二.数据库的常见概念1. DB:data base数据库,存储数据的容器2. DBMS:database management sysy ...

  4. Win7 VSCode 在线安装Rust语言及环境配置

    睡前彻底解决在VSCode中,按F12不跳转到标准库源码的问题. 首先,如果装过离线版,卸载掉. 然后去官网下载 rustup-init.exe https://www.rust-lang.org/t ...

  5. JUnit的基本使用

    一些关于单元测试的理念:    单元测试并不能证明你的代码是正确的,只能证明你的代码是没有错误的. Keep bar green and keep your code cool    关于JUnit的 ...

  6. package和import语句_4

    J2SDK中主要的包介绍   java.lang—包含一些Java语言的核心类,如String.Math.Integer.System和 Thread,提供常用功能. java.awt—包含了构成抽象 ...

  7. Maximum Subarray(最大连续子序列和)

    https://leetcode.com/problems/maximum-subarray/ 思路: 如果全为负值,那么取最大值 如果有非负值,那么我们依次计算到当前位置为止的最大值.假设有n个元素 ...

  8. HashSet -无序,不重复集合

    package cn.learn.collection.Set; import java.util.Objects; public class IsPerson { private int age; ...

  9. CSS动画划入划出酷炫

    HTML插入 <!DOCTYPE html> <html class="no-js iarouse"> <head> <meta char ...

  10. dataTable获取所有数据

    "drawCallback": function(settings,e) { var api = new $.fn.dataTable.Api( settings ); resul ...