1. 程式人生 > >一個求及格成績的簡單實現

一個求及格成績的簡單實現

namespace OS 一個 ace eof 簡單實現 logs str space

題目要求, 求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;
}

一個求及格成績的簡單實現