zoukankan      html  css  js  c++  java
  • 第十章-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;
    }
    

      

  • 相关阅读:
    消灭WinRAR广告
    DLL:操作数据库和表
    MySQL的概述和基础(学习整理)
    MySQL个人用户的安装配置详解
    Maven的几种新建项目方式
    解决Maven的jar包冲突问题
    Maven Web项目出现org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException错误
    Maven的概述和基础(学习整理)
    从Maven中央仓库下载jar包
    Maven的New中没有Servlet问题(IDEA)
  • 原文地址:https://www.cnblogs.com/52dxer/p/12276076.html
Copyright © 2011-2022 走看看