将中缀操作转换为后缀操作

时间:2016-11-21 03:31:12

标签: java postfix-notation

代码接受中缀操作并将其转换为后缀操作。我已经能够进行转换。我面临的问题是运营商之间的间距。另外,我想知道如何评估后缀操作。

    <bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <constructor-arg value="/login"/>
        <property name="useForward" value="true"/>
    </bean>

    <bean class="com.gigi.web.SpringDataUserDetailsService" id="userService"/>

    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider user-service-ref="userService"/>
        <security:authentication-provider ref="socialAuthenticationProvider" />
    </security:authentication-manager>

    <bean class="org.springframework.social.security.SocialAuthenticationFilter" id="socialAuthenticationFilter">
        <constructor-arg ref="authenticationManager" />
        <constructor-arg ref="userIdSource" />
        <constructor-arg ref="usersConnectionRepository" />
        <constructor-arg ref="connectionRegistry" />

        <property name="postLoginUrl" value="/home" />
        <property name="defaultFailureUrl" value="/login?error&amp;social" />
        <property name="signupUrl" value="/user/signup" />
    </bean>

    <bean class="org.springframework.social.connect.web.ProviderSignInUtils">
        <constructor-arg ref="connectionRegistry"/>
        <constructor-arg ref="usersConnectionRepository"/>
    </bean>

    <bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource" />

    <bean id="socialAuthenticationProvider"
        class="org.springframework.social.security.SocialAuthenticationProvider">
        <constructor-arg ref="usersConnectionRepository" />
        <constructor-arg ref="userService" />
    </bean>

    <bean id="userSocialConnectionService" class="com.gigi.web.UserSocialConnectionController"></bean>

    <bean id="usersConnectionRepository" class="com.gigi.social.SpringDataUsersConnectionRepository">
        <constructor-arg ref="connectionRegistry" />
    </bean>

    <bean id="connectionRegistry"
            class="org.springframework.social.security.SocialAuthenticationServiceRegistry">
        <property name="authenticationServices">
            <list>
                <bean class="org.springframework.social.facebook.security.FacebookAuthenticationService">
                    <constructor-arg value="${facebook.app.id}" />
                    <constructor-arg value="${facebook.app.secret}" />
                    <constructor-arg value="${facebook.app.namespace}" />
                </bean>
            </list>
        </property>
    </bean>

此后缀输出为:

import java.io.IOException;

public class InToPost {
   private Stack theStack;
   private String input;
   private String output = "";
   public InToPost(String in) {
      input = in;
      int stackSize = input.length();
      theStack = new Stack(stackSize);
   }
   public String doTrans() {
      for (int j = 0; j < input.length(); j++) {
         char ch = input.charAt(j);
         switch (ch) {
         case '+': 
         case '-':
            gotOper(ch, 1); 
            break; 
         case '*': 
         case '/':
         case '%':
            gotOper(ch, 2); 
            break; 
         case '(': 
            theStack.push(ch);
            break;
         case ')': 
            gotParen(ch); 
            break;
         default: 
            output = output + ch; 
            break;
         }
      }
      while (!theStack.isEmpty()) {
         output = output + theStack.pop();
      }
      System.out.println(output);
      return output; 
   }
   public void gotOper(char opThis, int prec1) {
      while (!theStack.isEmpty()) {
         char opTop = theStack.pop();
         if (opTop == '(') {
            theStack.push(opTop);
            break;
         }
         else {
            int prec2;
            if (opTop == '+' || opTop == '-')
               prec2 = 1;
            else
               prec2 = 2;
            if (prec2 < prec1) { 
               theStack.push(opTop);
               break;
            }
            else
            output = output + opTop;
         }
      }
      theStack.push(opThis);
   }
   public void gotParen(char ch){ 
      while (!theStack.isEmpty()) {
         char chx = theStack.pop();
         if (chx == '(') 
            break; 
         else
            output = output + chx; 
      }
   }
   public static void main(String[] args) 
   throws IOException {
      String input = "13 + 23 - 42 * 2";
      String output;
      InToPost theTrans = new InToPost(input);
      output = theTrans.doTrans(); 
      System.out.println("Postfix is " + output + '\n');
   }
   class Stack {
      private int maxSize;
      private char[] stackArray;
      private int top;
      public Stack(int max) {
         maxSize = max;
         stackArray = new char[maxSize];
         top = -1;
      }
      public void push(char j) {
         stackArray[++top] = j;
      }
      public char pop() {
         return stackArray[top--];
      }
      public char peek() {
         return stackArray[top];
      }
      public boolean isEmpty() {
         return (top == -1);
     }
   }
}

而我正试图得到一个如下所示的输出:

13  23 + 42  2*-

我无法在输出中分隔运算符。

0 个答案:

没有答案