Add task5.cpp

main
pv3e4i5aj 5 days ago
parent 63f86bef62
commit 2fd98d5bbe

@ -0,0 +1,189 @@
#include <stdio.h>
#include <string.h>
#define MARK 10
//定义结构体
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 start,int end)
{
//排完序后输出
for(int i=start;i<=end;i++)
{
printf("%d %d ",information[i].ID,information[i].classes);
for(int j=0;information[i].name[j]!='\0';j++)
{
printf("%c",information[i].name[j]);
}
printf(" %.1f %.1f %.1f\n",information[i].math_grade,\
information[i].physics_grade,information[i].English_grade);
}
}
int pow10(int M)
{
int num=1;
for(int i=1;i<=M;i++)
{
num*=10;
}
return num;
}
//在结构体中的从start同学到end同学里面查找要删掉的同学
int original_location(char delete_student[8],struct Student information[4],int start,int end)
{
int mark=MARK;//标记被删除信息学生的初始位置 ,初始化为MARK方便区分
if(delete_student[0]<='9'&&delete_student[0]>='0')
{
int num=0;
for(int i=0;i<strlen(delete_student);i++)
{
num+=(delete_student[i]-'0')*pow10(strlen(delete_student)-i-1);
}
for(int i=0;i<=2;i++)
{
if(num==information[i].ID)
{
mark=i;
break;
}
}
}
else//输入了字符串
{
for(int i=0;i<=2;i++)
{
if(strcmp(delete_student,information[i].name)==0)
{
mark=i;
break;
}
}
}
return mark;
}
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;
//输入被删除同学的信息
char delete_student[8];
scanf("%s", delete_student);
//判断同学是否在系统里面
if(original_location(delete_student,information,0,2)==MARK)
{
//说明同学不在系统里面
Sort(information,0,2);
output(information,0,2);
}
else
{
struct Student left[4];
int j=0;
for(int i=0;i<=2;i++)
{
if(i!=original_location(delete_student,information,0,2))
{
left[j]=information[i];
j++;
}
}
Sort(left,0,1);
output(left,0,1);
printf("Are you sure(yes/no)?\n");
char ch[1];
scanf("%s",ch);
if(ch[0]=='n')
{
Sort(information,0,2);
output(information,0,2);
}
}
}
Loading…
Cancel
Save