极其郁闷的问题,不看后悔,解决高分相送

onlyguy 2003-09-01 09:42:05
#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()不知为什么被跳过而没有被执行,各位大虾帮忙看看,高分相送
...全文
22 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangzumiao 2003-09-04
  • 打赏
  • 举报
回复
书山有路勤为劲,学海无涯苦作舟
main ()
{ int i,i=书山有路勤为劲,学海无涯苦作舟;

printf ("i")
}
高手吧,哈哈哈哈
syuui 2003-09-04
  • 打赏
  • 举报
回复
hellobcb(学海无涯酷作舟)
握握手吧。我和你一样。只会用C,不会用C++
onlyguy 2003-09-03
  • 打赏
  • 举报
回复
我2000的系统,tc2.0,可就是有问题啊,不知是怎么回事
ma100 2003-09-02
  • 打赏
  • 举报
回复
tc在2000下也可以,但在98下不行
skywater 2003-09-02
  • 打赏
  • 举报
回复
没有问题呀,我用的microsoft c/c++编译器,win2000操作系统。
huangxiaoli 2003-09-02
  • 打赏
  • 举报
回复
我什么都不会,就会看
hellobcb 2003-09-02
  • 打赏
  • 举报
回复
我就会c,不会c++
antijpn 2003-09-02
  • 打赏
  • 举报
回复
to MyNameEPC(MyName)
同dazzle84,虽然很多人在这里讨论C++,但是这里绝不仅仅讨论C++
zhuixe 2003-09-01
  • 打赏
  • 举报
回复
不要忘记程序最后释放内存
zhuixe 2003-09-01
  • 打赏
  • 举报
回复
改成如下程序就没问题了
while((c=getch())!=26) //26就是字符Ctrl^Z
{
printf("%c",c); //由于getch()不回显字符,故用printf()回显

.................
}


bigbigbigsoft 2003-09-01
  • 打赏
  • 举报
回复
try: scanf("%s",str); --- > scanf("%s\n", str)
dazzle84 2003-09-01
  • 打赏
  • 举报
回复
老大,这里是C\C++,不要自己用C++,就以为你很NB
实际上.....
算了,不说了
好自为之吧!
MyNameEPC 2003-09-01
  • 打赏
  • 举报
回复
看到printf就来气,把C的问题弄到这里C++来了。
dazzle84 2003-09-01
  • 打赏
  • 举报
回复
我在TC上运行好像也不行呀!
ma100你用什么运行的??
ma100 2003-09-01
  • 打赏
  • 举报
回复
我运行了一下,没有问题,你的编译器是什么

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧