Project introduction
Student information management system is a system based on C language, which is useful to bubble sort, pointer, structure, binary array and so on. Write each function through modular method, and call the functions of each module in the main interface function to realize the following specific functions:
- Addition, deletion, modification and query of student information
- Ranking of students' grades
- Count the number of students
- Displays information for all students.
- Archive student information
overall design
In this experiment, the main function opens the document that saves the data results and calls the main interface function; Draw the border in the main interface function welcom(), and display each function and the function implementation method corresponding to each function.
Input information function addInfo(): this function saves the input information through the structure student. In order to ensure the accuracy of subsequent operations of data, the information of each student is identified through the uniqueness of student number. By writing and calling an isIdSame() function, this function ensures the uniqueness of student number by traversing all student numbers and confirming student number, If the student number is repeated, the user will be prompted to re-enter the function.
Find student information function: find student information by student number, write and call a findIndex() function, which will traverse the student number information of the structure and return the coordinates of the student number. If the student number is not found, return - 1; Save the returned results through the variable target. If the target is not equal to - 1, the program finds the student number and outputs all the information of the student by writing and calling a showInfo() function; Otherwise, the output query this person, because the subscript cannot be negative.
Update student information function update(): find the student by student number and call findIndex() function to determine the location of the student. If the returned result is less than 0, the function ends and no one is found; If it is greater than 0, the student will be found, and the user needs to change the content of an item by nesting the do... while function switch selection statements.
Delete function del(): the steps of finding students are the same as the process of updating the student information function. If the findIndex() function is less than 0, the function ends. Otherwise, the array of structures is overwritten from the cursor forward through a for loop to achieve the effect of deletion.
Insert student information function inserInfo(): locate the insertion position by requiring the user to enter the position. If the total number entered by the user is greater than the total number of structure arrays, it will be inserted into the last array. Otherwise, through a for loop, move the array one bit back from the last, move the result of the position entered by the user to the next array, write and call the insertion function insercurrencinfo() to overwrite and insert the current position array. The insercurrencinfo() function is only responsible for writing all the information received from the element at this location.
Sorting function sortTotal(): create a temporary variable to provide information exchange between elements. Through the double loop nested structure, the scores of the structure are compared, and the positions are exchanged for bubble sorting. After the final sorting, the information of all students whose scores are sorted from high to low is output.
Display student information function showAllInfo(): this function traverses the student structure through the for loop through the global variable count (which records all the added, inserted or deleted student information and can accurately record the total number of students), so as to output all the generated information.
Student data archive function writeData(): this function defines a pointer to open "stu. Data archive" by writing Txt "and assign the address of the text to the pointer fp. Traverse the elements in the structure through a for loop, and input the attributes of the elements in the structure into "stu TXT text.
Detailed code
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <windows.h> void addInfo();// add to void welcom(); //main interface void showAllInfo();//Show all information void showInfo(struct Student student);//Show student information int findIndex(struct Student student[],int id);// Return the corresponding subscript according to the student number void del(); //delete void search();// Find student information void updata();//to update void sortTotal();//Sort by total score void writeData();//Write data to file void initData();//Initialization data reads data from the file and initializes the array void showCount(); // Display and store the number of students void inserInfo();//Insert student information void inserCurrentInfo(int site); //Insert at current position void con();//press any key to continue int find1(struct Student student[],int id); //Judge whether the student number has repetition, return 1, and return 0 without repetition void isIdSame(int x); //Verify whether the entered student number is duplicate void gotoxy(int x,int y);//Cursor positioning int color(int c); //Set color output struct Student{ int id; char name[20]; int _math; int _chinese; int _english; int total;// Total score } student[500]; int count=0;// Record the number of students stored in the current array //Main function int main(){ initData(); welcom(); return 0; } // Cursor positioning void gotoxy(int x, int y) { COORD pos; pos.X = x; //Abscissa pos.Y = y; //Ordinate SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } //Set color output int color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //Change text color return 0; }; // main interface void welcom() { while(1){ system("cls"); int n; int i,j = 1; color(10); //Light green border for (i = 5; i <= 35; i++) //Cycle y-axis coordinates and print out the upper and lower borders=== { for (j = 10; j <= 57; j++) //Cycle the x-axis coordinates and print out the left and right borders|| { gotoxy(j, i); if (i == 5 || i == 35) printf("="); //Output top and bottom borders=== else if (j == 10 || j == 56) printf("||"); //Output left and right borders|| } } color(15);//white gotoxy(25,8); printf("Student information management system"); color(14); //Set the font color to yellow gotoxy(15, 12); printf("1: Enter student information"); gotoxy(35, 12); printf("2.Find student information"); gotoxy(15, 16); printf("3.Delete student information"); gotoxy(35,16); printf("4.Modify student information"); gotoxy(15, 20); printf("5.Insert student information"); gotoxy(35,20); printf("6.Sort according to students' grades"); gotoxy(15, 24); printf("7.Count the total number of students"); gotoxy(35,24); printf("8.Show all student information"); gotoxy(15, 28); printf("9.Student data archive and exit"); gotoxy(25,32); int choose; printf("Please select:[ ]\b\b"); //\b the cursor returns one space color(15); // The color changes back to white scanf("%d", &choose); switch (choose){ case 1:addInfo(); break; case 2:search(); break; case 3:del(); break; case 4:updata(); break; case 5:inserInfo();break; case 6:sortTotal(); break; case 7:showCount(); break; case 8:showAllInfo(); break; case 9:writeData();exit(0); } } } // add to void addInfo(){ system("cls"); printf("\t Add student information\n"); printf("Please enter student number\n"); isIdSame(count); printf("Please enter your name\n"); scanf("%s",&student[count].name); printf("Please enter your language score\n"); scanf("%d",&student[count]._chinese); printf("Please enter your math score\n"); scanf("%d",&student[count]._math); printf("Please enter your English score\n"); scanf("%d",&student[count]._english); student[count].total=student[count]._chinese+student[count]._english+student[count]._math; printf("%s Information entered successfully\n\n",student[count].name); int choose; printf("1 Continue 2 to return to the main interface\n"); count++; scanf("%d",&choose); if(choose==1){ addInfo(); } system("cls"); } // Find display results void search(){ system("cls"); int id; printf("Please enter the student number you want to find\n"); scanf("%d",&id); int target = findIndex(student,id); //The objectives are shown in the table below int flag=1;//Is there a student number to query //for loop comparison if(target != -1) { printf("\n\t Query results\n\n"); showInfo(student[target]); con(); } else{ // Output query results printf("\n No one was found\n"); con(); } } // to update void updata(){ system("cls"); int id; printf("Please enter the student number you want to modify\n"); scanf("%d",&id); int target = findIndex(student,id); if(target<0){ printf("No one was found"); con(); }else{ int flag=1; do{ int choose=0; printf("Please enter the option to be modified\t(1.Student number\t2.full name\t3.language\t4.mathematics\t5.English):\n"); scanf("%d",&choose); switch (choose) { case 1: printf("Please enter student number\n"); // scanf("%d",&student[target].id); isIdSame(target); break; case 2: printf("Please enter your name\n"); scanf("%s",&student[target].name); break; case 3: printf("Please enter your language score\n"); scanf("%d",&student[target]._chinese); break; case 4: printf("Please enter your math score\n"); scanf("%d",&student[target]._math); break; case 5: printf("Please enter your English score\n"); scanf("%d",&student[target]._english); break; } student[target].total=student[target]._chinese+student[target]._english+student[target]._math; printf("%s The information of was modified successfully\n",student[target].name); printf("\n Press 1 to continue and 2 to exit the modification\n"); int choose2; scanf("%d",&choose2); if(choose2==1){ flag=1; }else{ flag=0; } }while(flag); } } //delete void del(){ system("cls"); int id; int target;//Subscript of target element printf("\n Please enter the student number of the student you want to delete\n"); scanf("%d",&id); target=findIndex(student, id); if(target<0){ printf("\n No one was found\n"); con(); } else{ for(int i=target;i<count;i++){ student[i]=student[i+1]; //The last element after the delete operation overwrites the previous element } printf("Delete succeeded\n"); con(); count--; } } //Insert student information void inserInfo(){ system("cls"); int site; //position printf("Please enter the location where you want to insert student information(Start from 0):\n"); scanf("%d",&site); //If the insertion position is greater than the total number, it is inserted in the last bit of the array if ( site > count){ inserCurrentInfo(count); printf("%s The information of was inserted successfully\n", student[count].name); }else{ //Not the last bit, move all of the array back one bit from the current position for (int i = count; i >= site; i--){ student[i + 1] = student[i]; } //Add student in current location inserCurrentInfo(site); printf("%s Student information inserted successfully\n", student[site].name); con(); } } //Insert at current position void inserCurrentInfo(int site){ printf("Please enter student number\n"); isIdSame(site); printf("Please enter your name\n"); scanf("%s", student[site].name); printf("Please enter your language score\n"); scanf("%d", &student[site]._chinese); printf("Please enter your math score\n"); scanf("%d", &student[site]._math); printf("Please enter your English score\n"); scanf("%d", &student[site]._english); student[site].total= student[site]._chinese+student[site]._english+student[site]._math; count++; con(); } // Judge whether the student number is repeated, return 1, otherwise return 0 int find1(struct Student student[],int id) { int temp = 0; for(int i=0;i<count;i++) { if(student[i].id==id) { temp=1; break; } } return temp; } //Verify whether the added student number is duplicate void isIdSame(int x){ int inputId; scanf("%d",&inputId); do{ if(find1(student,inputId)){ printf("Duplicate student number, please re-enter\n"); scanf("%d",&inputId); } else { student[x].id=inputId; break; } }while(1); } // Return subscript according to student number int findIndex (struct Student student[],int id){ int temp; for(int i=0;i<count;i++){ if(student[i].id==id){ temp=i; break; } else { temp = -1; } } return temp; } //Sort by total score void sortTotal(){ //Bubble sorting struct Student temp;// Temporary container for element to element exchange for (int i = 0; i < count - 1; i++){ for (int j = 0; j < count - 1 - i; j++){ if (student[j].total<student[j+1].total){ temp = student[j + 1]; student[j + 1] = student[j]; student[j]= temp; } } } printf("Sort complete"); showAllInfo(); } //press any key to continue void con(){ printf("\n press any key to continue\n"); getch(); }; //Show the total number of students void showCount(){ system("cls"); printf("\n\t The total number of students is:%d individual\n",count); con(); } //Initialization data void initData(){ FILE * fp = NULL; fp = fopen("stu.txt", "r"); if (!fp){ printf("File open failed\n"); exit(0);// Exit program } while (1){ //Read data and assign to array fscanf(fp, "%d%s%d%d%d%d", &student[count].id, student[count].name, &student[count]._chinese, &student[count]._math, &student[count]._english, &student[count].total); if (feof(fp)){ //Loop out at end of file break; } count++; } } //Write data to file void writeData(){ FILE * fp = NULL; fp = fopen("stu.txt", "w"); for (int i = 0; i < count; i++){ fprintf(fp, "%d\t%s\t%d\t%d\t%d\t%d\n", student[i].id, student[i].name, student[i]._chinese, student[i]._math, student[i]._english,student[i].total); } printf("Data saved successfully\n"); } // Show all information void showAllInfo(){ system("cls");//Clear screen for(int i=0;i<count;i++){ showInfo(student[i]); } con(); } // Show student information void showInfo(struct Student stu){//Elements passed into the array printf("Student number:%d\t full name:%s\t language:%d\t mathematics:%d\t English:%d\t Total score:%d",stu.id,stu.name,stu._chinese,stu._math,stu._english,stu.total); printf("\n-----------------Split line-----------------------\n"); }
main interface