| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 483 人关注过本帖
标题:十万火急,关于一个C语言结构的程序的请教和求助
只看楼主 加入收藏
xiaol_zhong
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-12-13
收藏
 问题点数:0 回复次数:0 
十万火急,关于一个C语言结构的程序的请教和求助
数据结构综合实验
实验内容:
·学生数据结构成绩管理系统
[基本要求]
(1)学生信息及成绩的录入
    要求包括的学生信息有:学号,姓名,性别,出生日期,民族 及数据结构成绩(具体内容可自行假设,至少录入10名以上学生).
    所录入的学生按学号散列存储(散列函数为 学号%5 取整,如 1002%5 =2),采用拉链法解决冲突.
(2)学生成绩的查询
    要求根据提供的学号完成学生成绩的查询(必须采用散列查找).
(3)学生成绩的分段统计和排序输出
    统计出各分数段学生人数(60分以下,60~70,71~80,...)
    采用堆排序,将学生成绩从高到低排序输出.
以上为题目,下面为我写的程序:其中查找功能不能实现,其它功能皆没有问题,程序可以运行,希望各路英雄好汉,帮帮忙看看程序中查找的函数哪里出了问题,谢谢你们了(程序在附件中也有,大家帮助运行一下)
#include<stdio.h>
#include<stdlib.h>
#include <conio.h>
#define M 20
typedef struct node
{
 int num;
 char name[15];
 char sex[2];
 char birth[10];
 char nationality[10];
 int mark;
 struct node*link;
 }Node;
static Node stu[M];

 int N=0;
 void add();
 int h(int k);
 void sift(Node r[],int t,int w);
 void statis();
 void linkinsert(Node *t[5],int k,int n);
 void search();
 void printsort();
void main()
{    clrscr();
    printf("\n==========================System of Student's Imformation =====================");
    printf("\n\n\tFuntion:");
    printf("\n\n         1 build information \n\n         2 search  the information ");
    printf("\n\n         3 sort of information \n\n         4 statistics of the mark");
        printf("\n\n         5 exit the system\n\n");
    printf("         Choice:");
    switch(getchar())
    {    case '1':add();break;
        case '2':search();break;
        case '3':printsort();break;
        case '4':statis();break;
        case '5':exit(0);break;
     }
}
void add()/*添加学生信息函数*/
{

 pt1: clrscr();
 printf("Input the number");
 scanf ("%d",&stu[N].num); getchar();
 printf("Inut the name");
 gets(stu[N].name);
 printf("Input the sex");
 gets(stu[N].sex);
 printf("Input the birth");
 gets(stu[N].birth);
 printf("Input the nationality");
 gets(stu[N].nationality);
 printf("Input the mark");
 scanf("%d",&stu[N].mark); getchar();
 N++;
    printf("\ndo you want to continue? y/n");
    switch(getchar())
    {    case 'y':getchar();goto pt1;
        case 'n':getchar();main();
    }


}


int h(int k)/*散列函数*/
{return(k%5);}

void linkinsert(Node *t[],int k ,int n)/*向用拉链法处理冲突的散列表t中插入学号关键字为k的记录*/
{int i;
 Node * p;
 i=h(k);

 if(t[i]==NULL)
   {
    p=(Node * )malloc(sizeof(Node));
        p->num=k;strcpy(p->name,stu[n].num );
         strcpy(p->name,stu[n].sex);  strcpy(p->birth,stu[n].birth);
         strcpy(p->nationality,stu[n].nationality);p->mark=stu[n].mark;
    p->link=NULL;
    t[i]=p;
    return;
    }
 else
 {
  p=t[i];
  while(p!=NULL)
       if(p->link!=NULL) p=p->link;
    else{p->link=(Node *)malloc(sizeof(Node));
         p=p->link;p->num=k;   strcpy(p->name,stu[n].num );
         strcpy(p->name,stu[n].sex);  strcpy(p->birth,stu[n].birth);
         strcpy(p->nationality,stu[n].nationality);
         p->mark=stu[n].mark;
         p->link=NULL;
         return;

          }
 }
}

Node *linksearch (Node * t[],int k)/*在用拉链法处理冲突的散列表t中查找关键字为给定值k的记录*/
{Node *p;
 int i;
 i=h(k);
 if(t[i]==NULL) return(NULL);
 p=t[i];
 while(p!=NULL)
   if(p->num==k) return(p);
   else p=p->link;
 return(NULL);
}


void search()/*输入学号,进行查找*/
{int n, n1,k;
 Node * stu1;
 Node *t[5];
 for(n=0;n<5;n++)t[n]=NULL;
 for(n1=0;n1<N;n1++)
 {
  linkinsert( t,stu[n1].num,n1);}/*把各学生信息数组stu[]建立散列表t[5]*/
 printf("Please input the number you want to search\n\n");
 scanf("%d",k);getchar();
 stu1=linksearch (t, k);/*输入学生学号向散列表中查找*/
 if(stu1==NULL)printf(" not the record!");
 else
   {printf("num        name         sex       birth     nationality  mark\n\n\n");
    printf("%-7d%-15s%-6s%-15s%-15s%-10d",stu1->num,stu1->name,stu1->sex,stu1->birth,stu1->nationality,stu1->mark);
   }  
 printf("\nIf you want to return,please input'y'.\n");
 switch(getchar())
 {case 'y': getchar();main();}
}

 
void sift(Node r[],int t,int w)/*用筛选法调整堆*/
{
int i,j;
Node x;
i=t;
x=r[i];
j=2*i+1;
while(j<=w)
{if((j<w)&&(r[j].mark>r[j+1].mark))
j++;
if(x.mark>r[j].mark)
{r[i]=r[j];i=j;j=2*j+1;}
else break;
}
r[i]=x;
}
void heapsort(Node r[],int n)/*堆排序*/
{
int i;
Node x;
for(i=n/2-1;i>=0;i--)
sift(r,i,n-1);
for(i=n-1;i>0;i--)
{x=r[0];r[0]=r[i];
r[i]=x;
sift(r,0,i-1);
}
}

void printsort()/*按分数从高到低输出各学生的记录*/
{int i;
 heapsort(stu,N);
 clrscr();
 getchar();
 for(i=0;i<N;i++)
    { printf("num    name           sex     birth         nationality    mark\n\n");
      printf("%-7d%-15s%-6s%-15s%-15s%-10d\n\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].birth,stu[i].nationality,stu[i].mark);
    }

 printf("\nIf you want to return,please input 'y'.\n");
 switch(getchar())
 {case 'y': getchar();main();}
}





 void statis()/*统计各分数段的人数*/
 { int s,n,n1=0,n2=0,n3=0,n4=0,n5=0;
   getchar();
  for(n=0;n<N;n++)
  {
   if(stu[n].mark<60)s=1;
   else s=stu[n].mark/10-4;
   switch(s)
   {
   case 1:n1++;break;
   case 2:n2++;break;
   case 3:n3++;break;
   case 4:n4++;break;
   case 5:n5++;break;}
 
  }

  printf("Under the 60:%d",n1);
  printf("\n60 ~ 70 :%d",n2);
  printf("\n70 ~ 80 :%d",n3);
  printf("\n80 ~ 90 :%d",n4);
  printf("\n90 ~ 100: %d",n5);
  printf("\n\nInput the 'y' to return:");
  switch(getchar()){case 'y':getchar();main();}
 }

yy.rar (2.15 KB)
搜索更多相关主题的帖子: 结构 C语言 
2008-12-13 21:36
快速回复:十万火急,关于一个C语言结构的程序的请教和求助
数据加载中...
 
   



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

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