zoukankan      html  css  js  c++  java
  • JAVA去注释小程序

    采用状态机的设计思路,实现一个去注释的小程序;去掉/**/和//两种注释:

    View Code
     1 package com.app;
    2
    3 import java.io.File;
    4 import java.io.FileInputStream;
    5 import java.io.FileNotFoundException;
    6 import java.io.FileOutputStream;
    7 import java.io.FileWriter;
    8 import java.io.IOException;
    9 import java.io.InputStream;
    10 import java.io.InputStreamReader;
    11 import java.io.OutputStream;
    12 import java.io.OutputStreamWriter;
    13 import java.io.Reader;
    14 import java.io.Writer;
    15 import java.util.Stack;
    16
    17 public class app {
    18 public enum stype{
    19 stype_type1, //当前是/*
    20 stype_type2, //当前是//
    21 stype_type3, //当前是数据
    22 }
    23
    24 private static stype app_stype;
    25
    26 public app(){
    27 app_stype = stype.stype_type3;
    28 }
    29
    30 /**
    31 * @param args
    32 */
    33 public static void main(String[] args) {
    34 // TODO Auto-generated method stub
    35 try{
    36 File Orifile = new File(app.class.getResource("main.cpp").getFile());
    37 File Desfile = new File("bak_" + Orifile.getName().toString());
    38
    39 InputStream in = new FileInputStream(Orifile);;
    40
    41 Reader reader = new InputStreamReader(in);
    42 FileWriter fw = new FileWriter(Desfile);
    43
    44 int curtemp = 0;
    45 int pretemp = 0;
    46
    47 while((curtemp = reader.read()) != -1){
    48
    49 if(app_stype == stype.stype_type1){
    50 if(('*' == pretemp)&&('/' == curtemp)){
    51 app_stype = stype.stype_type3;
    52 }
    53 }else if(app_stype == stype.stype_type2){
    54 if(('\n' == curtemp)&&('\r' == pretemp)){
    55 app_stype = stype.stype_type3;
    56 }
    57 }else{
    58 if(('/' == pretemp)&&('*' == curtemp)){
    59 app_stype = stype.stype_type1;
    60 }else if(('/' == pretemp)&&('/' == curtemp)){
    61 app_stype = stype.stype_type2;
    62 }else{
    63 if(('/' != curtemp)&&('*' != curtemp)){
    64 if(('/' == pretemp)&&('*' == pretemp)){
    65 fw.write(pretemp);
    66 }
    67 fw.write(curtemp);
    68 }
    69 }
    70 }
    71
    72 pretemp = curtemp;
    73 }
    74
    75 fw.flush();
    76 fw.close();
    77 in.close();
    78
    79
    80 } catch (FileNotFoundException e) {
    81 // TODO Auto-generated catch block
    82 e.printStackTrace();
    83 } catch (IOException e) {
    84 // TODO Auto-generated catch block
    85 e.printStackTrace();
    86 }
    87
    88 }
    89
    90 }



  • 相关阅读:
    弱智儿童欢乐多游戏android源码完整版
    小龙吃水果游戏IOS源码 V1.0
    PHP实现的轩宇淘宝客系统源码v2.0.1
    友点企业网站管理系统集电脑网站、手机网站、微信三站合一
    仿win8磁贴界面以及功能
    HarestGame史上最难游戏源码 v2.0
    关于js基本类型string
    本周作业
    罗辑思维 怎样成为一个高手
    第18周作业
  • 原文地址:https://www.cnblogs.com/fredric/p/2382804.html
Copyright © 2011-2022 走看看