在完成插入操作时,插入的元素都存在了最后,如果插入的时候按图片所示插学号,显示就是1,3,2的顺序,现在想完成插入操作时学号按顺序输出,1,2,3,大家帮忙给个算法,或者帮我改改我的插入算法能达到要求。谢谢。

糊涂的小神仙 发布于 2013/12/03 20:03
阅读 116
收藏 0
#include "stdio.h"
#include "malloc.h"
#define MAX 10
struct student;
typedef struct student *stu;
struct student
{
	int num[MAX];
	int score[MAX];
	int length;
};
stu info()
{
	stu s;
	int i,n;
    printf("用来录入学生信息\n");
    printf("键盘输入要录入学生人数:");
    scanf("%d",&n);
	s=(stu)malloc(sizeof(struct student));
	if(n>MAX)
	{
		printf("the space is not exist!\n");
	}
	else 
		s->length=n;
	for(i=0;i<n;i++)
	{
		printf("输入录入学生的学号:");
		scanf("%d",&s->num[i]);
	    printf("输入录入学生的成绩");
		scanf("%d",&s->score[i]);
	}
   printf("显示已录入学生的相关信息\n");
   for(i=0;i<n;i++)
   {
	   printf("学号%d     成绩 %d\n",s->num[i],s->score[i]);
   }
   return s;
}
locate(stu s,int num)
{
	int i=0;
	int score;
	stu ss;
	ss=s;
    while(ss->num[i]!=num&&i<ss->length)
    i++;
	if(i>=ss->length)
		printf("The student is not exist!\n");
	else
	{
	   score=ss->score[i];
	   printf("要查询的学生的成绩为:%d\n",score);
	}
   for(i=0;i<ss->length;i++)
   {
	   printf("学号%d     成绩 %d\n",s->num[i],s->score[i]);
   }
}
insert(stu s,int num,int score)
{
	int i=0;
	stu ss=(stu)malloc(sizeof(struct student));
	ss=s;
   	while(ss->num[i]!=num&&i<ss->length)
		i++;
	if(i<ss->length)
		printf("This num has been exist!\n");
	else
	{
       ss->num[ss->length]=num;
	   ss->score[ss->length]=score;
	   ss->length++;
	   printf("插入成功\n");
	   printf("学号%d    成绩%d\n",ss->num[ss->length]=num, ss->score[ss->length]=score);
	} 
	 printf("执行插入操作后录入的学生信息为:\n");
      if(ss->num[i]<num)
		  ss->num[i]=num;
	  ss->num[i]=ss->num[i++];
	 for(i=0;i<ss->length;i++)
		{ 
           printf("学号%d     成绩 %d\n",s->num[i],s->score[i]);
		}
}
update(stu s,int num,int score)
{
    int i=0;
	stu ss=(stu)malloc(sizeof(struct student));
 	ss=s;
   while(ss->num[i]!=num&&i<ss->length)
		i++;
	if(i>=ss->length)
		printf("The student's num is not exist!\n");
	else
	{   printf("输入修改后的成绩:\n");
		scanf("%d",&score);
		ss->score[i]=score;
		printf("修改成功");
		printf("学号%d     成绩%d\n",ss->num[i],ss->score[i]);
	} 
	printf("执行修改操作后录入的学生信息为:\n");
	for(i=0;i<ss->length;i++)
   {
	
		printf("学号%d     成绩 %d\n",s->num[i],s->score[i]);
   }
}
 delet(stu s,int h)
{
   int a,i=0;
   stu ss=(stu)malloc(sizeof(struct student));
   ss=s;
   while(ss->num[i]!=h&&i<=ss->length)
   i++;
   a=i;
   if(i>=ss->length)
	   printf("The student's num is not exist!\n");
   else
   {    
	   if(a==ss->length-1);   
       
       else
		   for(a;a<ss->length-1;a++)
		   {
		       ss->num[a]=ss->num[a+1];
	           ss->score[a]=ss->score[a+1];
		   }
   }
   
   ss->length--;
   printf("删除成功\n");
   printf("执行删除操作后录入的学生信息为:\n");
   for(i=0;i<ss->length;i++)
   {
	   
	   printf("学号%d     成绩 %d\n",s->num[i],s->score[i]);
   }
}  
main()
{
  int j,b,c;
  stu s=(stu)malloc(sizeof(struct student));
  s=info();
  do
  {
   printf("输入要完成的操作序号:1-4\n");
   printf("1 查询,2 插入,3 修改,4 删除\n");
   scanf("%d",&j);
   switch(j)
   { 
     case 1: 
    	printf("输入要查询的学生信息的学号\n");
     	scanf("%d",&b);
	    locate(s,b);
	    break;
	 case 2:
        printf("输入要插入的学生信息的学号,成绩:\n");
	    scanf("%d%d",&b,&c);
	    insert(s,b,c);
	    break;
     case 3:
	    printf("输入要修改的学生信息的学号:");
	    scanf("%d",&b);
	    printf("学号%d     成绩%d\n",b,c);
	    update(s,b,c);
	    break;
     case 4:
	    printf("输入要删除的学号:\n");
	    scanf("%d",&b);
	    delet(s,b);
        break;
   }
  }while(j<=4);
}



加载中
返回顶部
顶部