C/C++の宿題を片付けます 76代目
■ このスレッドは過去ログ倉庫に格納されています
0349デフォルトの名無しさん
2006/11/10(金) 23:06:46#include <stdio.h> #include <string.h> #include <stdlib.h>
typedef struct Word{ char *word;unsigned int num;struct Word *next;
}Word;Word *CountWord(void);
int main(){ Word *last = CountWord();Word *p;Word *temp;char *cf = NULL;
for(p = last; p != NULL; p = p->next) printf("%d %s \n", p->num, p->word);temp = last;
while(temp!=NULL){p = temp;cf = temp->word;temp = temp->next;free(p);free(cf);}return 0;}
Word *CountWord(void){FILE *fp;char buff[1024] = {'\0'};char *gDlim = " ,.\t\n";char *word = NULL;
unsigned num =1;unsigned len;Word *temp;Word *top = NULL;fp = fopen("dat.txt","r");
while(fgets(buff, sizeof(buff), fp)){word = strtok(buff,gDlim);while(word != NULL){
temp = (Word *)malloc(sizeof(Word));len = strlen(word);temp->word = (char *)malloc(len + 1);
strcpy(temp->word, word);temp->num = num;temp->next = top;top = temp;
num++;word = strtok(NULL, gDlim);}memset(buff, '\0', sizeof(buff));}fclose(fp);return temp;}
■ このスレッドは過去ログ倉庫に格納されています