某公司上机题

java C++ C 或者VB 中任何一种语言实现两个函数encode()decode(),分别实现对字符串的变换和复原.变换函数encode()顺序考察已知字符串的字符,按以下规则逐组生成新字符串:

  (1)若已知字符串的当前字符不是大于0的数字字符,则复制该字符于新字符串中.

  (2)若已知字符串的当前字符是一个数字字符,且它之后没有后继字符,则简单地将它复制到新字符串中

  (3)若已知字符串的当前字符是一个大于0的数字字符,并且还有后继字符,设该数字字符的面值为n,则将它的后继字符(包括后继字符是一个数字字符)重复复制n+1次到新字符串中.

(4)以上述一次变换为一组,在不同组之间另插入一个下划线'_'用于分隔.

(5)若已知字符串中包含有下划线'_',则变换为用 /UL

 

例如:encode()函数对字符串24ab_2t2的变换结果为 444_aaaaa_a_b_/UL_ttt_t_2

   

      复原函数decode()做变换函数encode()的相反的工作.按照上述规则逆运算,变回原来的字 符串。滤调多余的下划线字符。 

/******************************** Coder.java *************************************/

package code;
import java.util.*;
public class Coder {
   
    public static String encode(String temp) {
           String target = "";
           for(int i=0;i<temp.length();i++) {
                   if(temp.charAt(i)>'9' || temp.charAt(i)<'1') {//char = true
                           if(temp.charAt(i)=='_')
                                   target += "//UL";
                           else
                                   target += temp.charAt(i);
                   }
                   else  {
                             if(i!=temp.length()-1) {
                                     String miniStr = "";
                                     int n = Integer.parseInt(temp.charAt(i)+"");
                                     for(int j=0;j<=n;j++) {
                                             miniStr += temp.charAt(i+1);
                                     }
                                     target +=miniStr;
                             }
                             else {
                                      target +=temp.charAt(i);
                             }
                    }
                    if(i!=temp.length()-1)
               target +='_';
           }
      return target;
 }
 

 public static String decode(String origin) {
         String target="";
         String temp="";
         int start=0;
         temp='_'+origin+'_';
         origin = temp;
         ArrayList iflag = new ArrayList();
         for(int i=0;i<temp.length();i++) {
                if(temp.charAt(i)=='_') {
                       if(i==0 || temp.charAt(i-1)!='_' || i==temp.length()-1) {
                              iflag.add(new Integer(i));
                       }
                }
         }
         for(int i=0;i<iflag.size()-1;i++) {
                String subtemp = origin.substring(((Integer)iflag.get(i)).intValue()+1,
                                                                               ((Integer)iflag.get(i+1)).intValue());
                if(subtemp.length()!=1) {
                       if(subtemp.indexOf("//UL")!=-1) {
                              target += "_";
                       } 
                       else {
                              target += subtemp.length()-1;
                       }
                }
                else {
                       target += subtemp;
                }
         }
  
  return target;
 }
}

编码解法比较简单,主要是解码。如果让一百个人写,会有100种做法。

我的解码算法是把原字符串首位都加上一个下滑线(“_ ”),然后逐字节读取字符串,保存起分割作用的下滑线。(注:分割作用的下滑线前一字符必不为下滑线)这样就把原串划分为子串,在反向变换即可得到解码后的字符串。

另附图形界面代码:

/****************************** _View.java ***************************************/

package code;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class _View extends JFrame {
        public _View(String title) {
        super(title);
  
         btn_encode = new JButton("编码");
         btn_decode = new JButton("解码");
         lbl_origin = new JLabel("源  码: ");
         lbl_target = new JLabel("目标码: ");
         lbl_disTarget = new JLabel("");
         lbl_disTarget.setHorizontalAlignment(JLabel.LEFT);
         txt_input = new JTextField(30);
  
         btn_encode.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent e) {
                         String origin = txt_input.getText().trim();
                         String target = Coder.encode(origin);
                         lbl_disTarget.setText(target);
                         validate();
                }
         });
        btn_decode.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                        String origin = txt_input.getText().trim();
                        String target = Coder.decode(origin);
                        lbl_disTarget.setText(target);
                        validate();
                 }
        });
        con.setLayout(gb);
  
          addComponent(lbl_origin,0,0,2,1,15,15,15,5);
          addComponent(txt_input,2,0,4,1,15,15,15,15);
  
          addComponent(lbl_target,0,1,2,1,10,5,15,5);
          addComponent(lbl_disTarget,2,1,4,1,10,5,15,5);
  
          addComponent(btn_encode,2,2,2,1,5,5,10,5);
          addComponent(btn_decode,4,2,2,1,5,10,10,15);
  
          pack();
          //setResizable(false);
          setVisible(true);
 }
 
 public void addComponent(Component c,int gridx,int gridy,int width,int height,int left,int right,int top,int bottom) {
          gbc.gridx = gridx;
          gbc.gridy = gridy;
          gbc.gridwidth = width;
          gbc.gridheight = height;
          inset.left = left;
          inset.right = right;
          inset.top = top;
          inset.bottom = bottom;
  
          gb.setConstraints(c,gbc);
          con.add(c);
 }
 
 private JButton btn_encode;
 private JButton btn_decode;
 private JLabel lbl_origin;
 private JLabel lbl_target;
 private JLabel lbl_disTarget;
 private JTextField txt_input;
 private GridBagLayout gb = new GridBagLayout();
 private GridBagConstraints gbc = new GridBagConstraints();
 private Container con = getContentPane();
 private Insets inset = new Insets(0,0,0,0);
 
}
       

/***************************** Application.java ****************************************/

package code;
import javax.swing.*;
public class Application {
 
        public static void main(String[] args) {
                JFrame app = new _View("编码");
        }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值