极其郁闷的问题,不看后悔,解决高分相送
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct htnode{
char data;
int weight;
struct htnode *lchild ,*rchild,*parent,*next;
int mark; /*用作状态标志*/
char *code; /*指向该结点中字符huffman编码的指针*/
}HTNode; /*动态构造huffuman树的结点,用三叉链表作为存储结构*/
/*该模块用来统计文本中各个字符出现的频度*/
HTNode * CountWeight()
{
HTNode *head,*top,*move1,*move2,*new;
char c;
int flag=0;
printf("please input the text,end with ctrl+Z\n");
head=(HTNode *)malloc(sizeof(HTNode));
head->weight=head->mark=0; /*建立头结点,weight记录结点个数*/
head->lchild=head->rchild=head->parent=head->next=NULL;
head->code=NULL;
while((c=getchar())!=EOF)
{
while(c==' '||c=='\n')
{ c=getchar(); } /*对于回车和空格不予统计*/
if(!head->weight)
{top=move1=(HTNode *)malloc(sizeof(HTNode));
head->next=top;
top->weight=top->mark=0;
top->lchild=top->rchild=top->parent=top->next=NULL;
top->code=NULL;
top->data=c;
top->weight++;
head->weight++;
}
else {move2=top;
while(move2!=NULL){ /*遍历链表进行比较,统计字符的频度*/
if(move2->data==c)
{move2->weight++;
flag=1; /*找到结点,标志flag变为1*/
break;
}
else move2=move2->next;
} /*end while*/
if(!flag) /*判断是否有与c相同的字符结点,如果没有则建立一个新结点*/
{new=(HTNode *)malloc(sizeof(HTNode));
head->weight++;
new->weight=new->mark=0;
new->lchild=new->rchild=new->parent=new->next=NULL;
top->code=NULL;
new->data=c;
new->weight++;
move1->next=new;
move1=new;
move1->next=NULL;
}
flag=0; /*标记复原*/
}
} /*end while*/
return head;
}
/*该模块用来输出统计结果*/
void print(HTNode *p)
{
printf("the resault of the statistics is:\n");
p=p->next;
while(p!=NULL)
{
printf("%c:\t%d\n",p->data,p->weight);
p=p->next;
}
} /*end print*/
void as()
{
char str[10];
printf("please input the text which is only include 0 and 1\n");
fflush(stdin);
scanf("%s",str);
printf("%s\n",str);
getch();
}
void main()
{
HTNode *q;
q=CountWeight();
print(q);
as();
}
在执行的时候,as()里面的scanf()不知为什么被跳过而没有被执行,各位大虾帮忙看看,高分相送