简单而又郁闷的问题,解决的狂给分!
/*一个简单的建立链表来统计数据的程序*/
#include<stdio.h>
typedef struct node
{
char data;
int weight;
struct node *next;
}Node;
Node *CountWeight()
{
struct node *head,*move1,*move2,*new;
char c;int flag=0;
printf("please input the text end of ctrl+z\n");
head=move1=(Node *)malloc(sizeof(Node));
head->weight=0;
head->next=NULL;
while((c=getchar())!=EOF)
{
if(!head->weight)
{
head->data=c;
head->weight++;
}
else
{
move2=head;
while(move2!=NULL)
{
if(c==move2->data)
{move2->weight++;
flag=1;
break;
}
else move2=move2->next;
}
if(!flag)
{
new=(Node *)malloc(sizeof(Node));
new->data=c;
new->weight=0;
new->weight++;
move1->next=new;
move1=new;
move1->next=NULL;
}
}
}
return head;
}
void print(Node *p)
{
printf("the resault of the statistics is:\n");
while(p!=NULL)
{
printf("%c:\t%d\n",p->data,p->weight);
p=p->next;
}
}
void perform()
{
char str[10];
printf("please input the text which is only include 0 and 1\n");
gets(str);
printf("%s\n",str);
getch();
}
void main()
{
Node *Top;
Top=CountWeight();
print(Top);
perform();
}
不知道为什么在执行perform模块的时候,执行到语句gets(str);时总是不能停下来让我输入,大虾们帮我解决一下,高分相送!
问题点数:100、回复次数:8Top
1 楼zhuixe(竹子)回复于 2003-09-01 22:57:37 得分 0
可能是内存的问题Top
2 楼dazzle84(大峙)回复于 2003-09-01 23:05:19 得分 0
什么意思??
我找了好多同学,用他们的机子运行了,但是还是不行呀!!
你能不能用TC帮我看看Top
3 楼zhuixe(竹子)回复于 2003-09-01 23:05:26 得分 0
我运行了一下没问题,改成下面的程序试试
void main()
{
//Node *Top;
//Top=CountWeight();
//print(Top);
perform();
}Top
4 楼zhaohangcom(赵)回复于 2003-09-01 23:08:22 得分 0
mark ~要熄灯了Top
5 楼dazzle84(大峙)回复于 2003-09-01 23:11:15 得分 0
那样是没有问题,但是不注释掉就为什么不行呢???
前面的程序好像没有问题呀!!!???Top
6 楼zhuixe(竹子)回复于 2003-09-01 23:32:18 得分 0
改成就没问题了
while((c=getch())!=26) //26就是字符Ctrl^Z
{
printf("%c",c); //由于getch()不回显字符,故用printf()回显
Top
7 楼zhuixe(竹子)回复于 2003-09-01 23:33:52 得分 100
不过你的程序最后要注意释放内存,不然系统会受不了的Top
8 楼dazzle84(大峙)回复于 2003-09-01 23:50:12 得分 0
谢谢竹子!
很强呀!
问了好多人都没搞定呀!Top




