C语言学生管理系统:
此程序综合了链表,排序等知识,可以说是一个综合练习。
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
typedef struct Student
{
char name[20];
char sex;
int age;
int score;
char sid[20];
struct Student * pNext;
}ST, *PST;
void print(void); //欢迎界面
void showMenu(void); //显示菜单
void help(void); //显示帮助
PST create_list(void); //创建链表
void traverse_list(const PST); //遍历输出链表内容,用const常量修饰指针变量防止变量被修改
bool is_empty(PST); //判断链表是否为空
int length_list(PST); //计算链表长度
bool append_list(PST); //追加数据
bool insert_list(PST); //插入数据
bool delete_list(PST); //删除数据
ST get_list(PST); //获取数据
void sort_list(PST); //对链表数据按成绩降序排序
int main(void)
{
int val;
PST pHead = (PST)malloc(sizeof(ST));
print();
printf("\n\n\n");
pHead = create_list();
traverse_list(pHead);
while (true)
{
showMenu();
printf("Please to choose you want to do:\n");
scanf("%d", &val);
switch (val)
{
case 1:
append_list(pHead);
break;
case 2:
insert_list(pHead);
break;
case 3:
delete_list(pHead);
break;
case 4:
traverse_list(pHead);
break;
case 5:
sort_list(pHead);
break;
case 6:
help();
break;
case 7:
printf("The program will exit soon!\n");
break;
default:
printf("Input error! Please input again!");
}
if (7 == val)
break;
}
return 0;
}
void print(void)
{
printf("\t*************************************************\n");
printf("\t*************************************************\n");
printf("\t****** ******\n");
printf("\t***** Welcome to use Student Manage System! *****\n");
printf("\t****** ******\n");
printf("\t*************************************************\n");
printf("\t******************************-By LiFeng*********\n");
return;
}
void showMenu(void)
{
printf("\t*************************************************\n");
printf("\t************** 1. add ************************\n");
printf("\t************** 2. insert ************************\n");
printf("\t************** 3. delete ************************\n");
printf("\t************** 4. show ************************\n");
printf("\t************** 5. sort ************************\n");
printf("\t************** 6. help ************************\n");
printf("\t************** 7. exit ************************\n");
printf("\t*************************************************\n");
return;
}
PST create_list(void)
{
int i;
int j = 0;
int len;
PST Student;
PST pHead = (PST)malloc(sizeof(ST));
if (NULL == pHead)
{
printf("The memory is error!\n");
exit(-1);
}
PST pTail = pHead;
pTail->pNext = NULL;
printf("Please input the number of students: ");
scanf("%d", &len);
printf("Please input the NO.%d Student information!");
for (i=0; i<len; ++i) //for循环创建链表
{
printf("Please input the NO.%d Student information!");
printf("Name: ");
scanf("%s", Student->name[20]);
printf("Sex: ");
scanf("%c", &Student->sex);
printf("Age: ");
scanf("%d", &Student->age);
printf("Score: ");
scanf("%d", &Student->score);
printf("Sid: ");
scanf("%s", Student->sid[20]);
PST pNew = (PST)malloc(sizeof(ST));
if (NULL == pNew)
{
printf("The memory is error!\n");
exit(-1);
}
pNew = Student;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
void treverse_list(const PST pHead)
{
int i;
PST p = pHead->pNext;
if (is_empty(pHead))
{
printf("The list is empty!\n");
exit(1);
}
while (NULL == p->pNext)
{
printf("The information of NO.%d are\n", i);
printf("the name is: %s ", p->name[20]);
printf("the sex is: %c ", p->sex);
printf("the age is: %d ", p->age);
printf("the score is: %d ", p->score);
printf("the sid is: %s ", p->sid);
printf("\n");
p = p->pNext;
++i;
}
return;
}
bool is_empty(PST pHead)
{
PST p = pHead->pNext;
if (NULL == p)
return true;
else
return false;
}
int length_list(PST pHead)
{
int len;
PST p = pHead->pNext;
while(NULL != p)
{
++len;
p = p->pNext;
}
return len;
}
bool append_list(PST pHead)
{
int i;
int len;
int pos;
PST pTail = (PST)malloc(sizeof(ST));
PST p = pHead;
printf("Please input the information you want to append:\n");
printf("Name: ");
scanf("%s", &pTail->name[20]);
printf("Sex: ");
scanf("%c", &pTail->sex);
printf("Age: ");
scanf("%d", &pTail->age);
printf("Score: ");
scanf("%d", &pTail->score);
printf("Sid: ");
scanf("%s", &pTail->sid[20]);
len = length_list(pHead);
for (i=0; i<len; ++i)
{
p=p->pNext;
}
p->pNext = pTail;
while (p!=NULL && i<pos)
{
p = p->pNext;
}
return true;
}
bool insert_list(PST pHead)
{
int i = 0;
int pos;
ST temp;
PST p = pHead;
printf("Please input the position of the list: pos = ");
scanf("%d", &pos);
printf("Please input the information of the student:\n");
printf("Name: ");
scanf("%s", &temp.name[20]);
printf("Sex: ");
scanf("%c", &temp.sex);
printf("Age: ");
scanf("%d", &temp.age);
printf("Score: ");
scanf("%d", &temp.score);
printf("Sid: ");
scanf("%s", &temp.sid[20]);
while (p!=NULL && i<pos)
{
p = p->pNext;
++i;
}
if (NULL==p || i>pos)
{
printf("Error!\n");
exit(-1);
}
PST pNew = (PST)malloc(sizeof(ST));
*pNew = temp;
pNew->pNext = p->pNext;
p->pNext = pNew;
return true;
}
bool delete_list(PST pHead)
{
int i = 0;
int pos;
PST p = pHead;
PST q;
PST pVal = (PST)malloc(sizeof(ST));
printf("Please input the position of you want to delete: pos = ");
scanf("%d", &pos);
while (p!=NULL && i<pos-1)
{
p = p->pNext;
++i;
}
if (NULL==p || i>pos-1)
{
printf("Error!\n");
exit(-1);
}
*pVal = *p;
q = p->pNext;
p->pNext = p->pNext->pNext;
free(q);
printf("Delete successfully! The information of you delete:\n");
printf("Name: %s", pVal->name[20]);
printf("Sex: %c", pVal->sex);
printf("Age: %d", pVal->age);
printf("Score: %d", pVal->score);
printf("Sid: %s\n", pVal->sid[20]);
return true;
}
ST get_list(PST pHead)
{
int i = 0;
int pos;
ST stu;
PST p = pHead->pNext;
printf("Please input the position of you need: pos = ");
scanf("%d", &pos);
if ( is_empty(pHead) )
{
printf("The list is empty!\n");
return stu;
}
while (NULL!=p && i<pos)
{
p = p->pNext;
++i;
}
if (NULL==p || i>pos)
{
printf("Error!\n");
exit(-1);
}
stu = *p;
return stu;
}
void sort_list(PST pHead) //冒泡排序法按成绩降序排序
{
int i, j;
int len;
ST temp;
PST p, q;
len = length_list(pHead);
for (i=0, p=pHead; i<len; ++i, p=p->pNext)
{
for (j=0, q=pHead->pNext; j<len-i-1; ++j, q=q->pNext)
{
if (p->score < q->score)
{
temp = *p;
*p = *q;
*q = temp;
}
}
}
return;
}
void help(void)
{
printf("It is so easy! Do you need help?\n\n");
}