Home > Back-end >  There is a problem about Java Regex
There is a problem about Java Regex

Time:02-21

I want to achieve an effect:
Console:
2012.223 + 3132.21 * {91-33/(23 + 43.312) + 31} - 99
484.1065722=382
Input expression, illegal reply=Error, legal reply results:
Now realize matching Numbers (with +/-, decimal and decimal point, etc., shall not take I (imaginary))
ExpressionEvaluation. Main. Java
 
Package ExpressionEvaluation;

import java.util.*;
Import the Java. Util. Regex. *;

The import tree. *; .//the tree Node. Java and tree. The tree. The Java own compiled

Public class Main
{
Public static void the output (String line)
{
System.out.println(line);
}
Public static String input ()
{
Scanner s=new Scanner (System. In);
The String line=s.n extLine ();
The return line.
}
Public static Boolean match (the Pattern PTN, String STR)
{
The Matcher m=PTN Matcher (STR);
A Boolean flag=m.m atches ();
Return flag;
}
Public static void main (String args [])
{
String number_str=(\ \ "+ | -)? (1-9] [\ \ d * \ \.? \ \ d *) | (0 \ \. \ \ d * (1-9]) ";
The Pattern number=Pattern.com running (number_str);

String STR=input ();

System. The out. Println (match (number, STR));
}
}

tree. Node. Java
 
Package tree;

import java.util.ArrayList;

Public class Node
{
The public type data=https://bbs.csdn.net/topics/null;
Public Node Father=null;
Public ArrayList The children=new ArrayList (a);
@ Override
Public String toString ()
{
Return the data. The toString ();
}
Public void addChild (Node Child)
{
Children. The add (child);
}
Public void addChild (Node The child, int index)
{
Children. The add (index, child);
}
Public void setChild (Node The child, int index)
{
Children. The set (index, child);
}
Public Node GetChild (int index)
{
Return the children. Get (index);
}
}

tree. The tree. The Java
 
Package tree;

import java.util.ArrayList;
The import tree. The Node;

Public class Tree
{
Private Node The root=new Node (a);
Public Tree ()
{
//pass
}
Public Tree (type rdata)
{
Root. Data=https://bbs.csdn.net/topics/rdata;
}
Public Node GetRoot ()
{
Return the root;
}
}

  • Related