| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 612 人关注过本帖
标题:[求助]程序错误无法查出来
只看楼主 加入收藏
肖肖云南
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-8-15
收藏
 问题点数:0 回复次数:5 
[求助]程序错误无法查出来

请帮我看看这个题到底错在哪里?提示有三处错。



题目(8) 用链表存放学生数据。用结构体数组来存放学生数据是静态存储方法,浪费内存空间。现在我们改用链表来处理,每一结点中存放一个学生的数据。程序由三个函数组成,new_record函数用来新增加一个结点,listall函数用来打印输出已有的全部结点中的数据。程序开始运行时若键入“E”或“e”则表示要进行增加新结点的操作,若键入“L”,或“l”,表示要输出所有结点中数据。

#include"stdlib.h"
#include"stdio.h"
struct stud
{
char name[20];
long num;
int age;
char sex;
float score;
struct stud*next;
};
struct stud*head,*this,*new;
main()
{
char ch;
int flag=1;
head=NULL;
while(flag)
{
printf("\ntype'E'or'e'to enter new record,");
printf("type'L'or'l'to list all record:");
ch=getchar();getchar();
switch(ch)
{
case'e':
case'E': new record();break;
case'l':
case'L': listall();break;
default:flag=0;
}/*end switch*/
}/*end while*/
}/*end main*/
void new_record(void)
{
char numstr[20];
new=(struct stud*)malloc(sizeof(struct stud));
if(head==NULL)
head=new;
else
{
this=head;
while(this->next!=NULL)
this=this->next;
this->next=new;
}
this=new;
printf("\enter name: ");
gets(this->name);
printf("\nenter numeber: ");
gets(numstr);
this->num=atoi(numstr);
printf("\nenter age: ");
gets(numstr);
this->age=atoi(numstr);
printf("\nenter sex: ");
this->sex=getchar();
getchar();
printf("\nenter score: ");
gets(numstr);
this->score=atof(numstr);
this->next=NULL;
}
void listall(void)
{
int i=0;
if(head==NULL)
{
printf("\nempty list.\n");
return;
}
this=head;
do
{
printf("nrecord number %d\n",++i);
printf("name:%s\n",this->name);
printf("num:%ld\n",this->num);
printf("age:%d\n",this->age);
printf("sex:%c\n",this->sex);
printf("score:%6.2f\n",this->score);
this=this->next;
}while(this!=NULL);
}

运行情况如下:
type ’E’or’e’ to enter new record,type ’L’or’l’ to list all record:e↙
enter name:wangli↙
enter number:89101↙
enter age:18↙
enter sex:m↙
enter score:89.5↙
type ’E’or’e’ to enter new record,type ’L’or’l’ to list all record:e↙
enter name:zhangfu↙
enter number:89102↙
enter age:19↙
enter sex:m↙
enter score:90.5↙
type ’E’or’e’ to enter new record,type ’L’or’l’ to list all record:L↙
record number 1
name:wangli
num:89101
age:18
sex:m
score:89.50
record number 2
name:zhangfun
num:89102
age:19
sex:m
score:90.50
type ’E’or’e’ to enter new record,type ’L’or’l’ to list all record:c↙


http://single.xhblog.com/archives/2007/209693.shtml

[此贴子已经被作者于2007-8-25 8:42:40编辑过]

搜索更多相关主题的帖子: 程序错误 
2007-08-23 14:43
ConZhang
Rank: 1
来 自:北京
等 级:新手上路
帖 子:282
专家分:0
注 册:2007-8-7
收藏
得分:0 
2007-08-23 22:59
奔跑的鸟
Rank: 1
等 级:新手上路
帖 子:391
专家分:0
注 册:2006-1-20
收藏
得分:0 
..lz偷懒啊..

简单的快乐着~
2007-08-23 23:01
肖肖云南
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-8-15
收藏
得分:0 
怎么都偷懒呀,呵呵~~~~帮我看看嘛~~~~我自己又查出来一处
2007-08-24 14:26
肖肖云南
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-8-15
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

提示就是这样,请大家帮我看看呀,谢谢了
2007-08-25 08:45
coachard
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:1251
专家分:0
注 册:2007-8-12
收藏
得分:0 

#include <stdlib.h>
#include <stdio.h>
struct stud
{
char name[20];
long num;
int age;
char sex;
float score;
struct stud* next;
};
struct stud *head,*this1,*new1;
void new_record(void);
void listall(void);
int main(void)
{
char ch;
int flag=1;
head=NULL;
while(flag)
{
printf("\ntype'E'or'e'to enter new record,");
printf("type'L'or'l'to list all record:");
ch=getchar();getchar();
switch(ch)
{
case'e':
case'E': new_record();break;
case'l':
case'L': listall();break;
default:flag=0;
}/*end switch*/
}/*end while*/
return 0;
}/*end main*/
void new_record(void)
{
char numstr[20];
new1=(struct stud *)malloc(sizeof(struct stud));
if(head==NULL)
head=new1;
else
{
this1=head;
while(this1->next!=NULL)
this1=this1->next;
this1->next=new1;
}
this1=new1;
printf("\enter name: ");
gets(this1->name);
printf("\nenter numeber: ");
gets(numstr);
this1->num=atoi(numstr);
printf("\nenter age: ");
gets(numstr);
this1->age=atoi(numstr);
printf("\nenter sex: ");
this1->sex=getchar();
getchar();
printf("\nenter score: ");
gets(numstr);
this1->score=atof(numstr);
this1->next=NULL;
return;
}
void listall(void)
{
int i=0;
if(head==NULL)
{
printf("\nempty list.\n");
return;
}
this1=head;
do
{
printf("nrecord number %d\n",++i);
printf("name:%s\n",this1->name);
printf("num:%ld\n",this1->num);
printf("age:%d\n",this1->age);
printf("sex:%c\n",this1->sex);
printf("score:%6.2f\n",this1->score);
this1=this1->next;
}while(this1!=NULL);
return;
}


PS:语法帮你改好了,逻辑就不知道正不正确了。用了太多编译器默认的关键字,而且很粗心~~~~~~

偶学编程,也许本身就是一个错。。。
2007-08-25 09:14
快速回复:[求助]程序错误无法查出来
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020447 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved