⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formula.java

📁 womendoushou woh eou ewoeuwomen wone n
💻 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 + -