#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#define SL 50
typedef enum SEX { //类型定义,用于方便输入同时区分男女
Man = 0,
Woman=1
}SEX;
struct STU { //定义学生结构体
char name[SL];
SEX sex;
char birth[SL];
char id[SL];
int phoneNum;
struct STU* next; //实现链表功能,指向下一个学生
};
/*
struct STU* findLast(struct STU* head) {
if (head == NULL) {
return NULL;
}
struct STU* current = head;
while (current->next != NULL) {
current = current->next;
}
return current;
}
*/
void menu(int n) { //菜单函数,用来实现主界面
system("cls"); //清空屏幕
printf("+------------学生成绩管理系统------------+\n");
printf("|%20s%20s|\n", "", "");
printf("|%25s%-15d|\n", "学生人数:", n);
printf("| 0:退出管理系统 |\n");
printf("| 1:添加学生信息 |\n");
printf("| 2:删除学生信息 |\n");
printf("| 3:修改学生信息 |\n");
printf("| 4:查看学生信息 |\n");
printf("| 5:插入学生信息 |\n");
printf("|%20s%20s|\n", "", "");
printf("+----------------------------------------+\n\n");
printf(" 请选择您要进行的操作(0到5):");
}
int getStuNum(struct STU*head) { //定义函数,用于获得当前学生数目
int n = 0;
struct STU* current = head;
if (current== NULL) { //遍历链表,获得学生数量
return 0;
}
while (current->next != NULL) {
n += 1;
current = current->next;
}
n += 1;
return n;
}
void checkStu(struct STU* head) { //定义函数用于查看当前存储的学生数据
struct STU* current;
current = head;
system("cls");
printf("+------------学生成绩管理系统------------+\n");
while (current->next != NULL) {
printf("|%20s%-20s|\n","姓名:", current->name);
if (current->sex == 0) {
printf("|%20s%-20s|\n", "性别:", "男");
}
else {
printf("|%20s%-20s|\n", "性别:", "女");
}
printf("|%20s%-20s|\n", "生日:", current->birth);
printf("|%20s%-20s|\n", "学生号:", current->id);
printf("|%20s%-20d|\n", "手机号:", current->phoneNum);
printf("+----------------------------------------+\n");
current = current->next;
}
printf("|%20s%-20s|\n", "姓名:", current->name);
if (current->sex == 0) {
printf("|%20s%-20s|\n", "性别:", "男");
}
else {
printf("|%20s%-20s|\n", "性别:", "女");
}
printf("|%20s%-20s|\n", "生日:", current->birth);
printf("|%20s%-20s|\n", "学生号:", current->id);
printf("|%20s%-20d|\n", "手机号:", current->phoneNum);
printf("+----------------------------------------+\n");
}
struct STU * addStu( ) {
struct STU* head, * current, * next; //三个结构体指针变量分别表示,首个结构体指针,当前结构体指针,下一个结构体指针
char name[SL];
SEX sex;
int sex1; //sex1用来存储用户输入的数据,在保存时将其转换为已经定义好的sex类型
char birth[SL];
char id[SL];
int phoneNum;
int result;
system("cls");
printf("请输入学生姓名\n");
scanf("%s", name);
printf("请输入学生性别(男:0,女:1)\n");
scanf("%d", &sex1);
printf("请输入学生号\n");
scanf("%s", &id);
printf("请输入学生生日\n");
scanf("%s", &birth);
printf("请输入学生手机号\n");
scanf("%d", &phoneNum);
head = (struct STU*)malloc(sizeof(struct STU)); //通过malloc动态生成内存空间,存放首个指针
strcpy(head->name, name); /*C语言只有在定义字符数组的时候才能用“=”来初始化变量
然而结构体内数据需要随情况变化,所以可以用strcpy()函数来赋值*/
strcpy(head->birth, birth);
strcpy(head->id, id);
head->phoneNum = phoneNum;
if (sex1 == 0) {
head->sex = Man;
}
else {
head->sex = Woman;
}
current = head; //将当前指针指向头部指针,因为现在正在对头部进行操作
printf("是否继续录入?(0:退出,1:继续)\n"); //判断是否进行下一次
scanf("%d", &result);
getchar();
while (result == 1) {
printf("请输入学生姓名\n");
scanf("%s", name);
printf("请输入学生性别(男:0,女:1)\n");
scanf("%d", &sex1);
printf("请输入学生号\n");
scanf("%s", &id);
printf("请输入学生生日\n");
scanf("%s", &birth);
printf("请输入学生手机号\n");
scanf("%d", &phoneNum);
next = (struct STU*)malloc(sizeof(struct STU)); //再次动态分配内存,为新产生的结构体做准备
strcpy(next->name, name);
strcpy(next->birth, birth);
strcpy(next->id, id);
next->phoneNum = phoneNum;
if (sex1 == 0) {
next->sex = Man;
}
else {
next->sex = Woman;
}
current->next = next; //将当前指针的next指向下一个指针
current = next; //更改当前指针到下一个指针
printf("是否继续录入?(0:返回,1:继续)\n"); //判断是否进行下一次
scanf("%d", &result);
//getchar();
}
current->next = NULL; //最后将末尾的指针next值设为NULL
return head; //返回首地址
}
struct STU* deleteStuUse(struct STU* head,char name[SL]) { //定义函数,以删除学生信息
//这个方法是为了方便后续调用,菜单中的删除方法在下方实现
struct STU* current, * before;
current = head;
before = head;
if (strcmp(head->name, name) == 0) { //判断链表首端是否就是需要删除的个体
current = head->next;
free(head); //释放链表内存
return current;
}
while (current->next != NULL) { //判断链表中部
if (strcmp(current->name, name) == 0) {
before->next = current->next; //重新连接链表,并返回链表首端
free(current);
return head;
}
before = current;
current = current->next;
}
if (strcmp(current->name, name) == 0) { //判断链表末尾
free(current);
before->next = NULL;
return head;
}
}
struct STU* deleteStu(struct STU* head) {//定义删除学生信息的函数
struct STU* current,*before;
char name[SL];
system("cls");
current = head;
before = head;
printf("请输入您要删除的同学的姓名\n");
scanf("%s", &name);
if (strcmp(head->name, name) == 0) {
current = head->next;
free(head);
printf("删除成功!\n");
return current;
}
while (current->next != NULL) {
if (strcmp(current->name, name) == 0) {
before->next = current->next;
free(current);
printf("删除成功!\n");
return head;
}
before = current;
current = current->next;
}
if (strcmp(current->name, name) == 0) {
free(current);
before->next = NULL;
printf("删除成功!\n");
return head;
}
printf("删除失败,查无此人!\n");
return head;
}
struct STU* modifyStu(struct STU* head) { //修改同学信息
/*该函数实现的逻辑为,先找到目标对象
创建新对象,并将链表重新连接,保证修改信息不影响学生所处的位置
删除旧对象,以实现修改同学信息的功能
*/
struct STU* current,*new;
int sex1,phoneNum;
char sex[SL], id[SL], birth[SL];
char name[SL];
system("cls");
printf("请输入你要修改同学的姓名\n");
scanf("%s", &name);
current = head;
while (current->next != NULL) {
if (strcmp(current->name, name) == 0) {
new=(struct STU*)malloc(sizeof(struct STU));
printf("请输入学生性别(男:0,女:1)\n");
scanf("%d", &sex1);
printf("请输入学生号\n");
scanf("%s", &id);
printf("请输入学生生日\n");
scanf("%s", &birth);
printf("请输入学生手机号\n");
scanf("%d", &phoneNum);
strcpy(new->name, name);
strcpy(new->birth, birth);
strcpy(new->id, id);
new->phoneNum = phoneNum;
if (sex1 == 0) {
new->sex = Man;
}
else {
new->sex = Woman;
}
new->next = current->next;
current->next = new;
head = deleteStuUse(head, name);
printf("修改成功!\n");
return head;
}
current = current->next;
}
if (strcmp(current->name, name) == 0) {
new = (struct STU*)malloc(sizeof(struct STU));
printf("请输入学生性别(男:0,女:1)\n");
scanf("%d", &sex1);
printf("请输入学生号\n");
scanf("%s", &id);
printf("请输入学生生日\n");
scanf("%s", &birth);
printf("请输入学生手机号\n");
scanf("%d", &phoneNum);
strcpy(new->name, name);
strcpy(new->birth, birth);
strcpy(new->id, id);
new->phoneNum = phoneNum;
if (sex1 == 0) {
new->sex = Man;
}
else {
new->sex = Woman;
}
new->next = NULL;
current->next = new;
head=deleteStuUse(head, name);
printf("修改成功!\n");
return head;
}
printf("修改失败!查无此人。\n");
return head;
}
struct STU* insertStu(struct STU*head) { //插入学生信息
int n; //当前学生个数
int num; //插入的学生所处位数
struct STU* current=head,*new;
int sex1, phoneNum;
char sex[SL], id[SL], birth[SL];
char name[SL];
system("cls");
n = getStuNum(head);
printf("请问你想插入到第几位呢,目前共有%d位学生(可输入0-%d)\n", n,n);
scanf("%d", &num);
if (num == 0) {
new = (struct STU*)malloc(sizeof(struct STU));
printf("请输入您要删除的同学的姓名\n");
scanf("%s", &name);
printf("请输入学生性别(男:0,女:1)\n");
scanf("%d", &sex1);
printf("请输入学生号\n");
scanf("%s", &id);
printf("请输入学生生日\n");
scanf("%s", &birth);
printf("请输入学生手机号\n");
scanf("%d", &phoneNum);
strcpy(new->name, name);
strcpy(new->birth, birth);
strcpy(new->id, id);
new->phoneNum = phoneNum;
if (sex1 == 0) {
new->sex = Man;
}
else {
new->sex = Woman;
}
new->next = head;
printf("插入成功!");
return new;
}
for (int i = 0; i < num-1; i++) {
current = current->next;
}
new = (struct STU*)malloc(sizeof(struct STU));
printf("请输入您要插入的同学的姓名\n");
scanf("%s", &name);
printf("请输入学生性别(男:0,女:1)\n");
scanf("%d", &sex1);
printf("请输入学生号\n");
scanf("%s", &id);
printf("请输入学生生日\n");
scanf("%s", &birth);
printf("请输入学生手机号\n");
scanf("%d", &phoneNum);
strcpy(new->name, name);
strcpy(new->birth, birth);
strcpy(new->id, id);
new->phoneNum = phoneNum;
if (sex1 == 0) {
new->sex = Man;
}
else {
new->sex = Woman;
}
new->next = current->next;
current->next = new;
printf("插入成功!");
return head;
}
/*
void writeText(struct STU* head) {
FILE* fp = fopen("C:\\Users\\renboyu010214\\Desktop\\C语言练习\\readText.txt", "w+");
struct STU* current;
current = head;
system("cls");
fputs(
"+------------学生成绩管理系统------------+\n",fp);
while (current->next != NULL) {
fputs(("|%20s%-20s|\n","姓名:", current->name), fp);
if (current->sex == 0) {
fputs(("|%20s%-20s|\n", "性别:", "男"), fp);
}
else {
fputs(("|%20s%-20s|\n", "性别:", "女"), fp);
}
fputs(("|%20s%-20s|\n", "生日:", current->birth), fp);
fputs(("|%20s%-20s|\n", "学生号:", current->id), fp);
fputs(("|%20s%-20d|\n", "手机号:", current->phoneNum), fp);
fputs("+----------------------------------------+\n", fp);
current = current->next;
}
fputs(("|%20s%-20s|\n", "姓名:", current->name), fp);
if (current->sex == 0) {
fputs(("|%20s%-20s|\n", "性别:", "男"), fp);
}
else {
fputs(("|%20s%-20s|\n", "性别:", "女"), fp);
}
fputs(("|%20s%-20s|\n", "生日:", current->birth), fp);
fputs(("|%20s%-20s|\n", "学生号:", current->id), fp);
fputs(("|%20s%-20d|\n", "手机号:", current->phoneNum), fp);
fputs("+----------------------------------------+\n", fp);
fclose(fp);
printf("导出成功!\n");
}
*/
int main() {
int n;
int choose,choose2=1,choose3=1, choose4,choose5=1,choose6;
struct STU* stuMess=NULL;
begin:
n = getStuNum(stuMess);
menu(n);
scanf_s("%d", &choose);
//printf("%d", choose);
switch (choose)
{
case 0:
return 0;
case 1:
stuMess = addStu();
goto begin;
case 4:
checkStu(stuMess);
printf("输入任意键返回主页\n");
scanf("%d", &choose4);
getchar();
goto begin;
case 2:
while (choose2) {
stuMess=deleteStu(stuMess);
printf("是否继续删除(0:返回主页面,1:继续删除)\n");
scanf("%d", &choose2);
getchar();
}
goto begin;
case 3:
while (choose3) {
stuMess = modifyStu(stuMess);
printf("是否继续修改(0:返回主页面,1:继续修改)\n");
scanf("%d", &choose3);
getchar();
}
goto begin;
case 5:
while (choose5) {
stuMess = insertStu(stuMess);
printf("是否继续插入(0:返回主页面,1:继续插入)\n");
scanf("%d", &choose5);
getchar();
}
goto begin;
default:
break;
}
}
[C语言]实现简单的学生信息管理系统
最新推荐文章于 2024-08-21 15:15:07 发布