Comment detail

/*コメント*/を取り除く (Nested Flatten)
DCGは使いませんでした。逆にコードが長くなりそう。
1
2
3
4
5
6
7
8
9
txt([47,42|C],R):-comment(C,R).
txt([C|Cs],[C|Rs]):-txt(Cs,Rs).
txt([],[]).

comment([42,47|C],R):-txt(C,R).
comment([C|Cs],R):-comment(Cs,R).
comment([],[]).

:-txt("AAA/*BBB/*CCC*/DDD*/EEE",Rs),string_to_list(R,Rs),writeln(R).
Java です。katsu さんの Prolog コードの 焼き直しです。元は Prolog、そして、これは Java では書いてますが、アルゴリズムとしては関数型パラダイムですね。。。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class RemoveComment
  {
  static String txt (String s)
    {
    if (s .startsWith ("/*")) return comment (s .substring (2)) ;
    else if (s .equals ("")) return "" ;
    else return s .substring (0,1) + txt (s .substring (1)) ;
    }

  static String comment (String s)
    {
    if (s .startsWith ("*/")) return txt (s .substring (2)) ;
    else if (s .equals ("")) return "" ;
    else return comment (s .substring (1)) ;
    }


  public static void main (String [] _)
    {
    System.out .println (txt ("AAA")) ;
    System.out .println (txt ("AAA/*BBB*/")) ;
    System.out .println (txt ("AAA/*BBB")) ;
    System.out .println (txt ("AAA/*BBB*/CCC")) ;
    System.out .println (txt ("AAA/*BBB/*CCC*/DDD*/EEE")) ;
    System.out .println (txt ("AAA/a//*BB*B**/CCC")) ;
    }
  }

Index

Feed

Other

Link

Pathtraq

loading...