第1个回答 2011-07-16
#include <Stdio.h>
#include <string.h>
#define SIZE 21
int num[SIZE];
float score[SIZE];
char name[SIZE][SIZE];
static int n,m=10;
struct student
{
char name[20];
int num;
float score;
};
void print2(int z);
struct student stu[SIZE];
/////////////////////////////////////////////////////////
void creat_stu()
{
int i=1;
printf("in put the student number:\n");
scanf("%d",&n);
for(;i<=n;i++)
{
printf("input the name\n");
scanf("%s",&stu[i].name);
printf("input the number\n");
scanf("%d",&stu[i].num);
printf("input the score\n");
scanf("%f",&stu[i].score);
}
}
//////////////////////////////////
void Bubble_struct(struct student stu[SIZE])
{/////////////////按学号来排序
int i,j,k;
for(i=10;i>=1;i--)
{
for(j=1;j<i;)
{
k=j+1;
if(stu[j].num<=stu[k].num)
{
stu[0]=stu[j]; //num[j]<----->num[j+1]
stu[j]=stu[k];
stu[k]=stu[0 ];
}
j++;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
int find_struct(struct student stu[SIZE],int x)
{//////////////////根据输入的学号来查询 数据
/////////找到了就返回在数组中的位置,找不到就返回0
int top=1,bot=n,mid;
while(top<=bot)
{
mid=(top+bot)/2;
if (x<stu[mid].num) top=mid+1;
else if (x>stu[mid].num) bot=mid-1;
else
{
printf("学生学号 姓名 成绩 \n");
print2(mid);
return mid;
}
}
if(top>bot) printf("error!!!not exist!!\n");
return 0;
}
/////////////////////////////////////////////////////////////////////////////
void chang_struct(struct student stu[SIZE],int x)
{
int flg=find_struct(stu, x);
if (flg==0)
return;
printf("input the score\n");
scanf("%f",&stu[flg].score);
}
///////////////////////////////////////////
void delete_struct(struct student stu[SIZE],int x)
{/////////////根据学号删除数据
int i,j,k;
if (x>stu[1].num||x<stu[20-n].num) printf("no num");
for(j=1;j<=20-n;j++)
if(x==stu[j].num)
{
for(i=j;i<SIZE-n;i++)
{
k=i+1;
stu[i].num=stu[k].num;
stu[i].score=stu[1].score;
strcpy(stu[i].name,stu[k].name);
}
stu[20-n].num=0;
m++;////////////////////删除以后能插入的数据数量加1
}
}
/////////////////////////////////
void print2(int z)
{
///////////打印数据组
printf("%8d \t",stu[z].num);
printf("%s\t",stu[z].name);
printf(" %.2f\t",stu[z].score);
printf("\n");
}
void print()
{
printf("学生学号 姓名 成绩 \n");
int i=1;
for(;i<=n;i++)
print2(i);
}
int main()
{
int c=1,c1=1,x,i;
int z;
while(1)
{//////////////打印菜单
printf("0.推出操作\n");
printf("1.结构体操作\n");
scanf("%d",&c);
switch(c)
{
case 0: return 0;
case 1:printf("input data\n");
creat_stu();
print();
Bubble_struct (stu); ////////////////冒泡排序
print();
while(1)
{////////////打印菜单
printf("1.查询\n");
printf("2.修改\n");
printf("3.删除\n");
printf("0.返回\n");
//c1=getchar();
scanf("%d",&c1);
switch(c1)
{
case 0: break;
case 1: ///////////////查询
scanf("%d",&x);
find_struct(stu,x);
break;
case 3:
scanf("%d",&x);
delete_struct(stu,x);
break;
case 2:
scanf("%d",&x);
chang_struct(stu,x);
default: break;
}
if (c1==0) break;
print();
}
}
}
}
这个是只有一科成绩的,要想加你自己加吧,