Add task7.cpp

main
pv3e4i5aj 4 days ago
parent 4c596c388b
commit 8898d11188

@ -0,0 +1,276 @@
#include <stdio.h>
#include <string.h>
#define MARK 10
int input=0;
char range[17];
//定义结构体
struct Student
{
int ID;//学号
int classes;
char name[8];
float math_grade;
float physics_grade;
float English_grade;
float sum;
};
int pow10(int M)
{
int num=1;
for(int i=1;i<=M;i++)
{
num*=10;
}
return num;
}
//对结构体中的从start同学到end同学排序
void Sort(struct Student information[8],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=j;
}
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[8],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 main()
{
struct Student information[8];
//已有7个同学的信息
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;
information[3].ID=10004;strcpy(information[3].name, "Cai");
information[3].classes=11;information[3].math_grade=89.6;
information[3].physics_grade=56.9;information[3].English_grade=90.5;
information[3].sum=89.6+56.9+90.5;
information[4].ID=10005;strcpy(information[4].name, "Fu");
information[4].classes=14;information[4].math_grade=55.6;
information[4].physics_grade=67.9;information[4].English_grade=98.9;
information[4].sum=55.6+67.9+98.9;
information[5].ID=10006;strcpy(information[5].name, "Mao");
information[5].classes=12;information[5].math_grade=22.1;
information[5].physics_grade=45.9;information[5].English_grade=99.2;
information[5].sum=22.1+45.9+99.2;
information[6].ID=10007;strcpy(information[6].name, "Zhan");
information[6].classes=13;information[6].math_grade=35.6;
information[6].physics_grade=67.9;information[6].English_grade=88.0;
information[6].sum=35.6+67.9+88.0;
scanf("%d%s",&input,range);//输入类别和查询范围
struct Student new_information[8];
//下面分类讨论
if(input==1)
{
int start=0;int end=0;
start=10*(range[0]-'0')+(range[1]-'0');
end=10*(range[3]-'0')+(range[4]-'0');
int i=0;int j=0;
for(int i=0;i<=6;i++)
{
if(information[i].classes<=end&&information[i].classes>=start)
{
new_information[j]=information[i];
j++;//最后会有j个被选入
}
} //筛选出了所有学生
//排序
Sort(new_information,0,j-1);
output(new_information,0,j-1);
}
else if(input==2)
{
int start=0;int end=0;
start=10000*(range[0]-'0')+1000*(range[1]-'0')+100*(range[2]-'0')\
+10*(range[3]-'0')+(range[4]-'0');
end=10000*(range[6]-'0')+1000*(range[7]-'0')+100*(range[8]-'0')\
+10*(range[9]-'0')+(range[10]-'0');
int i=0;int j=0;
for(int i=0;i<=6;i++)
{
if(information[i].ID<=end&&information[i].ID>=start)
{
new_information[j]=information[i];
j++;//最后会有j个被选入
}
} //筛选出了所有学生
//排序
Sort(new_information,0,j-1);
output(new_information,0,j-1);
}
else if(input==3)
{
int j=0;
for(int i=0;i<=6;i++)
{
int judge=1;//判断是否缩写吻合的标志
for(int k=0;range[k]!='*';k++)
{
if(information[i].name[k]!=range[k])
{
judge=0;
break;
}
}
if(judge)
{
new_information[j]=information[i];
j++;//最后会有j个被选入
}
} //筛选出了所有学生
//排序
Sort(new_information,0,j-1);
output(new_information,0,j-1);
}
else if(input==4)
{
float sum = 0.0;
int a = 0; //a是整数的有几位,也是小数点在字符串的位置
for (int i = 0; range[i] != '\0'; i++)
{
if (range[i] != '.')
{
a++;
}
else
{
break;
}
}
//计算sum
for (int i = 0; range[i] != '\0'; i++)
{
if (i<a)
{
sum += 1.0 * pow10( a - i - 1) * (range[i] - '0');
}
else if(i>a)
{
sum += 1.0 / pow10( i - a) * (range[i] - '0');
}
}
int j=0;
for(int i=0;i<=6;i++)
{
if(information[i].sum>=sum)
{
new_information[j]=information[i];
j++;//最后会有j个被选入
}
} //筛选出了所有学生
//排序
Sort(new_information,0,j-1);
output(new_information,0,j-1);
}
else if(input==5)
{
int classes=10*(range[0]-'0')+(range[1]-'0');
int start=10000*(range[3]-'0')+1000*(range[4]-'0')\
+100*(range[5]-'0')+10*(range[6]-'0')+(range[7]-'0');;
int end=10000*(range[9]-'0')+1000*(range[10]-'0')\
+100*(range[11]-'0')+10*(range[12]-'0')+(range[13]-'0');;
int j=0;
for(int i=0;i<=6;i++)
{
if(information[i].classes==classes&&\
information[i].ID<=end&&information[i].ID>=start)
{
new_information[j]=information[i];
j++;//最后会有j个被选入
}
} //筛选出了所有学生
//排序
Sort(new_information,0,j-1);
output(new_information,0,j-1);
}
else
{
printf("请用正确的格式输入查询\n");
}
}
Loading…
Cancel
Save