challenge コメントの削除

ソースコードからコメント部分を削除するプログラム decomment を書いてください.
すくなくとも,decomment を記述したのと同じ言語で書かれているソースコードが
扱えるようにしてください.



Posted feedbacks - C

やっつけです

 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
  FILE *f;
  char c;
  int flg = 0;

  if(argc != 2)
    exit(EXIT_FAILURE);

  f = fopen(argv[1],  "r");

  while((c = fgetc(f)) != EOF) {
    if((flg == 0) && (c == '/')) {
      flg = 1;
      continue;
    } else if((flg == 1) && (c == '/')) {
      flg = 2;
    } else if((flg == 1) && (c == '*')) {
      flg = 3;
    } else if((flg == 1) && (c != '/') && (c != '*')) {
      putchar('/');
      flg = 0;
    } else if((flg == 3) && (c == '*')) {
      flg = 4;
    } else if((flg == 4) && (c == '/')) {
      flg = 5;
    }

    if(flg == 0) {
      putchar(c);
    }
    if((flg == 2) && (c == '\n')){
      flg = 0;
    }
    if(flg == 5) {
      flg = 0;
    }
  }

  exit(EXIT_SUCCESS);
}

相互再帰風に書いてみました

  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
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>

typedef void* (*filter)(int c, FILE* out);

void drive(filter f, FILE* in, FILE* out)
{
    int c;

    do {
        c = fgetc(in);

        if (c == EOF)
            break;
    } while ((f = f(c, out)));
}

#define CALL(func)                              \
    do { return func; } while(0)

#define FINISH()                                \
    do { return NULL; } while(0)


void* normal_code(int, FILE*);
void* next_of_slash_in_normal_code(int, FILE*);
void* string(int, FILE*);
void* next_of_backslash_in_string(int, FILE*);
void* single_quote(int, FILE*);
void* next_of_backslash_in_single_quote(int, FILE*);
void* comment(int, FILE*);
void* next_of_star_in_comment(int, FILE*);
void* oneline_comment(int, FILE*);

void* normal_code(int c, FILE* out)
{
    switch (c) {
    case '/':
        CALL(next_of_slash_in_normal_code);

    case '"':
        fputc(c, out);
        CALL(string);

    case '\'':
        fputc(c, out);
        CALL(single_quote);

    default:
        fputc(c, out);
        CALL(normal_code);
    }
}

void* next_of_slash_in_normal_code(int c, FILE* out)
{
    switch (c) {
    case '*':
        CALL(comment);
        
    case '/':
        CALL(oneline_comment);

    default:
        fputc('/', out);
        fputc(c, out);
        CALL(normal_code);
    }
}

void* string(int c, FILE* out)
{
    fputc(c, out);

    switch (c) {
    case '\\':
        CALL(next_of_backslash_in_string);

    case '"':
        CALL(normal_code);

    default:
        CALL(string);
    }
}

void* next_of_backslash_in_string(int c, FILE* out)
{
    fputc(c, out);
    CALL(string);
}

void* single_quote(int c, FILE* out)
{
    fputc(c, out);

    switch (c) {
    case '\\':
        CALL(next_of_backslash_in_single_quote);

    case '\'':
        CALL(normal_code);

    default:
        CALL(single_quote);
    }
}

void* next_of_backslash_in_single_quote(int c, FILE* out)
{
    fputc(c, out);
    CALL(single_quote);
}

void* comment(int c, FILE* out)
{
    if (c == '*')
        CALL(next_of_star_in_comment);
    else
        CALL(comment);
}

void* next_of_star_in_comment(int c, FILE* out)
{
    if (c == '/')
        CALL(normal_code);
    else
        CALL(comment);
}

void* oneline_comment(int c, FILE* out)
{
    if (c == '\n')
        CALL(normal_code);
    else
        CALL(oneline_comment);
}

void decomment(FILE* in, FILE* out)
{
    drive(normal_code, in, out);
}

int main(int argc, char** argv)
{
    decomment(stdin, stdout);
    return 0;
}

Index

Feed

Other

Link

Pathtraq

loading...