diff --git a/Calculator/.classpath b/Calculator/.classpath new file mode 100644 index 0000000..8727917 --- /dev/null +++ b/Calculator/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Calculator/.project b/Calculator/.project new file mode 100644 index 0000000..afb56be --- /dev/null +++ b/Calculator/.project @@ -0,0 +1,17 @@ + + + Calculator + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Calculator/.settings/org.eclipse.jdt.core.prefs b/Calculator/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..416f4fb --- /dev/null +++ b/Calculator/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/Calculator/bin/calculator/Calculator.class b/Calculator/bin/calculator/Calculator.class new file mode 100644 index 0000000..072c1d3 Binary files /dev/null and b/Calculator/bin/calculator/Calculator.class differ diff --git a/Calculator/bin/calculator/Stack$StackNode.class b/Calculator/bin/calculator/Stack$StackNode.class new file mode 100644 index 0000000..c57f95f Binary files /dev/null and b/Calculator/bin/calculator/Stack$StackNode.class differ diff --git a/Calculator/bin/calculator/Stack.class b/Calculator/bin/calculator/Stack.class new file mode 100644 index 0000000..1d358d2 Binary files /dev/null and b/Calculator/bin/calculator/Stack.class differ diff --git a/Calculator/src/calculator/Calculator.java b/Calculator/src/calculator/Calculator.java new file mode 100644 index 0000000..148e115 --- /dev/null +++ b/Calculator/src/calculator/Calculator.java @@ -0,0 +1,134 @@ +package calculator; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Vector; + +/** + * 实现对字符串的计算 + * 输入的是后缀表示 + * ABCD-*+EF/- + * 中缀表达式为: + * A+B*(C-D)-E/F + * 这个程序没有进行错误控制 + * 输入字符串时一定要按照标准输入 + * 否则将会出现错误 + * @author majinliang + * + */ +public class Calculator { + + public StringBuffer phrase;//存储当前正在形成的词,用于词法分析 + public String str;//存储整个字符串,用于词法分析 + + public static void main(String[] args) { + Calculator c = new Calculator(); + String str = null; + //从控制台读入一行字符串 + //输入数字只允许整数 + //因为词法分析时,只是对整数进行了考虑 + try { + BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); + str = stdin.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + Vector v = c.phraseAnalysis(str); + /*for(int i = 0; i < v.size(); i++){ + System.out.println(v.get(i)); + }*/ + c.run(v); + } + + /* + * 进行词法分析 + * 两个相邻的数字之间用空格隔开 + * 其他情况可以使用空格隔开,也可以不适用空格隔开 + */ + public Vector phraseAnalysis(String in){ + Vector phraseVector = new Vector(); + str = in; + int position = 0; + phrase = new StringBuffer(); + char ch = str.charAt(position); + while(position < str.length()){ + ch = str.charAt(position); + if(ch == '*'||ch == '-'||ch == '+'||ch == '/'){ + phrase.append(ch); + position= position + 1; + if(position < str.length()) + ch = str.charAt(position); + phraseVector.add(phrase.toString()); + phrase.setLength(0); + }else{ + if(ch >= '0' && ch <= '9'){ + while(ch>='0'&&ch<='9'){ + ch = str.charAt(position); + phrase.append(ch); + position= position + 1; + ch = str.charAt(position); + } + phraseVector.add(phrase.toString()); + phrase.setLength(0); + }else{ + position= position + 1; + + } + } + } + return phraseVector; + } + + + /* + * 判读一个字符串是否为数字 + */ + boolean isNumber(String str){ + int temp = 0; + try{ + temp = Integer.parseInt(str); + }catch(Exception e){ + return false; + } + return true; + } + + + public void run(Vector v){ + Stack stack = new Stack(v.get(0)); + for(int i = 1; i < v.size(); i++){ + if(isNumber(v.get(i))){ + stack.push(v.get(i)); + }else{ + if(v.get(i).equals("+")){ + String str1 = stack.pop(); + String str2 = stack.pop(); + double jieguo = Double.parseDouble(str2) + Double.parseDouble(str1); + stack.push(String.valueOf(jieguo)); + } + if(v.get(i).equals("-")){ + String str1 = stack.pop(); + String str2 = stack.pop(); + double jieguo = Double.parseDouble(str2) - Double.parseDouble(str1); + stack.push(String.valueOf(jieguo)); + } + if(v.get(i).equals("*")){ + String str1 = stack.pop(); + String str2 = stack.pop(); + double jieguo = Double.parseDouble(str2) * Double.parseDouble(str1); + stack.push(String.valueOf(jieguo)); + } + if(v.get(i).equals("/")){ + String str1 = stack.pop(); + String str2 = stack.pop(); + double jieguo = Double.parseDouble(str2) / Double.parseDouble(str1); + stack.push(String.valueOf(jieguo)); + } + } + } + + System.out.println(stack.pop()); + } + +} diff --git a/Calculator/src/calculator/Stack.java b/Calculator/src/calculator/Stack.java new file mode 100644 index 0000000..820cb59 --- /dev/null +++ b/Calculator/src/calculator/Stack.java @@ -0,0 +1,37 @@ +package calculator; +/* + * 栈 + */ +public class Stack { + + /* + * 栈中的节点 + */ + private class StackNode{ + private String data; + private StackNode next; + + public StackNode(String d, StackNode n){ + data = d; + next = n; + } + } + + //开始栈的部分 + private StackNode top; + + public Stack(String d){ + top = new StackNode(d, null); + } + + public void push(String str){ + StackNode node = new StackNode(str, top); + top = node; + } + + public String pop(){ + String str = top.data; + top = top.next; + return str; + } +}