import java.util.Stack;
public class StackTest {
private final String[] sym = { "Nand", "Nor", "Or", "And" };
private Stack opNumber = new Stack();
private Stack opSym = new Stack();
public void parseSource(String source) {
char sources[] = source.toCharArray();
for (int i = 0; i < sources.length; i++) {
if ('(' == sources[i]) {
} else if (')' == sources[i]) {
} else if (',' == sources[i]) {
} else {
int j = i;
for (; ((sources[j] >= 'A' && sources[j] <= 'Z') || (sources[j] >= 'a' && sources[j] <= 'z')); j++);
String value = source.substring(i,j);
Item item = new Item();
item.setValue(value);
boolean exist = false;
for(String sy : sym) {
if(sy.equals(value)) {
opSym.push(item);
exist = true;
break;
}
}
if(!exist) {
opNumber.push(item);
}
i = j;
}
}
}
private void calculate() {
StringBuilder sb = new StringBuilder();
while (opSym.size() > 0) {
Item item = (Item) opSym.pop();
Item first = (Item) opNumber.pop();
Item second = (Item) opNumber.pop();
if (sym[0].equals(item.getValue())) {
sb.append(second.getValue()).append(" Nand ").append(first.getValue());
} else if (sym[1].equals(item.getValue())) {
sb.append(second.getValue()).append(" Nor ").append(first.getValue());
} else if (sym[2].equals(item.getValue())) {
sb.append(second.getValue()).append(" Or ").append(first.getValue());
} else if (sym[3].equals(item.getValue())) {
sb.append(second.getValue()).append(" And ").append(first.getValue());
}
Item value = new Item();
value.setValue(sb.toString());
sb = new StringBuilder();
opNumber.push(value);
}
System.out.println(((Item) opNumber.pop()).getValue());
}
class Item {
String value;
int priority;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
}
public static void main(String[] args) {
StackTest stackTest = new StackTest();
stackTest.parseSource("Nand(yu,Nor(B,C))");
stackTest.calculate();
}
}
http://download.csdn.net/detail/yjflinchong/3144840
http://www.cnblogs.com/kevintian/articles/1086994.html
http://code.google.com/p/morphia/wiki/QuickStart
http://code.google.com/docreader/#p=morphia&s=morphia&t=Datastore
package com.test;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.bson.types.ObjectId;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.query.Query;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.test.entity.MyEntity;
public class MorphiaFindData {
public static void main(String[] args) throws UnknownHostException, MongoException {
Mongo mongo = new Mongo("localhost", 27017);
Datastore ds = new Morphia().createDatastore(mongo,"myDB");
// 通过ObjectId来查询
// MyEntity entity = ds.get(MyEntity.class,new ObjectId("4edcd4f35c65f882738a5da8"));
// System.out.println(entity.getName());
// MyEntity enty = new MyEntity();
// enty.setId(new ObjectId("4edcd4f35c65f882738a5da8"));
// MyEntity g = ds.get(enty)
// System.out.println(g.getName());
// List<ObjectId> lists = new ArrayList<ObjectId>();
// lists.add(new ObjectId("4edcd4f35c65f882738a5da8"));
// lists.add(new ObjectId("4edcdd455c653ebc1027bdf6"));
// Query<MyEntity> querys = ds.get(MyEntity.class,(Iterable<ObjectId>)lists);
// List<MyEntity> result = querys.asList();
// for(MyEntity mEntity : result) {
// System.out.println("Name: " + mEntity.getName());
// }
ds.find(MyEntity.class).get();//得到查询集中的第一个实体
//从返回的结果集中在根据选择条件选择相应的结果
Query<MyEntity> queries = ds.find(MyEntity.class);
Iterator<MyEntity> iterator = queries.iterator();
//在使用queries.iterator()方法时Mophia才会真的去访问Mongodb获取数据
while(iterator.hasNext()) {
System.out.println("Name: " + iterator.next().getName());
}
}
}