首页 > 其他 > 详细

一个求及格成绩的简单实现

时间:2018-03-05 23:22:07      阅读:324      评论:0      收藏:0      [点我收藏+]

题目要求, 求N个人的成绩及格线, 要求60%的人及格, 及格线高于60, 则取60. 且及格线是10的倍数.

  • result.h
#ifndef RESULT_H
#define RESULT_H

#include <stdlib.h>
#include <time.h>

class Result{
public:
    Result();
    Result(int n);
    Result &operator=(const Result &Re);
    int getPassLine();
    void printResult();
    ~Result();
    int passLine;
public:
    int *p;
    int n;
};

#endif // RESULT_H
  • result.cpp
#include "result.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <iostream>
using namespace std;
Result::Result()
{
    this->passLine = -1;
    this->n = -1;
    p = NULL;
}

Result::Result(int n)
{
    this->passLine = -1;
    if(n<=0)
    {
        this->n = -1;
        p = NULL;
        return;
    }
    this->n = n;
    p = new int[n];
    srand((unsigned int)time(NULL));
    for(int i = 0;i<n;++i)
    {
        p[i] = rand()%101;
        cout<<p[i]<< endl;
    }
}

Result &Result::operator=(const Result &Re)
{
    if(Re.n>0)
    {
        n = Re.n;
        this->p = new int[n];
        memcpy(this->p, Re.p,Re.n*sizeof(int));
        this->passLine = -1;
    }
    else
    {
        this->passLine = -1;
        this->n = -1;
        p = NULL;
    }
    return *this;
}

int Result::getPassLine()
{
    if(this->passLine ==-1&&this->n==-1)
    {
        return -1;
    }
    else if(this->passLine>=0)
    {
        return this->passLine;
    }
    else
    {
        if(n == 1)
        {
            this->passLine = p[0];
        }
        else
        {
            int tmp ;
            for(int i = 0;i<n-1;++i)
            {
                for(int j = 0; j<n-1-i;++j)
                {
                    if(p[j] > p[j+1])
                    {
                        tmp = p[j];
                        p[j] = p[j+1];
                        p[j+1] = tmp;
                    }
                }
            }
            tmp = (int)(n*0.4);
            this->passLine = p[tmp]/10*10;
            if (this->passLine>60)
            {
                this->passLine = 60;
            }
        }
    }
    return this->passLine;
}

void Result::printResult()
{
    if(this->n<=0)
    {
        return;
    }
    else
    {
        for(int i = 0; i< n; ++i)
        {
            cout << p[i]<< "," ;
        }
        cout<< endl;
    }
}

Result::~Result()
{
    delete []p;
}
  • main.cpp
#include <stdio.h>
#include "result.h"
#define N 10
int main(int argc, char *argv[])
{
    Result R(N);
    int line = R.getPassLine();
    R.printResult();
    printf("PassLine:%d\n", line);
    return 0;
}

一个求及格成绩的简单实现

原文:https://www.cnblogs.com/nonsupport/p/8511783.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!