第十章-1-结构体练习

/*
 * @Issue: 利用结构体类型编写程序,实现输入一个学生的数学期中和期末成绩,计算并输出平均值
 * @Author: 一届书生
 * @LastEditTime : 2020-02-08 12:19:13
 */
#include<iostream>
using namespace std;

struct study{
    int mid,end,average;
}math;

int main(){
    while(cin>>math.mid>>math.end){
        math.average=math.mid+math.end>>1;
        cout<<math.average<<endl;
    }
    return 0;
}

  

/*
 * @Issue: 利用sort编写程序,实现输入3个学生的学号、数学期中和期末成绩,然后计算平均成绩
 *         并输出成绩表
 * @Author: 一届书生
 * @LastEditTime : 2020-02-08 12:41:14
 */
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct s
{
    string id;
    int mid,end,average;
}student[3];

bool cmp(s a,s b){
    return a.average>b.average;
}
int main(){
    for(int i=0;i<3;i++){
        cin>>student[i].id>>student[i].mid>>student[i].end;
        student[i].average=student[i].mid+student[i].end>>1;
    }
    sort(student,student+3,cmp);
    cout<<"成绩单:"<<endl;
    for(int i=0;i<3;i++){
        cout<<student[i].id<<" "<<student[i].average<<endl;
    }
    return 0;
}

  

/*
 * @Issue: 利用指向结构体的指针编写程序,实现输入3个学生的学号、数学期中和期末成绩,然后计算平均成绩
 *         并输出成绩表
 * @Author: 一届书生
 * @LastEditTime : 2020-02-08 12:50:51
 */
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

struct stu
{
    string id;
    int mid,end,average;
}student[3];

bool cmp(stu p1,stu p2){
    return p1.average>p2.average;
}

int main(){
    stu *p;
    for(p=student;p<student+3;p++){
        cin>>p->id>>p->mid>>p->end;
        p->average=p->mid+p->end>>1;
    }
    
    sort(student,student+3,cmp);

    for(p=student;p<student+3;p++){
        cout<<p->id<<" "<<p->mid<<" "<<p->end<<" "<<p->average<<endl;
    }
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/52dxer/p/12276076.html