在学习了单链表的增删查改后,可以写一个简单的学生信息管理程序,但与真正的管理系统相比,它只是一个雏形,还差了很多内容,它还没有和文件联系在一起,因此对于里面输入的数据无法保存。
下来直接看代码,详细注释都在代码中。
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031) //避免scanf返回值被忽略的警告
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char XH[20]; //学号
char name[40]; //姓名
int sumscore; //总分
struct Student* next;
};
void getInput(struct Student* student); //输入学生信息
void printStudent(struct Student* student); //打印学生信息
void addStudent(struct Student** contacts); //增
void changeStudent(struct Student* contacts); //改
void delStudent(struct Student** contacts); //删
struct Student* findStudent(struct Student* contacts); //查
void displayContacts(struct Student* contacts); //全部列出
void releaseContacts(struct Student** contacts); //销毁链表
void getInput(struct Student* student) { //输入学生信息
printf("请输入姓名,学号,总成绩(用空格隔开):");//将学生信息放在一行,节省空间,较为美观
scanf("%s %s %d", &student->name,&student->XH,&student->sumscore);
}
void addStudent(struct Student** contacts) { //增
struct Student* student;
struct Student* temp;
student = (struct Student*)malloc(sizeof(struct Student));
if (student == NULL) {
printf("malloc fair!");
exit(1);
}
getInput(student);
if (*contacts != NULL) {
temp = *contacts;
*contacts = student;
student->next = temp;
}
else {
*contacts = student;
student->next = NULL;
}
}
void printStudent(struct Student* student) { //打印学生信息
printf("学生姓名:%s 学号:%s 总成绩:%d", student->name,student->XH,student->sumscore);
printf("\n");
}
struct Student* findStudent(struct Student* contacts) {
struct Student* current;
char input[20] = { 0 };
printf("请输入学生姓名:");
scanf("%s", &input);
current = contacts;
while (current != NULL && strcmp(current->name, input)) {
current = current->next;
}
return current;
}
void changeStudent(struct Student* contacts) { //改
struct Student* student;
student = findStudent(contacts);
if (student == NULL) {
printf("该学生不存在!\n");
}
else {
printf("请输入新的学生信息:");
scanf("%d", &student->sumscore);
}
}
void delStudent(struct Student** contacts) { //删
struct Student* student;
struct Student* current;
struct Student* previous;
//找到待删除的结点指针
student = findStudent(*contacts);
if (student == NULL) {
printf("找不到该学生!\n");
}
else {
current = *contacts;
previous = NULL;
//current定位到待删除的结点
while (current != NULL && current != student) {
previous = current;
current = current->next;
}if (current == NULL)
{
exit(-1);
}else if (previous == NULL) {
//待删除的结点是第一个结点
*contacts = current->next;
}else {
//待删除的结点不是第一个结点
previous->next = current->next;
}
free(student);
}
}
void displayContacts(struct Student* contacts) { //列出全部学生的信息
struct Student* current;
current = contacts;
while (current != NULL) {
printStudent(current);
current = current->next;
}
}
void releaseContacts(struct Student** contacts) { //退出登录
struct Student* temp;
while (*contacts != NULL) {
temp = *contacts;
*contacts =(*contacts)->next;
free(temp);
}
}
int main(void) {
long password;
printf(" 请输入登录密码:");
scanf("%ld", &password);
if (password != 521412) { //这个密码可以自己设定,此处示例
printf(" 密码错误!\n");//如果密码错误,则就此退出
}
else {
printf(" 登录成功!\n");
system("pause"); //暂停操作,此时按任意键可以继续
system("cls"); //清屏操作,之前的内容不显示,system()函数存在于stblib.h这个头文件里
int code;
struct Student* contacts = NULL;
struct Student* student;
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");//菜单界面
printf(" * 欢迎使用学生信息管理程序 *\n");
printf(" * 1.插入学生信息 *\n");
printf(" * 2.查找学生信息 *\n");
printf(" * 3.更改学生信息 *\n");
printf(" * 4.删除学生信息 *\n");
printf(" * 5.显示当前信息系统 *\n");
printf(" * 6.退出当前信息系统 *\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
while (1) {
printf("请输入指令代码:");
scanf("%d", &code);
switch (code) { //通过循环来根据指令执行相应的操作
case 1:
addStudent(&contacts);
break;
case 2:
student = findStudent(contacts);
if (student == NULL) {
printf("该学生不存在!\n");
}
else {
printStudent(student);
break;
}
case 3:
changeStudent(contacts);
break;
case 4:
delStudent(&contacts);
break;
case 5:
displayContacts(contacts);
break;
case 6:
goto END;
}
}
END:
releaseContacts(&contacts);
}
return 0;
}
该代码包含了密码登录这个环节,运行结果如下:
1.密码不正确
2.密码正确
按完任意键后清屏继续
这是我的输入示例
Good luck!