写一个类,包含encode() 和 decode()两个方法,要求用encode()方法完成下列动作,用decode()恢复encode()方法做的操作,还原初始数据,试以下列数据验证
  验证数据当时没记住,只知道是个大概,里面既有\还有_然后就是字母和数字了
  4c_4\3au30       2ab3_4b02    
   
问题一: 如果当前字符是不大于零的数字字符,则将其复制到新的数组中 
问题二: 如果当前字符是数字字符且无后续字符,则将其复制到新的数组中
问题三: 如果当前字符是大于零的数字字符且有后续字符,假设该字符面值为N,则将其后继字符复制N+1次添加到新的数组中
问题四: 在以上每一组后操作后都插入一个下划线字符
问题五: 将字符串中的下划线全部换成\UL

解决方案 »

  1.   

    谁帮忙看看decode()方法怎么写啊,encode()方法已经完成
      

  2.   

    import java.util.regex.Matcher;
    import java.util.regex.Pattern; public class test{

    public static void main(String[] arg){
    System.out.println("0a4c_4\\43p0a3");
    System.out.println(encode("0a4c_4\\43p0a3"));
    System.out.println(decode(encode("0a4c_4\\43p0a3")));
    }
    public static String encode(String inputStr){
    int strLen = inputStr.length();
    StringBuffer result = new StringBuffer();
    String current = "";
    for(int i = 0; i<strLen; i++){
    current = inputStr.substring(i, i+1);
    if(isValid(current)){
    int value = Integer.parseInt(current);
    if(value == 0)
      result = result.append("0");
    else if(i == (strLen -1))
      result = result.append(current);
    else if(inputStr.substring(i+1, i+2) == " ")
      result = result.append(current);
    else{
    for(int j = 0; j < value+1; j++)
        result.append(inputStr.substring(i+1, i+2));
    }
    result = result.append("_");
    }else if(current.equals("_")){
      result.append("_\\UL");
    }else if(!isValid(current)){
    result.append(current);
    }
    }
        return result.toString();
    }

      static boolean isValid(String str) {
          Pattern p = Pattern.compile("[0-9]+");
          Matcher m = p.matcher(str);
          if (!m.matches()) return false;
          return true;
        }
    }
      

  3.   

    找到原题了,如下:
    本题中的函数encode()和decode()分别实现对字符串的变换和复原.变换函数encode()顺序考察已知字符串的字符,按以下规则逐组生成新字符串:
      (1)若已知字符串的当前字符不是数字字符,则复制该字符于新字符串中.
      (2)若已知字符串的当前字符是一个数字字符,且它之后没有后继字符,则简单地将它复制到新字符串中
      (3)若已知字符串的当前字符是一个数字字符,并且还有后继字符,设该数字字符的面值为n,则将它的后继字符(包括后继字符是一个数字字符)重复复制n+1次到新字符串中.
      (4)以上述一次变换为一组,在不同组之间另插入一个下划线'_'用于分隔.例如:encode()函数对字符串26a3t2的变换结果为666_a_tttt_2
      复原函数decode()做变换函数encode()的相反的工作.即复制不连续相同的单个字符,而将一组连续相同的字符(不超过10个)变换成一个用于表示重复次数的数字符和一个重复出现的字符,并在复原过程中掠过变换函数为不同组之间添加的一个下划线字符.
      假定调用变换函数encode()时的已知字符串中不含下划线字符.
      

  4.   

    这是一道考正则表达式的题。
    不是很难,正则也不是一下子就能说清的,所以也不知道如何简单的注释代码。
    如果有兴趣,可以去Google一下,文章很多。我写了个例子,代码如下:
    /*
     * 创建日期 2005-6-18
     */
    package test;import java.lang.StringBuffer;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;/**
     * @author JohnWoo
     */
    public class Test {
        
        // 编码字符串:
        public static String encode( String src ){
            StringBuffer result = new StringBuffer();
            StringBuffer temp = null;
            
            Pattern pattern = Pattern.compile("(\\d)(.)");
            Matcher matcher = pattern.matcher( src );
            boolean found = matcher.find();
            while( found ){
                temp = new StringBuffer();
                for(int j=0;j<=Integer.parseInt( matcher.group(1) );j++) 
                    temp.append( matcher.group(2));
                matcher.appendReplacement( result, temp.toString() );
                result.append("_");
                found = matcher.find();
            }
            matcher.appendTail( result );
            
            return result.toString();
            
        }
            // 解析字符串:
        public static String decode( String src ){
            StringBuffer result = new StringBuffer();
            StringBuffer temp = null;
            
            Pattern pattern = Pattern.compile("(.)(\\1+?)_");
            Matcher matcher = pattern.matcher( src );
            
            boolean found = matcher.find();
            while( found ){
                temp = new StringBuffer();
                temp.append(matcher.group(2).length());
                temp.append( matcher.group(1) );
                matcher.appendReplacement( result, temp.toString() );
               
                found = matcher.find();
            }
            matcher.appendTail( result );
            
            return result.toString();                
        }    public static void main(String[] args) {
            String str = "26a3t2";
            String enc = Test.encode( str );
            System.out.println( enc );
            System.out.println( Test.decode( enc ));
        }
    }