The course design of Guang-gong--Student achievement statistic system

Source: Internet
Author: User
Tags assert prev

Preface: The title, this article is involved in the project is last year should learn sister asked to help write, pure C language, in Turbo C compiled through. Recently collated data, feel that the project involved in the structure, linked lists, file operations, assertions and other basic knowledge, as well as the small project function design and variable naming, such as easy to ignore the knowledge of the beginner C language friends should be helpful, so decided to publish. At the same time, the need to complete a similar course design students a reference, but should pay attention to understand the knowledge points, and should not copy and paste a hasty. 】

Problem Description:
The semester exam is over, the average grade of each student in a class is counted, the average score of each course is achieved, and the output of the failing person is listed according to the order of the individual average score from high to low. The input and output formats are custom-defined.

Implementation tips:
Exam courses are: Advanced mathematics, Physics, foreign languages, C language 4 courses. Input all the students, the data processing, output requirements of the content, the function of the program mainly includes the following aspects:
① Input Score
② Modify Records
③ Delete Records
The ④ output scores are sorted by average and the students with average grades are marked.
The ⑤ interface provides the above functional selection.
The number of ⑥ students is controlled automatically by the software according to the record number of records entered.
⑦ provides output to the file as well as reading performance from the file function.

test Data:(custom simulation data as follows)

Overall Design
Through the problem description and the analysis of the SOFTWARE function requirement, we make the whole idea of the design of the program, the key point is the data structure design and the preservation. This paper presents a solution to the above requirements with linear table and file IO, which are described in detail.
Data
In this paper, the linear table is designed bidirectional cyclic chain list, in which the node data field is the structural body stinfo.

User Information
typedef struct STUDENT
{
    int  id;        Learn number
    char Name[namelen];//name
    int  amaths;    High number of scores
    int  physics;   Physical score
    int  Foreign;   Foreign language score
    int  clan;  C Language score
    float GPA;  Average score
}stinfo;
User information node structure
typedef struct STUDENTNODE
{
    stinfo data;        <! Data domain
    struct studentnode *prev;   <! Pointer field
    struct Studentnode *next;
} Stnode;

algorithm
Many of the procedures require multiple linked lists for querying and sorting, for convenience, this article uses the traversal algorithm to query, using bubble method to sort. See Appendix for details.

File Actions
Includes reading and writing of the contents of the file. To make it easier for users to add student information directly to the file, to view the results of the program for data processing, and to demonstrate it, we did not manipulate the data in binary mode, but instead wrote the data as text to the file, thus adding code to manipulate the string. See Appendix for details.

Program Flowchart

partial operation and detail Description

1, the system provides five options, according to the prompts to operate.

2, the file operation involves two files, the macro definition is as follows

The
results of the #define Students_file "./students.txt"
/////The outcome of the record-keeping process
#define Score_file "./gpa.txt"

Under normal circumstances, the data from the file Students_file and manual input into the list of two channels, after processing by the linked list to file Students_file and file Score_file.

3, the main program is responsible for receiving the user input data, and upload the corresponding interface function, because the standard input (that is, the keyboard) is buffered, so you need to add the following line of code to empty the buffer.

while (GetChar ()!= ' \ n ') {;}

4, input ID number and results should be noted: The system will be limited to the ID number of 1000 to 1999, and can not repeat, the result range is limited to 0 to 100.

Interface Function Description
Related macro Definitions

#define OK      1
#define ERROR   0
#define EXIT    2
#define EXIST   1
#define NOTEXIST 0

System Operation Correlation function

*
 * Check whether the ID entered by the user exists in the linked list;
 * There is a return exist, there is no return notexist. 
* *
int checkid (stnode *sthead, int id);

*
 * Check if user input is reasonable;
 * no return value.
* *
void Inputscore (int *score);

*
 * Initialize the system, create the linked list and read the data from the Students_file to insert the list;
 * Initialize successfully return OK, otherwise return error.
*
/int initsystem (void);

*
 * Wait for the user to enter the node data and insert the list;
 * No return value.
*
/void Inputrecord (void);

*
 * Wait for user to modify node data;
 * No return value.
*
/void Alterrecord (void);

*
 * Wait for user to delete specific node;
 * No return value.
*
/void DeleteRecord (void);
 * * To process the linked list according to system requirements and output the result;
 * no return value.
*
/void Outputrecord (void);

*
 * exit system, save linked list data, free memory;
 * No return value.
*
/void Exit (void);

Linked list operation correlation function

*
 * Initialize a double loop list, *sthead as a chain header pointer;
 * Initialization succeeds returning OK, otherwise error returned.
*
/int initlist (Stnode **sthead);

*
 * Displays the content contained in the list sthead;
 * No return value.
* *
void Showlist (Stnode *sthead);

*
 * Copy list sthead as Cphead;
 * Replication successfully returns the list header pointer, otherwise null is returned.
* *
stnode *copylist (Stnode *cphead, Stnode *sthead);

*
 * Save the data in the linked list Sthead to the file FP;
 * No return value.
*
/void Savelist (FILE *fp, Stnode *sthead);

*
 * Release the memory occupied by the list sthead;
 * No return value.
* *
void Freelist (Stnode *sthead);

*
 * Inserts the node with St as the data field to the end of the list sthead;
 * Insert succeeds return OK, otherwise return error.
* *
int Insertnode (Stnode *sthead, Stinfo St);

*
 * Delete the node of the related ID number on the list sthead;
 * Delete successful return OK, otherwise return error.
* *
int deletenode (stnode *sthead, int id);

* * Change the result of the
 column field of the related ID number of the linked list sthead to score;
 * The modification returns OK successfully, otherwise the error returns.
* *
int Alternode (stnode *sthead, int ID, int column, int score);

*
 * To sort the nodes of the linked list Sthead in descending order of average;
 * No return value.
* *
void Descsort (Stnode *sthead);

*
 * Displays the list of sthead in the linked list;
 * No return value.
* *
void Showfail (Stnode *sthead);

Appendix

/************************************************ File name:resultsms.c Created date:2014-10-25 18:38 Modified dat e:2014-10-27 20:07 author:luhuadong email:luhuadong@163.com Description: *************************************  /#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h>/// The results of #define STUDENTS_FILE "./students.txt"///The result #define Score_file "./gpa.txt" #define OK 1 #define ER ROR 0 #define EXIT 2 #define EXIST 1 #define NOTEXIST 0///user profile data length #define STINFOLEN 80///Structural body Student Member name
    Length #define Namelen 32///user information typedef struct STUDENT {int id;
    Char Name[namelen];
    int amaths;
    int physics;
    int Foreign;
    int clan;
float GPA;

}stinfo;        User information node structure typedef struct STUDENTNODE {stinfo data; <!   Data domain struct Studentnode *prev; <!
Pointer field struct Studentnode *next;

}stnode;
Stnode *sthead = NULL;

int status = OK;/* Function declaration */int Checkid (stnode *sthead, int id);
void Inputscore (int *score);
int Initsystem (void);
void Inputrecord (void);
void Alterrecord (void);
void DeleteRecord (void);
void Outputrecord (void);

void Exit (void);
int initlist (Stnode **sthead);
void Showlist (Stnode *sthead);
Stnode *copylist (Stnode *cphead, Stnode *sthead);
void Savelist (FILE *fp, Stnode *sthead);
void Freelist (Stnode *sthead);
int Insertnode (Stnode *sthead, Stinfo St);
int Deletenode (Stnode *sthead, int id);
int Alternode (Stnode *sthead, int ID, int column, int score);
void Descsort (Stnode *sthead);

void Showfail (Stnode *sthead);   /* ********************************************************/int main (void) {int mode;
        Mode selection//system initialization if (ERROR = = Initsystem ()) {perror ("initialized system fail");
    return-1;
        while (ok = = status) {//System ("clear");
        printf ("++++++++++++++++++++++++++++++\n"); printf ("1-input score \ n 2-Modify record \ n 3-delete record \ n 4-output score \ n 5-exit \ n");
        printf ("++++++++++++++++++++++++++++++\n>>");
            while (scanf ("%d", &mode) = = 0) {while (GetChar ()!= ' \ n ') {;}
        printf ("Error, try again >>\n");

        while (GetChar ()!= ' \ n ') {;}
            Switch (mode) {case 1:inputrecord ();
            Case 2:alterrecord ();
            Case 3:deleterecord ();
            Case 4:outputrecord (); Case 5:exit ();
            Break
        Default:break;
} return 0; }/* ******************************************************** */int Checkid (stnode *sthead, int id) {StNode *currN
    Ode = sthead->next;
        while (Currnode!= sthead) {if (id = = currnode->data.id) {return EXIST;}
    Currnode = currnode->next;
return notexist;
    int Initsystem (void) {FILE *fp = NULL;
    Stinfo St;
    int i=0, j=0, k=0;        Char Readbuf[stinfolen]; <!
  Store ReadFromFile () Read string  Char Strstinfo[6][namelen]; <!
        Saves one record per read if (ERROR = = Initlist (&sthead)) {printf ("Created initial LIST failed.\n");
        status = ERROR;
    return ERROR;
    fp = fopen (Students_file, "R");
        if (NULL = fp) {printf ("Open%s failed.\n", students_file);
        status = ERROR;
    return ERROR;
            while (NULL!= fgets (readbuf, Stinfolen, FP)) {///A total of 6 fields per record for (i=0; i<6; i++) { while (('!= readbuf[j]) \ && (' \ n '!= readbuf[j]) \ &&amp ;
            (' t '!= readbuf[j])
            {strstinfo[i][k++] = readbuf[j++];
            } Strstinfo[i][k] = ';    j + +; <!
        Skip spaces k = 0;
        } j = 0;
        St.id = Atoi (strstinfo[0]);
        strcpy (St.name, strstinfo[1]); St.
        Amaths = Atoi (strstinfo[2]); St.
        Physics = Atoi (strstinfo[3]); St. ForEign = Atoi (strstinfo[4]); St.
        Clan = atoi (Strstinfo[5]); St.

        GPA = 0;
    if (Error = = Insertnode (Sthead, ST)) {return error;}
    printf ("Initialize linked list ==>\n");
    Showlist (Sthead);
    Fclose (FP);
return OK;
    } void Inputscore (int *score) {scanf ("%d", score);
    while (GetChar ()!= ' \ n ') {;}
        while (*score < 0 | | *score >) {printf ("" "Performance Range: 0--100" \n>> ");
        scanf ("%d", score);
    while (GetChar ()!= ' \ n ') {;}
} return;
    } void Inputrecord (void) {Stinfo st;
    printf ("Input ID:");
    scanf ("%d", &st.id);
    if (St.id > 1999 | | | St.id < 1000) {printf ("ID range is 1000~1999.\n");
        } if (EXIST = = Checkid (Sthead, st.id)) {printf ("Duplicate ID!\n");
    return;
    while (GetChar ()!= ' \ n ') {;}
    printf ("Name:");
    Gets (St.name);
    printf ("High number of results:"); Inputscore (&st.
    Amaths);
    printf ("Physical score:"); Inputscore (&st.
    Physics);
   printf ("Foreign Language Score:"); Inputscore (&st.
    Foreign);
    printf ("C Language score:"); Inputscore (&st.

    CLAN);
    if (ok = = Insertnode (Sthead, st)) {printf ("ok\n");}

    else {printf ("failed\n");}
return;
    } void Alterrecord (void) {int id, column, score;
    printf ("Input ID:");

    scanf ("%d", &id);
        if (notexist = = Checkid (sthead, id)) {printf ("ID does not exist!\n");
    return;
    printf ("1-high number \t2-physical \t3-foreign language \t4-c language \ \ r \ n Modify subject >>");
    scanf ("%d", &column);
    printf ("Revised result:");

    scanf ("%d", &score);
    if (ok = = Alternode (sthead, ID, column, score)) {printf ("ok\n");}

    else {printf ("failed\n");}
return;
    } void DeleteRecord (void) {int id;
    printf ("Input ID:");

    scanf ("%d", &id);
        if (notexist = = Checkid (sthead, id)) {printf ("ID does not exist!\n");
    return;
    } if (ok = = Deletenode (sthead, id)) {printf ("ok\n");}

    else {printf ("failed\n");}
return;
    } void Outputrecord (void) {FILE *fp = NULL; Stnode *cphead = Null;
        if (ERROR = = Initlist (&cphead)) {printf ("Created output LIST failed.\n");
        status = ERROR;
    return;

    } copylist (Cphead, Sthead);
        if (NULL = = Cphead) {printf ("Copy failed.\n");
    return;
    } descsort (Cphead);
    printf ("Sorted by average ==>\n");
    Showlist (Cphead);
    fp = fopen (Score_file, "w");
        if (NULL = fp) {printf ("Open%s failed.\n", score_file);
    return;
    printf ("Failed student ==>\n");
    Showfail (Cphead);
    Savelist (FP, cphead);
    Freelist (Cphead);

    Fclose (FP);
return;
    } void Exit (void) {FILE *FP = fopen (Students_file, "w");
        if (NULL = fp) {printf ("Open%s failed.\n", students_file);
    return;
    Savelist (FP, sthead);
    printf ("Save to file ... \ n");
    Freelist (Sthead);
    Fclose (FP);

    printf ("Successful exit.\n");
    status = EXIT;
return; }//**************************************************** int Initlist (StNOde **sthead) {*sthead = (Stnode *) malloc (sizeof (Stnode));
        if (NULL!= *sthead) {(*sthead)->prev = *sthead;
        (*sthead)->next = *sthead;
    return OK;
} return ERROR;
    } void Showlist (Stnode *sthead) {Stnode *currnode = NULL;
    ASSERT (NULL!= sthead);
    printf ("================================================================\n");

    printf ("Learn number \ t name \t\t high number \ t physics \ t foreign language \TC \ t average score \ n");
    Currnode = sthead->next;
                while (Currnode!= sthead) {printf ("%d\t%-8s\t%d\t%d\t%d\t%d\t%.2f\n", currnode->data.id, \ Currnode->data.name, Currnode->data. Amaths, \ Currnode->data. Physics, Currnode->data. Foreign, \ Currnode->data. Clan, Currnode->data.
        GPA);
    Currnode = currnode->next;
printf ("================================================================\n");
    } stnode *copylist (Stnode *cphead, Stnode *sthead) {Stinfo st; Stnode *currnode= NULL;

    ASSERT (null!= cphead && null!= sthead);
    Currnode = sthead->next;
        while (Currnode!= sthead) {st = currnode->data; St. GPA = (float) (St. Amaths + St. Physics + St. Foreign + St.
        Clan)/4;
        if (ERROR = = Insertnode (Cphead, ST)) {return NULL;}
    Currnode = currnode->next;
return cphead;
    } void Savelist (FILE *fp, Stnode *sthead) {Stnode *currnode = NULL;

    ASSERT (null!= fp && null!= sthead);
    Currnode = sthead->next; while (Currnode!= sthead) {fprintf (FP,%d%s\t%d%d%d%d%.2f\n, currnode->data.id, \ C Urrnode->data.name, Currnode->data. Amaths, \ Currnode->data. Physics, Currnode->data. Foreign, \ Currnode->data. Clan, Currnode->data.
        GPA);
    Currnode = currnode->next;

    } void Freelist (Stnode *sthead) {///release list stnode *currnode = sthead->next;
 while (Currnode!= sthead) {       Sthead->next = currnode->next;
        Free (currnode);
    Currnode = sthead->next;;   }//free (Currnode); <!
An error occurs when the User Information table is empty (sthead);
    int Insertnode (Stnode *sthead, Stinfo st) {stnode = NULL;

    ASSERT (NULL!= sthead);
    New = (Stnode *) malloc (sizeof (Stnode));

        if (NULL!= new) {new->data = st;
        New->next = Sthead;
        New->prev = sthead->prev;
        Sthead->prev->next = new;
        Sthead->prev = new;
    return OK;
} return ERROR;
    int Deletenode (Stnode *sthead, int id) {Stnode *currnode = sthead->next;
    Stnode *delnode = NULL;
            while (Currnode!= sthead) {if (id = = currnode->data.id) {delnode = Currnode;
            Currnode->prev->next = currnode->next;
            Currnode->next->prev = currnode->prev;
            Currnode = currnode->prev;
            Free (Delnode);
return OK;        } Currnode = currnode->next;
} return ERROR;
    int Alternode (Stnode *sthead, int ID, int column, int score) {Stnode *currnode = sthead->next;
    Stnode *delnode = NULL; while (Currnode!= sthead) {if (id = = currnode->data.id) {printf ("==>\n high number before modification:%d\t physics: %d\t Foreign Language:%D\TC language:%d\n ", Currnode->data. Amaths, Currnode->data. Physics, \ Currnode->data. Foreign, Currnode->data.

            CLAN); if (1 = = column) {currnode->data.
            Amaths = score;} else if (2 = = column) {currnode->data.
            Physics = score;} else if (3 = = column) {currnode->data.
            Foreign = score;} else if (4 = = column) {currnode->data.
            Clan = score;}

            else {return ERROR;} printf ("Modified ==>\n:%d\t Physics:%d\t Foreign Language:%D\TC language:%d\n", Currnode->data. Amaths, Currnode->data. Physics, \ Currnode->data. Foreign, CurrNode->data.
            CLAN);
        return OK;
    } Currnode = currnode->next; } return

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.