回答編集履歴
1
プログラムの例示を追加
test
CHANGED
@@ -39,3 +39,177 @@
|
|
39
39
|
|
40
40
|
|
41
41
|
ただし、状態遷移図を残して置かないと後から見てわけわからないプログラムになりがちかもしれません。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
# 追記
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
以上の考え方に基づいてプログラムにするとこうなります。
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```c
|
54
|
+
|
55
|
+
#include <stdio.h>
|
56
|
+
|
57
|
+
#include <stdlib.h>
|
58
|
+
|
59
|
+
#include <ctype.h>
|
60
|
+
|
61
|
+
#include <iso646.h>
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
enum state_t {
|
66
|
+
|
67
|
+
word,
|
68
|
+
|
69
|
+
punctuation,
|
70
|
+
|
71
|
+
comment,
|
72
|
+
|
73
|
+
comment_pending,
|
74
|
+
|
75
|
+
comment_finish_pending
|
76
|
+
|
77
|
+
};
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
int main(int argc, char *argv[])
|
82
|
+
|
83
|
+
{
|
84
|
+
|
85
|
+
FILE *fp;
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
switch(argc) {
|
90
|
+
|
91
|
+
case 1:
|
92
|
+
|
93
|
+
fp = stdin;
|
94
|
+
|
95
|
+
break;
|
96
|
+
|
97
|
+
case 2:
|
98
|
+
|
99
|
+
fp = fopen(argv[1],"r");
|
100
|
+
|
101
|
+
if(fp == NULL) {
|
102
|
+
|
103
|
+
fprintf(stderr, "%s:ファイルが開けませんでした%s\n", argv[0], argv[1]);
|
104
|
+
|
105
|
+
return EXIT_FAILURE;
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
break;
|
110
|
+
|
111
|
+
default:
|
112
|
+
|
113
|
+
fprintf(stderr, "引数の個数が間違っています");
|
114
|
+
|
115
|
+
return EXIT_FAILURE;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
int line_count = 0;
|
122
|
+
|
123
|
+
int word_count = 0;
|
124
|
+
|
125
|
+
int character_count = 0;
|
126
|
+
|
127
|
+
enum state_t state = punctuation;
|
128
|
+
|
129
|
+
int ch;
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
while((ch = getc(fp)) != EOF) {
|
134
|
+
|
135
|
+
character_count++;
|
136
|
+
|
137
|
+
if(ch=='\n') line_count++;
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
if(state==punctuation && not isalpha(ch) && ch!='/') {
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
else if(state==punctuation && isalpha(ch)) {
|
146
|
+
|
147
|
+
state = word;
|
148
|
+
|
149
|
+
word_count++;
|
150
|
+
|
151
|
+
} else if(state==punctuation && ch=='/') {
|
152
|
+
|
153
|
+
state = comment_pending;
|
154
|
+
|
155
|
+
} else if(state==word && ch=='/') {
|
156
|
+
|
157
|
+
state = comment_pending;
|
158
|
+
|
159
|
+
} else if(state==word && not isalpha(ch) && ch!='/') {
|
160
|
+
|
161
|
+
state = punctuation;
|
162
|
+
|
163
|
+
} else if(state==word && isalpha(ch)) {
|
164
|
+
|
165
|
+
} else if(state==comment_pending && isalpha(ch)) {
|
166
|
+
|
167
|
+
state = word;
|
168
|
+
|
169
|
+
word_count++;
|
170
|
+
|
171
|
+
} else if(state==comment_pending && not isalpha(ch) && ch!='*') {
|
172
|
+
|
173
|
+
state = punctuation;
|
174
|
+
|
175
|
+
} else if(state==comment_pending && ch=='*') {
|
176
|
+
|
177
|
+
state = comment;
|
178
|
+
|
179
|
+
word_count++; // コメントを一語扱いする場合
|
180
|
+
|
181
|
+
} else if(state==comment && ch!='*') {
|
182
|
+
|
183
|
+
} else if(state==comment && ch=='*') {
|
184
|
+
|
185
|
+
state = comment_finish_pending;
|
186
|
+
|
187
|
+
} else if(state==comment_finish_pending && ch=='/') {
|
188
|
+
|
189
|
+
state = punctuation;
|
190
|
+
|
191
|
+
} else if(state==comment_finish_pending && ch!='/') {
|
192
|
+
|
193
|
+
state = comment;
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
printf("行数=%d 単語数=%d 文字数=%d\n", line_count, word_count, character_count);
|
202
|
+
|
203
|
+
fclose(fp);
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
return 0;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
```
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
アスキーコードの範囲内しか考えていないので、文字数はバイト単位で数えてしまうことに気を付けてください。
|