主题:[求助]课程设计有点错。。
roader2046
[专家分:0] 发布于 2006-12-28 15:33:00
题目:学生成绩管理是学校教务管理的重要组成部分,其处理信息量很大,本实验是对学生的成绩管理作一个简单的模拟,用菜单选择操作方式完成下列功能:
(1)学生成绩;
(2)查询学生成绩;
(3)插入学生成绩;
(4)删除学生成绩。
我的程序:(能运行,但是功能不全,希望高手指点!)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define ERROR 0
#define OK 1
typedef struct Node
{
int id,score;
char name[21];
struct Node *next;
}students,*Linklist;
Linklist creat_new(){ //建立单链表
Linklist L=NULL;
students *s;
int id,score;
char name[21];
int flag=0;
printf("Please input the ID:\n",id);
scanf("%d",&id);
while (id!=flag)
{
s=new students;
s->id=id;
printf("Please input the NAME:\n",name);
scanf("%s",&name);
strcpy(s->name,name);
printf("Please input the SCORE:\n",score);
scanf("%d",&score);
printf("Please input the ID(Enter 0 to end):\n",id);
scanf("%d",&id);
s->score=score;
s->next=L;
L=s;
}
return L;
}
students *Get_Linklist(Linklist L,int id){ //以序号查找
students *s;
int j=1;
s=L;
while(s->next!=NULL&&j<=id)
{ s=s->next;
j++;
}
if(j==id)
return s;
else
return NULL;
}
int Insert_Linklist(Linklist L,int pos,int id,char name[],int score){
students *p,*s;
p=Get_Linklist(L,pos-1);
if(p==NULL)
{ printf("The position was wrong!\n");
return ERROR;
}
else
{ s=new students;
s->id=id;
strcpy(s->name,name);
s->score=score;
s->next=p->next;
p->next=s;
return OK;
}
}
int Del_Linklist (Linklist L,int pos){ //删除数据
students *p,*s;
p=Get_Linklist(L,pos-1);
if(p==NULL)
{ printf("The position don't exit!\n");
return ERROR;
}
else
if(p->next==NULL)
{ printf("The position don't exit!\n");
return ERROR;
}
else
{ s=p->next;
p->next=s->next;
delete s;
return OK;
}
}
main()
{
int t=1;
int pos,i;
Linklist L;
students *s;
int id,score;
char chocie,name[21];
while(t)
{ printf("\n");
printf("**********************************\n");
printf("* Welcome to the manage system! *\n");
printf("* Creat--------1 *\n");
printf("* Find---------2 *\n");
printf("* Insert-------3 *\n");
printf("* Delete-------4 *\n");
printf("* Quit---------5 *\n");
printf("**********************************\n");
printf("Please enter your chocie:\n");
chocie=getchar();
if(chocie=='1')creat_new();
else
if(chocie=='2')
{ printf("Please input the ID:\n");
scanf("%d",&i);
if(i==s->id)
{ Get_Linklist(L,i);
printf("%d %-5s %-5d\n",s->id,s->name,s->score) ; }
else printf("ERROR!\n");
}
else
if(chocie=='3')
{ printf("Please input the POSITION:\n");
scanf("%d",&pos);
printf("Please input the ID:\n");
scanf("%d",&id);
printf("Please input the NAME:\n");
scanf("%s",name);
printf("Please input the SCORE:\n");
scanf("%d",&score);
Insert_Linklist(L,pos,id,name,score);
}
else
if(chocie=='4')
{ printf("Please input the ID:\n");
scanf("%d",&pos);
if(pos==s->id)
Del_Linklist(L,pos);
else printf("ERROR!\n");
}
else
if(chocie=='5')
{
t=0;
}
}
}
回复列表 (共10个回复)
沙发
雪光风剑 [专家分:27190] 发布于 2006-12-28 15:44:00
不太严格
严格些应该给L和s先分配空间
板凳
雪光风剑 [专家分:27190] 发布于 2006-12-28 15:51:00
if(chocie=='1')creat_new();
else
if(chocie=='2')
{ printf("Please input the ID:\n");
scanf("%d",&i);
if(i==s->id)
{ Get_Linklist(L,i);
printf("%d %-5s %-5d\n",s->id,s->name,s->score) ; }
else printf("ERROR!\n");
}
else
if(chocie=='3')
{ printf("Please input the POSITION:\n");
scanf("%d",&pos);
printf("Please input the ID:\n");
scanf("%d",&id);
printf("Please input the NAME:\n");
scanf("%s",name);
printf("Please input the SCORE:\n");
scanf("%d",&score);
Insert_Linklist(L,pos,id,name,score);
}
else
if(chocie=='4')
{ printf("Please input the ID:\n");
scanf("%d",&pos);
if(pos==s->id)
Del_Linklist(L,pos);
else printf("ERROR!\n");
}
else
if(chocie=='5')
{
t=0;
}
}
这里写法不好
应该是
g=getch();
while(g!='5')
{
switch g
{
case'1':......
break;
case'2':......
break;
case'3':......
break;
case'4':......
break;
default
......
}
还有你的search环节有错 if(chocie=='2')
{ printf("Please input the ID:\n");
scanf("%d",&i);
if(i==s->id)
这里你还没有调用查找函数查询s->id为i的节点是否存在就直接开始取数据操作了
这里应该写成一个独立的Linklist Search(L,i),返回值为目标节点(存在)或者空节点(不存在)指针
3 楼
roader2046 [专家分:0] 发布于 2006-12-28 15:59:00
恩,我当初也想用SWITCH,但是不知道怎么的,用SWITCH,不管输入什么都退出来
这是SWITCH版的:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define ERROR 0
#define OK 1
typedef struct Node
{
int id,score;
char name[21];
struct Node *next;
}students,*Linklist;
Linklist creat_new(){ //建立单链表
Linklist L=NULL;
students *s;
int id,score;
char name[21];
int flag=0;
printf("Please input the ID:\n",id);
scanf("%d",&id);
printf("Please input the NAME:\n",name);
scanf("%s",&name);
printf("Please input the SCORE:\n",score);
scanf("%d",&score);
while (id!=flag)
{ s=new students;
s->id=id;
strcpy(s->name,name);
s->score=score;
s->next=L;
L=s;
scanf("%d",&id);
}
return L;
}
students *Get_Linklist(Linklist L,int id){ //以序号查找
students *p;
int j=0;
p=L;
while(p->next!=NULL&&j<id)
{ p=p->next;
j++;
}
if(j==id)
return p;
else
return NULL;
}
int Insert_Linklist(Linklist L,int pos,int id,char name[],int score){
students *p,*s;
p=Get_Linklist(L,pos-1);
if(p==NULL)
{ printf("The position was wrong!\n");
return ERROR;
}
else
{ s=new students;
s->id=id;
strcpy(s->name,name);
s->score=score;
s->next=p->next;
p->next=s;
return OK;
}
}
int Del_Linklist (Linklist L,int pos){ //删除数据
students *p,*s;
p=Get_Linklist(L,pos-1);
if(p==NULL)
{ printf("The position don't exit!\n");
return ERROR;
}
else
if(p->next==NULL)
{ printf("The position don't exit!\n");
return ERROR;
}
else
{ s=p->next;
p->next=s->next;
delete s;
return OK;
}
}
main()
{
int chocie,pos,i;
Linklist L;
students *s;
int id,score;
char name[21];
printf("\n");
printf("**********************************\n");
printf("* Welcome to the manage system! *\n");
printf("* Creat--------1 *\n");
printf("* Find---------2 *\n");
printf("* Insert-------3 *\n");
printf("* Delete-------4 *\n");
printf("* Quit---------5 *\n");
printf("**********************************\n");
printf("Please enter your chocie:\n");
scanf("%d",chocie);
switch(chocie)
{
case 1: creat_new();break;
case 2: { printf("Please input the ID:\n");
scanf("%d",&i);
if(i==s->id)
Get_Linklist(L,i);
else printf("ERROR!\n"); }break;
case 3: { printf("Please input the POSITION:\n");
scanf("%d",&pos);
printf("Please input the ID:\n");
scanf("%d",&id);
printf("Please input the NAME:\n");
scanf("%s",name);
printf("Please input the SCORE:\n");
scanf("%d",&score);
Insert_Linklist(L,pos,id,name,score); } break;
case 4: { printf("Please input the ID:\n");
scanf("%d",&pos);
if(pos==s->id)
Del_Linklist(L,pos);
else printf("ERROR!\n");}break;
case 5: exit(0);
}
}
那个查找函数是Get_Linklist啊,是独立的啊,能麻烦帮我改下吗?明天就要交了
4 楼
雪光风剑 [专家分:27190] 发布于 2006-12-28 16:08:00
晕了
刚发现你写的所有东西都不对
原因是你返回的是队尾指针!
而正确的单链表应该保存的是队头指针
基本上得有1/3东西重写
偶现在还在写论文呢
偷懒来回个帖的
5 楼
雪光风剑 [专家分:27190] 发布于 2006-12-28 16:10:00
你的程序没有做死循环
当然执行一下就会退掉的……
6 楼
雪光风剑 [专家分:27190] 发布于 2006-12-28 16:11:00
没有显示和返回也都是因为返回了队尾指针
7 楼
roader2046 [专家分:0] 发布于 2006-12-28 16:11:00
啊???不是吧...明天就要交了啊,1/3要重写??
55...大哥帮帮忙吧..
8 楼
roader2046 [专家分:0] 发布于 2006-12-28 16:14:00
大哥,麻烦帮我改下吧,差不 多就行,明天就要交了,自己弄实在赶不上了..
9 楼
雪光风剑 [专家分:27190] 发布于 2006-12-28 16:24:00
今天偶不睡自己的东西也赶不完
给你个建议:
找本数据结构的书
把所有的标准链表运算都写一遍
然后再套到这道题上
2小时内就能稿定
10 楼
roader2046 [专家分:0] 发布于 2006-12-28 16:53:00
呵呵,那我就只好自己弄了,还是谢谢你啊
我来回复