C language student management system source sharing
- 2020-05-17 06:00:05
- OfStack
This article example for you to share the C language student management system source code, for your reference, the specific content is as follows
#include<stdio.h>
#include<stdlib.h>
// The structure can store the maximum number of students information, immutable variables
int const MAX_LENGTH=100;
// An array of student information structures that can be stored at most 100 Individual student information
struct student{
int id; // Student id
char *name; // The name
int age; // age
float c_score; //C Language performance
float english_score; // English
float database_score; // Database score
float total_score; // Total score
}student_array[MAX_LENGTH];
// Amount of student information
int student_count=0;
// Function declaration
void print_all_students();
void input_info();
void query_info();
void modify_info();
void delete_info();
void compute_total_score();
void sort_info();
int search_one_student();
void print_one_student(struct student one);
void delete_one_student(int student_index);
char * get_item_name(int item_index);
void modify_one_student(int student_index);
void sort_by_id();
void sort_by_c_score();
void sort_by_english_score();
void sort_by_database_score();
// The main function
int main()
{
while(1)
{
printf(" Please select the feature to use: \n");
printf(" Input information, please enter 1 And hit enter! \n");
printf(" For information, please enter 2 And hit enter! \n");
printf(" To modify the information, please enter 3 And hit enter! \n");
printf(" To delete information, please enter 4 And hit enter! \n");
printf(" Calculate the total score, please enter 5 And hit enter! \n");
printf(" Sort information, please enter 6 And hit enter! \n");
printf(" Output all, please input 0 And hit enter! \n");
printf(" To exit the program, please enter -1 And hit enter! \n");
// Function selection variable
int function=0;
// Enter the selected function number value
scanf("%d",&function);
// According to the input function number, perform the corresponding function
switch(function){
case -1:
exit(1);
case 0:
print_all_students();
break;
case 1:
input_info();
break;
case 2:
query_info();
break;
case 3:
modify_info();
break;
case 4:
delete_info();
break;
case 5:
compute_total_score();
break;
case 6:
sort_info();
break;
default:
printf(" Please enter the correct function number!! \n\n");
break;
}
}
return 0;
}
// Input information function
void input_info()
{
printf(" Current function -- input information! \n");
// To see if there's room
if(student_count<MAX_LENGTH)
{
// The statement 1 Some temporary variables
int id=0; char *name=(char *)malloc(100);
int age=0; float c_score=0;
float english_score=0;
float database_score=0;
printf(" Please enter student information in the form of student number , The name , age ,C Language performance , English , Database score \n");
scanf("%d %s %d %f %f %f",&id,name,&age,&c_score,&english_score,&database_score);
printf(" Student information proofreading: student no. : %d, Name: %s, Age: %d,C Language performance :%f, English score: %f, Database achievements: %f\n",id,name,age,c_score,english_score,database_score);
// Student information is added to the array of structs
student_array[student_count].id=id;
student_array[student_count].name=name;
student_array[student_count].age=age;
student_array[student_count].c_score=c_score;
student_array[student_count].english_score=english_score;
student_array[student_count].database_score=database_score;
student_count++;
// Whether to continue to input information
printf(" Do you want to continue to input information? To continue, please enter y, Return to main menu input n\n");
char go_on;
scanf("%s",&go_on);
if(go_on=='y')
{
input_info();
}
}
else
{
printf(" The student struct data is full and can be deleted 1 Part of the student information! \n");
}
}
// Query information function
void query_info()
{
printf(" Current function -- query information! \n");
printf(" Please enter the student's student number \n");
int id=0;
scanf("%d",&id);
// Find the input id Student number
int student_index=search_one_student(id);
if(student_index!=-1)
{
print_one_student(student_array[student_index]);
}
else
{
printf(" No student with that number! \n");
}
// Whether to continue to query the information
printf(" Do you want to continue searching for information? To continue, please enter y, Return to main menu input n\n");
char go_on;
scanf("%s",&go_on);
if(go_on=='y')
query_info();
}
// Modify information function
void modify_info()
{
printf(" Current function - - modify information! \n");
printf(" Please enter the student's student number \n");
int id=0;
scanf("%d",&id);
// Find the input id Student number
int student_index=search_one_student(id);
if(student_index!=-1)
{
modify_one_student(student_index);
}
else
{
printf(" No student with that number! \n");
}
}
// Delete information function
void delete_info()
{
printf(" Current function -- -- delete information! \n");
printf(" Please enter the student's student number \n");
int id=0;
scanf("%d",&id);
// Find the input id Student number
int student_index=search_one_student(id);
if(student_index!=-1)
{
// To prevent student_index It's changed, it's passed in temp_index To calculate
int temp_index=student_index;
print_one_student(student_array[temp_index]);
// Confirm before deleting
printf(" Be sure to delete the student number %d Student information? To continue, please enter y\n",id);
char be_true;
scanf("%s",&be_true);
if(be_true=='y')
{
printf("%d\n", student_index);
// Perform the delete action
delete_one_student(student_index);
}
}
else
{
printf(" No student with that number! \n");
}
// Whether to continue to delete information
printf(" Do you want to continue deleting the information? To continue, please enter y, Return to main menu input n\n");
char go_on;
scanf("%s",&go_on);
if(go_on=='y')
delete_info();
}
// Calculate the total score function
void compute_total_score()
{
printf(" Current function -- calculate total score! \n");
for (int i = 0; i < student_count; ++i)
{
student_array[i].total_score=student_array[i].c_score+student_array[i].english_score+student_array[i].database_score;
print_one_student(student_array[i]);
printf(" Overall: %f\n", student_array[i].total_score);
}
printf(" Total points calculated complete!! \n");
}
// Ranking function
void sort_info()
{
printf(" Current function - - grades sorting! \n");
printf(" The information of all students before ranking is as follows: \n");
print_all_students();
int sort_type;
while(1)
{
printf(" Please enter the sorting field, student number: 1,C Language performance :2, English score: 3, Database achievements: 4\n");
scanf("%d",&sort_type);
if(sort_type>=1&&sort_type<=4)
break;
}
switch(sort_type)
{
case 1:
sort_by_id();
break;
case 2:
sort_by_c_score();
break;
case 3:
sort_by_english_score();
break;
case 4:
sort_by_database_score();
break;
}
printf(" The information of all students is as follows: \n");
print_all_students();
// Whether to continue to delete information
printf(" Do you want to continue sorting the information? To continue, please enter y, Return to main menu input n\n");
char go_on;
scanf("%s",&go_on);
if(go_on=='y')
sort_info();
}
// According to the student number entered, traverse the array of structs. If the student exists, return the subscript of the array -1
int search_one_student(int id)
{
for (int i = 0; i < student_count; ++i)
{
if(student_array[i].id==id)
{
return i;
}
}
return -1;
}
// Output information about a student
void print_one_student(struct student one)
{
printf(" Student information: student no. : %d, Name: %s, Age: %d,C Language performance :%f, English score: %f, Database achievements: %f\n",one.id,one.name,one.age,one.c_score,one.english_score,one.database_score);
}
// Output information for all students
void print_all_students()
{
if(student_count==0)
{
printf(" No student information available \n\n\n");
}
for (int i = 0; i < student_count; ++i)
{
print_one_student(student_array[i]);
}
}
void modify_one_student(int student_index)
{
// Output student information before modification
print_one_student(student_array[student_index]);
// The initial value of the field number
int item_index=0;
// No modification of student number field is allowed
while(1)
{
printf(" Please enter the number and name of the field to be modified: 1, Age: 2,C Language performance :3, English score: 4, Database achievements: 5\n");
scanf("%d",&item_index);
if(item_index>=1&&item_index<=5)
break;
}
switch(item_index)
{
case 1:
printf(" Please enter the %s The new value of the field \n", get_item_name(item_index));
char* item_value_1=(char *)malloc(100);
;
scanf("%s",item_value_1);
student_array[student_index].name=item_value_1;
break;
case 2:
printf(" Please enter the %s The new value of the field \n", get_item_name(item_index));
int item_value_2;
scanf("%d",&item_value_2);
student_array[student_index].age=item_value_2;
break;
case 3:
printf(" Please enter the %s The new value of the field \n", get_item_name(item_index));
float item_value_3;
scanf("%f",&item_value_3);
student_array[student_index].c_score=item_value_3;
break;
case 4:
printf(" Please enter the %s The new value of the field \n", get_item_name(item_index));
float item_value_4;
scanf("%f",&item_value_4);
student_array[student_index].english_score=item_value_4;
break;
case 5:
printf(" Please enter the %s The new value of the field \n", get_item_name(item_index));
float item_value_5;
scanf("%f",&item_value_5);
student_array[student_index].database_score=item_value_5;
break;
}
printf(" Modified successfully! The new student information is as follows: \n");
// Output student information after modification
print_one_student(student_array[student_index]);
// Whether to continue to delete information
printf(" Will you continue to modify the student's information? To continue, please enter y, Return to main menu input n\n");
char go_on;
scanf("%s",&go_on);
if(go_on=='y')
modify_one_student(student_index);
}
// Delete the array corresponding to the sequence number of the student information, put i The data group element behind the position moves forward to overlay i . student_count counter 1
void delete_one_student(int student_index)
{
for (int i = student_index; i < student_count-1; ++i)
{
student_array[i]=student_array[i+1];
}
student_count--;
printf(" Deleting completed \n\n\n");
}
// Returns the field name based on the field number entered
char * get_item_name(int item_index)
{
switch(item_index)
{
case 0:
return " Student id ";
case 1:
return " The name ";
case 2:
return " age ";
case 3:
return "C Language performance ";
case 4:
return " English ";
case 5:
return " Database score ";
default:
return "";
}
}
// In accordance with the id Sort, using the simplest bubble sort
void sort_by_id()
{
for (int i = 0; i < student_count; ++i)
{
for (int j = i; j < student_count; ++j)
{
if(student_array[i].id>student_array[j].id)
{
struct student temp=student_array[i];
student_array[i]=student_array[j];
student_array[j]=temp;
}
}
}
printf(" In accordance with the Student id Order to complete \n");
}
// In accordance with the C Language grade sorting, using the simplest bubble sorting method
void sort_by_c_score()
{
for (int i = 0; i < student_count; ++i)
{
for (int j = i; j < student_count; ++j)
{
if(student_array[i].c_score>student_array[j].c_score)
{
struct student temp=student_array[i];
student_array[i]=student_array[j];
student_array[j]=temp;
}
}
}
printf(" In accordance with the C Language performance Order to complete \n");
}
// Sort by English score, using the simplest bubble sort
void sort_by_english_score()
{
for (int i = 0; i < student_count; ++i)
{
for (int j = i; j < student_count; ++j)
{
if(student_array[i].english_score>student_array[j].english_score)
{
struct student temp=student_array[i];
student_array[i]=student_array[j];
student_array[j]=temp;
}
}
}
printf(" In accordance with the English Order to complete \n");
}
// Sort by database score, using the simplest bubble sort
void sort_by_database_score()
{
for (int i = 0; i < student_count; ++i)
{
for (int j = i; j < student_count; ++j)
{
if(student_array[i].database_score>student_array[j].database_score)
{
struct student temp=student_array[i];
student_array[i]=student_array[j];
student_array[j]=temp;
}
}
}
printf(" In accordance with the Database score Order to complete \n");
}
For more information about management systems, please click "management system topics" to learn