int ReadStuInfoFromFile(char *name, student **stu)
{
    FILE *f;
    student* stuTbl;
    int count = 0;
    *stu = NULL;

    if (name && stu != NULL){
        f = fopen(name, "rb");
        if (f != NULL){
            fread(&count, sizeof(count), 1, f);
            if (count > 0){
                stuTbl = (student*)malloc(sizeof(student)*count);
                if (stuTbl == NULL)
                    count = 0;
                else{
                    if (count != fread(stuTbl, sizeof(student), count, f)){
                        count = 0;
                        free(stuTbl);
                        stuTbl = NULL;
                    }
                }
                *stu = stuTbl;
            }
            fclose(f);
        }
    }
    return count;
}

int NoPass(student stu[], int n, student **noPassStudent, int *m)
{
    if (noPassStudent == NULL || m == NULL || stu == NULL) return -1;

    int count = 0;
    for (int i = 0; i < n; ++i){
        if (stu[i].sum / 3 >= 60) continue;
        noPassStudent[count++] = &stu[i];
    }
    *m = count;
    return 0;
}

int Pass(student stu[], int n, student **PassStudent, int *m)
{
    if (PassStudent == NULL || m == NULL || stu == NULL) return -1;

    int count = 0;
    for (int i = 0; i < n; ++i){
        if (stu[i].sum / 3 < 60) continue;
        PassStudent[count++] = &stu[i];
    }
    *m = count;
    return 0;
}

int SortStudents(student stu[], int n)
{
    if (stu == NULL) return -1;
    if (n < 2) return 0;

    int i, j;
    student tmp;
    for (i = 0; i < n - 1; i++){
        for (j = n - 1; j > i; --j){
            if (stu[j].sum > stu[j - 1].sum){
                tmp = stu[j];
                stu[j] = stu[j - 1];
                stu[j - 1] = tmp;
            }
        }
    }
    return 0;
}

int SearchStudent(student stu[], int n, int id, int *rank, student *rstu)
{
    int count = 0;
    if (SortStudents(stu, n) == 0){
        for (int i = 0; i < n; ++i){
            if (stu[i].id != id) continue;
            *rstu = stu[i];
            *rank = i;
            return 0;
        }
    }
    return -1;
}