源码变换——蓝桥杯2012年Java决赛

来源:互联网 发布:听觉音乐 淘宝 编辑:程序博客网 时间:2024/05/22 06:30

源文件变成了一团遭。这是因为,文本中的许多回车和空格都被忽略了。而有些符号在html中有特殊的含义,引起了更复杂的局面。

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

常用的有:HTML 需要转义的实体:&     --->  &amp;空格  --->  &nbsp;<     --->  &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

// 我的工具类public class MyTool{    public static void main(String[] args)    {        int a = 100;        int b = 20;        if(a>b && true)            System.out.println(a);        else            System.out.println("this! //aaa//kkk");  // 测试注释显示是否正确    }}

b.html

<html><body><br/><font color=green>//&nbsp;我的工具类</font><br/><b>public</b>&nbsp;<b>class</b>&nbsp;MyTool<br/>{<br/>&nbsp;&nbsp;&nbsp;&nbsp;<b>public</b>&nbsp;<b>static</b>&nbsp;<b>void</b>&nbsp;main(String[]&nbsp;args)<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;a&nbsp;=&nbsp;100;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;b&nbsp;=&nbsp;20;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(a&gt;b&nbsp;&amp;&amp;&nbsp;true)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(a);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;this!&nbsp;//aaa//kkk&quot;);&nbsp;&nbsp;<font color=green>//&nbsp;测试注释显示是否正确</font><br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>}<br/></body></html>

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

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

public class Main {    private static FileWriter fileWriter;    public static void main(String[] args) {        File readFile = new File("a.txt");        File writeFile = new File("b.html");        String in;        try {            FileReader fileReader = new FileReader(readFile);            BufferedReader bufferedReader = new BufferedReader(fileReader);            fileWriter = new FileWriter(writeFile);            fileWriter.write("<html><body>\n");            while ((in = bufferedReader.readLine()) != null) {                int pstartflag = -1;                int pendflag = -1;                int sstartflag = -1;                int sendflag = -1;                int cstartflag = -1;                int cendflag = -1;                int vstartflag = -1;                int vendflag = -1;                for (int i = 0; i < in.length(); i++) {                    char is = in.charAt(i);                    if (in.contains("public")) {                        pstartflag = in.indexOf("public");                        pendflag = pstartflag + 5;                    }                    if (in.contains("static")) {                        sstartflag = in.indexOf("static");                        sendflag = sstartflag + 5;                    }                    if (in.contains("class")) {                        cstartflag = in.indexOf("class");                        cendflag = cstartflag + 4;                    }                    if (in.contains("void")) {                        vstartflag = in.indexOf("void");                        vendflag = vstartflag + 3;                    }                    if (i == pstartflag || i == sstartflag || i == cstartflag                            || i == vstartflag) {                        fileWriter.write("<b>");                    }                    if (is == ' ') {                        fileWriter.write("&nbsp;");                    } else if (is == '  ') {                        fileWriter.write("&nbsp;&nbsp;&nbsp;&nbsp;");                    } else if (is == '&') {                        fileWriter.write("&amp;");                    } else if (is == '<') {                        fileWriter.write("&lt;");                    } else if (is == '>') {                        fileWriter.write("&gt;");                    } else if (is == '"') {                        fileWriter.write("&quot;");                    } else                    if (is == '/'                            && in.charAt(i + 1) == '/'                            && !(in.substring(i,in.length()).contains("\"") && in                                    .substring(0, i).contains("\""))) {                        fileWriter.write("<font color=green>");                        fileWriter.write(is);                    } else {                        fileWriter.write(is);                    }                    if (i == sendflag || i == vendflag || i == pendflag                            || i == cendflag) {                        fileWriter.write("</b>");                    }                }                if (in.contains("//")) {                    fileWriter.write("</font>");                }                fileWriter.write("<br/>\n");            }            fileWriter.write("</body></html>\n");        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                fileWriter.flush();                fileWriter.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}
0 0
原创粉丝点击