Add task6.cpp

main
pv3e4i5aj 4 days ago
parent 2fd98d5bbe
commit f8e67f8abb

@ -0,0 +1,171 @@
#include <stdio.h>
#include <string.h>
//定义结构体
struct Student
{
int ID;//学号
int classes;
char name[8];
float math_grade;
float physics_grade;
float English_grade;
float sum;
};
//对结构体中的从start同学到end同学排序
void Sort(struct Student information[4],int start,int end)
{
//先班级
for(int i=start;i<=end;i++)
{
for(int j=start;j<=end-1;j++)
{
if(information[j].classes>information[j+1].classes)
{
struct Student tmp=information[j];
information[j]=information[j+1];
information[j+1]=tmp;
}
}
}
//同班同学再成绩
for(int i=start;i<=end;i++)
{
int con=0;//记录连续了几人
for(int j=1;j<=end-i;j++)
{
if(information[i].classes==information[i+j].classes)
{con++;
}
else
{
break;
}
}
if(con)//有人是同班同学,在此内排序
{
for(int m=i;m<i+con;m++)
{
for(int n=i;n<=i+con-1;n++)
{
if(information[n].sum<information[n+1].sum)
{
struct Student tmp=information[n];
information[n]=information[n+1];
information[n+1]=tmp;
}
}
}
}
i=i+con;
}
}
//对非同班同学的打印输出
void output(struct Student information[4],int i)
{
printf("%d %d ",information[i].classes,information[i].ID);
for(int j=0;information[i].name[j]!='\0';j++)
{
printf("%c",information[i].name[j]);
}
printf(" %.1f %.1f %.1f ",information[i].math_grade,\
information[i].physics_grade,information[i].English_grade);
}
//输出modified
void out_modified(struct Student information[4],struct Student delete_student,int i)
{
if(delete_student.ID==information[i].ID)
{
printf("modified");
}
printf("\n");
}
int main()
{
struct Student information[4];
information[0].ID=10001;strcpy(information[0].name, "Zhang");
information[0].classes=11;information[0].math_grade=99.5;
information[0].physics_grade=88.5;information[0].English_grade=89.5;
information[0].sum=99.5+88.5+89.5;
information[1].ID=10002;strcpy(information[1].name, "Yang");
information[1].classes=12;information[1].math_grade=77.9;
information[1].physics_grade=56.5;information[1].English_grade=87.5;
information[1].sum=77.9+56.5+87.5;
information[2].ID=10003;strcpy(information[2].name, "Liang");
information[2].classes=11;information[2].math_grade=92.5;
information[2].physics_grade=99.0;information[2].English_grade=60.5;
information[2].sum=92.5+99.0+60.5;
//输入待修改同学的信息
struct Student delete_student;
scanf("%d %d %s %f %f %f", &delete_student.ID,\
&delete_student.classes,&delete_student.name,&delete_student.math_grade,\
&delete_student.physics_grade,&delete_student.English_grade);
delete_student.sum=delete_student.math_grade+delete_student.physics_grade\
+delete_student.English_grade;
for(int i=0;i<=2;i++)
{
if(delete_student.ID==information[i].ID)
{
information[i]=delete_student;
break;
}
}
//排序
Sort(information,0,2);
for(int i=0;i<=2;i++)
{
int con=0;//记录连续了几人
for(int j=1;j<=2-i;j++)
{
if(information[i].classes==information[i+j].classes)
{con++;
}
else
{
break;
}
}
if(con)//有人是同班同学,在此内排序
{
for(int m=i;m<=i+con;m++)
{
if(m==i)
{
printf("%d ",information[i].classes);
}
else
{
printf(" ");
}
printf("%d ",information[m].ID);
for(int n=0;information[m].name[n]!='\0';n++)
{
printf("%c",information[m].name[n]);
}
printf(" %.1f %.1f %.1f ",information[m].math_grade,\
information[m].physics_grade,information[m].English_grade);
out_modified(information,delete_student,m);
}
}
else
{
output(information,i);
out_modified(information,delete_student,i);
}
i=i+con;
}
}
Loading…
Cancel
Save