62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.Scanner;
private static String process_and(String content){
content = content.replaceAll("&","");
return content;
}
private static String process_and(String content){
content = content.replaceAll("\\s*&\\s*","");
return content;
}
((hell & o )&wolrd)) 输出:helloworld
((hello @ l)&(a world @ a)) 输出:lo world
/*
首先从系统中获得字符串输入,&运算表示字符串连接,@表示取相应字符串中,特定字母后面的子字符串,但是希望把这些操作混合在一起,初步想到用括号,但是没有想好怎么用,例如:输入:((hell & o )&wolrd)) 输出:helloworld
输入:((hello @ l)&(a world @ a)) 输出:lo world
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test{
public static final int PRIORITY_AND = 1; // & 优先级高
public static final int PRIORITY_AT = 2; // @ 优先级高
public static void main(String[] args){
String content = "((hello @ l)&(a world @ a))";
System.out.println(process(content,PRIORITY_AT));
}
private static String process_and(String content){
content = content.replaceAll("\\s*&\\s*","");
return content;
}
private static String process_at(String content){
//假设可以满足 abcd @ abc ABC= d ABC的情况
String regex = "(.+?)\\s+@\\s+([^\\s]+)(.*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
int index = 0;
String temp1 = null;
String temp2 = null;
while(matcher.find()){
temp1 = matcher.group(1);
temp2 = matcher.group(2);
//System.out.println("temp1:" + temp1);
index = temp1.indexOf(temp2);
//System.out.println("index:" + index);
if(index >= 0){
//如果index >= 0 那么表示group(1)中含有group(2)的内容,截取对应的后面的内容
temp1 = temp1.substring(index + temp2.length(), temp1.length());
}
//content应该是在消除了原来的@表达式后的返回
content = temp1 + matcher.group(3);
matcher = pattern.matcher(content);
}
return content;
}
private static String process1(String content,int priority){//不包含括号的情况下
content = content.replaceAll("[()]","");//此方法将处理单个括号的内容,传值进来时需要处理括号
if(priority == PRIORITY_AND){
content = process_and(content);
content = process_at(content);
}else if(priority == PRIORITY_AT){
content = process_at(content);
content = process_and(content);
}else{
System.err.println("The argument is invalid!");
throw new IllegalArgumentException("The argument of priority = " + priority + " is invalid!");
}
return content;
}
public static String process(String content,int priority){ //含括号的处理
String regex = "(.*?)(\\([^()]+\\))(.*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
content = matcher.group(1) + process1(matcher.group(2),priority) + matcher.group(3);
matcher = pattern.matcher(content);
}
//当所有括号都去除的时候别忘记了需要处理一下最后的部分
content = process1(content,priority);
return content;
}
}
public class Test {
public static void main(String[] args) {
String str = "((hello @ l)&(a world @ a))";
Calculator c = Calculator.getInstance();
System.out.println(c.eval(str));
}
}
import java.util.HashMap;
import java.util.Map;
/**
* 运算符
*/
public abstract class Operator {
/**
* 运算符
*/
private char operator;
/**
* 运算符的优先级别,数字越大,优先级别越高
*/
private int priority;
private static Map<Character, Operator> operators = new HashMap<Character, Operator>();
private Operator(char operator, int priority) {
setOperator(operator);
setPriority(priority);
register(this);
}
private void register(Operator operator) {
operators.put(operator.getOperator(), operator);
}
/**
* 截取运算
*/
public final static Operator ADITION = new Operator('@', 100) {
public String eval(String left, String right) {
int index = left.indexOf(right);
if(index < 0) {
return "";
}
return left.substring(index + right.length());
}
};
/**
* 连接运算
*/
public final static Operator SUBTRATION = new Operator('&', 200) {
public String eval(String left, String right) {
return left + right;
}
};
public char getOperator() {
return operator;
}
private void setOperator(char operator) {
this.operator = operator;
}
public int getPriority() {
return priority;
}
private void setPriority(int priority) {
this.priority = priority;
}
/**
* 根据某个运算符获得该运算符的优先级别
* @param c
* @return 运算符的优先级别
*/
public static int getPrority(char c) {
Operator op = operators.get(c);
if(op != null) {
return op.getPriority();
}
return 0;
}
/**
* 工具方法,判断某个字符是否是运算符
* @param c
* @return 是运算符返回 true,否则返回 false
*/
public static boolean isOperator(char c) {
return getInstance(c) != null;
}
public static boolean isOperator(String str) {
if(str.length() > 1) {
return false;
}
return isOperator(str.charAt(0));
}
/**
* 根据运算符获得 Operator 实例
* @param c
* @return 从注册中的 Operator 返回实例,尚未注册返回 null
*/
public static Operator getInstance(char c) {
return operators.get(c);
}
public static Operator getInstance(String str) {
if(str.length() > 1) {
return null;
}
return getInstance(str.charAt(0));
}
/**
* 根据操作数进行计算
* @param left 左操作数
* @param right 右操作数
* @return 计算结果
*/
public abstract String eval(String left, String right);
}
public interface Symbol {
/**
* 左括号
*/
public final static char LEFT_BRACKET = '(';
/**
* 右括号
*/
public final static char RIGHT_BRACKET = ')';
/**
* 中缀表达式中的空格,需要要忽略
*/
public final static char BLANK = ' ';
/**
* 后缀表达式的各段的分隔符
*/
public final static char SEPARATOR = '|';
}
import java.util.Stack;
import java.util.regex.Pattern;
public class Calculator implements Symbol {
private final static Calculator instance = new Calculator();
private Calculator() {
}
public static Calculator getInstance() {
return instance;
}
/**
* 计算中缀表达式
* @param expression
* @return
*/
public String eval(String expression) {
String str = infix2Suffix(expression);
// System.out.println(" Infix Expression: " + expression);
// System.out.println("Suffix Expression: " + str);
if(str == null) {
throw new IllegalArgumentException("Expression is null!");
}
String[] strs = str.split(Pattern.quote(String.valueOf(SEPARATOR)));
Stack<String> stack = new Stack<String>();
for(int i = 0; i < strs.length; i++) {
strs[i] = strs[i].trim();
if(!Operator.isOperator(strs[i])) {
stack.push(strs[i]);
} else {
Operator op = Operator.getInstance(strs[i]);
String right = stack.pop();
String left = stack.pop();
stack.push( op.eval(left, right) );
}
}
return stack.pop();
}
/**
* 将中缀表达式转换为后缀表达式
* @param expression
* @return
*/
public String infix2Suffix(String expression) {
if(expression == null) {
return null;
}
char[] chs = expression.toCharArray();
Stack<Character> stack = new Stack<Character>();
StringBuilder sb = new StringBuilder(chs.length);
boolean appendSeparator = false;
for(int i = 0; i < chs.length; i++) {
char c = chs[i];
if(appendSeparator) {
sb.append(SEPARATOR);
appendSeparator = false;
}
if(isLeftBracket(c)) {
stack.push(c);
continue;
}
if(isRightBracket(c)) {
while(stack.peek() != LEFT_BRACKET) {
sb.append(SEPARATOR);
sb.append(stack.pop());
}
stack.pop();
continue;
}
if(!Operator.isOperator(c)) {
sb.append(c);
continue;
}
appendSeparator = true;
if(Operator.isOperator(c)) {
if(stack.isEmpty() || stack.peek() == LEFT_BRACKET) {
stack.push(c);
continue;
}
int precedence = Operator.getPrority(c);
while(!stack.isEmpty() && Operator.getPrority(stack.peek()) >= precedence) {
sb.append(SEPARATOR);
sb.append(stack.pop());
}
stack.push(c);
}
}
while(!stack.isEmpty()) {
sb.append(SEPARATOR);
sb.append(stack.pop());
}
return sb.toString();
}
/**
* 判断某个字符是否为左括号
* @param c
* @return
*/
private boolean isLeftBracket(char c) {
return c == LEFT_BRACKET;
}
/**
* 判断某个字符是否为右括号
* @param c
* @return
*/
private boolean isRightBracket(char c) {
return c == RIGHT_BRACKET;
}
}