C言語でヒープソート法を使ってファイルから文字列を読み込み、それらを分類して出力するというプログラムを作っている途中で苦戦しています。
どこかが間違っているようでコンパイルできないのです。。。
文字列が書かれている file1.txt と file2.txt から文字列を読み込み、アルファベット順に並べ替えてそれらを出現回数を出力するということを実現したいです。
C
1コード
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int get_word(FILE *,char []);
struct node{ //文字列の木
char *word;
int count;
struct node *left;
struct node *right;
};
void make_heap(root);
int main(int argc, char *argv[]){
FILE *fp;
int i,j,v;
struct node *root=NULL;
struct node *p;
char word[64];
for(i=1;i<argc;i++){
if((fp=fopen(argv[i],"r"))==NULL){
fprintf(stderr,"cannot open%s\n",argv[i]);
continue;
}
while(get_word(fp,word)){
}
fclose(fp);
}
for(i=0;i<10;i++){
printf("%s %d\n",root->word,root->count);
root->count=0;
shift(root);
}
}
int get_word(FILE *fp,char word[])
{
int ch,k=0;
while(!isalpha(ch=getc(fp))&&ch!=EOF){
}
if(ch==EOF)return 0;
word[k++]=ch;
while(isalpha(ch=getc(fp)))word[k++]=ch;
word[k]='\0';
return 1;
}
void make_heap(struct node *p){
if(p->left!=NULL){
make_heap(p->left);
}
if(p->right!=NULL){
make_heap(p->right);
}
shift(p);
}
void shift(struct node *p){
struct node *q;
if(p->left==NULL) {
if(p->right==NULL){
return;
}
else{
if(p->right->count<p->count){
return;
}
else{
swap(p->right,p);//p->rightとpの間でwordとcountを交換する自作関数
shift(p->right);
}
}
}
else{
if(p->right==NULL){
if(p->left->count<p->count){
return;
}
else{
swap(p->left,p);
shift(p->left);
}
}
else{
if(p->left->count>p->right-.count){
q=p->left;
}
else{
q=p->right;
}
if(q->count<p->count){
return;
}
else{
swap(p,q);
shift(q);
}
}
}
}
あなたの回答
tips
プレビュー