蓝桥杯 源码变换

来源:互联网 发布:如何头文件javascript 编辑:程序博客网 时间:2024/05/17 08:16
超文本标记语言(即HTML),是用于描述网页文档的一种标记语言。 HTML通过文本来描述文档显示
import java.io.*;public class Main{public static void main(String[] args) throws IOException{File inFile=new File("test.java");FileInputStream fileInputStream=new FileInputStream(inFile);InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);BufferedReader bufferedReader=new BufferedReader(inputStreamReader);File outFile=new File("test.html");FileOutputStream fileOutputStream=new FileOutputStream(outFile);OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream);BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);outputStreamWriter.write("<html>");outputStreamWriter.write("<body>");String tempString;while((tempString=bufferedReader.readLine())!=null){tempString=tempString.replace("&", "&");tempString=tempString.replace(" ", " ");tempString=tempString.replace("<", "<");tempString=tempString.replace(">", ">");int index1=tempString.lastIndexOf("//");int index2=tempString.indexOf("\"");//不存在引号的情况if(index1!=-1&&index2==-1){String s1=tempString.substring(0,index1);String s2=tempString.substring(index1);s2="<font color=green>"+s2+"</font>";tempString=s1+s2;}//存在引号的情况else if(index1!=-1&&index2!=-1){int startMark=-1,endMark=-1;boolean isNote=true;for(int i=0;i<tempString.length();i++){if('\"'==tempString.charAt(i)){if(startMark==-1){startMark=i;}else{endMark=i;if(index1>startMark&&index1<endMark){isNote=false;break;}else{//引号的开始和结束位置均清零startMark=-1;endMark=-1;}}}}if(isNote==true){String s1=tempString.substring(0,index1);String s2=tempString.substring(index1);s2="<font color=green>"+s2+"</font>";tempString=s1+s2;}}tempString=tempString.replaceAll("\"", """);tempString=tempString.replaceAll("\t", "    ");tempString=tempString.replace(" public ", " <b>public</b> ");tempString=tempString.replace(" class ", " <b>class</b> ");tempString=tempString.replace(" static ", " <b>static</b> ");tempString=tempString.replace(" void ", " <b>void</b> ");outputStreamWriter.write(tempString+"<br/>"+"\r\n");}outputStreamWriter.write("\n</body>\n");outputStreamWriter.write("</html>\n");bufferedWriter.flush();bufferedReader.close();bufferedWriter.close();}}

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

如果把java的源文件直接拷贝到HTML文档中,用浏览器直接打开,会发现本来整齐有序的源文件变成了一团遭。 这是因为,文本中的许多回车和空格都被忽略了。而有些符号在html中有特殊的含义,引起了更复杂的局面。 为了源文件能正常显示,我们必须为文本加上适当的标签。对特殊的符号进行转义处理。

常用的有:
HTML 需要转义的实体:

"&"——>"&amp;"
" "——>"&nbsp;"
"<"——>"&lt;"
">"——>"&gt;"


此外,根据源码的特点,可以把 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与示例是不同的。


0 0
原创粉丝点击