C语言学生管理系统(练手)
#include <stdio.h>
#include <stdlib.h>
#include "studentclass.h"
int main(){
Student_menu();
int number_index = 0;
while (1){
printf("\n==========================\n");
printf("Please enter the number:");
if (scanf("%d",&number_index) == 1){
printf("==========================\n");
switch(number_index){
case 1:
Insert_Student();
break;
case 2:
printf("\n==========================\n");
printf("please enter student id:");
scanf("%d",&number_index);
printf("\n==========================\n");
Student_Head = Delete_Student_id(number_index);
Show_Error_Success(Student_Head);
Student_Length();
break;
case 3:
printf("\n==========================\n");
printf("please enter student id:");
scanf("%d",&number_index);
printf("\n==========================\n");
Student_Head = Student_SearchId(number_index);
if (Student_Head == NULL){
printf("\n==========================\n");
printf("= cannot not find student! \n");
printf("==========================\n");
}else{
printf("\n ==========================\n ");
printf("= Student_no :%ld \n ",Student_Head->sno);
printf("= Student_age :%d \n ",Student_Head->sage);
printf("= Student_name :%s \n ",Student_Head->sname);
printf("= Student_class :%s \n ",Student_Head->sclass);
printf("==========================\n");
}
break;
case 4:
printf("\n==========================\n");
printf("please enter student id:");
scanf("%d",&number_index);
printf("\n==========================\n");
Student_Head = Update_Student_id(number_index);
Show_Error_Success(Student_Head);
break;
case 5:
Student_All();
break;
case 6:
Student_menu();
break;
case 0:
exit(0);
break;
}
}else{
printf("System Error!");
exit(0);
}
}
return 0;
}
#ifndef STUDENTCLASS_INCLUDED
#define STUDENTCLASS_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 20
struct student{
long int sno;
int sage;
char sname[MAX_LENGTH];
char sclass[MAX_LENGTH];
struct student * next;
};
typedef struct student Student;
Student * Student_Head ;
void Student_menu();
Student getStudentInfo();
void Insert_Student();
void * Delete_Student_all();
Student * Delete_Student_id(long int sno);
Student * Update_Student_id(long int sno);
void Student_All();
Student * Student_SearchId(long int sno);
int getStudentLength();
void Student_Length();
void Show_Error_Success(void * str);
#endif // STUDENTCLASS_INCLUDED
#include "studentclass.h"
void Student_menu(){
printf("==========================\n");
printf("|| Student Mangerment || \n");
printf("|| v 1.0 || \n");
printf("==========================\n");
printf("= 1.注册 = \n");
printf("= 2.删除 = \n");
printf("= 3.查找 = \n");
printf("= 4.修改 = \n");
printf("= 5.显示 = \n");
printf("= 6.菜单 = \n");
printf("= 7.清空 = \n");
printf("= 0.退出 = \n");
printf("==========================\n");
}
Student getStudentInfo(){
Student stu ;
setbuf(stdin,NULL);
printf("\n==========================\n");
printf("please enter student no:");
scanf("%ld",&stu.sno);
printf("please enter student age:");
scanf("%d",&stu.sage);
getchar();
printf("please enter student name:");
gets(stu.sname);
printf("please enter student class:");
gets(stu.sclass);
printf("==========================\n");
setbuf(stdin,NULL);
return stu;
}
void Insert_Student(){
Student stu = getStudentInfo();
if (Student_Head == NULL){
Student * temp = (Student *)malloc(sizeof(Student));
if (temp == NULL){
printf("application error!\n");
return ;
}
temp->sno = stu.sno;
temp->sage = stu.sage;
strcpy(temp->sname,stu.sname);
strcpy(temp->sclass,stu.sclass);
temp->next = NULL;
Student_Head = temp;
}
else{
Student * temp = NULL;
for (temp = Student_Head;temp->next != NULL;temp = temp->next);
Student * current = (Student *)malloc(sizeof(Student));
if (current == NULL){
printf("application error!\n");
return ;
}
current->sno = stu.sno;
current->sage = stu.sage;
strcpy(current->sname,stu.sname);
strcpy(current->sclass,stu.sclass);
current->next = NULL;
temp->next = current;
}
}
void Student_All(){
Student * temp = Student_Head;
for (;temp != NULL;temp = temp->next){
printf("\n ==========================\n ");
printf("= Student_no :%ld \n ",temp->sno);
printf("= Student_age :%d \n ",temp->sage);
printf("= Student_name :%s \n ",temp->sname);
printf("= Student_class :%s \n ",temp->sclass);
printf("==========================\n");
}
}
Student * Student_SearchId(long int sno){
Student * temp = Student_Head;
for (;temp != NULL;temp = temp->next){
if (temp->sno == sno){
return temp;
}
}
return NULL;
}
void * Delete_Student_all(){
Student * temp = Student_Head;
Student * current = NULL;
while (temp->next != NULL){
current = temp->next;
temp->next = current->next;
free(current);
}
free(Student_Head);
Student_Head = NULL;
return NULL;
}
Student * Delete_Student_id(long int sno){
Student * temp = Student_Head;
Student * current = NULL;
int Flag = 0;
for (;temp != NULL;temp = temp->next){
if (temp->sno == sno){
Flag = 1;
break;
}
current = temp;
}
if (!Flag){
printf("connot not find student id\n");
return NULL;
}
if (temp == Student_Head){
Student_Head = temp->next;
free(temp);
}else{
current->next = temp->next;
free(temp);
}
return Student_Head;
}
Student * Update_Student_id(long int sno){
Student * temp = Student_Head;
int Flag = 0;
for (;temp != NULL;temp = temp->next){
if (temp->sno == sno){
Flag = 1;
break;
}
}
if (!Flag){
printf("connot not find !\n");
return NULL;
}
Student stu = getStudentInfo();
temp->sno = stu.sno;
temp->sage = stu.sage;
strcpy(temp->sname,stu.sname);
strcpy(temp->sclass,stu.sclass);
return Student_Head;
}
int getStudentLength(){
Student * temp = Student_Head;
int index = 0;
for (;temp != NULL;temp = temp->next){
index++;
}
return index;
}
void Show_Error_Success(void * str){
if (str == NULL){
printf("\n==========================\n");
printf("= error opertion! \n");
printf("==========================\n");
}else{
printf("\n==========================\n");
printf("= successful opertion! \n");
printf("==========================\n");
}
}
void Student_Length(){
printf("\n==========================\n");
printf("= Student_Length -> [%d] \n",getStudentLength());
printf("==========================\n");
}