编译原理作业:编写一个程序,使之将一个C程序除注释之外的所有保留字全部大写

花了两天的零碎时间把编译原理的代码写了出来,结果老师说不看了,那平时成绩又没了大哭,坑爹啊。还是把自己写的程序贴出来,当是一种对自己的鼓励吧,也当作是是一种警醒,该做的事要早做啊,不要拖延。

import java.io.*;


public class Test {
 
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        test tFile = new test();
        boolean cUpper = false;
       
        try {
        //读写文件
            br = new BufferedReader(new FileReader(
                    "e:\\main.cpp"));
            bw = new BufferedWriter(new FileWriter("e:\\test.cpp"));
            
            String line = null;//每读一行,就保留在line里面
            String s = "";//将读的所有行都保留在s里,再一次性写到目标文件中
            
            //每读一行,就检查是否有token,然后决定是否修改,将所有修改存入s中
            while ((line = br.readLine()) != null) {
            //以“/*”和“*/”为注释多行的情况
            if(line.trim().startsWith("/*")){
            cUpper = true;
            }else if(line.trim().endsWith("*/")){
            cUpper = false;
            }
     
           
            //以“//”为注释单独成行的情况
            else if(line.trim().startsWith("//")){
            s+=(line+"\n");
            continue;
            }
           
           
            if(true == cUpper){
            s+=(line+"\n");
            }else{
            if(line.trim().endsWith("*/")){
            if(line.trim().indexOf("/*") != -1){
            s+=(tFile.change(line)+"\n");
            }else{
            s+=(line+"\n");
            }
            }else{
            s+=(tFile.change(line)+"\n");//正常代码行,可能会有注释
            }
            }
           
            }
            
       bw.write(s);
       bw.write(13);
       bw.flush();
            //System.out.println(s);
       System.out.println("修改成功");
       }catch (Exception e) {
            e.printStackTrace();
       }finally {
            try {
                br.close();
            } catch (Exception e) {
            e.printStackTrace();
            }
            try {
             //   bw.close();
            } catch (Exception e) {
            e.printStackTrace();
            }
        }
 
    }
    
        public String change(String str){
       
        //优化后的代码
        //列出需要识别的token
    String[] s = {"auto","double","int","void","struct","break","else","long","switch","sizeof","return","float","if",
"case","enum","register","typedef","char","extern","const","while","for","bool","true","false"};
   
    //用正则表达式将单词分开
    String[] temp = str.split("\\b");
    String word = null;
    String[] temp2 = str.split("//");
    //String word2= null;
    String[] temp3 = null;
    String[] temp4 = str.split("/\\*");
    //String word2= null;
    String[] temp5 = null;
   
    //处理“//”在句中的情况
    if(temp2.length == 2){
    temp3 = temp2[0].split("\\b");
    for(int i=0;i<s.length;i++){
        word = s[i];
        for(int j=0;j<temp3.length;j++){
        if(temp3[j].trim().equals(word)){
        temp3[j]=temp3[j].toUpperCase();
        str = str.replaceAll(s[i],temp3[j]);
        }
        }
    }
    return str;
    }
   
    //处理“/*”在句中的情况
    else if(temp4.length == 2){
    temp5 = temp4[0].split("\\b");
    for(int i=0;i<s.length;i++){
        word = s[i];
        for(int j=0;j<temp5.length;j++){
        if(temp5[j].trim().equals(word)){
        temp5[j]=temp5[j].toUpperCase();
        str = str.replaceAll(s[i],temp5[j]);
        }
        }
    }
    return str;
    }
   
    //处理/*...*/多行注释的情况
    else{
for(int i=0;i<s.length;i++){
    word = s[i];
  
    for(int j=0;j<temp.length;j++){
    if(temp[j].equals(word)){
    temp[j]=temp[j].toUpperCase();
    str = str.replaceAll(s[i],temp[j]);
    }
    }
    }
return str;
}
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值