java编写的calculator类
答案:2 悬赏:40 手机版
解决时间 2021-02-19 19:53
- 提问者网友:十年饮冰
- 2021-02-19 10:07
java编写的calculator类
最佳答案
- 五星知识达人网友:长青诗
- 2021-02-19 10:21
import java.util.ArrayList;
import java.util.List;
public class Oper {
private static int num = 0;
private static double number = 0;
private static boolean flag = true;
private static List l = new ArrayList();
public String operator(String str) {
num = 0;
number = 0;
l = new ArrayList();
String[] ss = str.split("=");
str = ss[0];
String nstr = "";
String s = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ')') {
s = "l";
} else if (str.charAt(i) == '(') {
s = "x";
} else {
s = str.charAt(i) + "";
}
nstr += s;
}
for (int j = 0; j < str.length(); j++) {
if (nstr.charAt(j) == 'l') {
num++;
}
}
return this.Stringsplit(nstr);
}
private String Stringsplit(String str) {
// System.out.println(num);
String re = "";
try {
String[] first = str.split("l");
String[] second = first[0].split("x");
try {
second[second.length - 1] = this.bracket(second[second.length - 1]);
} catch (ArrayIndexOutOfBoundsException e) {
}
for (int i = 0; i < second.length; i++) {
if (i == second.length - 1) {
str += second[i];
} else if(i == second.length - 2){
str += second[i] ;
}else {
str += "x"+second[i] ;
}
}
for (int i = 1; i < first.length; i++) {
str += first[i] + "l";
}System.out.println(str);
if (--num > 0) {
this.Stringsplit(str);
}
l.add(str);
re = this.result(l.get(0));
} catch (ArrayIndexOutOfBoundsException e) {
re = this.bracket(str);
}
return re;
}
private String bracket(String str) {
List list = new ArrayList();
List list1 = new ArrayList();
String s = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '.') {
s += ".";
} else {
try {
s += Integer.parseInt(str.charAt(i) + "");
} catch (NumberFormatException e) {
list.add(s);
list1.add(str.charAt(i) + "");
s = "";
}
}
}
list.add(s);
return this.ALG(list, list1);
}
private String ALG(List arg0, List arg1) {
String str = "";
int i = 0;
for (; i < arg1.size(); i++) {
double sum = 0;
if (arg1.get(i).equals("*")) {
sum = Double.parseDouble(arg0.get(i))
* Double.parseDouble(arg0.get(i + 1));
arg0.remove(i + 1);
arg0.add(i + 1, sum + "");
sum = 0;
} else if (arg1.get(i).equals("/")) {
sum = Double.parseDouble(arg0.get(i))
/ Double.parseDouble(arg0.get(i + 1));
arg0.remove(i + 1);
arg0.add(i + 1, sum + "");
sum = 0;
} else {
str += arg0.get(i) + arg1.get(i);
}
}
str += arg0.get(i);
return str;
}
private String result(String str) {
List list = new ArrayList();
List list1 = new ArrayList();
String s = "";
for (int i = 0; i < str.length(); i++) {
try {
if (str.charAt(i) == '.') {
s += str.charAt(i);
} else {
s += Integer.parseInt(str.charAt(i) + "");
}
} catch (NumberFormatException e) {
list.add(s);
list1.add(str.charAt(i) + "");
s = "";
}
}
list.add(s);
for (int i = 0; i < list.size(); i++) {
}
double sum = 0;
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).equals("+")) {
sum = Double.parseDouble(list.get(i))
+ Double.parseDouble(list.get(i + 1));
list.remove(i + 1);
list.add(i + 1, sum + "");
sum = 0;
} else if (list1.get(i).equals("-")) {
sum = Double.parseDouble(list.get(i))
- Double.parseDouble(list.get(i + 1));
list.remove(i + 1);
list.add(i + 1, sum + "");
sum = 0;
}
}
return list.get(list.size() - 1);
}
public String stingsqrt(String str) {
String[] ss = str.split("sqrt");
double sqrtx = Double.parseDouble(ss[0]);
return "" + Math.sqrt(sqrtx);
}
public static void main(String[] args) {
Oper o = new Oper();
System.out.println(o.operator("(1+(1+1))"));
// System.out.println("---:" + num);
}
}
import java.util.List;
public class Oper {
private static int num = 0;
private static double number = 0;
private static boolean flag = true;
private static List
public String operator(String str) {
num = 0;
number = 0;
l = new ArrayList
String[] ss = str.split("=");
str = ss[0];
String nstr = "";
String s = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ')') {
s = "l";
} else if (str.charAt(i) == '(') {
s = "x";
} else {
s = str.charAt(i) + "";
}
nstr += s;
}
for (int j = 0; j < str.length(); j++) {
if (nstr.charAt(j) == 'l') {
num++;
}
}
return this.Stringsplit(nstr);
}
private String Stringsplit(String str) {
// System.out.println(num);
String re = "";
try {
String[] first = str.split("l");
String[] second = first[0].split("x");
try {
second[second.length - 1] = this.bracket(second[second.length - 1]);
} catch (ArrayIndexOutOfBoundsException e) {
}
for (int i = 0; i < second.length; i++) {
if (i == second.length - 1) {
str += second[i];
} else if(i == second.length - 2){
str += second[i] ;
}else {
str += "x"+second[i] ;
}
}
for (int i = 1; i < first.length; i++) {
str += first[i] + "l";
}System.out.println(str);
if (--num > 0) {
this.Stringsplit(str);
}
l.add(str);
re = this.result(l.get(0));
} catch (ArrayIndexOutOfBoundsException e) {
re = this.bracket(str);
}
return re;
}
private String bracket(String str) {
List
List
String s = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '.') {
s += ".";
} else {
try {
s += Integer.parseInt(str.charAt(i) + "");
} catch (NumberFormatException e) {
list.add(s);
list1.add(str.charAt(i) + "");
s = "";
}
}
}
list.add(s);
return this.ALG(list, list1);
}
private String ALG(List
String str = "";
int i = 0;
for (; i < arg1.size(); i++) {
double sum = 0;
if (arg1.get(i).equals("*")) {
sum = Double.parseDouble(arg0.get(i))
* Double.parseDouble(arg0.get(i + 1));
arg0.remove(i + 1);
arg0.add(i + 1, sum + "");
sum = 0;
} else if (arg1.get(i).equals("/")) {
sum = Double.parseDouble(arg0.get(i))
/ Double.parseDouble(arg0.get(i + 1));
arg0.remove(i + 1);
arg0.add(i + 1, sum + "");
sum = 0;
} else {
str += arg0.get(i) + arg1.get(i);
}
}
str += arg0.get(i);
return str;
}
private String result(String str) {
List
List
String s = "";
for (int i = 0; i < str.length(); i++) {
try {
if (str.charAt(i) == '.') {
s += str.charAt(i);
} else {
s += Integer.parseInt(str.charAt(i) + "");
}
} catch (NumberFormatException e) {
list.add(s);
list1.add(str.charAt(i) + "");
s = "";
}
}
list.add(s);
for (int i = 0; i < list.size(); i++) {
}
double sum = 0;
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).equals("+")) {
sum = Double.parseDouble(list.get(i))
+ Double.parseDouble(list.get(i + 1));
list.remove(i + 1);
list.add(i + 1, sum + "");
sum = 0;
} else if (list1.get(i).equals("-")) {
sum = Double.parseDouble(list.get(i))
- Double.parseDouble(list.get(i + 1));
list.remove(i + 1);
list.add(i + 1, sum + "");
sum = 0;
}
}
return list.get(list.size() - 1);
}
public String stingsqrt(String str) {
String[] ss = str.split("sqrt");
double sqrtx = Double.parseDouble(ss[0]);
return "" + Math.sqrt(sqrtx);
}
public static void main(String[] args) {
Oper o = new Oper();
System.out.println(o.operator("(1+(1+1))"));
// System.out.println("---:" + num);
}
}
全部回答
- 1楼网友:痴妹与他
- 2021-02-19 11:34
路过
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯