『征服Cポインタ』プログラミング例word_count
6361 ワード
詳細
C言語を復習して、『征服Cポインタ』のプログラミングの前の例word_を見ました.カウントしてみましたが、ちょっと問題があります.
main.cメインプログラム
word_manage.h
word_initialize.c
add_word.c
dump_word.c
word_finalize.c
get_word.c
質問:1、main.cファイルでは、例を見てinclude get_のみword.hとword_manage.h 2つのファイルですが、私はすべてのファイルをincludeに入れなければなりません.2、ファイルを読み取るときは1行目ではなく、2行目から読み込まなければなりません.
C言語を復習して、『征服Cポインタ』のプログラミングの前の例word_を見ました.カウントしてみましたが、ちょっと問題があります.
main.cメインプログラム
#include
#include
#include
#include "get_word.h"
#include "word_initialize.c"
#include "add_word.c"
#include "dump_word.c"
#include "word_finalize.c"
#define BUF_SIZE 1024
/* ,
*/
int main()
{
char file_input[BUF_SIZE],file_output[BUF_SIZE],buf[BUF_SIZE];
int word_len;
FILE *fp_in,*fp_out;
printf(" ( ),stdin :");
scanf("%s",file_input);
printf(" ( ),stdout :");
scanf("%s",file_output);
/* */
if(!strcmp(file_input,"stdin")) //strcmp 0
{
fp_in = stdin;
}
else
{
if((fp_in=fopen(file_input,"r"))==NULL)
{
printf("open input file error!
");
exit(1);
}
}
/* */
if(!strcmp(file_output,"stdout"))
{
fp_out = stdout;
}
else
{
fp_out = fopen(file_output,"w");
}
/* */
word_initialize();
/* */
while((word_len=get_word(buf,BUF_SIZE,fp_in)) != EOF)
{
if(word_len!=0)
{
add_word(buf);
}
}
/* */
dump_word(fp_out);
word_finalize();
}
word_manage.h
#ifndef WORD_MANAGE_H
#define WORD_MANAGE_H
#include
typedef struct word_tag
{
char *word;
int count; //
struct word_tag *next_word;
}WORD;
extern WORD *word_head;
void word_initialize(void);
void add_word(char *word);
void dump_word(FILE *fp);
void word_finalize(void);
#endif
word_initialize.c
#include "word_manage.h"
WORD *word_head = NULL; // word_manage.h
void word_initialize(void)
{
word_head=NULL;
}
add_word.c
#include
#include
#include
#include "word_manage.h"
/* */
static WORD *create_word(char *word) {
char *temp;
WORD *new_word;
new_word=malloc(sizeof(WORD));
temp=strdup(word); //char *strdup(const char *str) str , str
new_word->word=temp; // word
new_word->count=1;
new_word->next_word=NULL;
return new_word;
}
void add_word(char *word)
{
WORD *pos,*pre,*new_word; //pos ,pre
pos=word_head;
pre=NULL;
int result;
/* , */
while(pos!=NULL)
{
if((result=strcmp(pos->word,word))<0)
{
pre=pos;
pos=pos->next_word;
}
else break;
}
/* */
if(word_head != NULL && result ==0 )
{
pos->count++;
}
else
{
/* */
if(pre==NULL)
{
new_word = create_word(word);
new_word->next_word=word_head;
word_head=new_word;
}
/* */
else
{
new_word=create_word(word);
new_word->next_word=pos;
pre->next_word=new_word;
}
}
}
dump_word.c
#include
#include
#include
#include "word_manage.h"
void dump_word(FILE *fp)
{
WORD *pos;
for(pos = word_head;pos;pos = pos->next_word) {
fprintf(fp,"%-20s%5d
",pos->word,pos->count);
}
}
word_finalize.c
#include
#include "word_manage.h"
void word_finalize(void)
{
WORD *temp;
temp=word_head;
while(temp!=NULL)
{
word_head=word_head->next_word;
free(temp->word);
free(temp);
temp=word_head;
}
}
get_word.c
#include
#include
#include
/*
fp , buf , EOF
*/
int get_word(char *buf,int buf_size,FILE *fp)
{
int ch,len;
len=0;
while((ch=getc(fp))!=EOF&&isalnum(ch)) //isalnum(int c) c ture
{
buf[len]=ch; //buf[len] is a syntax sugar of *(buf+len)
if(len>=buf_size)
{
printf("Error:word too long!"); // buf_size
exit(1);
}
len++;
}
buf[len]='\0'; //
if(ch==EOF)
{
return EOF;
}
else
{
return len;
}
}
/*get_word.h*/
#ifndef GET_WORD_H
#define GET_WORD_H
/* fp , , EOF*/
int get_word(char *buf,int buf_size,FILE *fp);
#endif
質問:1、main.cファイルでは、例を見てinclude get_のみword.hとword_manage.h 2つのファイルですが、私はすべてのファイルをincludeに入れなければなりません.2、ファイルを読み取るときは1行目ではなく、2行目から読み込まなければなりません.