| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 505 人关注过本帖
标题:有人能来帮个忙么?关于一个C程序的,谢谢!
取消只看楼主 加入收藏
wrtqqq
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-7-25
收藏
 问题点数:0 回复次数:1 
有人能来帮个忙么?关于一个C程序的,谢谢!

这个是一个关于学生成绩管理的程序,其他部分没有问题,但是在最后1个子程序student *insert(student *head,student *new),第一次插入学生成绩可以,但是第2次到3个成绩输入完毕就不能进行程序了。麻烦高手帮我看看是哪里的问题?本程序在TURB C下可以直接编译~~~急!!!跪求!!!



#include <stdio.h>
#include<dos.h>
#include<stdlib.h>
#include<string.h>
#include<mem.h> /*内存操作函数*/
#include<ctype.h> /*字符操作函数*/
#include<alloc.h> /*动态地址分配函数*/
#define LEN sizeof(student)


typedef struct stu /*定义结构体数组用于缓存数据*/
{char num[6];
char name[5];
char CLASS[10];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}student;


student *init(); /*初始化函数*/
int menu_select(); /*菜单函数*/
student *create(); /*创建链表*/
void print(student *head); /* 显示全部记录*/
void search(student *head); /*查找记录*/
student *delete(student *head); /*删除记录*/
student *sort(student *head); /*排序*/
student *insert(student *head,student *new); /*插入记录*/


main()
{student *head,new;
head=init(); /*链表初始化,使head的值为NULL*/
for(;;) /*循环无限次*/
{switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,&new);break; /*&new表示返回地址*/
case 7:exit(0); /*如菜单返回值为7则程序结束*/
}
}
}


/*初始化函数*/
student *init()
{
return NULL; /*返回空指针*/
}

/*菜单选择函数*/
menu_select()
{int n;
struct date d; /*定义时间结构体*/
printf("\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("@@@@@ Attention @@@@@\n\n");
printf("1.The system only for study.\n\n");
printf("2.It is about student information.\n\n");
printf("3.You can press any key to enter the system.\n\n");
printf("4.Do it now\n\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\n");
printf(" Please Wait .........................");
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
clrscr(); /*清屏*/
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t This is student information system \n");
printf("\n\t\t WangHao 05030305\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MENU~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
printf("\t\t\t1. Please Enter the student record\n"); /*输入学生成绩记录*/
printf("\t\t\t2. Print the record of student\n"); /*显示*/
printf("\t\t\t3. Search record on name\n"); /*寻找*/
printf("\t\t\t4. Delete\n"); /*删除*/
printf("\t\t\t5. Sort the information again\n"); /*排序*/
printf("\t\t\t6. Insert a record to the list\n"); /*插入*/
printf("\t\t\t7. Quit\n"); /*退出*/
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
do{
printf("\n\t\t\tEnter your choice(1~7):");
scanf("%d",&n);
}while(n<1||n>7); /*如果选择项不在1~7之间则重输*/
return(n); /*返回选择项,主函数根据该数调用相应的函数*/
}

/*输入函数*/
student *create()
{int i,s;
char c;
student *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
list1:
clrscr();
for(;;)
{p=(student *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\nOut of date."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("Please enter the student num:");
scanf("%s",p->num);
if(p->num[0]=='0') break; /*如果学号首字符为0则结束输入*/
printf("Enter the name:");
scanf("%s",p->name);
printf("Enter the CLASS of the student:");
scanf("%s",p->CLASS);
printf("Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i<3;i++) /*3门课程循环3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0 || p->score[i]>100) /*确保成绩在0~100之间*/
printf("Data error,please enter again.\n");
}while(p->score[i]<0 || p->score[i]>100);
s=s+p->score[i]; /*累加各门成绩*/
}
p->sum=s; /*将总分保存*/
p->average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p->order=0; /*未排序前此值为0*/
p->next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
printf("Do you want to add anther student information? Y/N ?");
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') return(head);
if(c=='y'||c=='Y') goto list1;
}
}

}

/* 显示全部记录函数*/
void print(student *head)
{int i=0; /* 统计记录条数*/
student *p; /*移动指针*/
clrscr();
p=head; /*初值为头指针*/
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~student~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | CLASS | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d |%4s | %-4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p->num,p->name,p->CLASS,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}

/*查找记录函数*/
void search(student *head)
{student *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p->name,s) && p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~search~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
else
printf("\nThere is no num %s student on the list.\n",s); /*显示没有该学生*/
}

/*删除记录函数*/
student *delete(student *head)
{int n;
student *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1->num,s) && p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1->next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1->num,s)==0) /*学号找到了*/
{printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~record~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1->next; /*把第二个结点地址赋予head*/
else
p2->next=p1->next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\nNum %s student have been deleted.\n",s);
printf("\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}

/*排序函数*/
student *sort(student *head)
{int i=0; /*保存名次*/
student *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head->next; /*将原表的头指针所指的下一个结点作头指针*/
head->next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp->next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t->average<p1->average&&p1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t->next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t->next=p1; /*t的后继是p1*/
p2->next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1->order=i; /*将结点序号赋值给名次*/
p1=p1->next; /*指针后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}

/*插入记录函数*/
student *insert(student *head,student *new)
{student *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=new; /*p0指向要插入的结点*/
printf("\nPlease enter a new record.\n"); /*提示输入记录信息*/
printf("Enter the num:");
scanf("%s",new->num);
printf("Enter the name:");
scanf("%s",new->name);
printf("Enter the CLASS:");
scanf("%s",new->CLASS);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&new->score[i]);
if(new->score[i]>100||new->score[i]<0)
printf("Data error,please enter again.\n");
}while(new->score[i]>100||new->score[i]<0);
sum1=sum1+new->score[i]; /*累加各门成绩*/
}
new->sum=sum1; /*将总分存入新记录中*/
new->average=(float)sum1/3;
new->order=0;
if(head==NULL) /*原来的链表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*p1后移一个结点*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2->next=p0; /*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\nStudent %s have been inserted.\n",new->name);
return(head);
}


[此贴子已经被作者于2007-7-25 22:30:26编辑过]

搜索更多相关主题的帖子: include 内存 student 函数 
2007-07-25 22:25
wrtqqq
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-7-25
收藏
得分:0 
这个因该没关系的吧,加个clrscr();函数就可以了~~~~

你看看,我说的是在进入菜单6的时候,第一次的插入可以添加到记录里,第2次 到SCORE3的时候程序就不动了~~~~

按理来说基本功能是完成了,但是要做的完美点啊~~
2007-07-25 23:07
快速回复:有人能来帮个忙么?关于一个C程序的,谢谢!
数据加载中...
 
   



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

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