/* file stu.c, 程序 stu 的主源程序文件 */
#include "utilities.h"
#include "stu.h"

/* 全局性数据对象通常定义在主文件里，或按照归属原则定义在某个文件里 */
StuRec students[MAXNUM];

void commander (FILE* fp, char *fn) {
    int n, cmd;

    n = readSRecs(fp, MAXNUM, students);
    if (n <= 1) {
        printf("File %s: too few data items.\n", fn);
        return;
    }
    printf("File %s: %d student records read.\n", fn, n);

    do {
        cmd = getcmd("Cmds: 1,Statistics; 2,Histogram; "
                        "3,Sort and store to file\n", 1, 3);
        switch (cmd) {
        case 1: statistics(n, students);
                break;
        case 2: 	histogram(n, students, SEGLEN);
                break;
        case 3:	sortoutput(n, students);
                break;
        }
    } while (next("command"));
}

int main(void) {
    FILE *fp;
    char fn[128];

    do { /* 交互式获取文件名 */
        getnstr("Student record file name: ", 128, fn);
        if ((fp = fopen(fn, "r")) == NULL)
            printf("Can't open file: %s\n", fn);
        else {
            commander(fp, fn);
            fclose(fp);
        }
    } while (next("file"));

    return 0;
}

