간단 수식계산기

 | JAVA
2012. 5. 15. 18:35
Parse postfix arithmetic expressions
 
Parse
 
import java.io.IOException;
 
public class ParsePost {
  private Stack theStack;
 
  private String input;
 
  public ParsePost(String s) {
    input = s;
  }
 
  public int doParse() {
    theStack = new Stack(20);
    char ch;
    int j;
    int num1, num2, interAns;
 
    for (j = 0; j < input.length(); j++) {
      ch = input.charAt(j)
      theStack.displayStack("" + ch + " ");
      if (ch >= '0' && ch <= '9'// if a number push it
        theStack.push((int) (ch - '0'));   
      else // it's an operator
      {
        num2 = theStack.pop();
        num1 = theStack.pop();
        switch (ch) {
        case '+':
          interAns = num1 + num2;
          break;
        case '-':
          interAns = num1 - num2;
          break;
        case '*':
          interAns = num1 * num2;
          break;
        case '/':
          interAns = num1 / num2;
          break;
        default:
          interAns = 0;
        }
        theStack.push(interAns);
      }
    }
    interAns = theStack.pop();
    return interAns;
  }
 
  public static void main(String[] argsthrows IOException {
    String input = "1-2+3*4+5/6-7+8*9";
    int output;
    ParsePost aParser = new ParsePost(input);
    output = aParser.doParse();
    System.out.println("Evaluates to " + output);
  }
  class Stack {
    private int maxSize;
  
    private int[] stackArray;
  
    private int top;
  
    public Stack(int size) {
      maxSize = size;
      stackArray = new int[maxSize];
      top = -1;
    }
  
    public void push(int j) {
      stackArray[++top= j;
    }
  
    public int pop() {
      return stackArray[top--];
    }
  
    public int peek() {
      return stackArray[top];
    }
  
    public boolean isEmpty() {
      return (top == -1);
    }
  
    public boolean isFull() {
      return (top == maxSize - 1);
    }
  
    public int size() {
      return top + 1;
    }
  
    public int peekN(int n) {
      return stackArray[n];
    }
  
    public void displayStack(String s) {
      System.out.print(s);
      System.out.print("Stack (bottom>top): ");
      for (int j = 0; j < size(); j++) {
        System.out.print(peekN(j));
        System.out.print(' ');
      }
      System.out.println("");
    }
  }
}
 

 출처: http://www.java2s.com/Code/Java/Collections-Data-Structure/Parsepostfixarithmeticexpressions.htm

Parse postfix arithmetic expressions
 
Parse
 
import java.io.IOException;
 
public class ParsePost {
  private Stack theStack;
 
  private String input;
 
  public ParsePost(String s) {
    input = s;
  }
 
  public int doParse() {
    theStack = new Stack(20);
    char ch;
    int j;
    int num1, num2, interAns;
 
    for (j = 0; j < input.length(); j++) {
      ch = input.charAt(j)
      theStack.displayStack("" + ch + " ");
      if (ch >= '0' && ch <= '9'// if a number push it
        theStack.push((int) (ch - '0'));   
      else // it's an operator
      {
        num2 = theStack.pop();
        num1 = theStack.pop();
        switch (ch) {
        case '+':
          interAns = num1 + num2;
          break;
        case '-':
          interAns = num1 - num2;
          break;
        case '*':
          interAns = num1 * num2;
          break;
        case '/':
          interAns = num1 / num2;
          break;
        default:
          interAns = 0;
        }
        theStack.push(interAns);
      }
    }
    interAns = theStack.pop();
    return interAns;
  }
 
  public static void main(String[] argsthrows IOException {
    String input = "1-2+3*4+5/6-7+8*9";
    int output;
    ParsePost aParser = new ParsePost(input);
    output = aParser.doParse();
    System.out.println("Evaluates to " + output);
  }
  class Stack {
    private int maxSize;
  
    private int[] stackArray;
  
    private int top;
  
    public Stack(int size) {
      maxSize = size;
      stackArray = new int[maxSize];
      top = -1;
    }
  
    public void push(int j) {
      stackArray[++top= j;
    }
  
    public int pop() {
      return stackArray[top--];
    }
  
    public int peek() {
      return stackArray[top];
    }
  
    public boolean isEmpty() {
      return (top == -1);
    }
  
    public boolean isFull() {
      return (top == maxSize - 1);
    }
  
    public int size() {
      return top + 1;
    }
  
    public int peekN(int n) {
      return stackArray[n];
    }
  
    public void displayStack(String s) {
      System.out.print(s);
      System.out.print("Stack (bottom>top): ");
      for (int j = 0; j < size(); j++) {
        System.out.print(peekN(j));
        System.out.print(' ');
      }
      System.out.println("");
    }
  }
}
 
 

 

 

import java.io.IOException;
 
public class ParsePost {
  private Stack theStack;
 
  private String input;
 
  public ParsePost(String s) {
    input = s;
  }
 
  public int doParse() {
    theStack = new Stack(20);
    char ch;
    int j;
    int num1, num2, interAns;
 
    for (j = 0; j < input.length(); j++) {
      ch = input.charAt(j)
      theStack.displayStack("" + ch + " ");
      if (ch >= '0' && ch <= '9'// if a number push it
        theStack.push((int) (ch - '0'));   
      else // it's an operator
      {
        num2 = theStack.pop();
        num1 = theStack.pop();
        switch (ch) {
        case '+':
          interAns = num1 + num2;
          break;
        case '-':
          interAns = num1 - num2;
          break;
        case '*':
          interAns = num1 * num2;
          break;
        case '/':
          interAns = num1 / num2;
          break;
        default:
          interAns = 0;
        }
        theStack.push(interAns);
      }
    }
    interAns = theStack.pop();
    return interAns;
  }
 
  public static void main(String[] argsthrows IOException {
    String input = "1-2+3*4+5/6-7+8*9";
    int output;
    ParsePost aParser = new ParsePost(input);
    output = aParser.doParse();
    System.out.println("Evaluates to " + output);
  }
  class Stack {
    private int maxSize;
  
    private int[] stackArray;
  
    private int top;
  
    public Stack(int size) {
      maxSize = size;
      stackArray = new int[maxSize];
      top = -1;
    }
  
    public void push(int j) {
      stackArray[++top= j;
    }
  
    public int pop() {
      return stackArray[top--];
    }
  
    public int peek() {
      return stackArray[top];
    }
  
    public boolean isEmpty() {
      return (top == -1);
    }
  
    public boolean isFull() {
      return (top == maxSize - 1);
    }
  
    public int size() {
      return top + 1;
    }
  
    public int peekN(int n) {
      return stackArray[n];
    }
  
    public void displayStack(String s) {
      System.out.print(s);
      System.out.print("Stack (bottom>top): ");
      for (int j = 0; j < size(); j++) {
        System.out.print(peekN(j));
        System.out.print(' ');
      }
      System.out.println("");
    }
  }
}
 

 

이 글은 스프링노트에서 작성되었습니다.

'JAVA' 카테고리의 다른 글

어트리뷰트(애플릿)  (0) 2012.05.15
실시간 파일 변경 알림  (0) 2012.05.15
String(문자열)내의 한글 확인  (0) 2012.05.15
Socket Simple 채팅 프로그램  (0) 2012.05.15
SMS 보내기  (0) 2012.05.15
Posted by 사라링
BLOG main image
.. by 사라링

카테고리

사라링님의 노트 (301)
JSP (31)
J-Query (41)
JAVA (24)
디자인패턴 (1)
스트러츠 (3)
안드로이드 (11)
오라클 (45)
우분투-오라클 (1)
이클립스메뉴얼 (6)
스프링3.0 (23)
자바스크립트 (10)
HTML5.0 (17)
정보처리기사 (1)
기타(컴퓨터 관련) (1)
문제점 해결 (3)
프로젝트 (2)
AJAX (4)
하이버네이트 (3)
트러스트폼 (11)
Jeus (2)
재무관리(회계) (5)
정규식 (5)
아이바티스 (8)
취미 (2)
소프트웨어 보안 관련모음 (0)
정보보안기사 (6)
C언어 베이직 및 프로그램 (3)
보안 관련 용어 정리 (2)
넥사크로 (6)
웹스퀘어_ (0)
Total :
Today : Yesterday :