回答編集履歴
2
改行数の処理を追加
test
CHANGED
@@ -92,23 +92,25 @@
|
|
92
92
|
|
93
93
|
int main(int argc, char *argv[]) {
|
94
94
|
|
95
|
-
FILE *fp; /* 入力ファイル用ファイルポインタ */
|
95
|
+
FILE *fp; /* 入力ファイル用ファイルポインタ */
|
96
|
-
|
96
|
+
|
97
|
-
int ch; /* 入力文字 */
|
97
|
+
int ch; /* 入力文字 */
|
98
|
-
|
98
|
+
|
99
|
-
int line=0; /*行数*/
|
99
|
+
int line=0; /*行数*/
|
100
|
-
|
100
|
+
|
101
|
-
int word=0; /*単語数*/
|
101
|
+
int word=0; /*単語数*/
|
102
|
-
|
102
|
+
|
103
|
-
int character=0; /*文字数*/
|
103
|
+
int character=0; /*文字数*/
|
104
|
-
|
104
|
+
|
105
|
-
int comment=0; /*コメント数*/
|
105
|
+
int comment=0; /*コメント数*/
|
106
|
-
|
106
|
+
|
107
|
-
int comment_chars=0; /*コメント文字数*/
|
107
|
+
int comment_chars=0; /*コメント文字数*/
|
108
|
+
|
108
|
-
|
109
|
+
int comment_lines=0; /*コメント改行数*/
|
110
|
+
|
109
|
-
int whitespace=TRUE;/*空白文字を読んでいる最中は真(単語を読んでいる間,偽)*/
|
111
|
+
int whitespace=TRUE;/*空白文字を読んでいる最中は真(単語を読んでいる間,偽)*/
|
110
|
-
|
112
|
+
|
111
|
-
int back;
|
113
|
+
int back;
|
112
114
|
|
113
115
|
|
114
116
|
|
@@ -178,6 +180,26 @@
|
|
178
180
|
|
179
181
|
comment++;
|
180
182
|
|
183
|
+
while((ch = getSJIS(fp)) != EOF) {
|
184
|
+
|
185
|
+
if(ch=='\n') {
|
186
|
+
|
187
|
+
comment_lines++;
|
188
|
+
|
189
|
+
break;
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
if(ch!=' ' && ch!='\t') {
|
194
|
+
|
195
|
+
ungetc(ch,fp);
|
196
|
+
|
197
|
+
break;
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
181
203
|
continue;
|
182
204
|
|
183
205
|
}
|
@@ -230,9 +252,9 @@
|
|
230
252
|
|
231
253
|
}
|
232
254
|
|
233
|
-
printf("行数=%d 単語数=%d(%d) 文字数=%d(%d)\n",
|
255
|
+
printf("行数=%d(%d) 単語数=%d(%d) 文字数=%d(%d)\n",
|
234
|
-
|
256
|
+
|
235
|
-
line, word, word+comment, character, character+comment_chars);
|
257
|
+
line, line+comment_lines, word, word+comment, character, character+comment_chars);
|
236
258
|
|
237
259
|
fclose(fp);
|
238
260
|
|
@@ -240,6 +262,4 @@
|
|
240
262
|
|
241
263
|
}
|
242
264
|
|
243
|
-
|
244
|
-
|
245
265
|
```
|
1
コード追加
test
CHANGED
@@ -5,3 +5,241 @@
|
|
5
5
|
"/*" の初めの "/" がカウントされてしまっているのではないでしょうか.
|
6
6
|
|
7
7
|
"/" を読んだ時点でカウントを遅延させる(次が "*" で無かったら "/" の分もカウントする)等, の細工が必要なのではないでしょうか.
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
---
|
12
|
+
|
13
|
+
追加
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
文字コード SJIS(MS932) として修正してみました.
|
18
|
+
|
19
|
+
結果表示の()内がコメントを含む場合のカウントです.
|
20
|
+
|
21
|
+
```c
|
22
|
+
|
23
|
+
#include <stdio.h>
|
24
|
+
|
25
|
+
#include <stdlib.h>
|
26
|
+
|
27
|
+
#define TRUE 1
|
28
|
+
|
29
|
+
#define FALSE 0
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
int isSpace(int c) {
|
34
|
+
|
35
|
+
return c==' '||c=='\n'||c=='\t';
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
int isSJIS1(int c) {
|
40
|
+
|
41
|
+
return ((c>=0x81)&&(c<=0x9f))||((c>=0xe0)&&(c<=0xfc));
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
/*
|
46
|
+
|
47
|
+
* getc に
|
48
|
+
|
49
|
+
* ・SJIS全角も1文字として返す
|
50
|
+
|
51
|
+
* ・改行コード(\n,\r,\r\n)を'\n'に統一する
|
52
|
+
|
53
|
+
* 機能を追加
|
54
|
+
|
55
|
+
*/
|
56
|
+
|
57
|
+
int getSJIS(FILE *fp) {
|
58
|
+
|
59
|
+
int ch, c;
|
60
|
+
|
61
|
+
if((ch = getc(fp)) != EOF) {
|
62
|
+
|
63
|
+
if(isSJIS1(ch)) {
|
64
|
+
|
65
|
+
if((c = getc(fp)) != EOF) {
|
66
|
+
|
67
|
+
ch=(ch<<8)|(c&0xff);
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
} else if(ch=='\r') {
|
72
|
+
|
73
|
+
ch='\n';
|
74
|
+
|
75
|
+
if((c = getc(fp)) != EOF) {
|
76
|
+
|
77
|
+
if(c!='\n') {
|
78
|
+
|
79
|
+
ungetc(c,fp);
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
return ch;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
int main(int argc, char *argv[]) {
|
94
|
+
|
95
|
+
FILE *fp; /* 入力ファイル用ファイルポインタ */
|
96
|
+
|
97
|
+
int ch; /* 入力文字 */
|
98
|
+
|
99
|
+
int line=0; /*行数*/
|
100
|
+
|
101
|
+
int word=0; /*単語数*/
|
102
|
+
|
103
|
+
int character=0; /*文字数*/
|
104
|
+
|
105
|
+
int comment=0; /*コメント数*/
|
106
|
+
|
107
|
+
int comment_chars=0; /*コメント文字数*/
|
108
|
+
|
109
|
+
int whitespace=TRUE;/*空白文字を読んでいる最中は真(単語を読んでいる間,偽)*/
|
110
|
+
|
111
|
+
int back;
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
if(argc == 1) {
|
116
|
+
|
117
|
+
fp=stdin; /*標準入力を入力ファイルとする*/
|
118
|
+
|
119
|
+
} else if(argc == 2) {
|
120
|
+
|
121
|
+
/*指定したファイルを開く*/
|
122
|
+
|
123
|
+
if((fp=fopen(argv[1],"r"))== NULL) {
|
124
|
+
|
125
|
+
fprintf(stderr,"%s:ファイルが開けませんでした%s\n", argv[0],argv[1]);
|
126
|
+
|
127
|
+
exit(1);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
/*EOF が現れるまで 1 文字ずつ読みとる*/
|
136
|
+
|
137
|
+
while((ch = getSJIS(fp)) != EOF) {
|
138
|
+
|
139
|
+
// printf("standerd back=%c ch=%c\n",back,ch);
|
140
|
+
|
141
|
+
/* '/''*'の文字かどうか判定する*/
|
142
|
+
|
143
|
+
if(ch=='/') {
|
144
|
+
|
145
|
+
if((ch = getSJIS(fp)) != EOF) {
|
146
|
+
|
147
|
+
if(ch=='*') {
|
148
|
+
|
149
|
+
comment_chars+=2;
|
150
|
+
|
151
|
+
/* '/''*'の場合はEOFまで1文字ずつ読み取る*/
|
152
|
+
|
153
|
+
back = 0;
|
154
|
+
|
155
|
+
while((ch = getSJIS(fp)) != EOF) {
|
156
|
+
|
157
|
+
if(!isSpace(ch)) {
|
158
|
+
|
159
|
+
comment_chars++;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
//printf("innet back=%c ch=%c\n",back,ch);
|
164
|
+
|
165
|
+
/*一文字ずつ読み取り'*''/'を読み取ったらループを抜ける*/
|
166
|
+
|
167
|
+
if(back=='*' && ch=='/') {
|
168
|
+
|
169
|
+
break;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
/*一文字前に読み取った文字を代入する*/
|
174
|
+
|
175
|
+
back=ch;
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
comment++;
|
180
|
+
|
181
|
+
continue;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
ungetc(ch,fp);
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
ch = '/';
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
/*改行なら行数を増やす*/
|
196
|
+
|
197
|
+
if(ch=='\n') {
|
198
|
+
|
199
|
+
line++;
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
/*空白、改行、タブの場合は空白とする。*/
|
204
|
+
|
205
|
+
if(isSpace(ch)) {
|
206
|
+
|
207
|
+
whitespace = TRUE;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
/*空白の場合は単語を増やす*/
|
212
|
+
|
213
|
+
else if(whitespace) {
|
214
|
+
|
215
|
+
whitespace = FALSE;
|
216
|
+
|
217
|
+
/*単語数を増やす*/
|
218
|
+
|
219
|
+
word++;
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
if(!whitespace) {
|
224
|
+
|
225
|
+
/*文字数を増やす*/
|
226
|
+
|
227
|
+
character++;
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
printf("行数=%d 単語数=%d(%d) 文字数=%d(%d)\n",
|
234
|
+
|
235
|
+
line, word, word+comment, character, character+comment_chars);
|
236
|
+
|
237
|
+
fclose(fp);
|
238
|
+
|
239
|
+
exit(0);
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
```
|