/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Stack;
/**
* 表达式计算,允许括号负数小数,不可省略运算符
* @author HQ-Alien
*/
public class CalculatedNotation {
//中序表达式
private String notation;
//中间利用的栈
private Stack oprateSck;
//最终运算字符串组
private String[] reversePolish;
/**
* 默认无参构造
*/
public CalculatedNotation(){
this.oprateSck = new Stack();
reversePolish = new String[100];
}
/**
* 含参构造
* @param notation
*/
public CalculatedNotation(String notation){
this.oprateSck = new Stack();
this.notation = notation;
this.notation += "@";
reversePolish = new String[100];
}
/**
* 通过逆波兰式计算值
* @return
*/
public double calculate(){
for(int i = 0; i<reversePolish.length;i++){
if(reversePolish[i] == null)
break;
double x = 0;
if(isOprate(reversePolish[i])){
switch (reversePolish[i]){ //source6中不支持switch中使用字符串,可以转为char后再用
case "+":
x = (Double) oprateSck.pop() + (Double) oprateSck.pop() ;
break;
case "-":
x = (Double) oprateSck.pop() ;
x = (Double) oprateSck.pop() - x;
break;
case "*":
x = (Double) oprateSck.pop() * (Double) oprateSck.pop() ;
break;
case "/":
x = (Double) oprateSck.pop() ;
x = (Double) oprateSck.pop() / x;
break;
case "^":
x = (Double) oprateSck.pop() ;
x = Math.pow((Double) oprateSck.pop() , x);
break;
}
}else{
x = Double.valueOf(reversePolish[i]);
}
oprateSck.push(x);
}
double result = (Double) oprateSck.pop();
return result;
}
/**
* 转化为逆波兰式
*/
public void transToReversePolish(){
char[] infix = notation.toCharArray();
oprateSck.push('@');
int i=0;
int j=0;
while(i < infix.length){
while( infix[i] == ' ') i++;
if( infix[i] == '@') break;
if( infix[i] == '(' ){
oprateSck.push(infix[i]);
i++;
}else if( infix[i] == ')' ){
while( (Character)oprateSck.peek() != '(')
reversePolish[j++] = ""+ oprateSck.pop();//此处加个""是为了把char转为String
oprateSck.pop();
i++;
}else if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^'){
if( infix[i] == '-' ){
if(i==0 || infix[i-1] == '(')
reversePolish[j++] = "0";
}
char op = (Character)oprateSck.peek();
while(getPriority(op) >= getPriority(infix[i])){
reversePolish[j++] = "" + oprateSck.pop();//此处加个""是为了把char转为String
op = (Character)oprateSck.peek();
}
oprateSck.push(infix[i]);
i++;
}else{
if( (infix[i] < '0' || infix[i] > '9') && infix[i] != '.'){
System.out.println("你的表达式错了,错了!!!");
System.exit(1);
}
String temp = "";
while((infix[i] >= '0' && infix[i] <= '9') || infix[i] == '.'){
temp += infix[i];
i++;
}
reversePolish[j++] = temp;
}
}
char ch =(Character)oprateSck.pop();
while(ch != '@'){
if(ch == '(')
System.out.println("你的表达式绝对有问题的");
else{
reversePolish[j++] = "" + ch;
ch =(Character)oprateSck.pop();
}
}
oprateSck.clear();
}
/**
* 获得运算符优先级
* @param op
* @return
*/
public int getPriority(char op){
switch(op){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
case '(':
case '@':
default :
return 0;
}
}
/**
* 判断是否为操作符
* @param element
* @return
*/
public boolean isOprate(String element){
return element.equals("+") || element.equals("-") || element.equals("*") || element.equals("/") || element.equals("^");
}
/**
* @return the notation
*/
public String getNotation() {
return notation;
}
/**
* @param notation the notation to set
*/
public void setNotation(String notation) {
this.notation = notation;
this.notation += "@";
}
/**
* @return the reversePolish
*/
public String getReversePolish() {
String temp = "";
for(int i=0; i<reversePolish.length;i++){
if(reversePolish[i] == null)
return temp;
temp += reversePolish[i] +" ";
}
return temp;
}
}
表达式计算 逆波兰式 栈 可括号小数负数
2星 需积分: 47 98 浏览量
2015-12-25
14:14:12
上传
评论 4
收藏 6KB ZIP 举报
萧易桥
- 粉丝: 90
- 资源: 4
最新资源
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
- 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第1章docker基础(1.1-1.4)
- 基于python实现的水下压缩空气储能互补系统建模仿真与经济效益分析+源代码+论文
- 华中科技大学-自然语言处理实验,Bi-LSTM+CRF的中文分词框架,并且利用基于深度学习的方法进行中文命名实体识别++源码报告
- 基于动态罚函数的铁路车流分配与径路优化模型python源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈