java 代码
- package cacu;
- /**
- * myejb6.0 CacuStack.java
- */
- /**
- * @author sunxboy
- *
- * 10:19:05 AM May 30, 2007
- */
- public class CacuStack {
- CacuNode top;
- public CacuStack() {
- top = null;
- }
- public boolean isEmpty() {
- return top==null;
- }
- public boolean push(int data) {
- CacuNode node = new CacuNode(data);
- if(isEmpty()) {
- top = node;
- return true;
- }else {
- node.next=top;
- top = node;
- return true;
- }
- }
- public int pop() {
- int data = top.data;
- top = top.next;
- return data;
- }
- public static void main(String[] args) {
- CacuStack stack=new CacuStack ();
- stack.push(1);
- stack.push(2);
- stack.push(3);
- stack.push(4);
- stack.pop();
- stack.pop();
- while(!stack.isEmpty())
- {
- System.out.println(stack.pop());
- }
- }
- }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
java 代码
- /**
- *
- */
- package cacu;
- /**
- * @author sunxboy
- *
- */
- public class Cacu {
- CacuStack cs;
- public int cacu(String source) {
- cs = new CacuStack();
- for (int i = 0; i < source.length(); i++) {
- char ch = source.charAt(i);
- // 如果是数字
- if (ch <= '9' && ch >= '0') {
- cs.push(Integer.parseInt(ch+"")); //注意这里不能用Integer.valueOf(ch);
- } else {
- int num2 = cs.pop();
- int num1 = cs.pop();
- switch (ch) {
- case '+':
- cs.push(num1+num2);
- break;
- case '-':
- cs.push(num1-num2);
- break;
- case '*':
- cs.push(num1*num2);
- break;
- case '/':
- cs.push(num1/num2);
- break;
- }
- }
- }
- return cs.pop();
- }
- public static void main(String[] args) {
- InToPost post=new InToPost();
- Cacu cacu=new Cacu();
- // (a+b*c)*(e+f+m+n*j)
- // System.out.println(post.doTrans("1+1"));
- // System.out.println(cacu.cacu(post.doTrans(("1+1"))));
- System.out.println(post.doTrans("(9+2*7)*(5+6+8+9*5)"));
- System.out.println(cacu.cacu(post.doTrans("(9+2*7)*(5+6+8+9*5)")));
- }
- }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
java 代码
- /**
- *
- */
- package cacu;
- /**
- * @author sunxboy
- *
- */
- public class CacuNode {
- int data;
- CacuNode next;
- public CacuNode(int data) {
- this.data = data;
- }
- }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
java 代码
- /**
- * struture Stack.java
- */
- package cacu;
- /**
- * @author sunxboy
- *
- * 10:20:22 AM May 30, 2007
- */
- public class Stack {
- private Node top;
- public Stack() {
- top=null;
- }
- public boolean push(char data) {
- Node newNode = new Node(data);
- if(isEmpty()) {
- top= newNode;
- return true;
- }else {
- newNode.next = top;
- top = newNode;
- return true;
- }
- }
- public char pop(){
- char data = top.data;
- top =top.next;
- return data;
- }
- public boolean isEmpty() {
- return top==null;
- }
- public static void main(String[] args) {
- Stack stack=new Stack();
- stack.push('a');
- stack.push('b');
- stack.push('c');
- stack.push('d');
- stack.pop();
- stack.pop();
- while(!stack.isEmpty())
- {
- System.out.println(stack.pop());
- }
- }
- }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
java 代码
- /**
- * struture Node.java
- */
- package cacu;
- /**
- * @author sunxboy
- *
- * 10:20:13 AM May 30, 2007
- */
- public class Node {
- public char data;
- public Node next;
- public Node(char data) {
- this.data = data;
- }
- }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
java 代码
- /**
- * struture InToPost.java
- */
- package cacu;
- /**
- * @author sunxboy
- *
- * 10:20:04 AM May 30, 2007
- */
- public class InToPost {
- String output = "";
- Stack stack;
- public String doTrans(String source) {
- stack = new Stack();
- for (int i = 0; i < source.length(); i++) {
- char ch = source.charAt(i);
- switch (ch) {
- case '+':
- case '-':
- doOper(ch,1);
- break;
- case '*':
- case '/':
- doOper(ch,2);
- break;
- case '(':
- stack.push(ch);
- break;
- case ')':
- getParent();
- break;
- default:
- output = output + ch;
- break;
- }
- }
- // 将栈中全部取出
- while (!stack.isEmpty()) {
- output = output + stack.pop();
- }
- return this.output;
- }
- private void getParent() {
- while(!stack.isEmpty()) {
- char ch = stack.pop();
- if(ch=='('){
- break;
- }else{
- output=output+ch;
- }
- }
- }
- public void doOper(char ch1, int lev) {
- while (!stack.isEmpty()) {
- char popch = stack.pop();
- if (popch == '(') {
- stack.push(popch);
- break;
- } else {
- int lev2;
- if (popch == '+' || popch == '-') {
- lev2 = 1;
- } else {
- lev2 = 2;
- }
- // 将栈中取出的与传入的比较优先级
- if (lev > lev2) {
- stack.push(popch); // 将低级别的放回去
- break;
- } else {
- output = output + popch;
- }
- }
- }
- stack.push(ch1); //将高级别放在外层
- }
- public static void main(String[] args) {
- InToPost post=new InToPost();
- String temp="(a+b*c)*(e+f+m+n*j)";
- //abc*+ef+m+nj*+*
- System.out.println(post.doTrans(temp));
- }
- }