我要實作一個效果:
控制臺:
2012.223+3132.21*{91-[33/(23+43.312)]+31}-99
=382 484.1065722
輸入運算式,非法回復=Error,合法回復結果:
現在已經實作匹配數字(可帶+/-、小數和小數點等,不可帶i(虛數))
ExpressionEvaluation.Main.java
package ExpressionEvaluation;
import java.util.*;
import java.util.regex.*;
import tree.*; // tree.Node.java和tree.Tree.java自己編的
public class Main
{
public static void output(String line)
{
System.out.println(line);
}
public static String input()
{
Scanner s = new Scanner(System.in);
String line = s.nextLine();
return line;
}
public static boolean match(Pattern ptn, String str)
{
Matcher m = ptn.matcher(str);
boolean flag = m.matches();
return flag;
}
public static void main(String args[])
{
String number_str = "(\\+|-)?([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])";
Pattern number = Pattern.compile(number_str);
String str = input();
System.out.println(match(number, str));
}
}
tree.Node.java
package tree;
import java.util.ArrayList;
public class Node<type>
{
public type data = null;
public Node<type> father = null;
public ArrayList<Node<type>> children = new ArrayList<Node<type>>();
@Override
public String toString()
{
return data.toString();
}
public void addChild(Node<type> child)
{
children.add(child);
}
public void addChild(Node<type> child, int index)
{
children.add(index, child);
}
public void setChild(Node<type> child, int index)
{
children.set(index, child);
}
public Node<type> getChild(int index)
{
return children.get(index);
}
}
tree.Tree.java
package tree;
import java.util.ArrayList;
import tree.Node;
public class Tree<type>
{
private Node<type> root = new Node<type>();
public Tree()
{
// pass
}
public Tree(type rdata)
{
root.data = rdata;
}
public Node<type> getRoot()
{
return root;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/261538.html
標籤:Eclipse
