题目要求, 求N个人的成绩及格线, 要求60%的人及格, 及格线高于60, 则取60. 且及格线是10的倍数.
#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
#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;
}
#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;
}