public class calculator {
private int pos;
private String[] str;
public calculator() {
super();
this.pos = 0;
}
public double start(String string){
String regex="(?<!^)((?=[\\+\\-*/()])|(?<=[*/()])|(?<=[\\+\\-])(?<![\\+\\-*/(][\\+\\-])(?<!^[\\+\\-]))";
String[] result= string.split(regex);
this.str=result;
double a=plusMinus();
return a;
}
public double plusMinus(){
double rnt=multdiv();
if(str[pos].equals("+")){
pos++;
rnt+=plusMinus();
}else if(str[pos].equals("-")){
pos++;
rnt-=plusMinus();
}
return rnt;
}
public double multdiv(){
double rnt=number();
if(str[pos].equals("*")){
pos++;
rnt*=multdiv();
}else if(str[pos].equals("/")){
pos++;
rnt/=multdiv();
}
return rnt;
}
public double number( ){
double rnt=0;
if(str[pos].equals("(")){
pos++;
rnt=plusMinus();
if(!str[pos].equals(")")){
try {
throw new RuntimeException("输入格式有误:缺少\")\"");
} catch (Exception e) {
e.printStackTrace();
}
}else if(str[pos].equals(")")&&pos<str.length-1){
pos++;
}else if(str[pos].equals(")")&&pos==str.length-1){
}
}else if( isNumber(str[pos])&&pos<str.length-1){
rnt=Double.parseDouble(str[pos++]);
}else if(isNumber(str[pos])&&pos==str.length-1){
rnt=Double.parseDouble(str[pos]);
}
return rnt; }
private boolean isNumber(String str){
try{
Double.parseDouble(str);
}catch(NumberFormatException n){
return false;
}
return true;
}
}import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;
public class CalculatorA implements ActionListener{
private JFrame frame;
private JTextField field;
private JButton clear;
private JButton[] allButtons;
public CalculatorA(){
frame=new JFrame("vi-calculator");
field=new JTextField(20);
clear=new JButton("C");
allButtons=new JButton[18];
String str="+-*/()1234567890.=";
for(int i=0;i<str.length();i++){
allButtons[i]=(new JButton(str.substring(i, i+1)));
}
init();
addEventHandle();
}
private void addEventHandle(){
for(int i=0;i<allButtons.length;i++){
allButtons[i].addActionListener(this);
}
clear.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand().trim();
if("0123456789.+-*/()".indexOf(str)!=-1){
field.setText(field.getText()+str);
return;
}
if(str.equals("=")){
calculator cal=new calculator();
double result=cal.start(field.getText());
field.setText(result+"");
return ;
}
if(str.equals("C")){
field.setText("");
return;
}
}
private void init(){
JPanel north=new JPanel();
JPanel center=new JPanel();
north.setLayout(new FlowLayout());
center.setLayout(new GridLayout(3,6));
north.add(field);
north.add(clear);
for(int i=0;i<allButtons.length;i++){
center.add(allButtons[i]);
}
frame.setLayout(new BorderLayout());
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
}
public void show(){
frame.pack();
frame.setLocation(200,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
CalculatorA cal=new CalculatorA();
cal.show();
}
}
以前写的一个计算器,一直都有个问题没解决就放那了,今天想起来问一下
这个计算器在计算1-2+3这样的算式的时候是等于-4,就是先执行2+3,然后在执行前面的那个-号,其实整个程序都是这样从右往左进行计算,1/2*3同理,不知道怎么改进了
private String[] str; public Calculator() {
super();
this.pos = 0;
} public double start(String string) { String regex = "(?<!^)((?=[\\+\\-*/()])|(?<=[*/()])|(?<=[\\+\\-])(?<![\\+\\-*/(][\\+\\-])(?<!^[\\+\\-]))";
String[] result = string.split(regex);
this.str = result;
double a = plusMinus();
return a;
} public double plusMinus() {
double rnt = multdiv();
String nextOp = this.nextOperator(); //读取下一个操作符
if (str[pos].equals("+")) {
pos++;
//如果下一个运算符是具有相同优先级的,则优先计算这个运算符
if (nextOp.equals("+") || nextOp.equals("-")) {
rnt += number();//计算此运算符
this.str[--pos] = rnt + "";//这个地方很重要,将计算结果放入str中,并将pos指向这个结果,这样就可以继续运算
rnt = plusMinus();//从这个计算结果开始继续运算。
}
//对应加减来说,下一个运算符是具有更高优先级,先计算下一个运算符
else {
rnt += plusMinus();
}
} else if (str[pos].equals("-")) {
pos++;
if (nextOp.equals("+") || nextOp.equals("-")) {
rnt -= number();
this.str[--pos] = rnt + "";
rnt = plusMinus();
} else {
rnt -= plusMinus();
}
}
return rnt;
} public double multdiv() {
double rnt = number();
String nextOp = this.nextOperator();
if (str[pos].equals("*")) {
pos++;
if (nextOp.equals("*") || nextOp.equals("/")) {
rnt *= number();
this.str[--pos] = rnt + "";
rnt = multdiv();
} else {
rnt *= multdiv();
}
} else if (str[pos].equals("/")) {
pos++;
if (nextOp.equals("*") || nextOp.equals("/")) {
rnt /= number();
this.str[--pos] = rnt + "";
rnt = multdiv();
} else {
rnt /= multdiv();
}
}
return rnt;
} public double number() {
double rnt = 0;
if (str[pos].equals("(")) {
pos++;
rnt = plusMinus();
if (!str[pos].equals(")")) {
try {
throw new RuntimeException("输入格式有误:缺少\")\"");
} catch (Exception e) {
e.printStackTrace();
}
} else if (str[pos].equals(")") && pos < str.length - 1) {
pos++;
} else if (str[pos].equals(")") && pos == str.length - 1) {
} } else if (isNumber(str[pos]) && pos < str.length - 1) {
rnt = Double.parseDouble(str[pos++]);
} else if (isNumber(str[pos]) && pos == str.length - 1) {
rnt = Double.parseDouble(str[pos]);
}
return rnt; } private boolean isNumber(String str) { try {
Double.parseDouble(str);
} catch (NumberFormatException n) {
return false;
}
return true;
} private String nextOperator() {
int posNext = pos + 1;
while (posNext < str.length && !str[posNext].equals("+") && !str[posNext].equals("-") && !str[posNext].equals("*") && !str[posNext].equals("/") && !str[posNext].equals("(") && !str[posNext].equals(")")) {
posNext++;
}
if (posNext > str.length - 1) {
return "";
}
return str[posNext];
} public static void main(String[] a) {
String string = "1-3+2";
Calculator c = new Calculator();
System.out.println(c.start(string));
}
}