?? formula.java
字號:
/*
* Created on 2006-9-10
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package calculator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Formula {
// 兩類運算:
//1.*,/ 產生的 2.括號和一級運算+,-
private static BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut =
new PrintWriter(System.out,true);
private static PrintWriter stdErr =
new PrintWriter(System.out,true);
Operator operator = new Operator('+');
LinkedList list = new LinkedList();
double value;
int sort = 0;
public Formula() {}
public Formula(double number) {
value = number;
}
public Formula(Operator operator) {
this.operator = operator;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
public void setValue(double number) {
value = number;
}
public void setValue(Formula formula) {
value = formula.getValue();
}
public void setSort(int sort) {
this.sort = sort;
}
public void add(Formula formula) {
list.add(formula);
}
public double count(double i) {
return operator.count(i, getValue());
}
// 獲取運算值
public double getValue() {
if (sort == 0) {
return value;
} else {
ListIterator listIter = list.listIterator();
Formula formula = (Formula) listIter.next();
double i = formula.getValue();
while (listIter.hasNext()) {
formula = (Formula) listIter.next();
i = formula.count(i);
}
return i;
}
}
public boolean load(Operator oper) {
Operator operator = oper;
while(Analyze.analyze.hasNext()) {
Object obj = Analyze.analyze.nextElement();
if(obj instanceof Bracket) {
Bracket bracket = (Bracket) obj;
if( bracket.isLeftBracket() ) {
Formula buffer = new Formula();
buffer.setSort(1);
buffer.setOperator(operator);
operator = null;
if(!buffer.load(operator)) {
return false;
}
list.add(buffer);
} else if( sort == 1 ) {
return true;
} else return false;
} else if(obj instanceof Operator) {
if(operator != null) {
return false;
}
operator = (Operator) obj;
if( operator.isMAndD() && sort != 2 ) {
Formula buffer = new Formula();
buffer.setSort(2);
if(list.isEmpty()) {
return false;
}
if( list.getLast() instanceof Formula) {
buffer.setOperator(((Formula) list.getLast()).operator);
} else
return false;
buffer.add((Formula) list.getLast());
if(!buffer.load(operator)) {
return false;
}
list.removeLast();
list.add(buffer);
operator = null;
} else if(sort == 2 && !operator.isMAndD()){
Analyze.analyze.previousElement();
return true;
}
add(new Formula(0));
} else if(obj instanceof Formula) {
if(!list.isEmpty() && operator == null) {
return false;
}
Formula formula = (Formula) obj;
formula.setOperator(operator);
list.add(formula);
operator = null;
}
}
return true;
}
public String toString() {
return "" + getValue();
}
public String debug() {
return null;
}
public static void main(String args[]) {
stdOut.println("進行四則運算(+,-,*,/,÷,并可輸入括號)");
stdOut.print("請輸入一個表達式:");
stdOut.flush();
String analyzeString = "";
try {
analyzeString = stdIn.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Formula f = new Formula();
f.setSort(1);
Analyze.analyze = new Analyze(analyzeString);
stdOut.flush();
if(!Analyze.analyze.statrtParse()) {
stdErr.println("輸入了除括號外的其他符號!");
return;
}
if(!f.load(null)) {
stdErr.println();
stdErr.println("輸入不合法!");
return;
}
stdOut.println("答案為: "+f.getValue());
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -