利用结构体类型编制程序,实现输入一个学生的数学期中和期末成绩,然后计算其平均成绩
展开全部
#include <stdio.h>
#define N 100 //数组总长,按需调整
/*定义学生结构体*/
struct Student
{
int ID;
char Name[20];
float Mark1;
float Mark2;
// float Mark3;
float Sum;
};
/*声明学生数组及学生数量*/
struct Student students[N]; //预定义结构体数组长度
/*求总分*/
float Sums(struct Student stu)
{
return stu.Mark1+stu.Mark2;//+stu.Mark3;
}
/*输入学生信息*/
int Student_Input(){
int num=0;
while(1){
//printf("请输入学号:"); //请自行将while循环改为固定个数输入
students[num].ID=num;
// getchar();
printf("请输入姓名:");
scanf("%s",&students[num].Name);
getchar();
printf("请输入成绩1:"); //请自行将科目名称修改
scanf("%f",&students[num].Mark1);
getchar();
printf("请输入成绩2:");
scanf("%f",&students[num].Mark2);
getchar();
students[num].Sum=Sums(students[num]);
num++;
printf("是否继续?(y/n)"); //如改为固定个数输入,这个提示及下面判断可以去掉
if (getchar()=='n'){
break;
}
}
return num; //返回实际输入学生资料个数
}
/*按总分排序*/
void Student_SortBySum(int n){ //冒泡排序
int i,j;
struct Student tmp;
for (i=0;i<n;i++){
for (j=1;j<n-i;j++){
if (students[j-1].Sum<students[j].Sum){
tmp=students[j-1];
students[j-1]=students[j];
students[j]=tmp;
}
}
}
}
/*显示单独一个学生信息*/
void Student_Show_One(struct Student stu) //输出格式,按需要改动
{
printf("%10d%10s%8.2f%8.2f%8.2f\n",stu.ID,stu.Name,
stu.Mark1,stu.Mark2,stu.Sum);
}
/*显示全部学生信息*/
void Student_Display(int n)
{
int i;
printf("%10s%10s%8s%8s%8s\n","学号","姓名","成绩","成绩","总成线"); //输出类似表头
printf("---------------------------------------------------------\n");
for (i=0;i<n;i++) //逐个输出学生成线信息
{
Student_Show_One(students[i]);
}
}
/*主程序*/
int main(){
int n=0;
n=Student_Input(); //输入学生信息
Student_SortBySum(n); //排序
Student_Display(n); //输出
return 0;
}
展开全部
#include<stdio.h>
struct student{
char name[20];
int midscore;
int finalscore;
float average(){ return (midscore+finalscore)/2;}
};
void main()
{
struct student s1;
printf("输入期中成绩:");
scanf("%d",&s1.midscore);
printf("输入期末成绩:");
scanf("%d",&s1.finalscore);
printf("平均成绩为%4.1f\n",(float)s1.average());
}
struct student{
char name[20];
int midscore;
int finalscore;
float average(){ return (midscore+finalscore)/2;}
};
void main()
{
struct student s1;
printf("输入期中成绩:");
scanf("%d",&s1.midscore);
printf("输入期末成绩:");
scanf("%d",&s1.finalscore);
printf("平均成绩为%4.1f\n",(float)s1.average());
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询