ITeye博客

一个计算表达式的

xycdgc · 2010-05-17 17:05 · Java · 编程语言 · Markdown

老师留了个小作业,做一个能够算表达式的,要求是用截取字符串的方法熟悉java中切割字符串的处理,一种是不考虑+-*/优先级的,另外一种是考虑它们的优先级的,分别将两种情况的结果输出。哎,在学数据结构的时候,运用对应的数据结构真的是很方便解决问题的,没办法,老师这是让对字符串的操作,呵呵。写了下实现了,感觉是自己的第一想法,好多可以改进的。


   package mypack;

import java.io.*;

public class prog2 {
	public static void main(String args[]) {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		boolean input = true;
		String operator = "";
		String s = "";
		while (input) {
			try {
				System.out.print("请输入正确的表达式:");
				s = bf.readLine();

				if (s.matches("[0-9.*/+-]+") == false)
					throw new UnknownOperatorException("Unknown operator!!");
				s = s.concat(" ");

				String nowstring = s;

				operator = s;
				int j = 0;

				double newnumber = 0.0;
				double a = 0.0;
				double b = 0.0;
				String fuhao = "";

				for (int i = 0; i < s.length(); i++) {
					if (j == 0) {

						if (s.substring(i, i + 1).equals("+")
								|| s.substring(i, i + 1).equals("-")
								|| s.substring(i, i + 1).equals("*")
								|| s.substring(i, i + 1).equals("/")) {

							if (i == 0)
								a = 0.0;
							else
								a = Double.parseDouble(s.substring(j, i));

							fuhao = s.substring(i, i + 1);
							System.out.println(a);
							System.out.println(fuhao);
							j = i;
						}
					}

					if (j != 0 && j != i) {
						if (s.substring(i, i + 1).equals("+")
								|| s.substring(i, i + 1).equals("-")
								|| s.substring(i, i + 1).equals("*")
								|| s.substring(i, i + 1).equals("/")
								|| s.substring

								(i, i + 1).equals(" ")) {
							if (s.substring(i, i + 1).equals("-")
									&& s.substring(i - 1, i).equals("-")) {
								b = Double.parseDouble(s
										.substring(j + 1, j + 3));
							}

							else
								b = Double.parseDouble(s.substring(j + 1, i));
							System.out.println(b);
							if (fuhao.equals("+")) {
								newnumber = a + b;
							}
							if (fuhao.equals("-")) {
								newnumber = a - b;
							}
							if (fuhao.equals("*")) {
								newnumber = a * b;
							}
							if (fuhao.equals("/")) {
								if (b == 0)
									throw new MyException("Divided by Zero!!");
								newnumber = a / b;
							}

							String newstring = Double.toString(newnumber);

							String oldstring = s.substring(0, i);
							s = s.replace(oldstring, newstring);

							j = 0;
							i = 0;
						}
					}

				}
				System.out.println("(a)" + operator + "=" + s);
				// b方法
				String oldstring = nowstring;

				int cishu = 0;
				int m = 0;
				j = 0;
				for (int i = 0; i < oldstring.length(); i++) {
					if (cishu == 0) {

						if (oldstring.substring(i, i + 1).equals("*")
								|| oldstring.substring(i, i + 1).equals("/")) {

							j = i;
							for (int p = 0; p < j; p++) {
								if (oldstring.substring(p, p + 1).equals("+")
										|| oldstring.substring(p, p + 1)
												.equals("-")) {
									m = p;
								}
							}

							if (m == 0)
								a = Double.parseDouble(oldstring
										.substring(m, j));
							else
								a = Double.parseDouble(oldstring.substring(
										m + 1, j));

							fuhao = oldstring.substring(j, j + 1);

							cishu = 1;
						}
					}
					if (cishu == 1 && j != i) {
						if (oldstring.substring(i, i + 1).equals("*")
								|| oldstring.substring(i, i + 1).equals("/")

								|| oldstring.substring(i, i + 1).equals("+")
								|| oldstring.substring(i, i + 1).equals("-")
								|| oldstring.substring(i, i + 1).equals(" ")) {

							b = Double.parseDouble(oldstring
									.substring(j + 1, i));

							if (fuhao.equals("+")) {
								newnumber = a + b;
							}
							if (fuhao.equals("-")) {
								newnumber = a - b;
							}
							if (fuhao.equals("*")) {
								newnumber = a * b;
							}
							if (fuhao.equals("/")) {
								if (b == 0)
									throw new MyException("Divided by Zero!!");
								newnumber = a / b;

							}

							String newstring = Double.toString(newnumber);

							String myoldstring = "";
							if (m == 0)

								myoldstring = oldstring.substring(m, i);
							else
								myoldstring = oldstring.substring(m + 1, i);
							oldstring = oldstring.replace(myoldstring,
									newstring);

							i = 0;
							j = 0;
							cishu = 0;
						}
					}

				}

				j = 0;
				for (int i = 0; i < oldstring.length(); i++) {

					if (j == 0) {

						if (oldstring.substring(i, i + 1).equals("+")
								|| oldstring.substring(i, i + 1).equals("-")) {
							if (i == 0)
								a = 0;
							else
								a = Double.parseDouble(oldstring
										.substring(j, i));
							fuhao = oldstring.substring(i, i + 1);

							j = i;
						}
					}
					if (j != 0 && j != i) {
						if (oldstring.substring(i, i + 1).equals("+")
								|| oldstring.substring(i, i + 1).equals("-")

								|| oldstring.substring(i, i + 1).equals(" ")) {

							b = Double.parseDouble(oldstring
									.substring(j + 1, i));

							if (fuhao.equals("+")) {
								newnumber = a + b;
							}
							if (fuhao.equals("-")) {
								newnumber = a - b;
							}

							String newstring = Double.toString(newnumber);

							String myoldstring = oldstring.substring(0, i);
							oldstring = oldstring.replace(myoldstring,
									newstring);

							j = 0;
							i = 0;
						}
					}
				}
				System.out.println("(b)" + operator + "=" + oldstring);
				input = false;
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
	}
}

class UnknownOperatorException extends Exception {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public UnknownOperatorException(String message) {
		super(message);
	}
}

class MyException extends Exception {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MyException(String message) {
		super(message);
	}
}
   

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。