C语言实现学生管理系统的源码分享
#pragma once
//前面没有引用是应为这里都引用了,所以引用一次头文件就欧克了
#include
#include
#include
#include
#include
#include "enter.h"
//定义一个学生类型的结构体
struct student {
char name[20]; //姓名
char gender[3];//性别
char number[12]; //学号
char tel[12];//电话号码
short age;//年龄
//需要可以在添加
};
//结点
struct Node {
struct student data; //数据域
struct Node* next; //指针域
};
//创建链表
struct Node* createrList(void) {
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
if (headNode) {
//初始化
headNode->next = NULL;
}
return headNode;
}
//创建结点
struct Node* createNode(struct student data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode) {
//把数据存进去
newNode->data = data;
newNode->next = NULL;
}
return newNode;
}
//插入结点 参数:插入哪个链表 插入结点的数据是多少
void insertNodeByHead(struct Node* headNode, struct student data) {
//创建结点
struct Node* newNode = createNode(data);//赋值
//使插入的结点接在 headNode后面
newNode->next = headNode->next;
headNode->next = newNode;
}
//打印链表 也就是遍历
void printfNode(struct Node* headNode) {
struct Node* pMove = headNode->next;
printf(" 学号 姓名 性别 年龄 电话
");
while (pMove != NULL) {
printf("
%06s %s %s %hd %s", pMove->data.number, pMove->data.name, pMove->data.gender, pMove->data.age, pMove->data.tel);
pMove = pMove->next;
}
printf("
");
}
//指定位置删除
void deteleNodeAppoinNumber(struct Node* headNode, char number[12]) {
struct Node* posNode = headNode->next;
struct Node* posFrontNode = headNode;
if (posNode == NULL) {
printf(" 表中没有参数
");
}
else {
while (strcmp(posNode->data.number,number)!=0) {//如果不是的话
posFrontNode = posNode;
posNode = posNode->next;
if (posNode == NULL) {//找到最后一个也没有找到
printf(" 表中没有该学号的学生.
");
return;
}
}
//找到了
posFrontNode->next = posNode->next;
free(posNode);
printf(" Done it!
");
}
}
//更新已录入内容
void upDataNode(struct Node* headNode, char number[12]) {
struct Node* posNode = headNode->next;
struct Node* posFrontNode = headNode;
struct student info;
char chosen;
if (posNode == NULL) {
printf(" 无法修改,该表里面没有内容
");
}
else {
while (strcmp(posNode->data.number,number)!=0) {
posFrontNode = posNode;
posNode = posNode->next;
if (posNode == NULL) {
printf(" 该表中没有此学号的学生,无法修改
");
return;
}
}
while (true) {
printf(" 请输入要修改什么选项:"
"
(1.学号 2.姓名 3.性别 4.年龄 5.电话)"
"
(如果不需要了选择'Q')
-->:");
chosen = enter();
fflush(stdin);
switch (chosen) {
case '1':
printf(" 请输入需要更改的学生信息:
");
printf(" 请输入学号:");
scanf_s("%s", info.number,(int)sizeof(info.number));
strcpy_s(posNode->data.number, sizeof(posNode->data.number),info.number);
printf(" Done!
");
break;
case '2':
printf(" 请输入需要更改的学生信息:
");
printf(" 请输入姓名:");
scanf_s("%s", info.name, (int)sizeof(info.name));
fflush(stdin);
strcpy_s(posNode->data.name, sizeof(posNode->data.name), info.name);
printf(" Done!
");
break;
case '3':
printf(" 请输入%s的性别:", posNode->data.name);
scanf_s("%s", info.gender, (int)sizeof(info.gender));
fflush(stdin);
strcpy_s(posNode->data.gender, sizeof(posNode->data.gender), info.gender);
printf(" Done!
");
break;
case '4':
printf(" 请输入%s的年龄:", posNode->data.name);
scanf_s("%hd", &info.age);
posNode->data.age = info.age;
printf(" Done!
");
break;
case '5':
printf(" 请输入%s的电话:", posNode->data.name);
scanf_s("%s", info.tel, (int)sizeof(info.tel));
fflush(stdin);
strcpy_s(posNode->data.tel, sizeof(posNode->data.tel), info.tel);
printf(" Done!
");
break;
case'Q':
printf(" 退出此选项");
return;
default:
printf("
请重新输入(1~5 and (q quit ))
");
break;
}
}
}
}
//指定位置查看
void printfToInput(struct Node* headNode, char number[12]) {
struct Node* posNode = headNode->next;
struct Node* posFrontNode = headNode;
if (posNode == NULL) {
printf(" 该表为空
");
}else {
while (strcmp(posNode->data.number,number)!=0) {
posFrontNode = posNode;
posNode = posNode->next;
if (posNode == NULL) {
printf(" 没有找到该学生
");
return;
}
}
printf(" 学号 姓名 性别 年龄 电话
");
printf("
%06s %s %s %hd %s", posNode->data.number, posNode->data.name, posNode->data.gender, posNode->data.age, posNode->data.tel);
}
printf("
");
}
//读文件
bool readInfoFromFile(struct Node* headNode, char* fileName) {
struct student data;
//打开文件
FILE* fp;
fopen_s(&fp, fileName, "r");
if (fp == NULL) {
fopen_s(&fp, fileName, "w+");
}
//2操作
if (fp == NULL) { return EOF; }
while (fscanf_s(fp, "%s %s %s %hd %s", data.number,(int)sizeof(data.number), data.name, (int)sizeof(data.name), data.gender, (int)sizeof(data.gender), &data.age, data.tel, (int)sizeof(data.tel)) != EOF) {
insertNodeByHead(headNode, data);
}
//关闭文件
if (fp == NULL) { return EOF; }
fclose(fp);
return 0;
}
//写文件
bool weiteInfoToFile(struct Node* headNode, char* fileName) {
FILE* fp;
fopen_s(&fp, fileName, "w");
struct Node* pMove = headNode->next;
while (pMove) {
if (fp == NULL) { return EOF; }
fprintf_s(fp, "
%s %s %s %hd %s", pMove->data.number, pMove->data.name, pMove->data.gender, pMove->data.age, pMove->data.tel);
pMove = pMove->next;
}
if (fp == NULL) { return EOF; }
fclose(fp);
return 0;
}
//求链表长度
int LengthNode(struct Node* headNode) {
int lenth = 0;
struct Node* pMove = headNode->next;
while (pMove) {
lenth++;
pMove = pMove->next;
}
return lenth;
}
//格式化模式!
void formattedLinkedList(struct Node* headNode) {
struct Node* posNode ;
if (headNode == NULL) {
printf(" 该表为空
");
}
else {
while (headNode != NULL) {
posNode = headNode->next;
free(headNode);
headNode = posNode;
}
}
}