【编程题】(满分21分)    Excel是最常用的办公软件。每个单元格都有唯一的地址表示。比如:第12行第4列表示为:“D12”,第5行第255列表示为“IU5”。
    
    事实上,Excel提供了两种地址表示方法,还有一种表示法叫做RC格式地址。 第12行第4列表示为:“R12C4”,第5行第255列表示为“R5C255”。    你的任务是:编写程序,实现从RC地址格式到常规地址格式的转换。【输入、输出格式要求】    用户先输入一个整数n(n<100),表示接下来有n行输入数据。    接着输入的n行数据是RC格式的Excel单元格地址表示法。    程序则输出n行数据,每行是转换后的常规地址表示法。    例如:用户输入:
2
R12C4
R5C255    则程序应该输出:
D12
IU5
【注意】    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
    
    请把所有类写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。
    
    相关的工程文件不要拷入。
    
    请不要使用package语句。
    
    源程序中只能出现JDK1.5中允许的语法或调用。不能使用1.6或更高版本。 

解决方案 »

  1.   


    import java.util.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Excel
    {
        final char[] COL =
        { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
        'Z' };    public static void main(String[] args)
        {
    Scanner sc = new Scanner(System.in);
    int count = sc.nextInt();
    if (count <= 0 || count > 100)
    {
        System.out.println("次数超出限制");
        return;
    }
    ArrayList<String> ar = new ArrayList<String>();
    while (count > 0)
    {
        String command = sc.next();
        ar.add(command);
        count--;
    }
    for (String command : ar)
    {
        Excel e = new Excel();
        e.convert(command.trim().toUpperCase());
    }
        }    public void convert(String address)
        { String regex = "[A-Z]+[0-9]*";
    Pattern pt = Pattern.compile(regex);
    Matcher mather = pt.matcher(address);
    ArrayList<String> ar = new ArrayList<String>();
    while (mather.find())
    {
        String s = mather.group();
        ar.add(s);
    }
    if (ar.size() > 0)
    {
        String row = ar.get(0);
        String col = ar.get(1);
        int rowNum = Integer.parseInt(row.substring(1));
        int colNum = Integer.parseInt(col.substring(1));
        String newCol = "";     newCol += COL[colNum % 26 - 1];
        while (colNum / 26 > 0)
        {
    colNum /= 26;
    newCol += COL[colNum % 26 - 1];     }
        String buffer = "";
        for (int i = newCol.length() - 1; i >= 0; i--)
        {
    buffer += newCol.charAt(i);
        }     System.out.println(buffer + rowNum);
    }    }}
      

  2.   

    // From R5C255 to IU5 - 第5行第255列
    public static String transRC(String rc)
    {
    Pattern p = Pattern.compile("[0-9]+");
    Matcher m = p.matcher(rc);
    int line = 0;
    int row = 0;
    if (m.find())
    line = Integer.parseInt(m.group(0));
    if (m.find())
    row = Integer.parseInt(m.group(0));
    System.out.println("LINE: " + line + " ROW: " + row);
    return transCol(row) + line;
    } private static String transCol(int row)
    {
    if (row < 1 || row > 26 * 26)
    {
    return null;
    }
    else if (row <= 26)
    {
    return "" + (char) (row + 'A' - 1);
    }
    else
    {
    return "" + (char) (((row - row % 26) / 26) + 'A' - 1)
    + (char) ((row % 26) + 'A' - 1);
    }
    }
    默认RC方式全是R**C**似的行在前列在后了~