2016程序设计基础(C语言)课程设计_nnbs的博客-程序员秘密

技术标签: 设计  C  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 ;
}



版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/jlu_nnbs/article/details/53759561

智能推荐

android 背景图缩放,解决android:background背景图片被拉伸问题_weixin_39639600的博客-程序员资料

ImageView中XML属性src和background的区别:background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸。src是图片内容(前景),bg是背景,可以同时使用。此外:scaleType只对src起作用;bg可设置透明度,比如在ImageButton中就可以用android:scaleType控制图片的缩放方式如上所述,backg...

qt用odbc连接mysql_【原创】Qt 使用ODBC driver 连接SQL Server_午餐时间到了的博客-程序员资料

最近在做数据库的课程设计。第一个需要解决的问题是使用什么工具来实现这个系统。经过一番资料查找,决定使用SQL Server Express 2012作为服务器,使用Qt作为编写客户端程序语言。问题是client如何连接SQL Server? 下面是我的解决方法。1.开启windows上的SQL Server 的ODBC驱动ODBC 是一个调用级接口,它使得应用程序得以访问任何具有 ODBC 驱动程...

翻页时钟代码大公开_lovefan的博客-程序员资料_翻页钟开源代码

不少朋友向我要翻页时钟的代码,现在贴给大家。代码水平有限,见谅。看不明白的可以问我:)js// miniprogram/pages/flipClock/jsconst moment = require('../../../utils/moment-with-locales.min.js');const Lunar = require('../../../utils/lunar.js');var startX, endX;var moveFlag = true; // 判断执行滑动事件.

JAVA切换不了FTP服务器目录_解决linux下ftp指定访问目录无法修改的问题_豪睿刘爱上楼楼梯的博客-程序员资料

他的系统是CentOS,是RH派系的。我把vsftpd安装配置好了,以为大功告成,但客户端访问提示如下错误:500 OOPS: cannot change directory:/home/ftp原因是他的CentOS系统安装了SELinux,因为默认下是没有开启FTP的支持,所以访问时都被阻止了。//查看SELinux设置# getsebool -a|grep ftpftpd_disable_tr...

.NET实现网络爬虫_hyunbar的博客-程序员资料_.net 爬虫

爬虫的特征和运行方式User-Agent:主要用来将我们的爬虫伪装成浏览器。Cookie:主要用来保存爬虫的登录状态。连接数:主要用来限制单台机器与服务端的连接数量。代理IP:主要用来伪装请求地址,提高单机并发数量。爬虫工作的方式可以归纳为两种:深度优先、广度优先。深度优先就是一个连接一个连接的向内爬,处理完成后再换一下一个连接,这种方式对于我们来说缺点很明显。 广度优先...

c web service_简单并快乐着的博客-程序员资料

转自 http://blog.163.com/wzbwzb_187/blog/static/6632425720106424158919/ 以下是源代码: /**************filename: Server.cpp**************** 该程序通过标准socket实现简单Http服务器 运行该服务器可以通过浏览器访问服务器目录下的 Html文件和j

随便推点

Paxos、Raft、ZAB算法_知知之之的博客-程序员资料_paxos raft zab

Paxos算法是Leslie Lamport在1990年提出的一种基于消息传递的一致性算法。基于Paxos协议的数据同步与传统主备方式最大的区别在于:Paxos只需超过半数的副本在线且相互通信正常,就可以保证服务的持续可用,且数据不丢失。Paxos将系统中的角色分为提议者 (Proposer),决策者 (Acceptor),和最终决策学习者 (Learner):在多副本状态机中,每个副本同时具有Proposer、Acceptor、Learner三种角色。Paxos算法通过一个决议分为两个阶段(Learn阶段

Bibtex格式类型_小王同学w的博客-程序员资料_bibtex类型

出于保留资源和查找方便,转载自https://blog.csdn.net/kmsj0x00/article/details/85318057类型简介必需关键字可省略关键字@article期刊或杂志上的一篇文章。author, title, journal, year.volume, number, pages, month, [email protected]有确定出版社的书籍。author或editor, title, publisher, year.volume或number, series, address, e

SpringBoot-Google二步验证_apkqfa6158的博客-程序员资料

SpringBoot-Google二步验证概念:Google身份验证器Google Authenticator是谷歌推出的基于时间的一次性密码(Time-based One-time Password,简称TOTP),只需要在手机上安装该APP,就可以生成一个随着时间变化的一次性密码,用于帐户验证。Google身份验证器是一款基于时间与哈希的一次性密码算法的两步验证软件令牌,此软件...

Maven_weixin_41249041的博客-程序员资料

maven帮助构建项目 项目和项目之间的依赖关系,管理jar包。项目管理工具,管理java项目:1、项目对象模型 POM对象模型,每个maven工程中都有一个pom.xml文件,定义工程所依赖的jar包、本工程的坐标、打包运行方式。。2、依赖管理系统(基础核心 )maven通过坐标对项目工程所依赖的jar包统一规范管理。3、maven定义一套项目生命周期清理、初始...

css教程系列5:px、em、rem区别介绍_邢晋宇的博客-程序员资料

1 PXpx像素(Pixel)。相对长度单位。像素px是相对于显示器屏幕分辨率而言的。PX特点1. 浏览器无法调整px单位的字体,以em或rem为字体单位可调整字体。 2 EMem是相对长度单位。EM特点1. em的值并不是固定的;2. 行高:n倍的 em = n *父级元素的像素px大小(若无则*浏览器的默认字体)。3 REMrem是CSS3新增的一个相对单位(root em,根em),也是相对...

Matlab: 作图_weixin_34413357的博客-程序员资料

查看当前变量可以用who,查看当前变量及其结构,大小,类型等,可以用whos。控制图的大小figure(‘position’,[x0,y0,dx,dy]);figure(fig number);控制图例(legend)legend('leg1','leg2','Location','NorthWest');% hide the legend boxlegend boxoff...

推荐文章

热门文章

相关标签