| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 962 人关注过本帖
标题:这个程序哪里出现问题了?为什么不能继续输入信息呢?
只看楼主 加入收藏
初学者6
Rank: 2
等 级:论坛游民
帖 子:34
专家分:20
注 册:2016-12-30
结帖率:66.67%
收藏
 问题点数:0 回复次数:0 
这个程序哪里出现问题了?为什么不能继续输入信息呢?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
 
typedef struct student
 {
    long number;
    char name[10];
    char sex[10];
    char nation[20];
    long birthday;
    int Shuju;
struct student *next;
 }student,*stu;

 typedef stu Hash;
 typedef struct
 {
     int r[20];//记录分数
     int length;//长度
 }HeapType;
 int hash(int key);
 int Build_Hash(Hash *H,stu st);//创建哈希表
 int Search(Hash *H,int key);
 void HeapAdjust (HeapType &H , int low, int high);
 void HeapSort(HeapType &H);//heap:堆
 void Segment(HeapType h);//segment:分割划分
 int Sort(Hash *H);//分类
 void create(Hash *H);
 void Find(Hash *H);//查找哈希表
 void tongji(Hash *H);//统计
 void savedata(student *&p); //存储数据
 student *getdata(); //获取数据
 void main()
 {
     int n,i;
     student *getdata();
     Hash H[5];//哈希表
     for(i=0;i<5;i++)
     {
         H[i]=NULL;//对哈希表各表头初始化为 NULL
     }
     for(;;)
     {
         printf("\t\t\t* * * * 学生成绩管理系统 * * * *\n");
         printf("\n\t\t\t 1.创建并录入学生相关信息\n");  
         printf("\t\t\t 2.按学号查询学生成绩信息 \n");
         printf("\t\t\t 3.分段统计成绩与排序输出  \n");
         printf("\t\t\t 0.退出管理系统 \n");
         printf("\n\t\t\t* * * * * * * * * * * * * * * * *\n");
         printf("请选择 0-3:");
         scanf("%d",&n);
         switch(n)
         {
         case 1: create(H);break;
         case 2: Find(H);break;  
         case 3: tongji(H);break;
         case 0:exit(0);
             printf("谢谢使用...");
         default: printf("\n\n 您的输入有误!请重新输入:\n\n");
            break;
         }
     }
 }
 int hash(int key) //用除留余数法构造哈希函数 散列存储 输入一个关键字Key
 {
     return key=key%5;
 }
 int Build_Hash(Hash *H,stu st)//输入一组关键字,建立 Hash 表,用链地址法处理冲突
 {
     stu p;//指针P
     int i;
     i=hash(st->number);
     if(H[i]==NULL)
     {
         H[i]=st; //作为链表的第一个结点
     }
     else
     {
         for(p=H[i];p->next;p=p->next);
         {
             if(p->number==st->number)
             {
                 printf(" 已存在该学号的学生,添加学生信息出错!\n");
                 return 0;
             }
             p->next=st; //插入链表尾部.
         }
     }
     return 1;
 }
 int Search(Hash *H,int key)//成绩查询
 {
     int i;
     stu p;
     i=hash(key);
     if(H[i])
     {
         if((H[i]->number==key))//在表头
         {
             printf("学号:%d\n姓名:%s\n",key,H[i]->name);
             printf("性别:%s\n出生日期:%d\n民族:%s\n",H[i]->sex,H[i]->birthday, H[i]->nation);
             printf("\n分数如下:\n");
             printf("数据结构:%d\n ",H[i]->Shuju);
             return 1;
         }
         else//探测下一个地址,在链表中查找
         {
             for(p=H[i]->next;p;p=p->next)
             {
                 if(p->number==key)
                 {
                     printf("学号:%d\n姓名:%s\n",key,H[i]->name);
                     printf("性别:%s\n出生日期:%d\n民族:%s\n",H[i]->sex, H[i]->birthday, H[i]->nation);
                     printf("\n分数如下:\n");
                     printf("数据结构:%d\n ",H[i]->Shuju);
                     return 1;
                 }
             }
         }
     }
     return 0;
 }
 void HeapAdjust (HeapType &H , int low, int high)//堆排序
 {
     int rc; //rc 是 low
     int j;
     rc=H.r[low];   
     for(j=2*low;j<=high;j*=2 )  //j 沿 Key 较大的孩子结点向下筛选 //r[j]是 r[i]的左孩子
     {
         if ( j<high && H.r[j]<H.r[j+1])
             ++j;
             if (!(rc<H.r[j] )) break;  //rc 应插入在位置 s 上
             H.r[low] = H.r[j]; // j 为 Key 较大记录的下标
             low = j;
     }
     H.r[low] = rc; //插入
 }
 void HeapSort(HeapType &H)
 {
     int t,i;
     for(i=H.length/2;i>0;--i) //循环建立初始堆
         HeapAdjust ( H, i, H.length );// 建大顶堆
     for(i=H.length;i>1;--i)
     {
         t=H.r[1]; //将堆顶记录和当前未经排序子序列 H.r[1..i]中最后一个记录相互交 换
         H.r[1]=H.r[i];
         H.r[i]=t;
         HeapAdjust(H,1,i-1); //对 H.r[1..i-1]重新调整为大顶堆
     }
 }
 void Segment(HeapType h)//统计各个分数段的人数
 {
     int a,b,c,d,e;
     int i;
     a=b=c=d=e=0;
     for(i=1;i<=h.length;i++)
     {
         if(h.r[i]>=90)
             a++;
         else if(h.r[i]>=80)
             b++;
         else if(h.r[i]>70)
             c++;
         else if(h.r[i]>60)
             d++;
         else e++;
     }
     printf("\n90 分以上的有%d 人\n",a);
     printf("80-90 分的有%d 人\n",b);
     printf("70-80 分的有%d 人\n",c);
     printf("60-70 分的有%d 人\n",d);
     printf("60 分以下的有%d 人\n",e);
 }
 int Sort(Hash *H)//使用堆排序对数据结构成绩按从高到低排列输出
 {
     HeapType a;
     int i,j;
     stu st;
     j=1;
     a.length=0; //把哈希表中的数据放到堆结构中
     for(i=0;i<5;i++)
     {
         if(H[i])
         {
             st=H[i];
             while(st)
             {
                 a.r[j]=st->Shuju;
                 j++;
                 a.length++;
                 st=st->next;
            }
         }
    }
     if(a.length>1) //数据结构成绩
     {
         HeapSort(a);
         printf("数据结构成绩从高到低排列为:");
         for(i=a.length;i>0;i--)
            printf("%3d",a.r[i]);
         Segment(a);
     }
     return 0;
 }
 void create(Hash *H)//录入学生信息
 {
     FILE *fp;
     fp=fopen("D:\\stu.txt","wb");
     stu st;
     int m;
     int c;
     c=1;
     m=0;
     while(c)
     {
         m++;
         st=(stu)malloc(sizeof(student));
         st->next=NULL;
         st->number=0;
         printf("\n请输入第%d 名学生的学号:",m);
         scanf("%d",&st->number);
         fprintf(fp,"%d",st->number);
         printf("请输入姓名:");
         scanf("%s",st->name);
         fprintf(fp,"%s",st->name );
         printf("请输入性别:");
         scanf("%s",st->sex);
         fprintf(fp,"%s",st->sex );
         printf("请输入出生日期:");
         scanf("%d",st->birthday );
         fprintf(fp,"%d",st->birthday);
         printf("请输入民族:");
         scanf("%s",st->nation);
         fprintf(fp,"%s",st->nation );
         st->Shuju=0;
         printf("请输入数据结构成绩:");
         scanf("%d",&st->Shuju);
         fprintf(fp,"%d",st->Shuju);
         Build_Hash(H,st);
         printf("\n 继续?选(1/0):");
         scanf("%d",&c);
     }
     fclose(fp);
     printf("\n*********此系统共有%d 名学生!*************\n",m);
 }
 void Find(Hash *H)//查询学生信息
 {
     int key=0;
     int c;
     c=1;
     while(c)
     {
         printf("\n 请输入要查询学生的学号:");
         scanf("%d",&key);
         if(!Search(H,key))
             printf("不存在该学生的信息!\n");
         printf("要继续查询吗?输入 1 继续,输入 0 结束!(1/0):");
         scanf("%d",&c);
     }
 }
 //分段统计及排序
void tongji(Hash *H)
{
    Sort(H);
}
void upnation(stu p)
{
    FILE *fp;
    if((fp=fopen("D:\\stu.txt", "wb"))==NULL)
    {
         printf( "文件无法打开.\n ");
         exit(0);
     }
     while(p!=NULL)
     {
         fwrite(p,sizeof(student),1,fp);//写入数据
         p=p->next;
     }
     fclose(fp);//关闭文件
}
void savedata(student *&p)
{
    FILE *fp;
    if((fp=fopen("D:\\data.dat", "ab"))==NULL)
    {
        printf( "文件无法打开.\n ");
        exit(0);
     }
    while(p!=NULL)
    {
        fwrite(p,sizeof(student),1,fp);//写入数据
        p=p->next;
     }
     fclose(fp);//关闭文件
}
student *getdata()
{
    FILE *fp;
    student *p, *head=NULL, *tail=NULL;
    p=(student *)malloc(sizeof(student));
    if((fp=fopen("D:\\stu.dat","rb"))==NULL)
    {
        printf("文件不存在.\n");
        exit(0);
     }
    while(fread(p,sizeof(student),1,fp))
    {
        if(head==NULL)
        {
            head=p; tail=p;
        }
        else
        {
            p=(student*)malloc(sizeof(student));
            tail->next=p;               
            tail=p;
            tail->next=NULL;

        }
    }
     fclose(fp);
     system("cls");
     return head;
 }
搜索更多相关主题的帖子: include number nation 信息 
2017-01-04 15:24
快速回复:这个程序哪里出现问题了?为什么不能继续输入信息呢?
数据加载中...
 
   



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

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