#3
by c-genger » 4ヶ月前
組んでみました、参考になれば。
コード:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(){
enum{ DONE, E_SMALL_W, E_SMALL_WC, };
struct{
struct{ char w[64]; int c; } wc[1024];
int n;
} wn = {};
ungetc(' ', stdin);
char buf[sizeof(wn.wc[0].w)];
while((scanf("%63[^A-Za-z0-9_]", buf) == 1) && (scanf("%63[A-Za-z0-9_]", buf) == 1)){
if(!isdigit(buf[0])){
int i = 0;
while((i < wn.n) && (strcmp(wn.wc[i].w, buf) != 0)) ++i;
if(sizeof(wn.wc)/sizeof(wn.wc[0]) <= i) return E_SMALL_WC;
if(i == wn.n){
++wn.n;
strncpy(wn.wc[i].w, buf, sizeof(wn.wc[i].w));
}
++wn.wc[i].c;
}
}
if(getchar() != EOF) return E_SMALL_W;
for(int i=0; i<wn.n; ++i){
printf("%s ......%d\n", wn.wc[i].w, wn.wc[i].c);
}
return DONE;
}
コード:
% ./a.out < test.txt > answer.txt
組んでみました、参考になれば。
[code]
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(){
enum{ DONE, E_SMALL_W, E_SMALL_WC, };
struct{
struct{ char w[64]; int c; } wc[1024];
int n;
} wn = {};
ungetc(' ', stdin);
char buf[sizeof(wn.wc[0].w)];
while((scanf("%63[^A-Za-z0-9_]", buf) == 1) && (scanf("%63[A-Za-z0-9_]", buf) == 1)){
if(!isdigit(buf[0])){
int i = 0;
while((i < wn.n) && (strcmp(wn.wc[i].w, buf) != 0)) ++i;
if(sizeof(wn.wc)/sizeof(wn.wc[0]) <= i) return E_SMALL_WC;
if(i == wn.n){
++wn.n;
strncpy(wn.wc[i].w, buf, sizeof(wn.wc[i].w));
}
++wn.wc[i].c;
}
}
if(getchar() != EOF) return E_SMALL_W;
for(int i=0; i<wn.n; ++i){
printf("%s ......%d\n", wn.wc[i].w, wn.wc[i].c);
}
return DONE;
}
[/code]
[code]
% ./a.out < test.txt > answer.txt
[/code]