源碼變換——藍橋杯2012年Java決賽

源文件變成了一團遭。這是因爲,文本中的許多回車和空格都被忽略了。而有些符號在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


// 我的工具類
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();
            }

        }

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章