如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式处理文本和匹配模式是多么简单。 如果你不熟悉这个术语,那么“正则表达式”(Regular Expression)就是一个字符构成的串,它定义了一个用来搜索匹配字符串的模式。 许多语言,
1、AngularJS 表达式 AngularJS 表达式写在双大括号内:{{ expression }}。 AngularJS 表达式把数据绑定到 HTML,这与 ng-bind 指令有异曲同工之妙。 AngularJS 将在表达式书写的位置”输出”数据。 AngularJS 表达式 很像 JavaScript 表达式:它们可以包含文
本来目的是要学一些 解释器的。虽然也看到一些实现,但是感觉对我有点难度,于是从简单开始学习。
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class CaSame {
public List toString(String ss) {
List result = new ArrayList();
String num = "";
for (int i = 0; i < ss.length(); i++) {
if (Character.isDigit(ss.charAt(i)))
num = num + ss.charAt(i);
else {
if (num != "") {
result.add(num);
num = "";
}
result.add(String.valueOf(ss.charAt(i)));
}
}
if (num != "")
result.add(num);
return result;
}
public List Change(List first) {
Stack stack = new Stack();
List result = new ArrayList();
for (int i = 0; i < first.size(); i++) {
String temp = first.get(i);
char tc = temp.charAt(0);
if (Character.isDigit(tc)) {
result.add(temp);
} else {
if ("(".equals(temp)) {
stack.push(temp);
} else if (")".equals(temp)) {
while (!stack.isEmpty() && !stack.peek().equals( "(")) {
result.add(stack.pop());
}
if(!stack.isEmpty())
stack.pop();
} else {
if (!stack.isEmpty() && compare(stack.peek().charAt(0), tc)) {
result.add(stack.pop());
}
stack.push(temp);
}
}
}
while(!stack.isEmpty())
result.add(stack.pop());
return result;
}
public String Computer(List result) {
Stack stack = new Stack();
for(int i=0;i
char c = result.get(i).charAt(0);
if(Character.isDigit(c)){
stack.push(result.get(i));
}else{
int b = Integer.parseInt(stack.pop());
int a= Integer.parseInt(stack.pop());
int r = 0;
switch(c){
case '+':
r = a+b ;
break;
case '-':
r = a-b ;
break;
case '*':
r = a*b ;
break;
case '/':
r = a/b ;
break;
}
stack.push(String.valueOf(r));
}
}
return stack.peek();
}
public Boolean compare(char a, char b) {
char c[] = { '+', '-' };
char d[] = { '*', '/' };
if (in(a, d) && (in(b, c) || in(b, d)))
return true;
if (in(a, c) && in(b, c))
return true;
return false;
}
public Boolean in(char a, char[] b) {
for (int i = 0; i < b.length; i++) {
if (a == b[i])
return true;
}
return false;
}
public static void main(String[] args) {
CaSame cs = new CaSame();
List frist = cs.toString("(23-24)*12)");
// System.out.println(cs.compare('+', '*'));
frist = cs.Change(frist);
for (int i = 0; i < frist.size(); i++) {
System.out.print(frist.get(i));
}
System.out.println();
String result = cs.Computer(frist);
System.out.println(result);
}
}
整个过程包括,转换, 更改顺序,计算。