2012蓝桥杯软件大赛决赛(源码变换)

来源:互联网 发布:雄风3误射大陆反应知乎 编辑:程序博客网 时间:2024/06/09 23:37

第三题


【编程题】(满分22分)

    超文本标记语言(即HTML),是用于描述网页文档的一种标记语言。

    HTML通过文本来描述文档显示出来应该具有的“样子”。它主要通过标签来定义对象的显示属性或行为。

    如果把java的源文件直接拷贝到HTML文档中,用浏览器直接打开,会发现本来整齐有序的源文件变成了一团遭。这是因为,文本中的许多回车和空格都被忽略了。而有些符号在html中有特殊的含义,引起了更复杂的局面。

    为了源文件能正常显示,我们必须为文本加上适当的标签。对特殊的符号进行转义处理。

    常用的有:
    HTML 需要转义的实体:
    &     --->  &
    空格  --->   
    <     --->  &lt;
    >     --->  &gt;
    "     --->  &quot;
    此外,根据源码的特点,可以把 TAB 转为4个空格来显示。
    TAB   --->  &nbsp;&nbsp;&nbsp;&nbsp;

    为了显示为换行,需要在行尾加<br/>标签。

    为了显示美观,对关键字加粗显示,即在关键字左右加<b>标签。比如:

    <b>public</b>

    对单行注释文本用绿色显示,可以使用<font>标签,形如:

    <font color=green>//这是我的单行注释!</font>

    注意:如果“//”出现在字符串中,则注意区分,不要错误地变为绿色。

    不考虑多行注释的问题(/* .... */ 或 /** .... */)

    你的任务是:编写程序,把给定的源文件转化为相应的html表达。

【输入、输出格式要求】

    与你的程序同一目录下,存有源文件 a.txt,其中存有标准的java源文件。要求编写程序把它转化为b.html。

    例如:目前的 a.txt 文件与 b.html 文件就是对应的。可以用记事本打开b.html查看转换后的内容。用浏览器打开b.html则可以看到显示的效果。

    注意:实际评测的时候使用的a.txt与示例是不同的。    

【注意】

    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
    
    请把所有类写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。
    
    相关的工程文件不要拷入。
    
    请不要使用package语句。
    
    源程序中只能出现JDK1.5中允许的语法或调用。不能使用1.6或更高版本。  
    

代码如下:

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class Num3 {/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFileWriter fw=new FileWriter("E:\\寒假学习\\软件大赛\\决赛Java本科\\3\\b.html");fw.write("<html><body>\r\n");File file=new File("E:\\寒假学习\\软件大赛\\决赛Java本科\\3\\a.txt");BufferedReader reader=null;reader=new BufferedReader(new FileReader(file));        String tempString=null;while((tempString=reader.readLine())!=null){tempString=tempString.replaceAll("&","&");tempString=tempString.replaceAll(" "," ");tempString=tempString.replaceAll("<","<");tempString=tempString.replaceAll(">",">");tempString=tempString.replaceAll("\"",""");tempString=tempString.replaceAll("Tab","    ");tempString=tempString.replace("public","<b>public</b>");tempString=tempString.replaceAll("class","<b>class</b>");tempString=tempString.replaceAll("static","<b>static</b>");tempString=tempString.replaceAll("void","<b>void</b>");int index=tempString.lastIndexOf("//");String note = null ;    if(!judge(tempString,index)&&index!=-1){    note=tempString.substring(index);            tempString=tempString.replaceAll(note,"<font color=green>"+note+"</font>");        System.out.println(tempString);    }    System.out.println(tempString);fw.write(tempString+"<br/>"+"\r\n");}fw.write("</body></html>");        fw.flush();        fw.close();}     public static boolean judge(String tempString,int index){//判断字符串中索引为n处的"//"是否在括号内      int quot;        quot=tempString.lastIndexOf("\"");        if(quot!=-1)//证明引号存在        {        String quotString=tempString.substring(quot,tempString.indexOf("\"",quot+1) );//把引号中的内容提取出来        int id;if(quotString.contains("//")){          id=quot+quotString.lastIndexOf ("//");   if(id==index)     return true;}        }     return false;     }}


原创粉丝点击