2016程序设计基础(C语言)课程设计

Developed by Zhuxiaoxie Xiaoboren and Zhangcui

经过几个星期的奋战,我们三人终于把课设弄完了,在体验了成功的快感的同时也感受到了debug的痛苦与煎熬,幸而检查的时候程序没有挂,自觉写的还可以,所以扔上来纪念一下

总体思路

界面控制流程图

头晕勿看…

// main.c
#include <stdio.h>
#include "function.h"
#include "tool_f.h"
#include "console_output_control.h"

int main() {
    Init();
    int isRun [FUNCNUM] = {0}; 
    int state = 1;
    pCls p = CreateClass();
    while (state) {
        Cls();
        printf("*****************************************************************************\n");
        printf("*   1.  Read from a file                                            *\n");
        printf("*   2.  Append record manually                                      *\n");
        printf("*   3.  Calculate total and average score of every course           *\n");
        printf("*   4.  Calculate total and average score of every student          *\n"); 
        printf("*   5.  Sort in descending order by total score of every student    *\n"); 
        printf("*   6.  Sort in ascending order by total score of every student     *\n"); 
        printf("*   7.  Sort in ascending order by number                           *\n"); 
        printf("*   8.  Sort in dictionary order by name                            *\n"); 
        printf("*   9.  Search by number                                            *\n"); 
        printf("*   10. Search by name                                              *\n"); 
        printf("*   11. Statistic analysis for every course                         *\n"); 
        printf("*   12. List record                                                 *\n"); 
        printf("*   13. Write to a file                                             *\n"); 
        printf("*   14. Correct the information of a student                        *\n"); 
        printf("*   0 . Exit                                                        *\n");
        printf("*****************************************************************************\n\n"); 
        printf("               Please input your choice:                             \n\n");
        printf("                           ");

        scanf("%d",&state);
        TurnPage(state);
        if (!IsRun(isRun,state,p)) {
            continue ;
         }

        switch (state) {
            case 0 :
                isRun[0] = 1;
                Free(p);
                EndPut();
                break;
            case 1 :
                if (Input(p) == 0)
                    isRun[1] = 1;
                break;
            case 2:
                if (ManualInput(p)==0)
                    isRun[2] = 1;
                break;
            case 3:
                isRun[3] = 1;
                ScoreofCrs(p);
                break;
            case 4:
                isRun[4] = 1;
                ScoreofStu(p);
                break;
            case 5:
                isRun[5] = 1;
                SortbyDScore(p);
                break;
            case 6:
                isRun[6] = 1;
                SortbyAScore(p);

                break;
            case 7:
                isRun[7] = 1;
                SortbyNum(p);
                break;
            case 8:
                isRun[8] = 1;
                SortbyName(p);
                break;
            case 9:
                isRun[9] = 1;
                printf("Please input the Stu No. you want:\n");
                int no;
                scanf("%d",&no);
                pStu pstu = SearchNum (p,no);
                if (pstu == NULL) {
                    TurnPage(-1);
                    printf("No such a student!\n");
                 } else {
                    PrintListboard(p->sumcrs);
                    PrintStu(&pstu->data,p->sumcrs);
                 }
                 break;
            case 10:
                isRun[10] = 1;
                printf("Please input the name you want:\n");
                char s[NAMEMAX+1];
                getchar();
                gets(s);
                pstu = SearchName (p->inf.head->next,s);
                if (pstu == NULL) {
                    TurnPage(-1);
                    printf("No such a student!\n\n");
                    break;
                } 
                PrintListboard(p->sumcrs);
                while (pstu)
                {
                    PrintStu(&pstu->data,p->sumcrs);
                    pstu = SearchName(pstu->next,s);
                } 
                break;
            case 11:
                isRun[11] = 1;
                Analysis(p);
                break;
            case 12:
                isRun[12] = 1;
                ListRecord(p);
                break;
            case 13:
                isRun[13] = 1;
                WtF(p);
                break;
            case 14:
                isRun[14] = 1;
                Correct(p);
                break;
            default :
                printf("ERROR ! NO SUCH A CHOICE! \n");
                printf("Please input your choice again:\n");
         }
    } 
    return 0;
}
//function.h
/*  1.  Read from a file
 *  2.  Append record manually
 *  3.  Calculate total and average score of every course
 *  4.  Calculate total and average score of every student
 *  5.  Sort in descending order by total score of every student
 *  6.  Sort in ascending order by total score of every student
 *  7.  Sort in ascending order by number
 *  8.  Sort in dictionary order by name
 *  9.  Search by number
 *  10. Search by name
 *  11. Statistic analysis for every course
 *  12. List record
 *  13. Write to a file
 *  14. Correct the information of a student that has already been stored
 *  0 . Exit
 */

#define STUNOMAX 10 // Max of length of Stu No.
#define NAMEMAX 20  // Max of length of name
#define SUBMAX 6    // Max of subjects
#define STUMAX 30   // Max of students in a class

#define FILENAMEMAX 32  //Max of filename
#define RANKLEN 6   // Length of rank for output
#define SUMLEN 8    // Length of sum for output
#define AVERLEN 10  // Length of average for output
#define CRSLEN 10   // Length of course for output
#define MALLOCERR 1 // the num exit() use when malloc() errors occur


#define FUNCNUM 14 // num of functions in function.h

typedef struct {
    int stunum;             // student number
    int rank;               // rank in the class
    char name[NAMEMAX+1];   // namelist
    double score[SUBMAX+2]; // score of each subject, score[0] is the sum and score[sumcrs+1] is the average
} Data,*pData;

typedef struct Node {       // Node of linked list
    Data data;              
    struct Node *next;
} Student,*pStu;

typedef struct Listinf {    // pointers to the first and last node of the linked list
    pStu head,tail;
} Inf,*pInf;

typedef struct {
    Inf inf;                // v_3.0 replace array of structure with a linked list 
    int sumstu;             // sum of students in the class
    int sumcrs;             // sum of subjects in the exam
    double crssco[SUBMAX+1];// sum of score of each course
    double avercrs[SUBMAX+1];// average of each course
} Class,*pCls;


/* V 1.0 functions */
int Input (pCls p);         //Input the information of the class, return 0 if success ,else return -1
void ScoreofCrs (pCls p);   //Calculate total and average score of every course
void ScoreofStu (pCls p);   //Calculate total and average score of every student
void SortbyDScore(pCls p);  //Sort in descending order by total score of every student
void SortbyAScore(pCls p);  //Sort in ascending order by total score of every stuednet
void SortbyNum  (pCls p);   //Sort in ascending order by number
void SortbyName (pCls p);   //Sort in dictionary order by name
pStu SearchNum  (pCls p,int num);   //Search by number
pStu SearchName (pStu p,char *s);   //Search by name
void Analysis   (pCls p);   //Statistic analysis every course
void ListRecord (pCls p);   //List Record

/* V 2.0 New functions */
void WtF (pCls p);          //Write To File


/* V 3.0 New functions */
int ManualInput (pCls p);   //Append record manually,return 0 if success ,else return -1
void Correct (pCls p);      // Correct the imformation of a student that has already been stored



//function.c
#include <stdio.h>
#include <string.h>
#include "function.h"
#include "tool_f.h"
#include "console_output_control.h"

int Input (pCls p)          //Input the information of the class, return 0 if success ,else return -1
{
    int i,j;
    printf("\n");
    printf("In the file, data should be organized as following format\n\n");
    printf("First input the sum of students(no more than 30):\n\n");
    printf("Then input the sum of courses in the exam(no more than 6):\n\n");
    printf("Then input the imformation of each student \n\n(order : Student No. , Name , and score from course_1 to course_n)\n\n");
    printf("First please input your file's name:\n");
    FILE *fp ;
    char filename[FILENAMEMAX+1];
    getchar();
    gets(filename);
    if ((fp=fopen(filename,"r")) == NULL) {
        TurnPage(-1);
        printf("Can't find the file! Check please!\n");
        return -1;
    }

    fscanf(fp,"%d",&p->sumstu);
    if (p->sumstu > STUMAX) {
        TurnPage(-1);
        printf("TOO MUCH STUDENTS!! The sum of students should be no more than 30\n");
        printf("Please correct your data\n\n");
        return -1;
    }

    fscanf(fp,"%d",&p->sumcrs);
    if (p->sumcrs> SUBMAX) {
        TurnPage(-1);
        printf("TOO MUCH COURSES!! The sum of courses should be no more than 6 \n");
        printf("Please correct your data\n\n");     
        return -1;
    }

    Data data;
    memset(&data,0,sizeof(data));
    int issucceed = 1;
    printf("\n");
    printf("Warning: data as followed are not stored successfully:");

    int subsum = 0;
    for (i = 0;i < p->sumstu;i ++) {
        fscanf(fp,"%d",&data.stunum);
        fgetc(fp);
        fscanf(fp,"%s",data.name);
        for (j = 1;j <= p->sumcrs;j ++) {
            fscanf(fp,"%lf",&data.score[j]);
        }

        pStu pstu = NULL;
        pstu = find(p,data.stunum); 
        if (pstu != NULL) {
            issucceed = 0;
            printf("\n\n");
            printf("Fail to store \"%d %s\" ,for \"%d %s\" has already been stored\n",data.stunum,data.name,pstu->data.stunum,pstu->data.name);
            subsum ++;
            continue ;
        }

        add (p->inf,&data);
    }
    p->sumstu -= subsum;
    if (issucceed) {
        CleanLine();
        printf("\n");
        printf("All data have been stored successfully, choose \"List Record\" if you want to check\n");
    }
    printf("\n");
    fclose(fp);
    return 0;
}

int ManualInput (pCls p)    //Append record manually,return 0 if success ,else return -1
{
    printf("\n");
    printf("Please input the sum of students that you want to add\n");
    int i,n,j;
    scanf("%d",&n);
    if (p->sumcrs == 0) {
        printf("Please input the sum of courses (no more than 6)\n");
        scanf("%d",&p->sumcrs);
        if (p->sumcrs > SUBMAX) {
            TurnPage(-1);
            printf("TOO MANY COURSES!! The sum of students should be no more than 30\n\n");
            p->sumcrs = 0;
            return -1;
        }
    }
    printf("\n");
    Data data;
    if (n+p->sumstu>STUMAX) {
        TurnPage(-1);
        printf("TOO MANY STUDENTS!! The sum of students should be no more than 30\n\n");
        return -1;
    }

    memset(&data,0,sizeof(data));
    printf("Now please input the imformation of each student\n");
    printf("order : Student No. , Name ,  and score from course_1 to course_%d\n",p->sumcrs);
    printf("\n");
    int issucceed = 1;
    for (i=0;i<n;i++) {
        scanf("%d",&data.stunum);
        pStu pstu = NULL;

        getchar();   // read the '\r'
        scanf("%s",data.name);
        for (j = 1;j <= p->sumcrs;j ++) {
            scanf("%lf",&data.score[j]);
        }

        pstu = find(p,data.stunum); 
        if (pstu != NULL) {
            issucceed = 0;
            printf("\n");
            printf("Same Stu No. appears!\n");
            printf("Fail to store \"%d %s\" ,for \"%d %s\" has already been stored\n\n",data.stunum,data.name,pstu->data.stunum,pstu->data.name);
            continue ;
        }
        p->sumstu ++ ;
        add (p->inf,&data);
    }
    if (issucceed) {
        printf("All data have been stored successfully, choose \"List Record\" if you want to check\n\n");
    }
    return 0;
}

void ListRecord (pCls p)    //List Record
{
    PrintListboard(p->sumcrs);
    int i;
    pStu q;
    for (q=p->inf.head->next; q ; q=q->next) {
        PrintStu(&q->data,p->sumcrs);
    }
    return ;
}

void ScoreofCrs (pCls p)    //Calculate total and average score of every course
{
    int i,j;
    pStu p1,q1;
    // Calculate
    for (j=1;j<=p->sumcrs;j++) {
        double sum = 0.0;
        for (p1 = p->inf.head->next; p1 ; p1 = p1->next)
        {
            sum += p1->data.score[j];
        }
        p->crssco[j] = sum;
        p->avercrs[j] = sum / p->sumstu;
    }
    //Print
    char listboard[][10] = {
        "Stu No.",
        "Name",
        "Rank",
        "Sum",
        "Average",
        "Course1",
        "Course2",
        "Course3",
        "Course4",
        "Course5",
        "Course6",
    } ;
    printf("          ");
    for (i=0;i<p->sumcrs;i++) {
        printf("%-*s",CRSLEN,listboard[i+5]);
    }
    puts("\n");
    printf("Sum       ");
    for (j=1;j<=p->sumcrs;j++) {
        printf("%-*.2f",CRSLEN,p->crssco[j]);
    }
    puts("\n");
    printf("Average   ");
    for (j=1;j<=p->sumcrs;j++) {
        printf("%-*.2f",CRSLEN,p->avercrs[j]);
    }
    puts("\n");

    return ;
}

void ScoreofStu (pCls p)    //Calculate total and average score of every student
{
    int j;
    pStu q;
    for (q=p->inf.head->next; q ;q = q->next)
    {
        double sum = 0.0;
        for (j=1;j<=p->sumcrs;j++) {
            sum += q->data.score[j];
        }
        q->data.score[0] = sum;
        q->data.score[p->sumcrs+1] = sum / p->sumcrs;
    }
    printf("\nSuccess!\nYou can find the imformation by choosing \"List the Record\" if you want\n\n");
    return ;
}

void SortbyDScore(pCls p)   //Sort in descending order by total score of every student
{
    int i;
    Data temp;
    pStu maxid;
    // Select Sort
    pStu q,r;
    for (q=p->inf.head->next; q->next ; q = q->next)
    {
        maxid = q;
        for (r=q->next; r ;r= r->next)
        {
            if (r->data.score[0] > maxid->data.score[0]) {
                maxid = r;
            }
        }
        temp = q->data;
        q->data = maxid->data;
        maxid->data = temp;
    }
    int sum = 0;
    for (q=p->inf.head->next,r=p->inf.head,i=1;q;i++,r=q,q=q->next)
    {

        if (r != p->inf.head && r->data.score[0] == q->data.score[0]) {
            i --;
            sum ++;
        } else {
            i += sum;
            sum = 0;
        }
        q->data.rank=i;
    }
    printf("\nSuccess!\nYou can find the imformation by choosing \"List the Record\" if you want\n\n");

    return ;
}

void SortbyAScore(pCls p)   //Sort in ascending order by total score of every student
{
    int i;
    Data temp;
    pStu minid;
    // Select Sort
    pStu q,r;
    for (q=p->inf.head->next; q->next ; q = q->next)
    {
        minid = q;
        for (r=q->next; r ;r= r->next)
        {
            if (r->data.score[0] < minid->data.score[0]) {
                minid = r;
            }
        }
        temp = q->data;
        q->data = minid->data;
        minid->data = temp;
    }
    int sum = 0;
    pStu s = NULL;
    for (q=p->inf.head->next,r=p->inf.head,i=p->sumstu;q;i--,r=q,q=q->next)
    {

        if (r != p->inf.head && r->data.score[0] == q->data.score[0]) {
            if (s == NULL) {
                s = r;
            }
            i ++;
            sum ++;
        } else {
            i -= sum;
            if (sum != 0){
                for ( ;s!=q;s=s->next) {
                    s->data.rank -= sum;    
                }
            }
            s = NULL;
            sum = 0;
        } 
        q->data.rank=i;
    }
    if (s != NULL) {
        for ( ;s!=NULL;s=s->next) {
            s->data.rank -= sum;
        }
    }

    printf("\nSuccess!\nYou can find the imformation by choosing \"List the Record\" if you want\n\n");

    return ;
}

void SortbyNum  (pCls p)    //Sort in ascending order by number
{
    int i;
    Data temp;
    pStu minid;
    // Select Sort
    pStu q,r;
    for (q=p->inf.head->next; q->next ; q = q->next)
    {
        minid = q;
        for (r=q->next; r ;r= r->next)
        {
            if (r->data.stunum < minid->data.stunum) {
                minid = r;
            }
        }
        temp = q->data;
        q->data = minid->data;
        minid->data = temp;
    }
    printf("\nSuccess!\nYou can find the imformation by choosing \"List the Record\" if you want\n\n");

    return ;
}

void SortbyName (pCls p)    //Sort in dictionary order by name
{
    Data temp;
    pStu minid;
    // Select Sort
    pStu q,r;
    for (q=p->inf.head->next; q->next ; q = q->next) // traverse the linked list
    {
        minid = q;
        for (r=q->next; r ;r= r->next)
        {
            if (strcmp(r->data.name,minid->data.name) < 0) {
                minid = r;
            }
        }
        // swap the data field
        temp = q->data;
        q->data = minid->data;
        minid->data = temp;
    }
    printf("\nSuccess!\nYou can find the imformation by choosing \"List the Record\" if you want\n\n");
    return ;
}

pStu SearchNum  (pCls p, int num)   //Search by number
{
    pStu ret = NULL;
    pStu q;
    for (q=p->inf.head->next;q;q=q->next)
    {
        if (num == q->data.stunum) {
            ret = q;
            break;
        }
    }
    return ret;
}

pStu SearchName (pStu p,char* s)    //Search by name
{
    pStu ret = NULL;
    pStu q;
    if (p == NULL)
    {
        return ret;
    }
    for (q=p;q;q=q->next)
    {
        if (strcmp(q->data.name,s)==0) {
            ret = q;
            break;
        }
    }
    return ret;
}

void Analysis   (pCls p)    //Statistic analysis every course
{
    int i,j;
    int sub [SUBMAX+1][5]={0};
    pStu q;
    for (j=1;j<=p->sumcrs;j++) {
        for (q=p->inf.head->next;q;q=q->next)
        {
            if (q->data.score[j] >=90 && q->data.score[j] <=100) {
                sub [j][0] ++;
            }
            if (q->data.score[j] >=80 && q->data.score[j] <90) {
                sub [j][1] ++;
            }
            if (q->data.score[j] >=70 && q->data.score[j] <80) {
                sub [j][2] ++;
            }
            if (q->data.score[j] >=60 && q->data.score[j] <70) {
                sub [j][3] ++;
            }
            if (q->data.score[j] >=0 && q->data.score[j] <60) {
                sub [j][4] ++;
            }
        }
    }
    for (j=1;j<=p->sumcrs;j++) {
        printf("Course %d :\n",j);
        printf("         A     B     C     D     E\n");
        printf("num      ");
        for (i=0;i<5;i++) {
            printf("%d     ",sub[j][i]);
        }
        printf("\n");
        printf("percent  ");
        for (i=0;i<5;i++) {
            printf("%.2f  ",1.0*sub[j][i]/p->sumstu);
        }
        printf("\n\n");
    }
    return ;
}

void WtF (pCls p)           //Write to File
{
    FILE *fp;
    char filename[FILENAMEMAX] ;
    printf("Please input the filename you want:\n");
    getchar();
    gets(filename);
    if ((fp = fopen(filename,"w")) == NULL) {
        TurnPage(-1);
        printf("ERROR! Not able to write into a file !\n\n");
        return ;
    }
    FprintListboard(fp,p->sumcrs);
    int i;
    pStu q;
    for (q=p->inf.head->next;q;q=q->next)
    {
        FprintStu(fp,&q->data,p->sumcrs);
    }
    fclose (fp);
    return ;
}

void Correct (pCls p)       // Correct the imformation of a student that has already been stored
{
    int isStu = 0;
    pStu q;
    Data data;
    while (!isStu) {
        printf("Please input the Stu No.\n\n");
        int num;
        scanf("%d",&num);
        // find if the Stu No. that you inputs exists or not
        q = find(p,num);
        if (q == NULL) {
            TurnPage(-1);
            printf("Can't find the student using the Stu No. you offer!!\n\n");
            return ;
        }
        // Confirm the Stu No. you input
        printf("Is the information as followed you want to correct?  y/n ? \n\n");
        PrintListboard(p->sumcrs);
        PrintStu(&q->data,p->sumcrs);
        printf("\n");
        char yon = '\0';
        int isPrint = 0;
        getchar();
        while (!(yon == 'y') && !(yon == 'n')) {
            if (isPrint) {
                printf("Attention! please choose y/n to confirm your choice!\n\n");
            }
            scanf("%c",&yon);
            if (yon == 'y') {
                isStu = 1;
            } 
            if (yon == 'n') {
                isStu = 0;
            }
            isPrint = 1;
        }
        if (isStu == 0) {
            continue ;
        }
        // Initialization
        Data temp = q->data;
        memset(&q->data,0,sizeof(Data));
        memset(&data,0,sizeof(data));
        // read the data
        printf("Now please input new information to cover the old one\n");
        printf("order : Student No. , Name ,  and score from course_1 to course_%d\n",p->sumcrs);
        scanf("%d",&data.stunum);
        getchar();
        scanf("%s",&data.name);

        pStu pstu = NULL;
        int j;
        double sum = 0.0;
        for (j = 1;j <= p->sumcrs;j ++) {
            scanf("%lf",&data.score[j]);
            sum += data.score[j];
        }
        data.score[0] = sum;
        data.score[p->sumcrs+1] = sum / p->sumcrs;
        pstu = find(p,data.stunum); 
        // Judge if same Stu No. appears
        if (pstu != NULL) {
            q->data = temp;
            TurnPage(-1);
            printf("Same Stu No. appears!\n");
            printf("Fail to store \"%d %s\" ,for \"%d %s\" has already been stored\n\n",data.stunum,data.name,pstu->data.stunum,pstu->data.name);
            return ;
        }
    }
    // Copy the data field 
    q->data = data;
    printf("Success! Data you input has modified the wrong ones!\n\n");
    return ;
} 
//tool_f.h

pCls CreateClass (void);    //Create a Class
pStu find (pCls p, int stunum);//Find the student with Stu No.
void PrintListboard(int sumcrs);//Print the listboard to console
void PrintStu(pData pdata,int sumcrs);  //Print Student's inf to console
void FprintListboard(FILE *fp,int sumcrs);//Print the listboard to file
void FprintStu(FILE *fp,pData pdata,int sumcrs);//Print Student's inf to file
void Free (pCls p);         //Free all memory allocated by executions
void add (Inf inf,pData pdata);// Add a node to the linked list
int IsRun(int *isRun , int state, pCls p);// Judge whether a function can be run
//tool_f.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "function.h"
#include "console_output_control.h"

pCls CreateClass (void)         //Create a Class
{
    pCls p = (pCls)malloc(sizeof(Class));
    if (p == NULL) {
        TurnPage(-1);
        printf("ERROR! Not able to allocate a block of memory !\n\n");
        system("pause");
        exit(MALLOCERR);
    }
    memset(p,0,sizeof(Class));

    p->inf.head = p->inf.tail = (pStu)malloc (sizeof(Student));
    if (p->inf.head == NULL) {
        TurnPage(-1);
        printf("ERROR! Not able to allocate a block of memory !\n\n");
        system("pause");
        exit(MALLOCERR);
    }
    p->inf.head->next = NULL;
    return p;
}

pStu find (pCls p, int stunum)  //Find the student with Stu No.
{
    pStu q,ret = NULL;
    for (q=p->inf.head->next;q;q=q->next) {
        if (q->data.stunum == stunum) {
            ret = q;
        }
    }
    return ret;
}

void PrintListboard(int sumcrs) //Print the listboard to console
{

    char listboard[][10] = {
        "Stu No.",
        "Name",
        "Rank",
        "Sum",
        "Average",
        "Course1",
        "Course2",
        "Course3",
        "Course4",
        "Course5",
        "Course6",
    } ;
    int i;
    printf("%-*s",STUNOMAX,listboard[0]);
    printf("%-*s",NAMEMAX,listboard[1]);
    printf("%-*s",RANKLEN,listboard[2]);
    printf("%-*s",SUMLEN,listboard[3]);
    printf("%-*s",AVERLEN,listboard[4]);
    for (i=0;i<sumcrs;i++) {
        printf("%-*s",CRSLEN,listboard[i+5]);
    }
    puts("\n");
    return ;
}

void PrintStu(pData pdata,int sumcrs)           //Print Student's inf to console
{
    int i;
    printf("%-*d",STUNOMAX,pdata->stunum);
    printf("%-*s",NAMEMAX,pdata->name);
    printf("%-*d",RANKLEN,pdata->rank);
    printf("%-*.2f",SUMLEN,pdata->score[0]);
    printf("%-*.2f",AVERLEN,pdata->score[sumcrs+1]);
    for (i=1;i<=sumcrs;i++) {
        printf("%-*.2f",CRSLEN,pdata->score[i]);
    }
    puts("\n");
}

void FprintListboard(FILE *fp,int sumcrs)       //Print the listboard to file
{

    char listboard[][10] = {
        "Stu No.",
        "Name",
        "Rank",
        "Sum",
        "Average",
        "Course1",
        "Course2",
        "Course3",
        "Course4",
        "Course5",
        "Course6",
    } ;
    int i;
    fprintf(fp,"%-*s",STUNOMAX,listboard[0]);
    fprintf(fp,"%-*s",NAMEMAX,listboard[1]);
    fprintf(fp,"%-*s",RANKLEN,listboard[2]);
    fprintf(fp,"%-*s",SUMLEN,listboard[3]);
    fprintf(fp,"%-*s",AVERLEN,listboard[4]);
    for (i=0;i<sumcrs;i++) {
        fprintf(fp,"%-*s",CRSLEN,listboard[i+5]);
    }
    fputs("\n",fp);
    return ;
}

void FprintStu(FILE *fp,pData pdata,int sumcrs) //Print Student's inf to file
{
    int i;
    fprintf(fp,"%-*d",STUNOMAX,pdata->stunum);
    fprintf(fp,"%-*s",NAMEMAX,pdata->name);
    fprintf(fp,"%-*d",RANKLEN,pdata->rank);
    fprintf(fp,"%-*.2f",SUMLEN,pdata->score[0]);
    fprintf(fp,"%-*.2f",AVERLEN,pdata->score[sumcrs+1]);
    for (i=1;i<=sumcrs;i++) {
        fprintf(fp,"%-*.2f",CRSLEN,pdata->score[i]);
    }
    fputs("\n",fp);
    return ;
}

void add (Inf inf,pData pdata)          // Add a node to the linked list
{
    pStu pstu = (pStu)malloc (sizeof(Student)) ;
    if (pstu == NULL) {
        TurnPage(-1);
        printf("ERROR! Not able to allocate a block of memory !\n\n");
        system("pause");
        exit(MALLOCERR);
    }

    pstu->data = *pdata;
    pstu->next = inf.head->next;
    inf.head->next = pstu;
    return ;
}

void Free (pCls p)                      //Free all memory allocated by executions
{
    pStu q,r;
    for (q=p->inf.head->next,r=p->inf.head;q;r=q,q=q->next){
        free(r);
    }
    free(r);
    free(p);
    return ;
}

int IsRun(int *isRun, int state, pCls p)// Judge whether a function can be run
{
    int ret=0;
    int i;
    switch (state) {
        case 0:
            ret = 1; 
            break;
        case 2:
            for (i=3;i<=FUNCNUM;i++) {
                isRun [i] = 0;
            }
            ret = 1;
            break;
        case 1:
            if (isRun[1] || isRun[2]) {
                TurnPage(-1);
                printf("Data has already been loaded!\n");
                printf("You cannot cover the original data!\n");
                printf("\n");
            } else {
                ret = 1;
            }
            break;
        case 3:
        case 4:
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
        case 14:
            if (!(isRun[1] || isRun[2])) {
                TurnPage(-1);
                printf("Students\' Data has not been loaded !!!\n");
                printf("Please input your file or input manually first\n");
                printf("\n");
            } else {
                ret = 1;
            }
            break;
        case 5:
        case 6:
        case 12:    
        case 13:
            if (!(isRun[1] || isRun[2])) {
                TurnPage(-1);
                printf("Students\' Data has not been loaded !!!\n");
                printf("Please input your file or input manually first\n");
            } else if (!isRun[4]) {
                ScoreofStu(p);
                isRun[4] = 1;
                ret = 1;
            } else {
                ret = 1;
            } 
            break;
        default: 
            ret = 1;
    }
    return ret;
} 

//console_output_control.h

#define LINEMAX 150

void Init (void);           //Print prompt message
void PrintHead (void);      //Print the page header
void Cls(void);             //Clear the screen
void CleanLine (void);      //Clean the present line 
void TurnPage (int state);  //Turn to the page of running function
void EndPut (void);         //Print the closing
//console_output_control.c
#include <windows.h>
#include <unistd.h>
#include <stdio.h>
#include "console_output_control.h"

void Init (void)        //Print prompt message
{
    PrintHead();
    FILE *fp = fopen("Init.txt","r");
    if (fp == NULL) {
        printf("Fail to open the ceremony-text,but that won\'t matter\n\n");
        return ;    
    }
    char s[LINEMAX+1];
    while (fgets(s,LINEMAX,fp) != NULL) {
        Sleep(1250);
        puts(s);
    }
    Sleep(1250);
    return ;
}

void PrintHead (void)   //Print the page header
{
    printf("##    Student Score Management System \n");
    printf("##    Produced by Zhu Xiaoxie, Xiao Boren, and Zhang Cui\n\n");
}

void Cls(void)          //Clear the screen
{
    system("pause");
    system("cls");
    PrintHead();
    return ;
}

void CleanLine (void)   //Clean the present line 
{
    printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
    printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
    printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
    char c='\0';
    printf("%*c",70,c);
    printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
    printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
    printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
    return ;
}

void TurnPage (int state)//Turn to the page of running function
{
    // Create a string map
    char s[][LINEMAX+1] = {
        "* 0.  Exit ",
        "* 1.  Read from a file",
        "* 2.  Append record manually",
        "* 3.  Calculate total and average score of every course",
        "* 4.  Calculate total and average score of every student",
        "* 5.  Sort in descending order by total score of every student",
        "* 6.  Sort in ascending order by total score of every student",
        "* 7.  Sort in ascending order by number",
        "* 8.  Sort in dictionary order by name",
        "* 9.  Search by number",
        "* 10.  Search by name",
        "* 11.  Statistic analysis for every course",
        "* 12.  List record",
        "* 13.  Write to a file ",
        "* 14.  Correct the information of a student that has already been stored",
    };
    system("cls");
    PrintHead();
    if (state >= 0 && state <=14)
        printf("####  %s Page",s[state]);
    else 
        printf("####  ERROR Page!!!");
    printf("\n\n\n");
    return;
}

void EndPut (void)      //Print the closing
{
    printf("\t\tThe program is being shut down");
    int i;
    for (i=0;i<6;i++) {
        sleep(1);
        printf(".");
    }
    printf("\n\n");
    Sleep(500);
    printf("\t\t\t\tOK now~~~~~\n\n");
    Sleep(500);
    printf("\t\t\tBye~Bye~");
    printf("\n\n");
    Sleep(500);
    system("pause");
    return ;
}




版权声明:本文为jlu_nnbs原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。