JAVA四则运算字符串解释器
最近学习到后缀表达式,于是基于后缀表达式的思想,写了一个四则运算解释器,输入字符串类型的四则运算表达式,可以直接得到结果,支持括号嵌套.
实现时主要考虑以下两点:
- 字符串中运算符和数字分离
- 运算符优先级
- 括号的嵌套
- 运算符和数字分离:可以考虑用字符串数组存储
- 关于运算符优先级,最开始的想法是将乘除法看作一类,加减法看作一类,乘除法的优先级大于加减法,相同类型的运算符按照从左到右顺序依次计算.
- 括号的嵌套:由于括号内部本身也是表达式,因此可以使用递归处理,但重点在于括号的配对,由于使用递归,所以希望获取最大的嵌套单元.
具体实现:
首先实现运算符识别的代码,判断一个字符是否为运算符:
- public static boolean opjudge(char c) {
- if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
- return true;
- else
- return false;
- }
然后实现字符串表达式转为字符串数组, 由于要求输入的字符串运算符和数字之间没有空格,所以直接拆分不方便,而且运算符和数字的数目不确定,因此先使用ArrayList存储,然后再转成字符串数组,主要方法是遍历字符串中每一个字符并判断是否为运算符,如果是运算符,则直接将运算符转为字符串加入ArrayList,如果不是,则从此位置继续查找到下一个运算符出现的位置,两个位置之间即为操作数,使用substring截取字符串存入Arraylist,具体实现如下:
- public static String[] convert(String s) {
- ArrayList<String> arrayList = new ArrayList<>();
- for (int i = 0; i < s.length(); i++) {
- if (!opjudge(s.charAt(i))) {
- int j = i;
- while ((i < s.length()) && !opjudge(s.charAt(i)))
- i++;
- arrayList.add(s.substring(j, i));
- i--;
- } else
- arrayList.add(String.valueOf(s.charAt(i)));
- }
- return arrayList.toArray(new String[arrayList.size()]);
- }
然后对运算符进行分类和分级,加减法视作一类,乘除法视作一类,乘除法优先级高于加减法:
- public static int opvalue(String s) {
- switch (s) {
- case "+":
- case "-":
- return 1;
- case "*":
- case "/":
- return 2;
- default:
- return -1;
- }
- }
运算符优先级的比较,左边大于右边则返回true,( 两个运算符相等返回false)
- public static boolean opcompare(String s1, String s2) {
- if (opvalue(s1) > opvalue(s2))
- return true;
- else {
- return false;
- }
- }
括号内字符串的获取:
由于括号都是成对出现,要从左至右获取第一个最长的括号表达式,则从左至右遍历字符串时,当右括号 ‘)’ 出现的数目等于左括号’(’ 时,即为所求,此处使用计数器实现,返回的是字符串中从k位置(第k个元素,一个数字算一个元素 )开始第一个带完整括号的子串,比如输入bracketGet(“(8-(2+3))*2+(5+7)*3”,0),返回8-(2+3):
- public static String bracketGet(String s, int k) {
- int m=0;
- int i;
- String[] arr=convert(s);
- for(i=k;i<arr.length;i++){
- if(arr[i].equals("(")){
- m++;
- continue;
- }
- if(arr[i].equals(")")){
- m--;
- if(m==0)
- break;
- else
- continue;
- }
- }
- StringBuilder sb=new StringBuilder();
- for(int j=k+1;j<i;j++){
- sb.append(arr[j]);
- }
- return sb.toString();
- }
以上都是需要用到的配件,下面将利用上面写好的函数实现四则运算字符串的解析和运算,基本思想是使用两个栈,一个存储数字,一个存储运算符,先考虑没有括号的简单情况:
遍历字符串数组:
1.当前元素为操作数时,入数字栈
2.当前元素为运算符时:
- 如果运算符栈为空,则入运算符栈
- 如果运算符栈不空,则比较当前运算符和栈顶运算符优先级(按照之前定义的优先级,乘除大于加减,乘除相同,加减相同):如果当前运算符优先级大于栈顶运算符,则当前运算符入栈;反之,如果当前运算符优先级等于或小于栈顶运算符,则将栈顶运算符出栈,同时数字栈中出栈两个数字参与运算,结果压入数字栈中,然后当前运算符入栈;
- 最后将运算符栈中所有元素依次出栈,数字栈中相应将数字出栈参与运算得到结果并压入栈中;
- 最后返回数字栈中栈顶元素即为表达式结果.
- 有括号时括号内的运算步骤同上,可以获取括号内部的字符串,使用递归计算;
代码如下: - public static int caculate(String formula) {
- String[] arr = convert(formula);
- Stack<Integer> val = new Stack<>();
- Stack<String> op = new Stack<>();
- for (int i = 0; i < arr.length; i++) {
- if (arr[i].equals("(")) {
- val.push(caculate(bracketGet(formula, i)));//递归计算括号内部的值,并将该值入栈
- i = i + bracketGet(formula, i).length() + 1;//括号串所在的位置遍历过,i需要跳过该段
- } else {
- if (arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/")) {
- if (op.isEmpty())
- op.push(arr[i]);
- else if (opcompare(arr[i], op.lastElement())) {
- op.push(arr[i]);
- } else {
- switch (op.pop()) {
- case "+":
- val.push(val.pop() + val.pop());
- break;
- case "-":
- int c = val.pop();
- int d = val.pop();
- val.push(d-c);//减法,pop的顺序从右至左,所以需要先取出元素
- break;
- case "*":
- val.push(val.pop() * val.pop());
- break;
- case "/":
- int a = val.pop();
- int b = val.pop();
- val.push(b / a);//同减法
- break;
- default:
- break;
- }
- op.push(arr[i]);
- }
- } else
- val.push(Integer.parseInt(arr[i]));
- }
- }
- while (!op.isEmpty()) {
- switch (op.pop()) {
- case "+":
- val.push(val.pop() + val.pop());
- break;
- case "-":
- int c = val.pop();
- int d = val.pop();
- val.push(d-c);
- break;
- case "*":
- val.push(val.pop() * val.pop());
- break;
- case "/":
- int a = val.pop();
- int b = val.pop();
- val.push(b / a);
- break;
- default:
- break;
- }
- }
- return val.pop();
- }
- System.out.println(caculate("(((8-(2+3))*2+(5+7)*3))"));
- 输出:42 正确
以上实现是使用int类型,除法可能会不准确,改为double会准确
后面又发现上面的实现方法仍然有冗余之处,下面是改进的代码,主要是优化了运算符的优先级,依次为 除法 乘法 减法 加法 依次降低, 遍历到运算符时,优先级高才入栈, 否则将栈中运算符出栈参与运算,直到栈空或 栈顶运算符优先级低于遍历到的运算符,才将该运算符入栈.
主要改动的是opjudge(),opcompare(), caculate()中少量改动,其余代码同上;
具体代码如下:- public static int caculate(String formula) {
- public static int opvalue2(String s) {
- switch (s) {
- case "+":
- return 1;
- case "-":
- return 2;
- case "*":
- return 3;
- case "/":
- return 4;
- default:
- return -1;
- }
- }
- public static boolean opcompare2(String s1, String s2) {
- if (opvalue2(s1) >= opvalue2(s2))
- return true;
- else {
- return false;
- }
- }
- public static double caculate2(String formula) {
- String[] arr = convert(formula);
- Stack<Double> val = new Stack<>();
- Stack<String> op = new Stack<>();
- for (int i = 0; i < arr.length; i++) {
- if (arr[i].equals("(")) {
- val.push(caculate2(bracketGet(formula, i)));
- i = i + bracketGet(formula, i).length() + 1;
- } else if (arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/")) {
- while (!op.isEmpty() && opcompare2(op.lastElement(), arr[i])) {
- switch (op.pop()) {
- case "+":
- val.push(val.pop() + val.pop());
- continue;
- case "-":
- double c = val.pop();
- double d = val.pop();
- val.push(d - c);
- continue;
- case "*":
- val.push(val.pop() * val.pop());
- continue;
- case "/":
- double a = val.pop();
- double b = val.pop();
- val.push(b / a);
- continue;
- default:
- break;
- }
- }
- op.push(arr[i]);
- }
- else
- val.push(Double.parseDouble(arr[i]));
- }
- while (!op.isEmpty()) {
- switch (op.pop()) {
- case "+":
- val.push(val.pop() + val.pop());
- break;
- case "-":
- double c = val.pop();
- double d = val.pop();
- val.push(d - c);
- break;
- case "*":
- val.push(val.pop() * val.pop());
- break;
- case "/":
- double a = val.pop();
- double b = val.pop();
- val.push(b / a);
- break;
- default:
- break;
- }
- }
- return val.pop();
- }
运算实例:
- public static int opvalue2(String s) {
- public static void main(String[] args) {
- double a=(1+2*(3.0/2)/(2+8)-(2*6.0)/(1+7))-(21+3*(5-2-(7*(4-3))));
- System.out.println(a);
- System.out.println(caculate2("(1+2*(3/2)/(2+8)-(2*6)/(1+7))-(21+3*(5-2-(7*(4-3))))"));
- }
- //output:
- -9.2
- -9.2
至此,完成了四则运算字符串解释器,还有很多可以完善, 比如异常的处理,以及代码的简洁性,作为一个初学者,如果能被看到的话,希望多提建议:
完整版代码如下:
- public static void main(String[] args) {
- import java.util.ArrayList;
- import java.util.Stack;
- public class Main {
- public static void main(String[] args) {
- double a=(1+2*(3.0/2)/(2+8)-(2*6.0)/(1+7))-(21+3*(5-2-(7*(4-3))));
- System.out.println(a);
- System.out.println(caculate2("(1+2*(3/2)/(2+8)-(2*6)/(1+7))-(21+3*(5-2-(7*(4-3))))"));
- }
- //对运算符优先级进一步排序 减法大于加法 除法大于乘法
- public static double caculate2(String formula) {
- String[] arr = convert(formula);
- Stack<Double> val = new Stack<>();
- Stack<String> op = new Stack<>();
- for (int i = 0; i < arr.length; i++) {
- if (arr[i].equals("(")) {
- val.push(caculate2(bracketGet(formula, i)));
- i = i + bracketGet(formula, i).length() + 1;
- } else if (arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/")) {
- while (!op.isEmpty() && opcompare2(op.lastElement(), arr[i])) {
- switch (op.pop()) {
- case "+":
- val.push(val.pop() + val.pop());
- continue;
- case "-":
- double c = val.pop();
- double d = val.pop();
- val.push(d - c);
- continue;
- case "*":
- val.push(val.pop() * val.pop());
- continue;
- case "/":
- double a = val.pop();
- double b = val.pop();
- val.push(b / a);
- continue;
- default:
- break;
- }
- }
- op.push(arr[i]);
- }
- else
- val.push(Double.parseDouble(arr[i]));
- }
- while (!op.isEmpty()) {
- switch (op.pop()) {
- case "+":
- val.push(val.pop() + val.pop());
- break;
- case "-":
- double c = val.pop();
- double d = val.pop();
- val.push(d - c);
- break;
- case "*":
- val.push(val.pop() * val.pop());
- break;
- case "/":
- double a = val.pop();
- double b = val.pop();
- val.push(b / a);
- break;
- default:
- break;
- }
- }
- return val.pop();
- }
- public static String bracketGet(String s, int k) {
- int m=0;
- int i;
- String[] arr=convert(s);
- for(i=k;i<arr.length;i++){
- if(arr[i].equals("(")){
- m++;
- continue;
- }
- if(arr[i].equals(")")){
- m--;
- if(m==0)
- break;
- else
- continue;
- }
- }
- StringBuilder sb=new StringBuilder();
- for(int j=k+1;j<i;j++){
- sb.append(arr[j]);
- }
- return sb.toString();
- }
- public static String[] convert(String s) {
- ArrayList<String> arrayList = new ArrayList<>();
- for (int i = 0; i < s.length(); i++) {
- if (!opjudge(s.charAt(i))) {
- int j = i;
- while ((i < s.length()) && !opjudge(s.charAt(i)))
- i++;
- arrayList.add(s.substring(j, i));
- i--;
- } else
- arrayList.add(String.valueOf(s.charAt(i)));
- }
- return arrayList.toArray(new String[arrayList.size()]);
- }
- public static boolean opjudge(char c) {
- if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
- return true;
- else
- return false;
- }
- public static int opvalue2(String s) {
- switch (s) {
- case "+":
- return 1;
- case "-":
- return 2;
- case "*":
- return 3;
- case "/":
- return 4;
- default:
- return -1;
- }
- }
- public static boolean opcompare2(String s1, String s2) {
- if (opvalue2(s1) >= opvalue2(s2))
- return true;
- else {
- return false;
- }
- }
- import java.util.ArrayList;
最近学习到后缀表达式,于是基于后缀表达式的思想,写了一个四则运算解释器,输入字符串类型的四则运算表达式,可以直接得到结果,支持括号嵌套.
实现时主要考虑以下两点:
- 字符串中运算符和数字分离
- 运算符优先级
- 括号的嵌套
- 运算符和数字分离:可以考虑用字符串数组存储
- 关于运算符优先级,最开始的想法是将乘除法看作一类,加减法看作一类,乘除法的优先级大于加减法,相同类型的运算符按照从左到右顺序依次计算.
- 括号的嵌套:由于括号内部本身也是表达式,因此可以使用递归处理,但重点在于括号的配对,由于使用递归,所以希望获取最大的嵌套单元.
具体实现:
首先实现运算符识别的代码,判断一个字符是否为运算符:
public static boolean opjudge(char c) {
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
return true;
else
return false;
}
然后实现字符串表达式转为字符串数组, 由于要求输入的字符串运算符和数字之间没有空格,所以直接拆分不方便,而且运算符和数字的数目不确定,因此先使用ArrayList存储,然后再转成字符串数组,主要方法是遍历字符串中每一个字符并判断是否为运算符,如果是运算符,则直接将运算符转为字符串加入ArrayList,如果不是,则从此位置继续查找到下一个运算符出现的位置,两个位置之间即为操作数,使用substring截取字符串存入Arraylist,具体实现如下:
public static String[] convert(String s) {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if (!opjudge(s.charAt(i))) {
int j = i;
while ((i < s.length()) && !opjudge(s.charAt(i)))
i++;
arrayList.add(s.substring(j, i));
i--;
} else
arrayList.add(String.valueOf(s.charAt(i)));
}
return arrayList.toArray(new String[arrayList.size()]);
:
public static void main(String[] args) {
double a=(1+2*(3.0/2)/(2+8)-(2*6.0)/(1+7))-(21+3*(5-2-(7*(4-3))));
System.out.println(a);
System.out.println(caculate2("(1+2*(3/2)/(2+8)-(2*6)/(1+7))-(21+3*(5-2-(7*(4-3))))"));
}
//output:
-9.2
-9 }
//对运算符优先级进一步排序 减法大于加法 除法大于乘法
public static double caculate2(String formula) {
String[] arr = convert(formula);
Stack<Double> val = new Stack<>();
Stack<String> op = new Stack<>();
for (int i = 0; i < arr.length; i++) {
if (arr[i].equals("(")) {
val.push(caculate2(bracketGet(formula, i)));
i = i + bracketGet(formula, i).length() + 1;
} else if (arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/")) {
while (!op.isEmpty() && opcompare2(op.lastElement(), arr[i])) {
switch (op.pop()) {
case "+":
val.push(val.pop() + val.pop());
continue;
case "-":
double c = val.pop();
double d = val.pop();
val.push(d - c);
continue;
case "*":
val.push(val.pop() * val.pop());
continue;
case "/":
double a = val.pop();
double b = val.pop();
val.push(b / a);
continue;
default:
break;
}
}
op.push(arr[i]);
}
else
val.push(Double.parseDouble(arr[i]));
}
while (!op.isEmpty()) {
switch (op.pop()) {
case "+":
val.push(val.pop() + val.pop());
break;
case "-":
double c = val.pop();
double d = val.pop();
val.push(d - c);
break;
case "*":
val.push(val.pop() * val.pop());
break;
case "/":
double a = val.pop();
double b = val.pop();
val.push(b / a);
break;
default:
break;
}
}
return val.pop();
}
public static String bracketGet(String s, int k) {
int m=0;
int i;
String[] arr=convert(s);
for(i=k;i<arr.length;i++){
if(arr[i].equals("(")){
m++;
continue;
}
if(arr[i].equals(")")){
m--;
if(m==0)
break;
else
continue;
}
}
StringBuilder sb=new StringBuilder();
for(int j=k+1;j<i;j++){
sb.append(arr[j]);
}
return sb.toString();
}
public static String[] convert(String s) {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if (!opjudge(s.charAt(i))) {
int j = i;
while ((i < s.length()) && !opjudge(s.charAt(i)))
i++;
arrayList.add(s.substring(j, i));
i--;
} else
arrayList.add(String.valueOf(s.charAt(i)));
}
return arrayList.toArray(new String[arrayList.size()]);
}
public static boolean opjudge(char c) {
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
return true;
else
return false;
}
public static int opvalue2(String s) {
switch (s) {
case "+":
return 1;
case "-":
return 2;
case "*":
return 3;
case "/":
return 4;
default:
return -1;
}
}
public static boolean opcompare2(String s1, String s2) {
if (opvalue2(s1) >= opvalue2(s2))
return true;
else {
return false;
}
JAVA四则运算字符串解释器的更多相关文章
- Java常量字符串String理解
Java常量字符串String理解 以前关于String的理解仅限于三点:1.String 是final类,不可继承2.String 类比较字符串相等时时不能用“ == ”,只能用 "eq ...
- Java String字符串/==和equals区别,str。toCharAt(),getBytes,indexOf过滤存在字符,trim()/String与StringBuffer多线程安全/StringBuilder单线程—— 14.0
课程概要 String 字符串 String字符串常用方法 StringBuffer StringBuilder String字符串: 1.实例化String对象 直接赋值 String str=& ...
- java截取字符串中的数字
java从字符串中提取数字 随便给你一个含有数字的字符串,比如: String s="eert343dfg56756dtry66fggg89dfgf"; 那我们如何把其中的数字提取 ...
- 三张图彻底了解Java中字符串的不变性
转载: 三张图彻底了解Java中字符串的不变性 定义一个字符串 String s = "abcd"; s中保存了string对象的引用.下面的箭头可以理解为"存储他的引用 ...
- java中字符串的非空判断
问题如下:在java 中 字符串为null 如何判断String str;if(str==null) ??str.equal("null") ?? 答:我觉得应该搞清楚字符串对象和 ...
- java中字符串String 转 int(转)
java中字符串String 转 int String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法 ...
- Java:字符串类String的功能介绍
在java中,字符串是一个比较常用的类,因为代码中基本上处理的很多数据都是字符串类型的,因此,掌握字符串类的具体用法显得很重要了. 它的主要功能有如下几种:获取.判断.转换.替换.切割.字串的获取.大 ...
- Java空字符串与null的区别和判断字符串是否为空的方法
Java空字符串与null的区别: 1.类型null表示的是一个对象的值,而并不是一个字符串.例如声明一个对象的引用,String a = null ;""表示的是一个空字符串,也 ...
- android112 jni 把java的字符串转换成c的字符串,数组处理
package com.itheima.charencode; import android.os.Bundle; import android.app.Activity; import androi ...
随机推荐
- scanf的一个问题(暂未解决)
如下代码,没有按照预想的那样运行: int a; char b; printf("input a integer\n"); scanf("%d", &a ...
- TOP排行新闻列表。
效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- C# mongoDB Driver 使用对象方式查询语法大全
#region 查询方法 /// <summary> /// 获取单个对象 /// </summary> /// <typeparam name="T" ...
- ThreadLocal、Volatile、synchronized、Atomic
前言 对于ThreadLocal.Volatile.synchronized.Atomic这四个关键字,我想一提及到大家肯定都想到的是解决在多线程并发环境下资源的共享问题,但是要细说每一个的特点.区别 ...
- Python divmod方法
有95条数据 每十条存一页 all_item = 95 pager = 10 result = all_item.__divmod__(pager) print(result) (9{商},5{余数} ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1787 题意概括 有一棵节点为n个(n≤500000)的树.接下来m次询问(m≤500000),每次 ...
- 8. 博客系统| 富文本编辑框和基于bs4模块防御xss攻击
views.py @login_required def cn_backend(request): article_list = models.Article.objects.filter(user= ...
- MQ确认机制之事务机制----confirm串行
一:介绍 1.说明原理 A:生产者将信道设置成confirm模式,一旦信道进到confirm模式,所有该信道上发布的消息都会被指派一个唯一的ID(从1开始). 一旦消息被投递到所有匹配的队列后,bro ...
- MXNet 中的 hybird_forward 的一个使用技巧
from mxnet.gluon import nn from mxnet import nd class SliceLike(nn.HybridBlock): def __init__(self, ...
- 机器学习 支持向量机(SVM) 从理论到放弃,从代码到理解
基本概念 支持向量机(support vector machines,SVM)是一种二分类模型,它的基本模型是定义在特征空间上的间隔最大的线性分类器.支持向量机还包括核技巧,这使它成为实质上的非线性分 ...