课下选作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 ...
随机推荐
- upc组队赛17 Bits Reverse【暴力枚举】
Bits Reverse 题目链接 题目描述 Now given two integers x and y, you can reverse every consecutive three bits ...
- CVTRES : fatal error CVT1100: duplicate resource
升级某些VC6工程到VS2017,除了目录问题外,就是这个. 解决方法: Properties > Linker > Manifest File 第一项,Generate Manifest ...
- java文件上传下载 使用SmartUpload组件实现
使用SmartUpload组件实现(下载jsmartcom_zh_CN.jar) 2017-11-07 1.在WebRoot创建以下文件夹,css存放样式文件(css文件直接拷贝进去),images存 ...
- php和java的优势
现在市场上的电子商务软件基本上可归结为两大阵营,即PHP阵营和Java阵营.但对接触电子商务不久的用户来说,看到的往往只是它们的表相,只是明显的价格差异,却很难看出它们之间的实际差异.下面我们就为大家 ...
- SimplePropertyRetriever
var SimplePropertyRetriever = { getOwnEnumerables: function (obj) { return this._getProp ...
- spring-第十二篇之两种后处理器
1.扩展IoC容器使用后处理器扩展 bean后处理器:对容器中的bean进行后处理,也就是额外的加强. 容器后处理:对IoC容器进行后处理,增强容器功能. 2.bean后处理器 负责处理容器 ...
- 源码分析--HashMap(JDK1.8)
在JDK1.8中对HashMap的底层实现做了修改.本篇对HashMap源码从核心成员变量到常用方法进行分析. HashMap数据结构如下: 先看成员变量: 1.底层存放数据的是Node<K,V ...
- go语言从例子开始之Example38.排序
Go 的 sort 包实现了内置和用户自定义数据类型的排序功能.我们首先关注内置数据类型的排序. Example: package main import ( "fmt" &quo ...
- Codeforces Round #394 (Div. 2) - C
题目链接:http://codeforces.com/contest/761/problem/C 题意:给定n个长度为m的字符串.每个字符串(字符串下标从0到m-1)都有一个指针,初始指针指向第0个位 ...
- linux--mysql的安装与配置
linux centos下,mysql安装有三种方式:二进制tar包安装,rpm安装,yum安装(最简单) 查看有没有安装过: yum list installed mysql* rpm -qa | ...