Free考研资料 - 免费考研论坛

 找回密码
 注册
打印 上一主题 下一主题

这个C程序错在哪儿?

[复制链接]
跳转到指定楼层
楼主
zhjnjnu 发表于 08-11-3 22:36:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
哪位高手帮忙看看,这个C程序为什么不能在Tubor C上运行?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
沙发
zanghongtu2006 发表于 08-11-4 18:30:51 | 只看该作者
这么短的程序,下次直接贴就行了。
弄个word,给你调程序还得费考元,你觉得有几个人愿意下啊?
还费论坛资源。
板凳
zanghongtu2006 发表于 08-11-4 18:36:06 | 只看该作者
能告诉我你的程序想干吗么?
你把struct student *creat(void)中void删了就能运行了。
不过真的不知道你这个程序是干涉么的。
运行过程一点提示都没有。输入什么都不知道。我也没时间猜了,你照着改了自己调吧。
地板
 楼主| zhjnjnu 发表于 08-11-4 21:56:50 | 只看该作者
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;float score;struct student *next;};
int n;

struct student *creat()?/*这个函数用于创建链表,返回值为链表的头指针*/
{struct student *head;
struct student *p1,*p2;n=0;
p1=p2=(struct student *)malloc(LEN);
scanf(\"%ld,%f\",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf(\"%ld,%f\",&p1->num,&p1->score);/*输入学生的学号和分数*/
}
p2->next=NULL;
return(head);
}
void print(struct student *head)/*这个函数用于输出这个链表*/
{ struct student *p;
  printf(\"\\nNow,These %d records are:\\n\",n);
  p=head;
  if(head!=NULL)
   do
     {printf(\"%ld %5.1f\\n\",p->num,p->score);
      p=p->next;
     }while(p!=NULL);
}
void main()
{ struct student *head;
  head=creat();/*创建链表*/
  print(head);/*输出链表*/
  printf(\"\\n\");
}
这其实是C程序设计(第三版)(谭浩强)上的几个小程序,被我整合成一个程序,编译没错,但运行时输入数据后出现错误,真的很郁闷,望高手指点。
5#
leeosun 发表于 08-11-4 22:26:32 | 只看该作者
这个程序大部分是对的啊,吧main前的void去掉,然后去掉#define NULL 0
即可,我在DEV-C++下运行,结构正确噢
6#
zanghongtu2006 发表于 08-11-4 23:08:42 | 只看该作者
struct student *creat()?/*这个函数用于创建链表,返回值为链表的头指针*/ {struct student *head;
就这一句,把creat()后的?去掉就全好了。
你那个问号哪儿来的?
7#
wonder858 发表于 08-11-5 21:58:48 | 只看该作者

对着呢

对着呢,我在VC++下只删除了 create()后的  ? 就可以运行了  没错啊。
根据你的程序,我提一下建议:
在从前往后建链表时用   do.....while()
输出时   用while()
更为合适


呵呵呵
8#
wonder858 发表于 08-11-5 22:06:00 | 只看该作者

使用指针可能的问题

没结果 可能隐含指针错的原因
指针悬空    p->next不空才可取p->next的成员
建链表时  不要指错了  或忘了 指向 等
您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系我们|Free考研资料 ( 苏ICP备05011575号 )

GMT+8, 24-10-1 15:28 , Processed in 0.136424 second(s), 12 queries , Gzip On, Xcache On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表