편집 기록

편집 기록
  • 프로필 알 수 없는 사용자님의 편집
    날짜2018.04.20

    자바 스택 자료구조 후위표기식과 연산 질문드립니다!


    자바 입문자입니다 후위표기식의 연산값이 원하는대로 안나오네요 도와주세요 ㅠㅠ

    제가원하는값은 6인데 -48이 나옵니다 ㅠㅠ

    class Node{

    Object data; Node link;
    
    Node(Object data){
        this.data = data;
        this.link = null;
    }
    

    }

    class LinkedStack{

    Node top;
    
    LinkedStack(){
        this.top = null;
    }
    boolean isEmpty() {
        return top==null;
    }
    void push(Object data) {
        Node newNode = new Node(data);
        newNode.link = top;
        top = newNode;
    }
    Object pop() {
        if(isEmpty()) {
            System.out.println("no data");
            return 0;
        }else {
            Node temp = top;
            top = temp.link;
            return temp.data;
        }
    }
    void delete() {
        if(isEmpty()) {
            System.out.println("no data");
        }else {
            top = top.link;
        }
    }
    Object peek() {
        if(isEmpty()) {
            System.out.println("no data");
            return 0;
        }else {
            return top.data;
        }
    }
    void printStack() {
        if(isEmpty()) {
            System.out.println("no data");
        }else {
            Node temp = top;
            while(temp != null) {
                System.out.print(temp.data + " ");
                temp = temp.link;
            }
            System.out.println();
        }
    }
    

    }

    class Operate{

    public static char[] toPostfix(String infix) {
        char testCh;
        int stackSize = infix.length();
        char[] postfix = new char[stackSize];
        int j=0;
        LinkedStack s = new LinkedStack();
    
        for(int i=0; i<stackSize; i++) {
            testCh = infix.charAt(i);
            switch(testCh) {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    postfix[j] = testCh;
                    j++;
                    break;
                case '+':
                case '-':
                case '*':
                case '/':
                    s.push(testCh);
                    break;
                case ')':
                    postfix[j] = (char)s.pop();
                    j++;
                    break;
                default:
            }
        }
        postfix[j] = (char)s.pop();
        return postfix;
    }
    
    public static int calPostfix(char[] postfix) {
        int opr1;
        int opr2;
        int result=0;
        LinkedStack s = new LinkedStack();
    
        for(int i=0; i<postfix.length; i++) {
            if(postfix[i]!='+' && postfix[i]!='-' && postfix[i]!='*' && postfix[i]!='/') {
                s.push(postfix[i]-'0');
            }else {
                if(postfix[i]=='+') {
                    opr2 = (int)s.pop();
                    opr1 = (int)s.pop();
                    s.push(opr1+opr2);
                }else if(postfix[i]=='-') {
                    opr2 = (int)s.pop();
                    opr1 = (int)s.pop();
                    s.push(opr1-opr2);
                }else if(postfix[i]=='*') {
                    opr2 = (int)s.pop();
                    opr1 = (int)s.pop();
                    s.push(opr1*opr2);
                }else {
                    opr2 = (int)s.pop();
                    opr1 = (int)s.pop();
                    s.push(opr1/opr2);
                }
            }
        }
        result = (int)s.pop();
        return result;
    }
    

    }

    public class HomeWork_01{

    public static void main(String[] args) {
        String infix = "(3*5)-((6/2)+(3*2))";
        System.out.println(infix);
    
        char[] postfix = Operate.toPostfix(infix);
        System.out.println(postfix);
    
        int result = Operate.calPostfix(postfix);
        System.out.println(result);
    }
    

    }

  • 프로필 김창규님의 편집
    날짜2018.04.15

    자바 스택 자료구조 후위표기식과 연산 질문드립니다!


    자바 입문자입니다 후위표기식의 연산값이 원하는대로 안나오네요 도와주세요 ㅠㅠ

    제가원하는값은 6인데 -48이 나옵니다 ㅠㅠ

    class Node{

    Object data; Node link;
    
    Node(Object data){
        this.data = data;
        this.link = null;
    }
    

    }

    class LinkedStack{

    Node top;
    
    LinkedStack(){
        this.top = null;
    }
    boolean isEmpty() {
        return top==null;
    }
    void push(Object data) {
        Node newNode = new Node(data);
        newNode.link = top;
        top = newNode;
    }
    Object pop() {
        if(isEmpty()) {
            System.out.println("no data");
            return 0;
        }else {
            Node temp = top;
            top = temp.link;
            return temp.data;
        }
    }
    void delete() {
        if(isEmpty()) {
            System.out.println("no data");
        }else {
            top = top.link;
        }
    }
    Object peek() {
        if(isEmpty()) {
            System.out.println("no data");
            return 0;
        }else {
            return top.data;
        }
    }
    void printStack() {
        if(isEmpty()) {
            System.out.println("no data");
        }else {
            Node temp = top;
            while(temp != null) {
                System.out.print(temp.data + " ");
                temp = temp.link;
            }
            System.out.println();
        }
    }
    

    }

    class Operate{

    public static char[] toPostfix(String infix) {
        char testCh;
        int stackSize = infix.length();
        char[] postfix = new char[stackSize];
        int j=0;
        LinkedStack s = new LinkedStack();
    
        for(int i=0; i<stackSize; i++) {
            testCh = infix.charAt(i);
            switch(testCh) {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    postfix[j] = testCh;
                    j++;
                    break;
                case '+':
                case '-':
                case '*':
                case '/':
                    s.push(testCh);
                    break;
                case ')':
                    postfix[j] = (char)s.pop();
                    j++;
                    break;
                default:
            }
        }
        postfix[j] = (char)s.pop();
        return postfix;
    }
    
    public static int calPostfix(char[] postfix) {
        int opr1;
        int opr2;
        int result=0;
        LinkedStack s = new LinkedStack();
    
        for(int i=0; i<postfix.length; i++) {
            if(postfix[i]!='+' && postfix[i]!='-' && postfix[i]!='*' && postfix[i]!='/') {
                s.push(postfix[i]-'0');
            }else {
                if(postfix[i]=='+') {
                    opr2 = (int)s.pop();
                    opr1 = (int)s.pop();
                    s.push(opr1+opr2);
                }else if(postfix[i]=='-') {
                    opr2 = (int)s.pop();
                    opr1 = (int)s.pop();
                    s.push(opr1-opr2);
                }else if(postfix[i]=='*') {
                    opr2 = (int)s.pop();
                    opr1 = (int)s.pop();
                    s.push(opr1*opr2);
                }else {
                    opr2 = (int)s.pop();
                    opr1 = (int)s.pop();
                    s.push(opr1/opr2);
                }
            }
        }
        result = (int)s.pop();
        return result;
    }
    

    }

    public class HomeWork_01{

    public static void main(String[] args) {
        String infix = "(3*5)-((6/2)+(3*2))";
        System.out.println(infix);
    
        char[] postfix = Operate.toPostfix(infix);
        System.out.println(postfix);
    
        int result = Operate.calPostfix(postfix);
        System.out.println(result);
    }
    

    }