结构体作业题: 1)提供5位同学的一组信息,包括学号、姓名、数学成绩、计算机成绩,求得每位同学的平均分和总分,然后按照总分从高到低排后输出对应的学生信息。 要求:请使用结构体定义学生实体,包含学号等信息。 2)有n个机构体变量,内含学生学号,姓名和3门课程的成绩。要求输出平均成绩最高的学生信息(包含学号,姓名,3门课程成绩以及平均成绩)。 注意:n的值通过标识符定义 #define n 3 请使用结构体和结构体指针完成上述要求。、
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "string.h"
/************************************************
ALIENTEK战舰STM32开发板实验4
串口实验
技术支持:www.openedv.com
淘宝店铺:http://eboard.taobao.com
关注微信公众平台微信号:"正点原子",免费获取STM32资料。
广州市星翼电子科技有限公司
作者:正点原子 @ALIENTEK
************************************************/
//------------科目总数---------------------------
#define Subject 2
/////////////////////////////////////////////////
//-----------学员ID号----------------
#define Stu_ID_1 "20170718"
#define Stu_ID_2 "20170719"
#define Stu_ID_3 "20170720"
#define Stu_ID_4 "20170721"
#define Stu_ID_5 "20170722"
////////////////////////////////////////
//------------学员学名------------------
#define Name_1 "学员A"
#define Name_2 "学员B"
#define Name_3 "学员C"
#define Name_4 "学员D"
#define Name_5 "学员E"
///////////////////////////////////////
//------------数学成绩------------------
#define Math_Results_1 60
#define Math_Results_2 25
#define Math_Results_3 43
#define Math_Results_4 64
#define Math_Results_5 85
///////////////////////////////////////
//------------计算机成绩------------------
#define Comp_Results_1 11
#define Comp_Results_2 52
#define Comp_Results_3 33
#define Comp_Results_4 98
#define Comp_Results_5 22
/////////////////////////////////////////
////------------总分------------------
//#define Tot_Sco_1 (Math_Results_1+Comp_Results_1)
//#define Tot_Sco_2 (Math_Results_2+Comp_Results_2)
//#define Tot_Sco_3 (Math_Results_3+Comp_Results_3)
//#define Tot_Sco_4 (Math_Results_4+Comp_Results_4)
//#define Tot_Sco_5 (Math_Results_5+Comp_Results_5)
/////////////////////////////////////////
////------------平均分------------------
//#define Ave_Sco_1 (Math_Results_1/Subject)
//#define Ave_Sco_2 (Math_Results_2/Subject)
//#define Ave_Sco_3 (Math_Results_3/Subject)
//#define Ave_Sco_4 (Math_Results_4/Subject)
//#define Ave_Sco_5 (Math_Results_5/Subject)
/////////////////////////////////////////
typedef struct{
char *Stu_ID;
char *Name;
u16 Math_Results;
u16 Comp_Results;
u16 Ave_Sco;
u16 Tot_Sco;
}Stu;
Stu Stu_1,Stu_2,Stu_3,Stu_4,Stu_5;
u16 relatively[6]={0,0,0,0,0};
int main(void)
{
int i;
char j;
u16 gap,n,temp;
u16 relatively[6]={0,0,0,0,0};
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //LED端口初始化
KEY_Init(); //初始化与按键连接的硬件接口
while(1)
{
Stu_1.Stu_ID=Stu_ID_1;
Stu_1.Name=Name_1;
Stu_1.Math_Results=Math_Results_1;
Stu_1.Comp_Results=Comp_Results_1;
Stu_1.Tot_Sco=Stu_1.Math_Results+Stu_1.Comp_Results;
Stu_1.Ave_Sco=Stu_1.Tot_Sco/Subject;
relatively=Stu_1.Tot_Sco;
i++;
Stu_2.Stu_ID=Stu_ID_2;
Stu_2.Name=Name_2;
Stu_2.Math_Results=Math_Results_2;
Stu_2.Comp_Results=Comp_Results_2;
Stu_2.Tot_Sco=Stu_2.Math_Results+Stu_2.Comp_Results;
Stu_2.Ave_Sco=Stu_2.Tot_Sco/Subject;
relatively=Stu_2.Tot_Sco;
i++;
Stu_3.Stu_ID=Stu_ID_3;
Stu_3.Name=Name_3;
Stu_3.Math_Results=Math_Results_3;
Stu_3.Comp_Results=Comp_Results_3;
Stu_3.Tot_Sco=Stu_3.Math_Results+Stu_3.Comp_Results;
Stu_3.Ave_Sco=Stu_3.Tot_Sco/Subject;
relatively=Stu_3.Tot_Sco;
i++;
Stu_4.Stu_ID=Stu_ID_4;
Stu_4.Name=Name_4;
Stu_4.Math_Results=Math_Results_4;
Stu_4.Comp_Results=Comp_Results_4;
Stu_4.Tot_Sco=Stu_4.Math_Results+Stu_4.Comp_Results;
Stu_4.Ave_Sco=Stu_4.Tot_Sco/Subject;
relatively=Stu_4.Tot_Sco;
i++;
Stu_5.Stu_ID=Stu_ID_5;
Stu_5.Name=Name_5;
Stu_5.Math_Results=Math_Results_5;
Stu_5.Comp_Results=Comp_Results_5;
Stu_5.Tot_Sco=Stu_5.Math_Results+Stu_5.Comp_Results;
Stu_5.Ave_Sco=Stu_5.Tot_Sco/Subject;
relatively=Stu_5.Tot_Sco;
i++;
for(gap=i/2;gap>0;gap/=2){
for(n=gap;n<i;n++){
for(j=n-gap;(j>=0)&&(relatively[j]>relatively[j+gap]);j-=gap){
temp=relatively[j];
relatively[j]=relatively[j+gap];
relatively[j+gap]=temp;
}
}
}
for(i=5;i>=0;i--)
{
if(relatively==Stu_1.Tot_Sco)
{
printf("Student1:\n");
printf("ID is %s\n",Stu_1.Stu_ID);
printf("name is %s\n",Stu_1.Name);
printf("Math_Results is %d\n",Stu_1.Math_Results);
printf("Comp_Results is %d\n",Stu_1.Comp_Results);
printf("Ave_Sco is %d\n",Stu_1.Ave_Sco);
printf("Tot_Sco is %d\n\n",Stu_1.Tot_Sco);
}else
if(relatively==Stu_2.Tot_Sco)
{
printf("Student2:\n");
printf("ID is %s\n",Stu_2.Stu_ID);
printf("name is %s\n",Stu_2.Name);
printf("Math_Results is %d\n",Stu_2.Math_Results);
printf("Comp_Results is %d\n",Stu_2.Comp_Results);
printf("Ave_Sco is %d\n",Stu_2.Ave_Sco);
printf("Tot_Sco is %d\n\n",Stu_2.Tot_Sco);
}else
if(relatively==Stu_3.Tot_Sco)
{
printf("Student3:\n");
printf("ID is %s\n",Stu_3.Stu_ID);
printf("name is %s\n",Stu_3.Name);
printf("Math_Results is %d\n",Stu_3.Math_Results);
printf("Comp_Results is %d\n",Stu_3.Comp_Results);
printf("Ave_Sco is %d\n",Stu_3.Ave_Sco);
printf("Tot_Sco is %d\n\n",Stu_3.Tot_Sco);
}else
if(relatively==Stu_4.Tot_Sco)
{
printf("Student4:\n");
printf("ID is %s\n",Stu_4.Stu_ID);
printf("name is %s\n",Stu_4.Name);
printf("Math_Results is %d\n",Stu_4.Math_Results);
printf("Comp_Results is %d\n",Stu_4.Comp_Results);
printf("Ave_Sco is %d\n",Stu_4.Ave_Sco);
printf("Tot_Sco is %d\n\n",Stu_4.Tot_Sco);
}else
if(relatively==Stu_5.Tot_Sco)
{
printf("Student5:\n");
printf("ID is %s\n",Stu_5.Stu_ID);
printf("name is %s\n",Stu_5.Name);
printf("Math_Results is %d\n",Stu_5.Math_Results);
printf("Comp_Results is %d\n",Stu_5.Comp_Results);
printf("Ave_Sco is %d\n",Stu_5.Ave_Sco);
printf("Tot_Sco is %d\n\n",Stu_5.Tot_Sco);
}
}
while(1);
}
}
|