69,336
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
int cur_num;
struct student
{
int no; //学号
char name[20]; //姓名
float score[3];//三门科目成绩
float sum;
}stu[10];
void compare(struct student stu[])
{
int i, j, k, l;
j = cur_num - 1;
for(i = 0; i < 10;i++) /*越界*/
{
if(stu[j].no < stu[i].no)
{
k = i;
for(l=10;l>k;l--)
stu[l]=stu[l-1];
stu[k]=stu[j];
break;
}
}
}
void display(struct student stu[])
{
int i;
for(i=0;i<10;i++)
{
if(stu[i].no==0)
break;
printf("%d\t%s\t%.2f\t\n",stu[i].no,stu[i].name,stu[i].sum);
}
}
void input()
{
char choose;
int i;
for(i=0;i<10;i++)
{
printf("学号:");
scanf("%d",&stu[i].no);
printf("姓名:");
scanf("%s",stu[i].name);
printf("三门课程成绩:\n");
printf("语文:");
scanf("%f",&stu[i].score[0]);
printf("数学:");
scanf("%f",&stu[i].score[1]);
printf("英语:");
scanf("%f",&stu[i].score[2]);
stu[i].sum=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
printf("continue? (y)or(n):");
getchar();
scanf("%c", &choose);
printf("\n\n");
if(choose=='n')
break;
}
cur_num = i + 1;
}
void input1()
{
int j = cur_num;
if (cur_num >= 10)
return;
printf("学号");
scanf("%d",&stu[j].no);
printf("姓名:");
scanf("%s",stu[j].name);
printf("三门课程成绩:\n");
printf("语文:");
scanf("%f",&stu[j].score[0]);
printf("数学:");
scanf("%f",&stu[j].score[1]);
printf("英语:");
scanf("%f",&stu[j].score[2]);
stu[j].sum=stu[j].score[0]+stu[j].score[1]+stu[j].score[2];
cur_num++;
}
int main()
{
printf(" **********************\n");
printf(" *欢迎来到学生管理系统*\n");
printf(" **********************\n");
input();
printf(" ↓插入前的学员信息如下:↓\n");
printf("学号\t姓名\t三科分数总和\t\n");
display(stu);
printf("请输入你要插入的学员信息:\n");
input1();
printf(" ↓插入后的学员信息如下:↓\n");
compare(stu);
display(stu);
system("pause");
}
参考一下吧