zoukankan      html  css  js  c++  java
  • c++实验二(2)

    建立一个名为CStudent的类,该类有以下几个属性:学号、姓名(使用字符指针)、成绩,并为上述属性定义相应的方法。
    用C++ 面向对象的程序设计方法,找到并输出存放在CStudent类动态数组中学生成绩最高的学生信息(需考虑分数相同的情况,输出学号、姓名和成绩)。


    #include<iostream>
    #include<cstring>
    using namespace std;
    class Cstudent
    {
    private :
    long long number;
    char *p;//字符指针
    int score;
    public:
    Cstudent(long n,char *q,int s);//默认构造
    Cstudent(const Cstudent &s);//复制构造
    friend void max(Cstudent*st,int count1);//友元函数方便后面比较成绩大小 实质上就是一个数组排序;
    void input();//输入相应的数据
    void show();//展示
    ~Cstudent(); //析构函数
    };
    Cstudent::Cstudent(long n=0,char *q="no mame",int s=0)//有默认值
    {
    p=new char[20];//只用在构造函数的地方分配空间
    strcpy(p,q);
    number=n;
    score=s;
    }
    Cstudent::Cstudent(const Cstudent &s)
    {
    number=s.number;
    score=s.score;
    p=new char (*s.p);//深复制函数
    }
    void Cstudent:: input()//录入数据
    {
    long long num;
    int s;
    cout<<"请输入姓名:"<<endl;
    gets(p);
    cout<<"请输入学号,成绩:"<<endl;
    cin>>num>>s;
    number=num;
    score=s;
    cin.ignore();
    }
    void Cstudent ::show()
    {
    cout<<"姓名:";
    puts(p);
    cout<<"学号:"<<number<<endl<<"成绩:"<<score<<endl;
    }
    Cstudent::~Cstudent()
    {
    delete []p;//与前面的new对应
    }
    void max(Cstudent*st,int count1)
    {
    int maxx=0;
    int c=0;
    int j;
    for(int i=0;i<count1;i++)
    {
    if(st[i].score>maxx)
    {
    maxx=st[i].score;
    j=i;
    }
    }
    for(int i=0;i<count1;i++)
    {
    if(st[i].score==maxx) c++;
    }
    if(c==1)
    {
    cout<<"分数最高的同学是:"<<endl;
    st[j].show();
    }
    else
    {
    cout<<"下面的同学并列最高分:"<<endl;
    for(int i=0;i<count1;i++)
    {
    if(st[i].score==maxx)
    {
    st[i].show();
    cout<<endl;
    }
    }
    }
    }
    int main()
    { cout<<"请输入对象的个数:"<<endl;
    int a;
    cin>>a;
    cin.ignore();
    const int count=a;
    Cstudent*str=new Cstudent[count];
    for(int i=0;i<count;i++)
    {
    str[i].input();
    }
    max(str,count);
    return 0;
    }

  • 相关阅读:
    LaTeX中表格多行显示的最简单设置方法
    获取Google音乐的具体信息(方便对Google音乐批量下载)
    移动硬盘提示格式化解决的方法,未正确删除导致不能读取文件提示格式化解决方式
    Android Service 服务(一)—— Service
    华为C8816电信版ROOT过程
    Linux crontab 命令格式与具体样例
    Python用subprocess的Popen来调用系统命令
    我的EJB学习历程
    接口和逻辑--多进程或单一进程
    uva 11354
  • 原文地址:https://www.cnblogs.com/cy846586184/p/12940316.html
Copyright © 2011-2022 走看看