• C++学生信息处理


    #include
    #include
    using namespace std;
    template
    int getArrayLen(T& array) //使用模板定义一个函数getArrayLen,该函数将返回数组array的长度
    {
    return (sizeof(array) / sizeof(array[0]));
    }

    class Student{
    public:
    char name[20];
    int id;
    int score;
    public:
    Student(int i = 0, char* c="", int s= 0)
    {
    id = i;
    strcpy(name,c);
    score = s;
    }
    Student& operator = (const Student& rhs )
    {
    id = rhs.id;
    strcpy(name,rhs.name);
    score = rhs.score;
    return *this;
    }
    void gets()
    {
    cin>>name>>id>>score;
    }
    void print()
    {
    cout<<"name: "<<name<<" id: "<<id<<" score: "<<score<<endl;
    }
    } ;

    void rank(int n,Student a[])//排序函数
    {
    int i, j; Student t;
    for(i = 0 ; i < n ; i++ )
    for(j = i + 1; j < n -1 ; j ++)
    {
    if(a[i].score < a[j].score){t = a[i]; a[i] = a[j] ;a[j] = t;}
    }
    }

    int average(Student a[],int n)//求平均数
    {
    int sum = 0,average;
    for(int i = 0 ; i < n ; i++)
    sum+=a[i].score;
    average = sum / n;
    return average;
    }
    void input(Student s[],int n)//输入信息的函数
    for(int i = 0 ; i < n ; i++)
       s[i].gets();
    }
    void output(Student s[],int n)//输出信息的函数
    for(int i = 0 ; i < n ; i++)
          s[i].print();
    }

    //主函数
    int main()
    {
    Student class1[10];//十个人的信息
    cout<<"******************************************"<<endl;
    cout<<"please enter name id score:"<<endl;
    input(class1,10);
    cout<<"name              id             score  :"<<endl;
    cout<<"******************************************"<<endl;
    output(class1,10);
    cout<<"******************************************"<<endl;
    cout<<"this class average score is:"<<average(class1,4)<<endl;
    cout<<"******************************************"<<endl;
        rank(10,class1);
    output(class1,4);//输出前四的信息
    return 0;
    }
  • 相关阅读:
    uva 10561 sg定理
    二进制下 求分数化小数的循环节问题
    zoj 2562 反素数
    uva 11916 解模方程a^x=b (mod n)
    Android 组件化方案探索与思考
    2018谷歌I/O开发者大会8大看点汇总 新品有哪些
    Glide高级详解—缓存与解码复用
    Android性能优化:手把手带你全面实现内存优化
    Android几种强大的下拉刷新库
    Android app 在线更新那点事儿(适配Android6.0、7.0、8.0)
  • 原文地址:https://www.cnblogs.com/whieenz/p/5247172.html
Copyright © 2020-2023  润新知